How to plot real time serial data.

41 visualizaciones (últimos 30 días)
Kulbir
Kulbir el 27 de Abr. de 2023
Comentada: Kulbir el 1 de Mayo de 2023
I want to plot real time raw serial data. the data comes in the form of real(no configureble settings) and is broken into packets with a total length of 9 bytes. AND GETTING the ERROR, PLZ HELP ME OUT
% Set up the serial port connection
serialPort = serialport("COM4", 115200); % Replace "COM1" with the appropriate port name
configureTerminator(serialPort, "LF",10);
% serialPort.Terminator;
% Initialize variables
seqNum = 0;
ch1Data = [];
ch2Data = [];
button1 = 0;
button2 = 0;
% Continuously read data packets from the USB port
while true
% Read a packet from the serial port
packet = read(serialPort, 9, "uint8");
% Verify that the packet start byte is correct
if packet(1) ~= hex2dec("59")
continue; % Packet start byte is incorrect, skip to next packet
end
% Extract data from the packet
seqNumPacket = packet(2);
ch1Packet = typecast(uint8(packet(3:5)), "int8");
ch2Packet = typecast(uint8(packet(6:8)), "int8");
statusByte = packet(9);
% Check for button presses
button1 = bitget(statusByte, 7);
button2 = bitget(statusByte, 6);
% Check that the sequence number is correct
if seqNumPacket ~= seqNum + 1
% Sequence number is incorrect, data may be out of order or lost
% You may want to add error handling or logging here
end
% Update variables with new data
seqNum = seqNumPacket;
ch1Data(end+1) = ch1Packet;
ch2Data(end+1) = ch2Packet;
% Do something with the data
% ...
end

Respuesta aceptada

cdawg
cdawg el 28 de Abr. de 2023
The entire error is cut off in your screenshot, but it looks like you're trying to assign a vector into ch1Data(end+1) which only accepts a scalar. I don't know what "packet" actually is in your script, so I'll make one up:
ch1Data = [];
packet = [2 3 1 6 3 9 0];
ch1Packet = typecast(uint8(packet(3:5)), "int8")
ch1Packet = 1×3
1 6 3
ch1Data(end+1) = ch1Packet
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Instead, you can try something like this:
ch1Data = [ch1Data ch1Packet] % or if ch1Packet is a column vector in your real case, do ch1Data = [ch1Data; ch1Packet]
  1 comentario
Kulbir
Kulbir el 1 de Mayo de 2023
Hi @cdawg, Thank you for your answer.
I am getting the ouput in command window in digit form. It would be so helpful if you suggest me how i can draw the real time data(speech signal) in plotting graph. my recording taken would be like 10 seconds. thank you

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Code Generation and Deployment en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by