Why can't Matlab save the acceleration data higher than 8.67KSPS

3 views (last 30 days)
I used the following codes to save my acceleration data from my Teensy4.1 board connected to my Macbook comport. I find out that It cant save data greater than 8.67KHz sampling frequency. I am doing something wrong?
% Setup a connection to the USB port
portName = '/dev/cu.usbmodem131678501'; % Adjust to your port name
s = serialport(portName, 2000000);
configureTerminator(s, 'LF');
s.Timeout = 20;
fopen(s);
% Create a file for writing
fileID = fopen('accelerometertest.txt', 'w');
% Read data from the serial port and write to the file
try
while true
dataLine = fgets(s); % Read a line from the serial port
fprintf(fileID, '%s', dataLine); % Write the data to the file
end
catch ME
% If any error occurs or you stop the script, close the connections
fclose(fileID);
fclose(s);
delete(s);
disp('Serial connection and file closed.');
rethrow(ME);
end
  2 Comments
dpb
dpb on 15 Oct 2023
You can try buffering to memory instead of writing each record--that should speed it up at least some, but the high-level overhead of MATLAB itself is problably going to be limiting as compared to running directly at a lower level coding protocol.
Walter Roberson
Walter Roberson on 15 Oct 2023
You can do better if you can send the data in binary instead of as text.

Sign in to comment.

Answers (1)

Pratyush
Pratyush on 16 Oct 2023
Hi tje_b,
I understand that you have written a script for reading data from the serial port and writing it to a file, but you cannot save data greater than 8.67KHz sampling frequency. The issue you're experiencing might be related to the limitations of the serial communication between your Teensy4.1 board and your MacBook.
In your code, you set the baud rate to 2,000,000 (2 Mbps). However, the maximum baud rate supported by the Teensy4.1 board is 12 Mbps.
To increase the sampling frequency, you can try increasing the baud rate in your code to its maximum value of 12,000,000. Modify the line s = serialport(portName, 2000000); to s = serialport(portName, 12000000);.
Make sure that both the Teensy4.1 board and your MacBook's serial port need to support the chosen baud rate for successful communication.
  1 Comment
Walter Roberson
Walter Roberson on 16 Oct 2023
It doesn't matter. The /dev name shows it is a USB device rather than a true serial port. Baud rate is irrelevant for USB: transfer speed is according to the protocol version negotiated between the host and the peripheral.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!