C2000 Modbus RTU Slave

21 次查看(过去 30 天)
Semih
Semih 2024-9-10
评论: Semih 2024-10-18,6:41
Hi,
I'm programming a DSP TMS320F28069M using Simulink. I have successfully received data and unpack that receive data via uint16 format. And I use a matlab function block. This reason that I used, to compare the required bits for Modbus communication. Although byte unpack block output type is uint16, I couldn't use this receive value. For example, let the SCI received data be '01 02 00 00 00 00 02 10'. First of compare the first byte. If first byte is 01, transmits the eighth byte. But if not sends second byte. I always saw the second byte was gone. I guess received or compare data type is wrong but I couldn't find it. I changed the value C1 instead of 01 in the if loop. When I enter the C1 byte into the system, an error occurs. How can I do this?
Inside the function block:

回答(1 个)

Deep
Deep 2024-9-17
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:
  1. Change the output data type to uint8.
  2. 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:
For more information, you may refer to the documentation for “Byte Unpack” block in R2024a - https://www.mathworks.com/help/releases/R2024a/ti-c2000/ref/byteunpack.html.
Hope this helps!
  1 个评论
Semih
Semih 2024-10-18,6:41
Thank you for your answer. I have changed the serial terminal application. And easily follow the dataset and use the your mentioned.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by