Skip to main content

Worked Example

This section provides a practical example of sending Modbus TCP commands to a Brainboxes ED device.

Writing a Single Coil

In this example, we are sending Modbus commands to write single bits to an ED-527 which are either high or low.

Function Code

To write a single bit as either high or low, use the Write Single Coil function code: 05

Data Values

ValueResult
0x0000Output bit LOW
0xFF00Output bit HIGH

ED-527 Output Addresses

The ED-527 has its 16 output register addresses starting at 0x0001 through to 0x0010.

Example: Set Output 1 High

To set the bit high in register 1, the Modbus packet will look like this:

0017 0000 0006 FF 05 0001 FF00

Packet Breakdown

FieldValueDescription
TrID0017Transaction ID
Prot0000Protocol (always 00 for Modbus TCP)
Len0006Number of bytes in the rest of the transaction
UIFFUnit Identifier
Fn05Modbus function code for writing a single coil
Addr0001Address of the Modbus register to set
DataFF00Data value (0xFF00 = HIGH, 0x0000 = LOW)
Understanding the Fields
  • Transaction ID (TrID): A unique identifier for this transaction, useful for matching responses to requests
  • Protocol (Prot): Always 0000 for Modbus TCP
  • Length (Len): The number of remaining bytes in the message (Unit Identifier + Function Code + Address + Data)
  • Unit Identifier (UI): Used for routing in gateway scenarios; typically FF for direct connections
  • Function Code (Fn): Specifies the action to perform (05 = Write Single Coil)
  • Address (Addr): The target coil address
  • Data: The value to write to the coil

Example: Set Output 1 Low

To set the same output low, change the data value to 0x0000:

0018 0000 0006 FF 05 0001 0000
FieldValueDescription
TrID0018Transaction ID (incremented)
Prot0000Protocol
Len0006Length
UIFFUnit Identifier
Fn05Function code (Write Single Coil)
Addr0001Address of register 1
Data0000Set bit LOW