XMC Modbus TCP Client Setup
The XMC has Modbus TCP capability with the built in ethernet port. Modbus TCP uses port number 502. The XMC requires a library provided by Automationdirect to use Modbus TCP Client functionality. There is a unique library for the XMC-E08A/XMC-E08A-DC and XMC-E16A/XMC-E16A-DC.
-
XMC-E08A/XMC-E08A-DC version can be downloaded here.
-
XMC-E16A/XMC-E16A-DC version can be downloaded here.
-
Sample Programs can be downloaded here.
Function Codes | Modbus Transcription |
---|---|
Function Code 01 (hex:01) | Read Coils |
Function Code 02 (hex:02) | Read Discrete Inputs |
Function Code 03 (hex:03) | Read Holding Registers |
Function Code 04 (hex:04) | Read Input Registers |
Function Code 05 (hex:05) | Write Single Coil |
Function Code 06 (hex:06) | Write Single Register |
Function Code 15 (hex:0F) | Write Multiple Coils |
Function Code 16 (hex:10) | Write Multiple Registers |
Step | Action |
---|---|
1 | Download the library for your model of XMC (see links at start of topic). |
2 | Open your XG5000 project for the XMC. |
3 | In the Project window, right-click on Library. Select Import From File รจ Library |
4 |
Select the library for your XMC. This example is for the XMC-E16A/XMC-E16A-DC. Press Open.
|
5 |
Library function blocks will be populated in the Library section of the Project window.
|
SOCKET_TCPCONNECT is required to use the XMC Modbus TCP Client function blocks. The function block must be executed to create a TCP connection prior to sending/reading Modbus Data. Depending on the configuration of the connected server, if there is no data communication for a specific period or if the connection is terminated based on the various situations, you may need to close the channel with SOCKET_CLOSE and reconnect again with SOCKET_TCPCONNECT.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
ConnectCH | Input | Enter the socket service channel to use for the connection (1~8). |
LocalPort | Input | Connect with the specified port number. However, if it is 0, an empty TCP port other than the well-known port is automatically assigned (1~65535). |
Timeout | Input |
Timeout time*
* 1 sec Waiting |
TargetIP | Input | Destination IP to connect (0~255). |
TargetPort | Input | Port number of device being connected to (1~65535). |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorID | Output | Error Identification value. |
LocalPortOut | Output | Local port number assigned to the connection (1~65535). |
XMC Dedicated Ethernet Connections
The XMC has a default of 3 dedicated connections for the built-in Ethernet port. This can be changed to a value between 1-8 in the Ethernet settings screen. This value corresponds to the ConnectCH input value of the SOCKET_TCPCONNECT function block.
Step | Action |
---|---|
1 |
Double-click on Local Ethernet Parameter in the Project window.
|
2 |
Modify No. of Dedicated Connections value to the number of TCP sockets your application will need.
|
SOCKET_TCPCONNECT Example
SOCKET_CLOSE is used to close a TCP connection to a target device. It must be executed if attempting to connect to a target device that has stopped communicating or timed out.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel of the connection to close (1~8). |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorID | Output | Error Identification value. |
SOCKET_CLOSE Example
This function block is used to perform a Modbus Function Code 01 read of coils. It is required to execute SOCKET_TCPCONNECT prior to executing this function block.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel to use for the established connection (1~8). |
Timeout | Input |
Timeout time*
* 1 sec waiting |
UnitID | Input | Identification of Modbus TCP server (0~255). |
Transaction | Input | Transaction number of the Modbus frame (16#0 ~ 16#FFFF). |
Address | Input | Starting address of coils to be read (0~65535). |
QtyCoils | Input | Quantity of coils to be read (0~2000). |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorString | Output | Details of error that occurred. |
Coils | Output | Value of the Coils being read. |
MODBUS_READ_COILS_FC01 Example
This function block is used to perform a Modbus Function Code 02 read of discrete inputs. It is required to execute SOCKET_TCPCONNECT prior to executing this function block.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel to use for the established connection (1~8). |
Timeout | Input |
Timeout time*
* 1 sec waiting |
UnitID | Input | Identification of Modbus TCP server (0~255). |
Transaction | Input | Transaction number of the Modbus frame (16#0 ~ 16#FFFF). |
Address | Input | Starting address of discrete inputs to be read (0~65535). |
QtyCoils | Input | Quantity of discrete inputs to be read (0~2000). |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorString | Output | Details of error that occurred. |
Coils | Output | Value of the discrete inputs being read. |
MODBUS_READ_DISCRETE_INPUTS_FC02 Example
This function block is used to perform a Modbus Function Code 03 read of a holding register. It is required to execute SOCKET_TCPCONNECT prior to executing this function block.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel to use for the established connection (1~8). |
Timeout | Input |
Timeout time*
* 1 sec waiting |
UnitID | Input | Identification of Modbus TCP server (0~255). |
Transaction | Input | Transaction number of the Modbus frame (16#0 ~ 16#FFFF). |
Address | Input | Starting address of holding registers to be read (0~65535). |
RegisterQty | Input | Quantity of holding registers to be read (0~125). |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorString | Output | Details of error that occurred. |
Registers | Output | Value of the holding registers being read. |
MODBUS_READ_HOLDING_REGISTER_FC03 Example
This function block is used to perform a Modbus Function Code 04 read of input registers. It is required to execute SOCKET_TCPCONNECT prior to executing this function block.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel to use for the established connection (1~8). |
Timeout | Input |
Timeout time*
* 1 sec waiting |
UnitID | Input | Identification of Modbus TCP server (0~255). |
Transaction | Input | Transaction number of the Modbus frame (16#0 ~ 16#FFFF). |
Address | Input | Starting address of input registers to be read (0~65535). |
RegisterQty | Input | Quantity of input registers to be read (0~125). |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorString | Output | Details of error that occurred. |
Registers | Output | Value of the input registers being read. |
MODBUS_READ_INPUT_REGISTERS_FC04 Example
This function block is used to perform a Modbus Function Code 05 write of a single coil. It is required to execute SOCKET_TCPCONNECT prior to executing this function block.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel to use for the established connection (1~8). |
Timeout | Input |
Timeout time*
* 1 sec waiting |
UnitID | Input | Identification of Modbus TCP server (0~255). |
Transaction | Input | Transaction number of the Modbus frame (16#0 ~ 16#FFFF). |
Address | Input | Address of Coil to write (0~65535). |
Coil | Input | Data to write. |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorString | Output | Details of error that occurred. |
MODBUS_WRITE_SINGLE_COIL_FC05 Example
This function block is used to perform a Modbus Function Code 06 write of a single register. It is required to execute SOCKET_TCPCONNECT prior to executing this function block.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel to use for the established connection (1~8). |
Timeout | Input |
Timeout time*
* 1 sec waiting |
UnitID | Input | Identification of Modbus TCP server. (0~255) |
Transaction | Input | Transaction number of the Modbus frame. (16#0 ~ 16#FFFF) |
Address | Input | Address of Register to write. (0~65535) |
Register | Input | Data to write. |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorString | Output | Details of error that occurred. |
MODBUS_WRITE_SINGLE_REGISTER_FC06 Example
This function block is used to perform a Modbus Function Code 15 write of multiple coils. It is required to execute SOCKET_TCPCONNECT prior to executing this function block.
Element Type | Element Name | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel to use for the established connection (1~8). |
Timeout | Input |
Timeout time*
* 1 sec waiting |
UnitID | Input | Identification of Modbus TCP server (0~255). |
Transaction | Input | Transaction number of the Modbus frame (16#0 ~ 16#FFFF). |
Address | Input | Address of Coils to write (0~65535). |
QtyCoils | Input | Amount of data to write. |
Coils | Input | Data to write. |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorString | Output | Details of error that occurred. |
MODBUS_WRITE_MULTIPLE_COILS_FC15 Example
This function block is used to perform a Modbus Function Code 16 write of multiple registers. It is required to execute SOCKET_TCPCONNECT prior to executing this function block.
Element Name | Element Type | Description |
---|---|---|
Execute | Input | Edge detected request to execute the function block. |
Channel | Input | Enter the socket service channel to use for the established connection (1~8). |
Timeout | Input |
Timeout time*
* 1 sec Waiting |
UnitID | Input | Identification of Modbus TCP server. (0~255) |
Transaction | Input | Transaction number of the Modbus frame. (16#0 ~ 16#FFFF) |
Address | Input | Starting address of Registers to write. (0~65535) |
RegisterQty | Input | Quantity of registers to write (0~123) |
Registers | Input | Data to write. |
DONE | Output | Status that function block is done. |
Busy | Output | Function block is executing. |
Error | Output | Status that an error occurred. |
ErrorString | Output | Details of error that occurred. |
MODBUS_WRITE_MULTIPLE_REGISTERS_FC16 Example
LM202