How to plot real time serial data.
41 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
cdawg
on 28 Apr 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")
ch1Data(end+1) = ch1Packet
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]
More Answers (0)
See Also
Categories
Find more on Code Generation and Deployment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!