Hi Semih,
As per my understanding, you are working on a project involving Modbus RTU communication in Simulink. Your goal is to correctly receive Modbus data and filter it based on the first byte of the data packet. However, you're encountering an issue where the first byte of the data packet isn’t correctly matching with chosen values inside your MATLAB Function.
In the attached model, the “Byte Unpack” block is configured to output uint16 values with a 2-byte alignment. This setup causes two bytes to be combined into a single uint16 value. For instance, when receiving 0x01 0x02, it is interpreted as 0x0102 (or 258 in decimal). Consequently, your if statement is comparing 258 == 1, which is undesired and explains the discrepancy you are experiencing.
This diagram shows an example on how two bytes are combined into a single uint16 value, leading to misinterpretation of the incoming data:
To correctly identify the first byte, you should modify the "Byte Unpack" block parameters as follows:
- Change the output data type to uint8.
- Set the alignment to 1 byte to ensure each byte is unpacked separately.
This diagram shows an example on how each byte will be treated independently with the correct block parameters:
As an additional note, you can also handle mixed data types by unpacking the first two bytes as separate uint8 values, while interpreting the remaining data chunks as uint16:
Hope this helps!