Simulink real time not correct parsing of packet input

조회 수: 18 (최근 30일)
Ilaria
Ilaria 2025년 9월 24일
답변: Satyam 2025년 10월 16일 19:29
Hi, I’m sending data over a serial line at a baud rate of 115200. The data are structured like this
[header byte1 byte2 byte3 byte4 byte5 byte6] and are sent as uint8. Using a Python script that parses the data—detecting the header and splitting the first 3 bytes as one signal and the next 3 as a second signal—I’m able to correctly reconstruct my data as follows:
while True:
try:
# Read a single byte while waiting for the 'R' header
byte_header = ser.read(1)
if byte_header == b'R':
# If the header is 'R', read the next 6 bytes
data_block = ser.read(6)
if len(data_block) == 6:
# Extract the two 3-byte blocks
block1_bytes = data_block[0:3]
block2_bytes = data_block[3:6]
# Convert the bytes to 24-bit integer values
# Adjust 'byteorder' and 'signed' if needed
value1 = int.from_bytes(block1_bytes, byteorder='little', signed=False)
value2 = int.from_bytes(block2_bytes, byteorder='little', signed=False)
# Append the new data points to the respective queues
data_channel1.append(value1)
data_channel2.append(value2)
# print(f"Header: R, CH1 Value: {value1}, CH2 Value: {value2}")
# print(f" Byte 0: {data_block[0]}, Byte 1: {data_block[1]}, Byte 2: {data_block[2]}")
else:
print("Incomplete data received after the 'R' header.")
except serial.SerialException as e:
print(f"Serial device disconnected or error: {e}")
ser = None
break
Now I’m trying to use SIMULINK DESKTOP REAL-TIME, using a serial port with a DB9 connector. This is my schematic. I receive the data correctly, but when I try to parse them using the same strategy as in Python, what I see is not correct. The serial data are correctly recived,because as i said I can correctly reconstruct in phyton, but in simulink something is not working. Do you know what I'm missing?
MATLAB FUNCTION FOR PARSING
function [ch1, ch2] = parsePacket(pkt, ready)
%#codegen
% pkt: [1x7] uint8 = [header, ch1_b0, ch1_b1, ch1_b2, ch2_b0, ch2_b1, ch2_b2]
% ready: boolean (true se il blocco Packet Input ha consegnato dati validi)
% ch1, ch2: valori int32 ricostruiti a 24 bit signed
ch1 = int32(0);
ch2 = int32(0);
if ready
% Controlla header (0x52 = 82 = 'R')
if length(pkt) == 7 && pkt(1) == uint8(82)
% --- Channel 1 ---
raw1 = uint32(pkt(2)) + bitshift(uint32(pkt(3)),8) + bitshift(uint32(pkt(4)),16);
if raw1 >= 2^23 % correzione segno (24 bit signed)
raw1 = raw1 - 2^24;
end
ch1 = int32(raw1);
% --- Channel 2 ---
raw2 = uint32(pkt(5)) + bitshift(uint32(pkt(6)),8) + bitshift(uint32(pkt(7)),16);
if raw2 >= 2^23 % correzione segno (24 bit signed)
raw2 = raw2 - 2^24;
end
ch2 = int32(raw2);
end
end
Packet input settings
simulink blocks
not correct output

답변 (1개)

Satyam
Satyam 2025년 10월 16일 19:29
Hi Ilaria,
Since your serial data are received correctly but parsed incorrectly in Simulink, the issue is likely related to packet alignment or timing. You can try troubleshooting your issue:
  • Packet Synchronization: The Packet Input block reads fixed-size packets (7 bytes). If the sender transmits continuously, Simulink may misalign the header ('R'). Use the Data Error port to detect misalignment and handle it inside your parsePacket function by checking for the header byte (0x52).
  • Sample Time & Baud Rate: At 115200 baud, one packet (~7 bytes) takes about 0.6 ms to transmit. With a 1 ms Simulink sample time, occasional desynchronization can occur. Try increasing the sample time to 0.002–0.005 s and ensure consistent transmission intervals.
I hope it would solve your query.

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by