- Accessing virtual serial ports in Linux using Serial Device Server
- Introduction
- Installing prerequisites
- Creating ports
- Checking file names
- Using socat
- Using lsof
- External Links
- JustChecking’s Weblog On All
- Life, science, programming, and all… convincing and meaningless.
- HowTo: Virtual Serial Ports on Linux using socat, and more
- To create a pair of VSP’s
- Connecting executable and VSP
- Virtual network interfaces
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:
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.
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: sudo ./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:
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’ thus the ports are inaccessible. An uninstall/reinstall of the driver helps but it feels like some band aid.
If this happens, I suggest:
1. Delete the NPORT related tty* ports
2. Run ‘sudo ./mxaddsvr [NPort IP Address] 2’ again
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.
Introduction
Virtual serial ports are commonly used in development of programs using serial connection as well as debugging existing applications — to check what kind of data is transmitted over a serial connection.
The idea of virtual serial port is to create two virtual serial ports linked with a null modem cable, attaching one end to tested application and the other end to (usually) serial terminal (e.g. Cutecom).
Installing prerequisites
To create a pair of ports you will need a utility called «socat». It is located in «universe» repository, so you should be able to find it in Synaptic. Alternatively install using
sudo apt-get install socat
Creating ports
After installing socat you have to execute following command:
That should create and link the virtual port pair for as long as socat is running.
If you are like me and want a little more feedback on what is happening instead of a command line that is hanging, use verbose mode instead:
You can put the -d argument up to four times, increasing the information fed back to you each time.
Checking file names
Using socat
The easiest way to tell which file names are assigned to these virtual ports is to tell socat to print information about opened pseudo terminals during initialization using following options (verbose mode):
Your applicantion should connect to these files.
Using lsof
Another way is to list socat’s open files:
You should notice file /dev/ptmx, that is pseudo terminal multiplexer and directly below each entry should be listed /dev/pts/X file, which are the ends of created pair.
External Links
VirtualSerialPort (последним исправлял пользователь pc-212-51-221-162 2010-10-28 11:21:26)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details
JustChecking’s Weblog On All
Life, science, programming, and all… convincing and meaningless.
HowTo: Virtual Serial Ports on Linux using socat, and more
socat (SOcket CAT) – multipurpose relay – is a command line based utility that establishes two bidirectional byte streams and transfers data between them.
socat is #4 on the Top 100 Network Security Tools list, available in most distro repositories (on Debian/Ubuntu sudo apt-get install socat does the trick), really light on resources, and very efficient.
Sounds simple, does wonders!
Sidenote: socat is actually #71 on the mentioned list, but since socat is a much enhanced version of netcat, which is #4, it seems to me logical to count socat as #4 also.
One of the wonders you can do is creating pairs of “virtual” ports/interfaces/sockets etc., even hybrid pairs like port-socket, etc., where one (or both) ends of the pair can also be real objects. See socat man page for more details.
Creating pairs of virtual serial ports (VSP), is quite often wanted feature, yet it’s hard to find a solution online (try Googling it)… Pair of VSP’s is very useful to have esp. if you’re into embedded device programming, where many embedded development kits support debugging via serial link – PharLap in my case. Yes, it does have Ethernet debugging since ETS 14, but it’s a very unstable option.
Even if you’re using Ethernet debugging, it’s often much easier to have your embedded system in a virtual machine (VirtualBox, VMWare etc.), and do the debugging via virtual NIC.
To cut the talk short, many thanks to Gerhard Rieger, author of socat, for the following tips!
To create a pair of VSP’s
socat -d -d pty,raw,echo=0 pty,raw,echo=0
and that’s it! As long as the socat is running, you have a pair of VSP’s open (their names are printed by socat on initialization). See socat man page for more details on what the above command does.
Connecting executable and VSP
socat -d -d pty,raw,echo=0 "exec:myprog . pty,raw,echo=0"
where the executable myprog will be connected with the VSP through stdio.
Virtual network interfaces
In a similar fashion, you can create pairs of virtual network interfaces – tun/tap devices (again, this pair exists as long as the master process – socat – is alive):
sudo socat -d -d tun:10.0.0.1/8 tun:192.168.0.1/24
Yes, that’s it, you’ve got a pair of virtual network interfaces!