Accessing virtual serial ports in Linux using Serial Device Server

Contrary to popular belief, serial port technology from the IT Stone-ages is still alive and kicking.

In the industrial IoT area, serial communication has been proven to be reliable, simple and trustworthy. So if you enter a regular plant, sooner or later you will find some thirty year old device which is still talking serial.

The protocol on serial ports can be a very simple, human readable output. You see this a lot with devices which are/were connected to a (matrix) printer. Each measurement (like the weight from a scale) was printed on one line. You still read these simple lines deviced by carriage return/line feed.

But output can also be more elaborate like NMEA, Telegram or even more exotic formats.

If we are looking at the RS-232 protocol, there is an important physical limitation: the cable length. The communication becomes less reliable when the length of the cable is increased. It’s possible to compensate with eg. a lower baudrate or better quality of cable. The rule of thumb is a maximum of 50 feet/15 meters but I recommend up to 13/feet/3 meters.

Is it possible to bypass this limitation? Yes, this enters the virtual serial port.

With this solution, the physical cable is plugged in into a so-called Device Server or Device Gateway. This gateway is then connected to the same IP network as your target device is (eg. an industrial PC). On this industrial PC, virtual port drivers are loaded which mimic the physical ports on the gateway. The network and gateway becomes transparent for the RS-232 protocol.

So the maximum length of a serial cable can be extended dramatically with the reach of the local IP network.

Let’s check out how this works with a Moxa NPort 5210A Serial Device Server.

This is an older model but it still works fine. It has two serial ports supporting RS-232. Having two ports is very convenient to test the solution as we will see later on.

Disclaimer: I bought this device from a second-hand hardware site. But I’m happy to review a newer device if you have a spare one 🙂

Serial ports for Windows

This Moxa NPort passes the data from and to its physical ports to virtual ‘RealCOM’ ports on your target. So we need a couple of things to set up. First we need to download two tools from the Moxa site:

A. Device Search utility

Your Moxa has no correct IP address for your local network yet. This tool scans the network for NPort devices. After logging in, you can set a correct (fixed) IP address for your moxa. Your target device will reach out to your Moxa so this address is vital. Check out if you can ping the Nport from your Industrial PC.

B. NPort Administrator

Once the IP address is available, this second tool can install the RealCOM serial port drivers for you:

Serial sort for Linux

The shiny GUI Administrator tool for Windows is not available for Linux.

Note: I have not found a ‘Device Search utility’ for Linux. But you can use the Windows version just for giving the NPort a fixed IP address.

Now in Linux, we will add RealTTY serial port support for the Moxa NPort using command line tooling. For this, we need to download the Linux drivers, install them in Linux and attach the drivers to the Moxa NPort.

At the same Moxa site, drivers are available. The following scripts installs the drivers and creates virtual serial ports:

1. Download the driver tarball from:
https://www.moxa.com/Moxa/media/PDIM/S100000200/moxa-real-tty-drivers-for-linux-3.x.x-4.x.x-driver-v1.19.tgz

2. If you use a browser like Firefox, it will be place in the Downloads folder:
Go to the specific folder

3. Unzip the tarball:
sudo tar -xf moxa-real-tty-drivers-for-linux-3.x.x-4.x.x-driver-v1.19.tgz

4. A sub folder 'moxa' is created
cd moxa

5. Install the drivers (I ignored the secure function. Please check this twice in production):
sudo ./mxinst

6. The drivers are placed in cd /usr/lib/npreal2/driver/
cd /usr/lib/npreal2/driver/

7. Attach the Moxa NPort to your device, 2 ports are created:
./mxaddsvr [NPort IP Address] 2 
eg. ./mxaddsvr 192.168.43.177 2

Now you can check the existence of the new virtual serial ports:

8. Check the existence of the new virtual ports:
ls -l /dev/ttyr*

You will see the new serial ports. Notice the automatic elevated rights:

Testing the ports

As I mentioned, my NPort has two serial ports. So I assembled my own null modem. This is a cable for special RS232 communication, it crosses the read and write lines and does a self-confirmation of the handshake.

It’s perfect for doing a read and write action with just one step:

Now I’m able to send a message over port ttyr01 and retrieve it on port ttyr02.

First I set up a terminal which listens to incoming data for port ttyr02:

cat /dev/ttyr02

Then I send a message to port ttr01:

echo This is a message over virtual ports > /dev/ttyr01

This is what I see when I send the message:

The prompt is returned. The message is picked up by the serial port ttyr01.

Let’s check out the second port:

So the message is received!

We can send a few more messages:

Each message arrives at the other side:

Notice, while sending the messages, the read and write indication lights on the Moxa (indicated P1 and P2) are flashing. This means data is flowing through our Moxa!

Be careful with apt-get update, apt-get upgrade

There is one downside to installing drivers this way. The Moxa Nport virtual serial port driver does not survive an apt-get upgrade. After performing an upgrade, the driver is corrupted. An uninstall/reinstall of the driver helps but it feels like some band aid.

Conclusion

If your sensors or devices with serial ports are places at a large distance from your Industrial PC, a Serial Device Server or Gateway is the perfect solution for your range needs.

The same goes for Industrial PCs which lack the availability of physical Serial ports.

Advertenties

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google photo

Je reageert onder je Google account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s