MTCP Server NJNX e
MTCP Server NJNX e
File MTCP_Server_NX.slr
Conditions The FB Modbus TCP Server provides some read/write features in accordance with the
of use specifications defined by the Modbus organization.
The Modbus TCP Server function block is offered 'as is' and may serve as a basis for
development.
Users should previously test its adequacy to the final application.
Omron could not be held responsible in case of malfunction.
Principe The function block MTCP_Server_NJNX switch to LISTEN mode waiting for a Modbus
TCP Client connection when Start input is activated.
We recommend using the FB in a periodical task to not overload the primary task.
List of functions supported by MTCP_Server_NJNX:
Code Modbus Function
0x01 Read Coils
0x02 Read Discret Inputs
0x03 Read Holding Registers
0x04 Read Input Registers
0x05 Write Single Coil
0x06 Write Single Register
0x08 Echo Back
0x10 Write Multiple Registers
0x15 Write Multiple Coils
0x17 Read Write Multiple Registers
Input variable
Name type range Description
Start Bool OFF, ON Server Activation
Local_TcpPort UINT 1-65535 Local TCP port Number (défaut 502)
ConnectionTimeout TIME Delay of no activity. Client is gone and server
should be restarted.
Set to 0, keep alive function used
Reset_Counter Bool OFF, ON ON: Reset the SdRcv_Counter to zero
Input/output Variables
Name type range Description
Registers Array of 0-FFFF Register area
1024 Words ARRAY[0..1023] OF WORD
Coils Array of OFF, ON Coil area
1024 bool ARRAY[0..1023] OF BOOL
Output Variables
Name type Range Description
Connected Bool OFF, ON ON : client is connected
Error Bool OFF, ON Error flag
ErrorID UINT 0 - FFFF Error Code returned by the socket or
Modbus TCP server (see error code list
below).
TCP_Status _eCONNECTION_STATE Enum _CLOSED
_LISTEN
_SYN SENT
_SYN RECEIVED
_ESTABLISHED
_CLOSE_WAIT
_FIN WAIT1
_CLOSING
_LAST ACK
_FIN WAIT2
_TIME WAIT
IP_Client String[24] w.x.y.z IP address of connected client
Port_Client UINT 0-65535 Port number of connected client
SdRcv_Counter UDINT 0- Number of response sent
4294967295
Code Description
0001 ILLEGAL FUNCTION
Modbus
0002 ILLEGAL DATA ADDRESS
Exception
0003 ILLEGAL DATA VALUE
2000 Local IP Address Setting Error
2001 TCP/UDP Port Already in Use
2002 Address Resolution Failed
2003 Socket Status Error
2004 error Local IP Address Not Set
2006 Socket Timeout
2007 Socket Handle Out of Range
2008 Socket Communications Resource Overflow
In the event of an accidental break in the Ethernet connection, the server does not
know whether the client is still connected.
You can execute cyclic readings to control the presence of the client or define a
maximum period of inactivity with the input ConnectionTimeout of the function block
or even use the Keep Alive option so that the server regularly interrogates the client
by sending a request [TCP Keep-Alive] according to the delay defined in this option
(here 10s) then after 20s the port is released for a new connection.
When using multiple clients involving fast connections and disconnections, the Linger
option allows the Ethernet socket to abruptly close the connection via a [RESET]
rather than following the standard procedure FIN_WAIT 1 then FIN WAIT2.
To emulate a Modbus TCP client, you can use the utility software Etherway free to
download on:
https://www.support-omron.fr/details/logiciel.php?id=2017-07-28%20-%2015-34-
21%20-%20636541960
Modbus protocol
Read Multiple Coils Fn01
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0CH 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1CH 31 1 30 0 29 1 28 1 27 1 26 1 25 0 24 0 23 1 22 1 21 0 20 1 19 18 17 16
2CH 47 46 45 44 43 42 41 40 39 38 1 37 0 36 1 35 0 34 1 33 1 32 0
3CH 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
Italic characters show the ON/OFF(1/0) status of its bit condition.
Attention : In the MTCP_Server NJNX library, coil addresses start at address 0000.
DM 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1000 A B 1 2
1001 5 6 7 8
1002 9 7 1 3
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0CH 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1CH 31 1 30 0 29 1 28 1 27 1 26 1 25 0 24 0 23 1 22 1 21 0 20 1 19 18 17 16
2CH 47 46 45 44 43 42 41 40 39 38 1 37 0 36 1 35 0 34 1 33 1 32 0
3CH 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
Italic characters show the ON/OFF(1/0) status of its bit condition.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2000 3 A C 5
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1000 3 A C 5
1001 9 7 1 3
Example : write 10 coils ( xxxx xx11 1100 1101) coils 20 to 29.(X = ignored.)
Request Response
Data Donnée
Function Code 0x0F Function Code 0x0F
Starting Address(H) 0x00 Starting Address(H) 0x00
Starting Address(L) 0x14 Starting Address(L) 0x14
Quantity of coils(H) 0x00 Coil quantity (H) 0x00
Quantity of coils(L) 0x0A Coil quantity (L) 0x0A
Data byte quantity 0x02
1rst value (low byte) 0x3A
2nd value (high byte) 0x01
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0CH 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1CH 31 0 30 0 29 0 28 1 27 0 26 0 25 1 24 1 23 1 22 0 21 1 20 0 19 0 18 0 17 0 16 0
Italic characters show the ON/OFF(1/0) status of its bit condition.
Attention : In the MTCP_Server NJNX library, coil addresses start at address 0000.
Example: read registers 1 & 2 (1001-2002) and write CAFE into register 0003.
Request Response
Data Donnée
Function Code 0x17 Fonction Code 0x17
Starting Address(H) to read 0x00 Nbre of byte 0x04
Starting Address(L) to read 0x01 Valeur du register 1 (poids faible) 0x10
Quantity of Registers(H) to read 0x00 Valeur du registre 1 (poids fort) 0x01
Quantity of Registers(L) to read 0x02 Valeur du register 2 (poids faible) 0x20
Starting Address(H) to write 0x00 Valeur du registre 2 (poids fort) 0x02
Starting Address(L) to write 0x03
Nbre of registers (H) to write 0x00
Nbre of registers (L) to write 0x01
Nbre of byted’octets à écrire 0x02
1ère valeur (poids fort) 0xCA
1ère valeur (poids faible) 0xFE
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0000 3 A C 5
0001 1 0 0 1
0002 2 0 0 2
0003 C A F E