Framer

pymodbus.framer.ModbusAsciiFramer module

Modbus ASCII Frame Controller.

[ Start ][Address ][ Function ][ Data ][ LRC ][ End ]

1c 2c 2c Nc 2c 2c

  • data can be 0 - 2x252 chars

  • end is “\r\n” (Carriage return line feed), however the line feed character can be changed via a special command

  • start is “:”

This framer is used for serial transmission. Unlike the RTU protocol, the data in this framer is transferred in plain text ascii.

pymodbus.framer.ModbusRtuFramer module

Modbus RTU Frame controller.

[ Start Wait ] [Address ][ Function Code] [ Data ][ CRC ][ End Wait ]

3.5 chars 1b 1b Nb 2b 3.5 chars

Wait refers to the amount of time required to transmit at least x many characters. In this case it is 3.5 characters. Also, if we receive a wait of 1.5 characters at any point, we must trigger an error message. Also, it appears as though this message is little endian. The logic is simplified as the following:

block-on-read:
    read until 3.5 delay
    check for errors
    decode

The following table is a listing of the baud wait times for the specified baud rates:

------------------------------------------------------------------
 Baud  1.5c (18 bits)   3.5c (38 bits)
------------------------------------------------------------------
 1200   13333.3 us       31666.7 us
 4800    3333.3 us        7916.7 us
 9600    1666.7 us        3958.3 us
19200     833.3 us        1979.2 us
38400     416.7 us         989.6 us
------------------------------------------------------------------
1 Byte = start + 8 bits + parity + stop = 11 bits
(1/Baud)(bits) = delay seconds

pymodbus.framer.ModbusSocketFramer module

Modbus Socket Frame controller.

Before each modbus TCP message is an MBAP header which is used as a message frame. It allows us to easily separate messages as follows:

[         MBAP Header         ] [ Function Code] [ Data ]         [ tid ][ pid ][ length ][ uid ]
  2b     2b     2b        1b           1b           Nb

while len(message) > 0:
    tid, pid, length`, uid = struct.unpack(">HHHB", message)
    request = message[0:7 + length - 1`]
    message = [7 + length - 1:]

* length = uid + function code + data
* The -1 is to account for the uid byte

pymodbus.framer.ModbusTlsFramer module

Modbus TLS Frame controller.

No prefix MBAP header before decrypted PDU is used as a message frame for Modbus Security Application Protocol. It allows us to easily separate decrypted messages which is PDU as follows:

[ Function Code] [ Data ]

1b Nb