TCP and COM port forwarding

<< Click to Display Table of Contents >>

Navigation:  Multi-Protocol MasterOPC Server > The Linux runtime system > Working with an OPC server >

TCP and COM port forwarding

We recommend debugging the OPC server configuration on Windows, and then migrating it to a Linux system - this approach will be faster to configure.

But what to do, if the Windows computer does not have access to the necessary equipment? For example, the device is connected to a remote Linux machine to the COM port, and it is not possible to connect it to the developer's computer. In this case you can forward the ports and conduct debugging as if the instruments were connected directly.

You can use a special Linux utility called socat to solve this problem:

http://www.dest-unreach.org/socat/doc/socat.html

To install socat from the repository, run a command in the terminal:

sudo apt-get install socat -y

Socat has an extensive number of startup options, which are described on the program's website. Below we will discuss the most typical use cases. We will not give a complete description of each command - you can also find a detailed description of each of them on the program's website.

COM port forwarding.

Before you can forward a COM port, you must determine the parameters of the port - its name, as well as set the speed. In order to define the name of the port use the command:

dmesg | grep tty

The command displays a list of ports.

In this case the standard port tty0 (COM1) is available and USB0 is a USB-COM converter with an FTDI chip and we will route it through - its full name is /dev/ttyUSB0. This is the port we will use in the following discussion, you will need to specify your own port name in the commands.

To see the speed settings, use the command:

sudo stty -F /dev/ttyUSB0 -a    

linux_rabota_s_opc_serverom_probros_tcp_i_com_portov1

As we can see the speed is set to 9600. If a different speed is required, then set it with a command (in this case the speed is set to 9600):

sudo stty -F /dev/ttyUSB0 9600

You can then try to send something to the port. The command is used to do this:

echo -en "ABCD" > /dev/ttyUSB0

If you have a USB converter, the transmission indicator on it should blink when you send it. If you get an access error, try to set permissions on the file (COM ports in Linux are represented as files), using the command:

sudo chmod -R 777 /dev/ttyUSB0

If all is successful, you can perform the forwarding. To redirect the stream from the TCP port to the COM port, run the socat command:

socat /dev/ttyUSB0,raw TCP-LISTEN:8282&

In this case, the socat passed the name of our COM port, with the parameter raw - raw data (ie, the data is not processed in any way, but simply forwarded). TCP:LISTEN:8282 - is the enabling of listening to TCP port 8282 (TCP server mode), all incoming data on this port will be forwarded to the COM port. The symbol & (ampersand) at the end means that the command will run in a separate thread. It is possible to omit this symbol, but in this case socat will run in the terminal thread until you press CTRL+C and other commands will not be available. Therefore it is better to run in a separate thread.

To stop socat running in a thread, you can kill the process accessing the desired port by using the command:

fuser -k 8282/tcp

After the forwarding is done, it is possible to access TCP port 8282 from the OPC server.

If you want to perform the reverse operation - from the COM port on Linux, connect as a client to the TCP port and translate to it the data coming to the COM port, then you use the command to do this:

sudo socat pty,link=/dev/ttyUSB0,raw  tcp:192.168.198.1:8282&

In this case the data from port USB0 will be sent to the TCP server located at IP address 192.168.198.1 on port 8282.

If you also need to perform the conversion from COM to TCP on the computer side, you will need:

1.COM0COM - is included in the Modbus OPC server and is located in the folder c:\Program Files\MPSSoft\MasterOPC Universal Modbus Server\com0com\. Instructions on how to install and configure this utility are included in the help system of the OPC server.

2.Serial-to-IP - a utility with a simple graphical interface that converts data from TCP port to COM, and the utility can work both in TCP client mode and in TCP server mode. You can download the utility by following this link.

linux_rabota_s_opc_serverom_probros_tcp_i_com_portov2

 
TCP port forwarding

If you want to forward one TCP port to another, use the command:

sudo socat TCP-LISTEN:502,fork,reuseaddr TCP:192.168.198.131:502&

In this case the 502 port of the Linux computer will be listened and the data coming from it will also be sent to the 502 port of the computer with IP address 192.168.198.131.

UDP port forwarding

For UDP forwarding a similar command to TCP is used:

sudo socat UDP-LISTEN:47808,fork,reuseaddr udp:192.168.198.131:47808&

In this case the UDP port 47808 (standard BACnet port) will be listening and the data coming from it will be sent to the 47808 port of the computer with IP address 192.168.198.131.