How to read text and strings from serial port?

조회 수: 16 (최근 30일)
Robert Knutson
Robert Knutson 2022년 3월 19일
댓글: Robert Knutson 2022년 3월 20일
I’m designing a motor controller using an Arduino Mega board. I need to be able to log the speeds of the motors, so right now I’m using the Arduino to read the speeds and print those to the serial monitor. Unfortunately, my motors only output speed, not direction, so it will output the same speed for clockwise or counterclockwise rotation. I need to be able to distinguish between the two and plot the resulting data.
Whenever the Arduino commands the motor to change directions I have it send the direction of rotation over the serial monitor, "CW", or "CCW". The problem is that I’m storing the motor speeds received by the Arduino in an array, and whenever a direction change command comes in, this is getting stored as a NaN in my array. How do I fix this?
Code shown below and below that is the data I’m currently getting back from the Arduino. It should be more of a sine wave with positive and negative values.
clear;
clc;
clf;
data=zeros(2,100000); % Initalize array that data will be stored in
% Create Serial Object
s=serialport('COM4',115200);
% Initalize varriables for data reading loop
i=2;
t=0;
tic;
h=plot(NaN,NaN,'r'); % Open plot object to speed up plotting speed
while (t <= 30)
t=toc; % log time since loop start
data(1,i) = readline(s); % Read data sent over serialport and log it
data(2,i) = t; % Save the time the data was aquired in an array
set(h, 'XData', data(2,1:i),'YData',data(1,1:i)); % Used to speed up plotting
drawnow
i=i+1; % increment array index
end
data=data(:,1:i-1); % Remove data beyond the final read value
SampleSpeed = i/t % Calcuate sample speed, used for debugging
clear s; % Close serial port object
This is what the data should look like:

채택된 답변

Walter Roberson
Walter Roberson 2022년 3월 19일
data=zeros(2,100000); % Initalize array that data will be stored in
data is numeric.
data(1,i) = readline(s); % Read data sent over serialport and log it
readline() reads the line as a string object (not as a character vector). You then try to store that string object into the numeric location data(1,i) . It happens that converting string() to numeric is defined, and the operation proceeds to double() the string which does an internal equivalent to str2double(). And when the line that was read was text representation of a numeric value, that works fine. But if the line read was like "CCW" then the conversion to numeric is going to give NaN.
What you can do is assign the result of the readline(s) to a temporary variable, and test whether the temporary variable is "CW" or "CCW". If is, then do something appropriate. Otherwise, continue on to assign the temporary variable to data(1,i)
However, it is not clear to me what the appropriate behaviour is when you receive CW or CCW.
  댓글 수: 5
Walter Roberson
Walter Roberson 2022년 3월 20일
IncomingData = strtrim(readline(s));
Robert Knutson
Robert Knutson 2022년 3월 20일
That worked! Thanks!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by