i tried pulling my data from arduino port but got error. I am a novice

4 views (last 30 days)
serialPort = 'COM5';
baudrate = 115200;
s = serialport(serialPort,baudrate);
pause(4);
numsamples = 1000;
data = zeros(numsamples,4);
for i = 1:numsamples
line = readline(s);
values = sscanf(line, '%d,%d,%d,%d');
if numel(values) == 3
data(i,:) = values';
end
pause(0.1)
end
time = data(:,1);
figure;
plot(time, data(:,2), 'r',time, data(:,3), 'b',time, data(:,4), 'g');
xlabel('Time(s)');
ylabel('Acceleration (m/s^2)');
legend('X','Y','Z');
title('ESP_now receiver data');
Error in untitled2 (line 13)
values = sscanf(line, '%d,%d,%d,%d');
  5 Comments
tje_b
tje_b on 14 Apr 2023
Thanks it is available, i change code and the com port to 13 in the device manger. it seems working but thie code has some errors.
% Define serial port and open the connection
serialPort = 'COM13'; % Replace with your Arduino's serial port
baudrate = 115200;
s = serialport(serialPort, baudrate);
pause(2); % Give the connection some time to be established
% Read accelerometer data from the Arduino
numSamples = 100; % Number of samples to read
data = zeros(numSamples, 3); % Preallocate a matrix to store the data
for i = 1:numSamples
line = readline(s); % Read a line from the serial port
values = textscan(line, '%d,%d,%d'); % Parse the line using textscan
if numel(values) == 3
data(i, :) = cell2mat(values)'; % Convert the cell array to a matrix and store it in the data matrix
end
pause(0.1); % Adjust the delay if necessary
end
% Close the serial port connection
clear s;
% Convert counts to acceleration (m/s^2)
countsPerG = 256; % For a range of +/-2g
g = 9.81; % Gravitational acceleration
data = (data / countsPerG) * g;
% Process the accelerometer data (e.g., plot it)
time = (0:numSamples - 1) * 0.1; % Assuming 100 ms delay between samples
figure;
plot(time, data(:, 1), 'r', time, data(:, 2), 'g', time, data(:, 3), 'b');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
legend('X', 'Y', 'Z');
title('ADXL345 Accelerometer Data');
the error is
>> untitled3
Unable to perform assignment because the size of the left side is 1-by-3 and the size of the right side is 3-by-0.
Error in untitled3 (line 18)
data(i, :) = cell2mat(values)'; % Convert the cell array to a matrix and store it in the data matrix
Cris LaPierre
Cris LaPierre on 14 Apr 2023
Sorry, I was getting distracted by the red error text appearing because you were trying to run your code here. That would be expected as hardware is not supported in the 'Run Code' feature in Answers, and is not the error you were referring to. I have removed the output from your code to remove the confusion.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 14 Apr 2023
Moved: Walter Roberson on 14 Apr 2023
line = readline(s); % Read a line from the serial port
values = textscan(line, '%d,%d,%d'); % Parse the line using textscan
readline(s) is returning a line that does not start with whitespace and then '-' followed by digits, or '+' followed by digits, or digits, or '-.' followed by digits, or '+.' followed by digits, or '.' followed by digits. That is, the line is either empty or is entirely whitespace or starts with something that cannot be a number.
In the case that it started with a number but then did not have a comma after the number, or had a comma followed by something that is not a number, then values{1} would contain the first number but values{2} would be empty
You should be doing something like
line = readline(s); % Read a line from the serial port
values = cell2mat(textscan(line, '%d,%d,%d')); % Parse the line using textscan
if numel(values) == 3
data(i,:) = values;
end
The difference between this and what you are doing is that you are checking numel() on the cell array returned by textscan(), but textscan() always returns a cell array with as many elements as there are active % formats in the format description, with the cell array entries potentially being empty.
  19 Comments
Walter Roberson
Walter Roberson on 14 Apr 2023
Edited: Walter Roberson on 14 Apr 2023
Your code loops numSamples = 100; times, gathering data each time, and does not display anything until afterwards. It will not stream. You would need to display the data inside the loop.
% Define serial port and open the connection
serialPort = 'COM13'; % Replace with your Arduino's serial port
baudrate = 115200;
s = serialport(serialPort, baudrate);
configureTerminator(s,"CR")
pause(2); % Give the connection some time to be established
% Read accelerometer data from the Arduino
numSamples = 10000; % Number of samples to read
data = zeros(numSamples, 3); % Preallocate a matrix to store the data
countsPerG = 256; % For a range of +/-2g
g = 9.81; % Gravitational acceleration
dt = 0.1;
max_retries = 25;
pause_time = 0.1; % Adjust the delay if necessary
hX = animatedline('Color', 'r', 'displayname', 'X');
hold on
hY = animatedLine('Color', 'g', 'displayname', 'Y');
hZ = animatedLine('Color', 'b', 'displayname', 'Z');
legend show
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
title('ADXL345 Accelerometer Data');
hold off
for i = 0:numSamples-1
gotline = false;
for trycount = 1 : max_retries
pause(pause_time);
line = readline(s); % Read a line from the serial port
%result of readline() sometimes shows up as double([])
%but char([]) is empty char and strtrim '' is fine empty char
%so we do not need to specifically test for []
line = strtrim(char(line));
if isempty(line); continue; end
values = cell2mat(textscan(line, '%f,%f,%f')); % Parse the line using textscan
if numel(values) == 3
gotline = true;
break;
end
end
if ~gotline
error('failed to get valid line in %d tries', max_retries);
end
data(i+1,:) = values / countsPerG * g;
addpoints(hX, dt*i, data(i+1,1));
addpoints(hY, dt*i, data(i+1,2));
addpoints(hZ, dt*i, data(i+1,3));
drawnow limitrate
end
% Close the serial port connection
clear s;

Sign in to comment.

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!