pyModbusTCP.constants
The constants of the package: Modbus function codes, Modbus exception codes and client error codes.
from pyModbusTCP.constants import EXP_ILLEGAL_FUNCTION, MB_EXCEPT_ERR
Function codes
Constant |
Value |
Function |
|---|---|---|
|
0x01 |
Read Coils |
|
0x02 |
Read Discrete Inputs |
|
0x03 |
Read Holding Registers |
|
0x04 |
Read Input Registers |
|
0x05 |
Write Single Coil |
|
0x06 |
Write Single Register |
|
0x07 |
Read Exception Status (serial line only, not implemented) |
|
0x0B |
Get Comm Event Counter (serial line only, not implemented) |
|
0x0C |
Get Comm Event Log (serial line only, not implemented) |
|
0x0F |
Write Multiple Coils |
|
0x10 |
Write Multiple Registers |
|
0x11 |
Report Server ID (serial line only, not implemented) |
|
0x14 |
Read File Record (not implemented) |
|
0x15 |
Write File Record (not implemented: see below) |
|
0x16 |
Mask Write Register (not implemented: see below) |
|
0x17 |
Read/Write Multiple Registers |
|
0x18 |
Read FIFO Queue (not implemented) |
|
0x2B |
Encapsulated Interface Transport (with |
The tuples of codes:
SUPPORTED_FUNCTION_CODES: the functions implemented by the client and the server of the package, all the codes of the table but 0x07, 0x0B, 0x0C, 0x11, 0x14 to 0x16 and 0x18 (ModbusClient.custom_request()sends any other code, a handler of the server answers it). These codes are known forread_only(0x15, 0x16) and for the size of a request relayed bymbtcp-tcp2rtuto the serial line.WRITE_FUNCTION_CODES: the standard functions that write data, 0x05, 0x06, 0x0F, 0x10, 0x15, 0x16 and 0x17.ModbusServer(read_only=True)refuses them whatever their handler: 0x15 and 0x16 are not implemented, but a handler of the application (or a gateway) could answer them.READ_FUNCTION_CODES: the functions that only read data, 0x01 to 0x04 and 0x2B (with the MEI type 0x0E): the only ones that a read-only server gives to its fallback handler.
Modbus exception codes
The codes a server sends back when it can’t process a request: ModbusClient.last_except on the client side, the code of a
ModbusException on the server side. EXP_TXT and
EXP_DETAILS map each code to a short and a long description.
Constant |
Value |
Meaning |
|---|---|---|
|
0x00 |
no exception |
|
0x01 |
illegal function |
|
0x02 |
illegal data address |
|
0x03 |
illegal data value |
|
0x04 |
slave device failure |
|
0x05 |
acknowledge |
|
0x06 |
slave device busy |
|
0x07 |
negative acknowledge |
|
0x08 |
memory parity error |
|
0x0A |
gateway path unavailable |
|
0x0B |
gateway target device failed to respond |
Client error codes
The values of ModbusClient.last_error (see
Handle errors for their meaning). MB_ERR_TXT maps each code to a description.
Constant |
Value |
Description |
|---|---|---|
|
0 |
no error |
|
1 |
name resolve error |
|
2 |
connect error |
|
3 |
socket send error |
|
4 |
socket recv error |
|
5 |
recv timeout occur |
|
6 |
frame format error |
|
7 |
modbus exception |
|
8 |
bad CRC on receive frame (not set by ModbusClient) |
|
9 |
socket is closed |
Other constants
VERSION: the version of the package (alsopyModbusTCP.__version__).MODBUS_PORT: the standard Modbus/TCP port, 502.MAX_PDU_SIZE(253),MIN_PDU_SIZE(1),MBAP_SIZE(7): frame sizes, in bytes.MBAP_MIN_LENGTH(2),MBAP_MAX_LENGTH(254): the range of the length field of the MBAP header (the unit id and the PDU,MIN_PDU_SIZE + 1toMAX_PDU_SIZE + 1).