Modbus: Soulimane MAMMAR
Modbus: Soulimane MAMMAR
Soulimane MAMMAR
March 6, 2021
Modbus Overview
First, a master sends a request telling the slave 1 to return the value of one register
starting at address 2.
slave id (1 byte)
| function code (read holding registers)
| | address of first register to read (2 bytes)
| | | number of registers to read (2 bytes)
| | | | checksum (2 bytes)
| | | | |
01 03 00 02 00 01 25 CA
The request also includes a checksum All slaves except for 1 must ignore the message.
Slave 1 is expected to send a response message similar to the following:
The drawback is that TCP/IP stack is much more difficult to support in some
types of field devices where Modbus RTU would work fine.
01 03 02 00 01 25 CA
-----------------------------------------------------
slave id function function-specific data CRC
1 byte 1 byte 2 bytes
Request
[ID][FC][ADDR][NUM][CRC]
ADDR - the address of the first coil (2 bytes)
NUM - the number of coils to read (2 bytes)
A read coils request is always 8 bytes long
Response
[ID][FC][BC][DATA(1+)][CRC]
BC - the nubmer of bytes of DATA in the response (1 byte)
DATA - a sequence of bytes that contains the state of coils (1 byte per 8 coils)
Example:
Request:
ID FC ADDR NUM CRC
[01] [01] [00 0A] [00 02] [9D C9]
Response:
ID FC BC DATA CRC
[01] [01] [01] [03] [11 89]
Soulimane MAMMAR Modbus
Function Codes
Read Discrete Inputs - 0x02
This function code allows the master to query the state of slave’s discrete inputs.
Request
[ID][FC][ADDR][NUM][CRC]
ADDR - the address of the first discrete input (2 bytes)
NUM - the number of discrete input to read (2 bytes)
A read discrete inputs request is always 8 bytes long
Response
[ID][FC][BC][DATA(1+)][CRC]
BC - the nubmer of bytes of DATA in the response (1 byte)
DATA - a sequence of bytes that contains the state of discrete inputs(1 byte per
8 coils)
Example:
Request: (always 8 bytes)
ID FC BC DATA CRC
[01] [02] [01] [02] [20 49]
Response: (at least 6 bytes)
ID FC BC DATA CRC
[01] [02] [01] [02] [20 49]
Soulimane MAMMAR Modbus
Function Codes
In some cases a slave might be unable to process a master request. For such occasions
Modbus defines an Exception Response Frame:
[ID][FC][EC][CRC]
FC - the function code of the request that led to the exception with the most
significant bit set to 1 (1 byte)
EC - an exception code explaining what happened (1 byte)
The function code of read coils is 0x01, but in the exception response the most
significant bit has been set to 1, so the code becomes 0x81:
0000 0001 => 1000 0001
(0x01) (0x81)