Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Function Block

Reference MTCP_Client_NJ OMRON ELECTRONICS S.A.S.


Revision 2.3 3 Parvis de la Gare
94130 Nogent sur Marne
Author JP Viskovic
Date 23/07/2021
+ Support http://support-omron.fr/

Modbus TCP Client for NJ Controller

Function Modbus TCP client for Built-in Ethernet Port of NJ & NX Controller
Connexion

Read/write
Fn

File MTCP_Client_NJ.zip
Function Block MTCP_Client_NJ Modbus TCP Client for NJ Controller

Conditions of The FB Modbus TCP Client provides some read/write features in accordance with
use the specifications defined by the Modbus organization.

The Modbus TCP Client 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_Client_Connect establish the connection with a remote
Modbus TCP server when Connect input is activated.
Connected output could allow execution of read/write FB via the Enable input.

List of read/write functions provided:

Code Modbus Function Function Block


0x01 Read Coils MTCP_Client_Fn01
0x02 Read Discret Inputs MTCP_Client_Fn02
0x03 Read Holding Registers MTCP_Client_Fn03
0x04 Read Input Registers MTCP_Client_Fn04
0x05 Write Single Coil MTCP_Client_Fn05
0x06 Write Single Register MTCP_Client_Fn06
0x10 Write Multiple Registers MTCP_Client_Fn10
0x17 Read Write Multiple Registers MTCP_Client_Fn17

The Keep-Alive option allows to regularly test the server (request [TCP Keep-
Alive]).
In case of non-response, the connection is reseted (request [RST]).
A high frequency may unnecessarily load the communication..

1- I/O variable of MTCP_Client_Connect

