Clear Filters
Clear Filters

Error when try to receive serialport information from STM32 in Mac

7 views (last 30 days)
I'm using the following MATLAB code to receive the speed info from an STM32 using macOS. I have already printed the speed information in the console, and the port information is '/dev/tty.usbmodem14303' from the % ls /dev/tty.* command. However, I encountered the following error on this line: s = serialport(port, baudrate);, even though the baud rate is correct. Any ideas? thanks.
here is what I get from the console:
% Specify serial port and baud rate
port = "/dev/tty.usbmodem14303";
baudrate = 115200;
% Create a serial port object
s = serialport(port, baudrate);
% Prepare the figure for plotting
figure;
hold on; % Keep the plot visible as we add new points
grid on; % Add a grid for better visibility
xlabel('Time (s)');
ylabel('Serial Data');
title('Real-time Serial Port Data');
% Initialize a variable to store the start time of the plot
startTime = datetime('now');
% Initialize an empty array to store data points
data = [];
% Loop to read and plot data
while 1
% Check if data is available to read
if s.NumBytesAvailable > 0
% Read the available data from the serial port
newData = readline(s);
% Convert the read data to a numeric value
% Note: This assumes the data can be directly converted. You may need to adjust this
% part depending on the format of your incoming data.
newData = str2double(newData);
% Append the new data to our data array
data(end+1) = newData;
% Compute the elapsed time since the start of plotting
elapsedTime = seconds(datetime('now') - startTime);
% Generate a time vector for the x-axis
timeVector = linspace(0, elapsedTime, numel(data));
% Plot the updated data
plot(timeVector, data, 'b-'); % Plot as a blue line
drawnow; % Update the plot
% This part is optional and allows the plot to auto-scroll
% Adjust xlim to show a specific window of recent data
if elapsedTime > 10 % Adjust this to your preferred window size in seconds
xlim([elapsedTime-10, elapsedTime]); % Show the last 10 seconds of data
end
end
end
% Close the serial port when done
clear s

Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!