How to read text and strings from serial port?
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Robert Knutson
am 19 Mär. 2022
Kommentiert: Robert Knutson
am 20 Mär. 2022
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:

0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 19 Mär. 2022
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Arduino Hardware finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!