How to read text and strings from serial port?

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:

 Accepted Answer

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 Comments

What im trying to do is to invert data(1,i) if the direction should be negative. I tried doing this with a purely dirivative based approach but it ended up causing more problems then it solved. To try to solve this, my thought was that if we knew the direction should be positive or negative then the logic woud be easier to sort out.
I tried putting this in my while loop but now its not storing any data, any idea what i need to change?
IncomingData = readline(s);
if IncomingData == "CW"
direction = 1;
disp(direcction)
elseif IncomingData == "CCW"
direction = -1;
disp(direcction)
else
IncomingData = data(1,i);
end
IncomingData = readline(s);
if IncomingData == "CW"
direction = 1;
disp(direcction)
elseif IncomingData == "CCW"
direction = -1;
disp(direcction)
else
IncomingData = direction * double(data(1,i));
end
I have a suspicion that the carriage return at the end of each new line sent is messing this up, but I’m not sure how to proceed...
when a CW output is triggered, this is what it looks like
now if i try to do
strcmp(IncomingData, "CW")
It returns a logical false, which it shouldnt.
ive also tried including the carrage return symbol and that doesnt work either
strcmp(IncomingData, "CW \r")
IncomingData = strtrim(readline(s));
That worked! Thanks!!!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!