Input Variables
Name type range Description
Enable Bool OFF, ON FB Activation
IPaddress STRING n.n.n.n IP Address of the server
Port UINT 0-65535 Remote port n° on server (502 by default
Connect Bool OFF, ON Request to connect to the server

Output Variables
Name type Range Description
Connected Bool OFF, ON ON : Connected to the server
Error Bool OFF, ON Error flag
ErrorID UINT 0 - 65535 Error Code returned by the socket or Modbus
TCP server (see error code list below).
EtnConnErrror Bool OFF, ON ON: Ethernet connexion problem
TCP_Socket _sSocket Structure DstAdr, Handle and SrcAdr

JPV 23/07/2021 Page 2/7


Function Block MTCP_Client_NJ Modbus TCP Client for NJ Controller

Socket_Status _eCONNECTION_STATE

2- Input Variables of FB MTCP_Client_Fn03, Fn04, Fn05, Fn06, Fn10 and Fn17

MTCP_Client_Fn01 type range Description


MTCP_Client_Fn02
Enable Bool OFF, ON FB Activation (use Connected output of FB connect)
TCP_Socket _sSocket structure Handle of the socket used (given by the FB Connect)
Unit_ID BYTE 00-FF Unit Identifier (255 by default)
Coil_Address WORD 0 - FFFF Address of 1rst coil/discret input
Coil_Qty UINT 0 - 00FF Number of coils/discret inputs
Send_Request Bool OFF, ON Read Command

MTCP_Client_Fn03 type range Description


Enable Bool OFF, ON FB Activation (use Connected output of FB connect)
TCP_Socket _sSocket structure Handle of the socket used (given by the FB Connect)
Unit_ID BYTE 00-FF Unit Identifier (255 by default)
Register_Address WORD 0 - FFFF Address of 1rst register
Register_Qty UINT 0 - 00FF Number of registers
Send_Request Bool OFF, ON Read Command

MTCP_Client_Fn05 type range Description


Enable Bool OFF, ON FB Activation (use Connected output of FB connect)
TCP_Socket _sSocket structure Handle of the socket used (given by the FB Connect)
Unit_ID BYTE 00-FF Unit Identifier (255 by default)
Coil_Address WORD 0 - FFFF Address of the coil
Set_Value Bool OFF, ON ON/OFF value to be written
Send_Request Bool OFF, ON Write command

MTCP_Client_Fn06 type range Description


Enable Bool OFF, ON FB Activation (use Connected output of FB connect)
TCP_Socket _sSocket structure Handle of the socket used (given by the FB Connect)
Unit_ID BYTE 00-FF Unit Identifier (255 by default)
Register_Address WORD 0 - FFFF Address of the register
Set_Value WORD 0 - FFFF Value to write
Send_Request Bool OFF, ON Write command

MTCP_Client_Fn10 type range Description

JPV 23/07/2021 Page 3/7


Function Block MTCP_Client_NJ Modbus TCP Client for NJ Controller

Enable Bool OFF, ON FB Activation (use Connected output of FB connect)


TCP_Socket _sSocket structure Handle of the socket used (given by the FB Connect)
Unit_ID BYTE 00-FF Unit Identifier (255 by default)
Register_Address WORD 0 - FFFF Address of 1rst register
Register_Qty UINT 0 - 00FF Number of registers
Registers ARRAY 0 - FFFF Source of data (Array of 128 WORD)
Send_Request BOOL OFF, ON Write command

MTCP_Client_Fn17 type range Description


Enable Bool OFF, ON FB Activation (use Connected output of FB connect)
TCP_Socket _sSocket structure Handle of the socket used (given by the FB Connect)
Unit_ID BYTE 00-FF Unit Identifier (255 by default)
Reg_Addr_Read WORD 0 - FFFF Address of the 1st register to read
Reg_Qty_Read UINT 0 - 00FF Numbre of register to read
Reg_Addr_Write WORD 0 - FFFF Address of 1st register to write
Reg_Qty_Write UINT 0 - 00FF Number of registers to write
Registers ARRAY 0 - FFFF Source of data (Array of 128 WORD)
Send_Request BOOL OFF, ON Read-Write command

3- Output Variables

Name type Range Description


Cmd_Ok Bool OFF, ON ON : Command executed
Error Bool OFF, ON Execution error flag
ErrorID WORD 0 - FFFF Error Code returned by the socket or Modbus TCP
server (see error code list below).
Register (Fn03, ARRAY Read values are returned in an array of 128 WORD
Fn04 & Fn17 only)
Coils (Fn01, Fn02, ARRAY Read status ON/OFF are returned in an array of
Fn05 and Fn06 1024 Boolean (ARRAY[0..1023] OF BOOL)
only)

Error Code returned

Code Description
0001 ILLEGAL FUNCTION
0002 ILLEGAL DATA ADDRESS
0003 ILLEGAL DATA VALUE
0004 SLAVE DEVICE FAILURE
Modbus
0005 ACKNOWLEDGE
Exception
0006 SLAVE DEVICE BUSY
0008 MEMORY PARITY ERROR
000A GATEWAY PATH UNAVAILABLE
000B GATEWAY TARGET DEVICE FAILED TO RESPOND
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

JPV 23/07/2021 Page 4/7


Function Block MTCP_Client_NJ Modbus TCP Client for NJ Controller

Precautions in Using Socket Services


9-7-1 Precautions for UDP and TCP Socket Services
• Communications processing are sometimes delayed when multiple functions of the built-in Ether-
Net/IP port are used simultaneously or due to the contents of the user program.
• Communications efficiency is sometimes reduced by high communications traffic on the network line.
• The close processing for a close request instruction discards all of the buffered send and receive data
for the socket. For example, send data from a send request instruction immediately before the close
processing is sometimes not sent.
• After a socket is open, the built-in EtherNet/IP port provides a receive buffer of 9,000 bytes per TCP
socket and 9,000 bytes per UDP socket to enable data to be received at any time. If the receive buffer
is full, data received by that socket is discarded. Make sure that the user application always executes
receive requests to prevent the internal buffer from becoming full.

9-7-2 Precautions for UDP Socket Services


• The destination IP address can be set to a broadcast address for a UDP socket to broadcast data to
all nodes on the network. However, in this case, the maximum length of send data is 1,472 bytes.
Data lengths broken into multiple fragments (1,473 bytes or more in UDP) cannot be sent.
• For UDP socket, controls to confirm the reliability of communications, such as the confirmation of
send data, are not performed. To improve the reliability of communications when you use UDP sockets,
make sure the user program confirms that data is sent and resends data when necessary.

9-7-3 Precautions for TCP Socket Services


• If the TCP socket is closed on the remote node without warning during communications (i.e., if the
connection is closed), the socket at the local node must also be closed. You can use the Read TCP
Socket Status instruction (SktGetTCPstatus) to see if the connection is closed. Immediately close the
socket at the local node if the TCP socket at the remote node is closed.
• If the remote node’s TCP socket closes without warning, the data to send may remain in the buffer at
the local node. The remaining data is discarded in the local node’s TCP close processing. The steps
that are required in applications to avoid this include sending data from the sending node that permits
closing and closing the socket only after checking the remote node.
• While open processing is performed for a TCP socket, a port that was closed first cannot be opened
again for 60 seconds from the time the close processing is performed for the remote socket. However,
this is not true if you specified 0 (automatic assignment by the Unit) as the port for the SktTCPConnect
instruction.
• You can use Connect from another socket to open a connection to a socket that was opened with
Accept. A connection is not opened if you try to use Connect from another socket to open a connection
to a socket that was opened with Connect. Also, a connection is not opened if you attempt to use
Accept from another socket to open a socket that was opened with Accept. Furthermore, you cannot
use Connect from more than one other node to establish multiple connections with a single TCP
socket that was opened with Accept on the built-in EtherNet/IP port.
• You can use the keep-alive function for TCP sockets at the built-in EtherNet/IP port. The keep alive
function checks whether a connection is normally established when no data is sent or received for a
certain period on the communications line where the connection was established. The built-in Ether-
Net/IP port responds to checks from other nodes even if keep alive is not specified.

JPV 23/07/2021 Page 5/7


Function Block MTCP_Client_NJ Modbus TCP Client for NJ Controller

Modbus protocol
I/O memory area (CIO) Read Multiple Coils Fn01

Example: read 19 bits (CIO 0001.04 to 0002.06)


Request Response
Data Data
Function Code 0x01 Function Code 0x01
Starting Address(H) 0x00 Byte Count 0x03
Starting Address(L) 0x14 Coil Status 27-20 0xCD
Quantity of Coils(H) 0x00 Coil Status 35-28 0x6B
Quantity of Coils(L) 0x13 Coil Status 38-36 0x05

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.

Reads registers in I/O memory area Fn03

Example: read 3 words (DM 1000 to DM 1002)


Request Response
Data Data
Function Code 0x03 Function Code 0x03
Starting Address(H) 0x03 Byte Count 0x06
Starting Address(L) 0xE8 Register Value(H)DM1000 0xAB
Quantity of Registers(H) 0x00 Register Value(L) DM1000 0x12
Quantity of Registers(L) 0x03 Register Value(H)DM1001 0x56
Register Value(L) DM1001 0x78
Register Value(H)DM1002 0x97
Register Value(L) DM1002 0x13

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

Writes single coil Fn05

Example: write 1 coil. (0002.02 ON)


Request Response
Data Data
Function Code 0x05 Function Code 0x05
Output Address(H) 0x00 Output Address(H) 0x00
Output Address(L) 0x22 Output Address(L) 0x22
Output Value(H) 0xFF Output Value(H) 0xFF
Output Value(L) 0x00 Output Value(L) 0x00

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.

JPV 23/07/2021 Page 6/7


Function Block MTCP_Client_NJ Modbus TCP Client for NJ Controller

Writes single register Fn06

Example: write &h3AC5 to register 2000.


Request Response
Data Data
Function Code 0x06 Function Code 0x06
Register Address(H) 0x07 Register Address(H) 0x07
Register Address(L) 0xD0 Register Address(L) 0xD0
Register Value(H) 0x3A Register Value(H) 0x3A
Register Value(L) 0xC5 Register Value(L) 0xC5

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2000 3 A C 5

Writes registers Fn10

Example: write 2 words into registers 1000-1001.


Request Response
Data Data
Function Code 0x10 Function Code 0x10
Starting Address(H) 0x03 Starting Address(H) 0x03
Starting Address(L) 0xE8 Starting Address(L) 0xE8
Quantity of Registers(H) 0x00 Quantity of Registers(H) 0x00
Quantity of Registers(L) 0x02 Quantity of Registers(L) 0x02
Byte Count 0x04
Registers Value(H) 0x3A
Registers Value(L) 0xC5
Registers Value(H) 0x97
Registers Value(L) 0x13

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

Read and Write multiple registers Fn17

Example: read registers 1 & 2 (1001-2002) and write CAFE into register 0003.
Requête Réponse
Donnée 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

JPV 23/07/2021 Page 7/7

You might also like