Using the command window as a serial monitor for arduino

6 visualizaciones (últimos 30 días)
Oscar
Oscar el 4 de Feb. de 2025
Comentada: Oscar el 24 de Feb. de 2025
I want to use the command window as a serial monitor to plot data sent by arduino or another device serially. I need to send some data to arduino too. I have the following code.
% lectura de puerto serial COM
clc;
clear;
close all;
%comandos de apertura del puerto serial
com_avr = serial('COM4',115200);
fopen(com_avr);
while(1)
%para leer
data = readline(com_avr); disp(data); %para leer
data = readline(com_avr); disp(data);
str = input("Send to Arduino: ","s");
val = sscanf(str, "%f");
fprintf(com_avr, "%f", val);
%muestra lo que se envio
data = readline(com_avr); disp(data);
%HERE MUST TO PLOT THE DATA
while 1
data = readline(com_avr);
plot(x,data,'bd','LineWidth',2)
axis([x-2 x 40 160])
hold on
drawnow
x = x + 0.002;
end
%secuencia de comandos de salida
if str == 'z'
disp("exit")
break;
end
end
disp("closing communication...")
%finaliza la comunicacion serial y libera el puerto
close(com_avr);
What could I do to open a plot graph and plot the data sent from Arduino?
May you give me any sugestion to improve this code?

Respuesta aceptada

Kothuri
Kothuri el 7 de Feb. de 2025
Hi Oscar,
In the above code, "while(1)" will cause an infinite loop and causes issue. And for plotting the data,
  • you can predefine the "plot" object before the loop.
  • Use "set()" to update data instead of replotting.
You can modify the above code as follows:
clc;
clear;
close all;
% Open Serial Port
com_avr = serial('COM4',115200);
fopen(com_avr);
% Initialize Plot
figure;
hold on;
grid on;
title("Real-time Serial Data Plot");
xlabel("Time (s)");
ylabel("Sensor Value");
xlim([0 10]);
ylim([40 160]);
% Plot Handle
h = plot(NaN, NaN, 'bd-', 'LineWidth', 2);
x = [];
y = [];
tic; % Start Timer
% Main Loop
while true
% Read Data
data = readline(com_avr);
sensor_value = str2double(data);
% Update Plot
x = [x toc];
y = [y sensor_value];
set(h, 'XData', x, 'YData', y);
xlim([max(0, x(end)-10) max(10, x(end))]); % Sliding window
drawnow;
% Send Data to Arduino
str = input("Send to Arduino: ", "s");
val = str2double(str);
writeline(com_avr, num2str(val));
% Check Exit Condition
if str == "z"
disp("Exit");
break;
end
end
% Close Serial Port
disp("Closing communication...");
clear com_avr;
The usage of "serial()" will be discontinued in future releases of MATLAB. Instead you can use "serialport()".
You can refer the below documentation links on "serial" and "set"
  1 comentario
Oscar
Oscar el 24 de Feb. de 2025
hello Kothuri, thanks for your comments, I have already solved my issues I use the serialport function and a more specific format. Greetings.

Iniciar sesión para comentar.

Más respuestas (1)

MathWorks MATLAB Hardware Team
MathWorks MATLAB Hardware Team el 24 de Feb. de 2025
Hello,
With the MATLAB Support Package for Arduino Hardware, you can efficiently read data from an Arduino.
If your board supports WiFi, you might also consider using its wireless capabilities to gather data.
Additionally, please let us know what specific data you are trying to read from the Arduino. Understanding your use case better will allow us to assist you more effectively.
Thank you,
MATLAB Hardware Team
MathWorks

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by