EZT-430i Communications Manual
Serial
Communication
CSZ
EZT-430i
REV
C
4.11
Transmitting and Receiving Messages
In order to reliably communicate with EZT-430i , it is important to develop an efficient means of transmitting
and receiving messages. Modbus is a structured protocol and it must be properly followed. It is
recommended, if possible, to locate an existing communication driver to incorporate into your software.
Developing one from scratch can be challenging. However, if one is not available, or you choose to develop
one yourself, the following guidelines may be of assistance.
Transmitting Messages
When sending a message to EZT-430i , it is important to remember that Modbus RTU protocol does not have
start-of-transmission or end-of-transmission characters. All messages are “framed” using timeouts between
characters. EZT-430i uses its own fixed timeout setting of 135ms. Thus, if the entire message is not received
by EZT-430i within 135ms, it will discard the data it has received and assume the next data byte received is
the start of the next valid message.
The timeout must be considered carefully when developing your application. In creating your message, there
are several steps that must be executed in order to build the packet and format the data properly into
hexadecimal to send out the serial port of your PC. If you write code in a manner that steps byte by byte
through sending the message out the serial port, formatting each piece of data prior to sending it, there is a
good possibility that two much time may pass between characters, thus causing a failed transmission.
Therefore, it is recommended that the entire message, including the CRC, be created and assembled prior to
being sent to the serial port. By assembling the main body of the message first, you can then pass it to the
CRC algorithm which can step sequentially through the message, generate the CRC and append it to the
message body. Once the message is completely assembled, it can then be sent out the serial port as a
completed packet. This will insure that the message reaches EZT-430i within the proper time frame.
Receiving Messages
Due to the fact that Modbus RTU protocol does not have start-of-transmission or end-of-transmission
characters, if the serial port driver you are using does not support an interval timeout setting allowing you to
automatically terminate a read after a specified time passes between bytes (signaling the end of a message),
you must know how long the message will be that you are receiving. That allows you to know how many
bytes to read from your serial port and when you have received the entire message. If you rely on a
maximum timeout period to terminate the read, depending upon the length of the received message, you will
either loose a portion of the message or have to set the timeout period so high, that it will greatly affect the
throughput of your code.
As can be seen from the previous examples for read and write commands in Section 4.3.1, the length of the
returned message will very based on the type of command, and for read commands, how many registers are
being returned. Response messages can vary in length from as little as 5 bytes for an exception response to
as many as 125 bytes for a read command. Therefore, in order to read in the message efficiently, you need
to know what type of command it is in response to.
The response messages are always coded with the first two bytes of the message as the controller address
and command type. When executing a read, read in only the first 2 bytes of data at the serial port. Examine
the second byte and determine what the command is. If it is a write command (0x06 or 0x10), you know the
response message is 8 bytes long. You can then read in the next 6 bytes of data from the serial port to
complete the message. You can then caulate the CRC for the first 6 bytes of that message, and compare it to
the last 2 bytes. If they match, then the communication completed successfully.
If the response is to a read command (0x03), you must then perform a single byte read from your serial port in
order to get the next byte of the message. The third byte in a read response message is the number of data
bytes in the message. By reading in this value, you then know how many data bytes follow. Note that this
value does not include the 2 bytes for the CRC. Thus, when reading in the rest of the message, you will read
in the number of data bytes plus an additional two, in order to get the CRC. You can then caulate the CRC
for the message and compare it to the last two bytes. If they match, the data you received is valid.