Setup Virtual Serial Ports Using tty0tty in Linux

Before starting off with this tutorial, let's get on with a little bit of background on serial communication, null modems, and virtual ports.

A serial port (COM port) is an asynchronous port on the computer used to connect a serial device and capable of transmitting data one bit at a time or serially. Most serial ports on computers conform to the RS-232C or RS-422 standards. A serial port is a physical interface for communicating digital data between a DTE (Data Terminal Equipment) -usually your computer and a DCE (Data Communication Equipment) – your modem or another serial device. A standard RS-232 cable is used to connect a DCE device to a DTC device via their serial ports.

You can't always expect to pair up DTE and DCE equipment. The devices can be configured as either DTE or DCE. The DTE interface can be used by both your PC and the attached serial device. However, the DTE interface is designed to work with a DCE device. If you want to connect two DTE devices together, a special type of serial cable is required called a "null modem" cable. A null modem cable makes the other end of the PC or device's DTE interface look like a DCE interface.

Null modems cables are commonly used for data transfer between computers or two software packages running on the same computer. For example, if you are using a single computer with two serial ports, you can assign the first serial port to one software and the second serial port to the other software. Connecting a physical null modem cable between the serial ports would then allow the two programs to communicate with each other. Although this works just fine, the null modem cable makes the computer more susceptible to RFI because of the physical cable looped between two serial ports.

null_modem_emulator

A virtual serial port allows you to emulate a physical serial port and thus removes the necessity of bulky cable and physical communication interfaces. Such virtual ports allow your software packages to communicate using the computer's internal memory. A special type of software that combines two virtual serial ports together and allows data to pass between them is known as a null modem emulator.

Let's move on to our topic now. The Linux null modem emulator (tty0tty) is a kernel-module virtual serial port driver for Linux. This creates virtual tty ports pairs and you can use any pair to establish communication between two legacy serial applications.

Installing tty0tty:

  1. Download the tty0tty package from one of these sources:
    1.1 http://sourceforge.net/projects/tty0tty/files/
    1.2 clone the repo https://github.com/freemed/tty0tty
  2. Extract it
    2.1 tar xf tty0tty-1.2.tgz
  3. Build the kernel module from provided source
    3.1 cd tty0tty-1.2/module
    3.2 make
  4. Copy the new kernel module into the kernel modules directory
    4.1 sudo cp tty0tty.ko /lib/modules/$(uname -r)/kernel/drivers/misc/
  5. Load the module
    5.1 sudo depmod
    5.2 sudo modprobe tty0tty
    5.3 You should see new serial port/libs in /dev/ (ls /dev/tnt*)
  6. Give appropriate permissions to the new serial ports
    6.1 sudo chmod 666 /dev/tnt*

You can now access the serial ports as /dev/tnt0 (1,2,3,4 etc)

Note that the consecutive ports are interconnected. For example, /dev/tnt0 and /dev/tnt1 are connected as if using a direct cable.

Persisting across boot:

Edit the file /etc/modules (Debian) or /etc/modules.conf and add the following line:
tty0tty

Note that this method will not make the module persist over kernel updates so if you ever update your kernel, make sure you build tty0tty again and repeat the process.

References

Er. Ruraj Joshi's answer to a question in askubuntu.com

FlexRadio Systems Knowledge Center

Leave a Reply