Introduction
The TCP and UDP communication that we perform daily utilize ports within the devices to access the application.
For example, when a web page is requested, port 80 is used because this port number has been defined as the web server port for HTML communications with the TCP protocol. Other commonly defined ports are:
- 21 (FTP)
- 23 (Telnet)
- 57 (Mail Transfer)
- 9600 (Omron FINS)
A complete list of well-known ports can be found at List_of_TCP_and_UDP_port_numbers
It is important to note that a unique port is assigned to each specific application or protocol.
To better understand the concept of ports and protocols, a simple analogy can be applied. Imagine each device as a building with an address. This address will be the IP-address of the device, such as 192.168.250.15. Within the building are two elevators, one for UDP and one for TCP and each floor represents a different port number. If you are attempting to view a web page within the building, you would know that port 80 is used for an HTML request using the TCP protocol. Therefore, elevator TCP is chosen, floor 80 is the location of the web-server, and HTML is the message to deliver (HTML get-request) to the web server. Once the web server on port 80 using TCP receives the HTML message, a reply is sent to your request and the web page is displayed. If the wrong floor (port) is chosen, the application on that floor could not understand the HTML request, or perhaps there is no application at all.
Port Numbers
There are a total of 65535 port numbers available and the first 1024 are dedicated to a specific function. Port numbers up to 49151 are known to be used by certain applications, but can be used by any application. Port numbers from 49152 to 65535 are reserved for temporary user or allocation. A specific device does not need to have all 65535 port numbers available, especially if it only services a few specific ports as its primary function.
Sockets
To handle TCP or UDP communications, a device must already have a reservation made indicating that a connection is present, that data has arrived, and that storage for the data is available. This reservation is called a socket and is considered to be an entry point for a certain port number.
A message directed to a certain port is received and depending on this port number, then delivered to the socket that registered itself for that port number. The socket is associated to an application and that socket will inform the application of the connection status as well as perform data transfer to and from the application. The number of sockets available is typically proportional to the resources of a device.
The purpose of socket communication
In order for two devices to communicate to each other, they need to know what language (protocol) is used. This protocol allows accurate interpretation of the data being sent and received. HTML, FTP, Telnet and Omron's FINS protocols are different and each has a specific purpose. Socket communication provides the flexibility for a CJ1W-ETN21 to communicate with various known protocols in other devices even if the protocol is not implemented. The CJ1W-ETN21 has all of the tools available to perform full TCP or UDP communications to any IP-device, to any port. Additionally, it can process 8 TCP sockets and 8 UDP sockets at the same time.
CJ1W-ETN21 Socket Communication
The following service functions are available for the TPC protocol.
- Socket open passive (waiting for another device to open a connection, works as a server)
- Socket open active (actively opening a socket to another device, works as a client)
- Send data
- Receive data
- Close socket (end of communication)
The following service functions are available for the UDP protocol.
- Socket open (reserve the socket for communication)
- Receive data
- Send data
- Close socket (end of communication)
Controlling the sockets
There are two ways the socket communications can be initiated. The first implementation of socket communication in the CJ1W-ETN21 used FINS commands. Newer units from version 1.5 and above added a second method to include bit control (socket service request switches) which allows setting registers and bits for socket communications. There are also status bits available to monitor the current state of the connection.
Implementation example
In the manual Ethernet Units Construction of Applications (Cat. No. W421), section 6 details a full explanation of how socket communications function. Section 6-7-8 provides a TCP/IP communications programming example that is also attached to this article. The programming example opens a TCP socket in passive mode on port number 4096. The CJ1W-ETN21 starts waiting for another device to connect and send or receive data. The connection can be closed from the CJ1W-ETN21 or by the other device (in this case, it is preferable for the other device to close the connection as it is the initiator of the connection).
This example uses the CJ1W-ETN21 in passive mode acting as the server. To test this communication, there needs to be another device/program acting as a client. For this, it is recommended to use the free, multi-purpose tool called Multiway. This can also be downloaded from the myOmron download area.
Using Multiway, set up a terminal on a TCP connection and connect to port 4096 and to the IP-address of the CJ1W-ETN21 (step 3 below).
In the example program, there are bits to open and close the connection as well as send and receive data. The status bits show the status of the connection and if data is sent and/or received.
Communication test steps
- Set the CJ1W-ETN21 to TCP Passive Open for port 4096. In this example, socket number 1 is selected but this can also be automatic.
- Set the TCP_Open_Bit. This bit will stay on as long as no TCP connection is made to port 4096.
- In the Multiway terminal, set the IP-address of the CJ1W-ETN21 plus the destination port 4096, then press connect. The connection state will change to connected and the TCP_Open_Bit will turn off. The connection is now open and data can be sent and received.
- Enter some data in the send data area and the the TCP_Send_Bit and the data entered will appear in the Multiway Terminal (in ASCII or hexadecimal depending on the setting).
- To receive data, set the TCP_Receive_Bit.
- Enter some data in the Multiway terminal and this data will then be received by the CJ1W-ETN21 and placed in the receive data area.
Additional information
The protocol (language) determines the interpretation of the sent/received data. This is considered the application level and can be implemented in the PLC program and therefore makes it possible for the PLC to communicate with any IP-device over TCP or UDP ethernet. Examples are barcode scanners, remote serial ports and endless ethernet based devices. The application should also check the integrity of the connection and if data is properly sent and/or received. If needed, create a heartbeat signal to check if the other device is still connected and responding to determine if the connection still needs to remain open.
For debugging purposes, it can be useful to "sniff" the conversation between two devices to examine the messages and sequence closely. Wireshark is a free tool that provides sniffing features and can be downloaded from www.wireshark.org
To properly sniff communications, an ethernet switch with a port mirroring function is needed. Mirroring duplicates all data traffic to another (third) port, while non-mirroring switches only pass data from/to two ports used for both devices. Port mirroring is normally a function implemented in managed switches which are typically more expensive than unmanaged switches. However, Westermo provides an unmanaged switch with mirroring functions as the SDW-550. Setting switches 2-8 to ON will enable the mirror function to port 1.