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
| Value | Result |
|---|---|
0x0000 | Output bit LOW |
0xFF00 | Output 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
| Field | Value | Description |
|---|---|---|
| TrID | 0017 | Transaction ID |
| Prot | 0000 | Protocol (always 00 for Modbus TCP) |
| Len | 0006 | Number of bytes in the rest of the transaction |
| UI | FF | Unit Identifier |
| Fn | 05 | Modbus function code for writing a single coil |
| Addr | 0001 | Address of the Modbus register to set |
| Data | FF00 | Data 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
0000for 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
FFfor 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
| Field | Value | Description |
|---|---|---|
| TrID | 0018 | Transaction ID (incremented) |
| Prot | 0000 | Protocol |
| Len | 0006 | Length |
| UI | FF | Unit Identifier |
| Fn | 05 | Function code (Write Single Coil) |
| Addr | 0001 | Address of register 1 |
| Data | 0000 | Set bit LOW |