How to plot real time serial data.

조회 수: 43 (최근 30일)
Kulbir
Kulbir 2023년 4월 27일
댓글: Kulbir 2023년 5월 1일
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

채택된 답변

cdawg
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")
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
Kulbir
Kulbir 2023년 5월 1일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Code Generation and Deployment에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by