How to plot real time serial data.
33 次查看(过去 30 天)
显示 更早的评论
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![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1368399/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1368399/image.jpeg)
% 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 个评论
采纳的回答
cdawg
2023-4-28
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]
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!