Linux Serial Port Communication

I found projects called Linux Serial Sniffer, jpnevulator, and Moni. The first two look like they do exactly what you want. The last one calls itself a monitor, but it actually looks like a standard serial communication program. Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un.x-like operating systems. How to connect to a serial port as simple as using SSH? Ask Question Asked 7 years. How can I realize two way communication with a serial port the simplest possible way on Unix/Linux? Command-line terminal tty.

This is a step-by-step guide to using the serial port from a program running under Linux; it was written for the Raspberry Pi serial port with the RaspbianWheezy distribution. However, the same code should work on other systems.

  • 2Step 1: Connect to a terminal emulator using a PC
  • 3Step 2: Test with Python and a terminal emulator

Step 0: Note whether your Raspberry Pi has Wireless/Bluetooth capability

By default the Raspberry Pi 3 and Raspberry Pi Zero W devices use the more capable /dev/ttyACM0 to communicate over bluetooth, so if you want to program the serial port to control the IO pins on the header, you should use the auxiliary UART device /dev/ttyS0 instead. On these wireless devices, it is possible switch the GPIO serial port back to /dev/ACM0 with `/boot/config.txt` directives by disabling bluetooth with `bdtoverlay=`pi3-disable-bt` or by forcing the bluetooth to use the mini-UART with `dtoverlay=pi3-miniuart-bt`. See https://www.raspberrypi.org/documentation/configuration/uart.md for details.

Step 1: Connect to a terminal emulator using a PC

Follow the instructions at RPi_Serial_Connection#Connections_and_signal_levels, and RPi_Serial_Connection#Connection_to_a_PC, so that you end up with your Pi's serial port connected to a PC, running a terminal emulator such as minicom or PuTTY.

The default Wheezy installation sends console messages to the serial port as it boots, and runs getty so you can log in using the terminal emulator. If you can do this, the serial port hardware is working.

Troubleshooting

ProblemPossible causes
Nothing at all shown on terminal emulatorConnected to wrong pins on GPIO header

Faulty USB-serial cable or level shifter

/boot/cmdline.txt and /etc/inittab have already been edited (see below)

Flow control turned on in terminal emulator

Wrong baud rate in terminal emulator

Text appears corruptedWrong baud rate, parity, or data settings in terminal emulator

Faulty level shifter

Can receive but not send

(nothing happens when you type)

Connected to wrong pins on GPIO header

Flow control turned on in terminal emulator

Faulty level shifter

Step 2: Test with Python and a terminal emulator

You will now need to edit files /etc/inittab and /boot/cmdline.txt as described at RPi_Serial_Connection#Preventing_Linux_using_the_serial_port. When you have done this - remember to reboot after editing - the terminal emulator set up in Step 1 will no longer show any output from Linux - it is now free for use by programs. Leave the terminal emulator connected and running throughout this step.

We will now write a simple Python program which we can talk to with the terminal emulator. You will need to install the PySerial package:

Now, on the Raspberry Pi, type the following code into a text editor, taking care to get the indentation correct (Note that for devices with wireless (3, zero W) you must use /dev/ttyS0 instead of /dev/ttyAMA0):

Save the result as file serialtest.py, and then run it with:

If all is working, you should see the following lines appearing repeatedly, one every 3 seconds, on the terminal emulator:

Try typing some characters in the terminal emulator window. You will not see the characters you type appear straight away - instead you will see something like this:

If you typed Enter in the terminal emulator, it will appear as the character sequence r - this is Python's way of representing the ASCII 'CR' (Control-M) character.

Example

Troubleshooting

ProblemPossible causes
Python error 'No module named serial'python-serial package not installed
Python error '[Errno 13] Permission denied: '/dev/ttyAMA0'User not in group 'dialout'

getty is still running (from /etc/inittab)

/dev/ttyAMA0 doesn't have rw access by group 'dialout'

For other problems (e.g. text appears corrupted) refer to the troubleshooting table in Step 1.

More about reading serial input

The serial connection we are using above is:

  • bi-directional - the PC transmits characters (actually, 8-bit values which are interpreted as ASCII characters) which are received by the Pi, and the Pi can transmit characters which are received by the PC.
  • full-duplex - meaning that the PC-to-Pi transmission can happen at the same time as the Pi-to-PC transmission
  • byte-oriented - each byte is transmitted and received independently of the next byte. In other words, the serial communication does not group transmitted data into packets, or lines of text; if you want to send messages longer than one byte, you will need to add your own means of grouping bytes together.

So, the line rcv = port.read(10) will wait for characters to arrive from the PC, and:

  • if it has read 10 characters, the call to read() will finish, returning those 10 characters as a string.
  • if it has been waiting for the timeout period given to serial.Serial() - in this case, 3 seconds - it will return whatever characters have arrived so far. (If no characters arrive, this will return an empty string).

Any characters which arrive after the read() call has finished will be saved (buffered) by the kernel and can be retrieved the next time you call read(). However, there is a limit to how many characters can be saved; once the buffer is full characters will be lost.

This means that a call such as port.read(10) is only useful if you know the transmitting end is going to send exactly 10 bytes of data; if it sends more that 10, they will not be returned from the call, and if it sends fewer than 10, your program will pause until the timeout has expired.

Using the readline() call

If you are reading data from the serial port organised as (possibly variable-length) lines of text, you may want to use PySerial's readline() method. To see the effect of this, replace the rcv = port.read(10) with rcv = port.readline() in the serialtest.py program above.

The documentation for readline() says it will receive characters from the serial port until a newline (Python 'n') character is received; when it gets one it returns the line of text it has got so far. You may expect, therefore, that typing a few characters on the terminal emulator, followed by the Enter key, will return those characters immediately.

However, on many terminal emulators this won't work! Pressing the Enter key sends an ASCII CR (13 decimal, Control-M) character, but a newline character is ASCII LF (10 decimal, Control-J). So, readline() will not finish until you type Control-J in the terminal (or until the timeout has expired).

PySerial's documentation gives various means of reading other newline characters; however, the most reliable solution is to do it yourself:

Retrieved from 'https://elinux.org/index.php?title=Serial_port_programming&oldid=489886'
< Serial Programming

Serial Programming: Introduction and OSI Network Model-- RS-232 Wiring and Connections-- Typical RS232 Hardware Configuration-- 8250 UART-- DOS-- MAX232 Driver/Receiver Family-- TAPI Communications In Windows-- Linux and Unix-- Java-- Hayes-compatible Modems and AT Commands-- Universal Serial Bus (USB)-- Forming Data Packets-- Error Correction Methods-- Two Way Communication-- Packet Recovery Methods-- Serial Data Networks-- Practical Application Development-- IP Over Serial Connections

  • 1The Classic Unix C APIs for Serial Communication
    • 1.1Introduction
    • 1.2Serial I/O via Terminal I/O
  • 2Serial I/O on the Shell Command Line
    • 2.3Permanent Configuration
    • 2.6uucp

The Classic Unix C APIs for Serial Communication[edit]

Introduction[edit]

Scope[edit]

This page is about the classic Unix C APIs for controlling serial devices. Languages other than C might provide appropriate wrappers to these APIs which look similar, or come with their own abstraction (e.g. Java). Nevertheless, these APIs are the lowest level of abstraction one can find for serial I/O in Unix. And, in fact they are also the highest abstraction in C on standard Unix. Some Unix versions ship additional vendor-specific proprietary high-level APIs. These APIs are not discussed here.

Actual implementations of classic Unix serial APIs do vary in practice, due to the different versions of Unix and its clones, like Linux. Therefore, this module just provides a general outline. It is highly recommended that you study a particular Unixversion's manual (man pages) when programming for a serial device in Unix. The relevant man pages are not too great a read, but they are usually complete in their listing of options and parameters. Together with this overview it should be possible to implement programs doing serial I/O under Unix.

Basics[edit]

Linux, or any Unix, is a multi-user, multi-tasking operating system. As such, programs usually don't, and are usually not allowed to, access hardware resources like serial UARTs directly. Instead, the operating system provides

  1. low-level drivers for mapping the device into the file system (/dev and/or /device/ file system entries),
  2. the standard system calls for opening, reading, writing, and closing the device, and
  3. the standard system call for controlling a device, and/or
  4. high-level C libraries for controlling the device.

The low-level driver not only maps the device into the file system with the help of the kernel, it also encapsulates the particular hardware. The user often does not even know or care what type of UART is in use.

Classic Unix systems often provide two different device nodes (or minor numbers) for serial I/O hardware. These provide access to the same physical device via two different names in the /dev hierarchy. Which node is used affects how certain serial control signals, such as DCD (data carrier detect), are handled when the device is opened. In some cases this can be changed programmatically, making the difference largely irrelevant. As a consequence, Linux only provides the different devices for legacy programs.

Device names in the file system can vary, even on the same Unix system, as they are simply aliases. The important parts of a device name (such as in /dev) are the major and minor numbers. The major number distinguishes a serial port, for example, from a keyboard driver, and is used to select the correct driver in the kernel. Note that the major number differs between different Unix systems. The minor number is interpreted by the device driver itself. For serial device drivers, it is typically used to detect which physical interface to use. Sometimes, the minor number will also be used by the device driver to determine the DCD behavior or the hardware flow control signals to be used.

The typical (but not standardized, see above) device names under Unix for serial interfaces are:

/dev/ttyxxx
Normal, generic access to the device. Used for terminal and other serial communication (originally for teletypes). More recently, they are also used in modem communication, for example, whereas the /dev/cuaxxx was used on older systems.
See the following module on how terminal I/O and serial I/O relate on Unix.
/dev/cuaxxx
Legacy device driver with special DCD handling. Typically this was used for accessing a modem on old Unix systems, such as running the UUCP communication protocol over the serial line and the modem. The cu in the name stands for the [[#cu]] program. The a for ACU (automatic call unit).

The xxx part in the names above is typically a one or two digit number, or a lowercase letter, starting at 'a' for the first interface.

PC-based Unix systems often mimic the DOS/Windows naming for the devices and call them /dev/comxxx. Linux system generally call serial ports /dev/ttySxxx instead.

To summarize, when programming for the serial interface of a Unix system it is highly advisable to provide complete configuration for the device name. Not even the typical /dev path should be hard coded.

Note, devices with the name /dev/ptyxxx are pseudo terminal devices, typically used by a graphical user interface to provide a terminal emulator like xterm or dtterm with a 'terminal' device, and to provide a terminal device for network logins. There is no serial hardware behind these device drivers.

Serial I/O via Terminal I/O [edit]

Basics[edit]

Serial

Serial I/O under Unix is implemented as part of the terminal I/O capabilities of Unix. And the terminal I/O capabilities of Unix were originally the typewriter/teletype capabilities. Terminal I/O is not limited to terminals, though. The terminal I/O API is used for communication with many serial devices other than terminals, such as modems and printers.

The terminal API itself has evolved over time. These days three terminal APIs are still used in Unix programs and can be found in recent Unix implementations. A fourth one, the very old one from Unix Version 6 exists, but is quite rare these days.

The three common ones are:

  1. V7, 4BSD, XENIX style device-specific ioctl-based API,
  2. An old one called termio
  3. A newer one (although still already a few decades old), which is called termios (note the additional 's').

The newer termios API is based on the older termio API, and so the two termio... APIs share a lot of similarities. The termios API has also undergone changes since inception. For example, the method of specifying the baud rate has changed from using pre-defined constants to a more relaxed schema (the constants can still be used as well on most implementations).

Systems that support the newer termios often also support the older termio API, either by providing it in addition, or by providing a termios implementation with data structures which can be used in place of the termio data structures and work as termio. These systems also often just provide one man page under the older name termio(7) which is then in fact the termios man page, too.

In addition, some systems provide other, similar APIs, either in addition or as a replacement. termiox is such an API, which is largely compatible with termio and adds some extensions to it taken from termios. So termiox can logically be seen as an intermediate step between termio and termios.

The terminal I/O APIs rely on the standard system calls for reading and writing data. They don't provide their own reading/writing functions. Reading and writing data is done via the read(2) and write(2) system calls. The terminal I/O APIs just add functions for controlling and configuring the device. Most of this happens via the ioctl(2) system call.

Unfortunately, whichever of the standard APIs is used, one fact holds for all of them: They are a slight mess. Well, not really. Communication with terminals was and is a difficult issue, and the APIs reflect these difficulties. But due to the fact that one can do 'everything' with the APIs, it is overwhelming when one 'just' wants to do some serial communication. So why is there no separate serial-I/O-only API in Unix? There are probably two reasons for this:

  1. Terminals/teletypes were the first, and apparently very important, serial devices which were connected to Unix. So that API was created first.
  2. Once the API was there, there was no need to create a separate one for serial I/O only, since a large part of terminal I/O is serial I/O, and all needed features were already there in the terminal I/O API.

So which API should one use? There is one good reason to use the old V7 API. It is the simplest among the APIs - after going through some initialization woes on modern Unix systems. In general, however, the newer termios API makes the most sense, although it is the most complex one.

Linux

Line Discipline[edit]

When programming serial interfaces on Unix, there is one phrase - line discipline - which can drive programmers crazy. The line discipline provides the hardware-independent interface for the communication between the computer and the terminal device. It handles such things as editing, job control, and special character interpretation, and performs transformations on the incoming and outgoing data.

This is useful for terminal communication (e.g. when a backspace character should erase the latest character from the send buffer before it goes over the wire, or when different end-of-line character sequences between the terminal and the computer need to be converted). These features are, however, hardly useful when communicating with the plethora of other serial devices, where unaltered data communication is desired.

Much of the serial programming in Unix is hitting the line discipline which is in use over the head so it doesn't touch the data. Monitoring what actually goes over the wire is a good idea.

Unix V6/PWB[edit]

Unix Bell Version 6 with the programmer's workbench (PWB) was released in 1975 to universities. It was the first Unix with an audience outside AT&T. It already had a terminal programming API. Actually, at that point it was the typewriter API. That API is not described here in depth.

The usage of this API can in theory be identified by the presence of the following signature in some source code:

In theory, because at that time the C language was still a little bit different.

data is supposed to point to a

structure. That structure later became struct sgttyb in Unix V7. Finding the V6 API in source code should be rare. Anyhow, recent Unix versions and clones typically don't support this API any more.

Unix V7[edit]

See Serial Programming:Unix/V7

termios[edit]

termios is the API that is in general recommended for serial I/O in Unix. A simple terminal program with termios can look like it follows. Please note this program is not intended as a general framework for own programs. It lacks error handling, doesn't buffer data, and uses very inefficient polling, wasting lot of CPU cycles. The program just demonstrates some basics for serial I/O:

See Serial_Programming:Unix/termios

termio / ioctl(2) [edit]

See Serial Programming:Unix/termio

Serial I/O on the Shell Command Line [edit]

Introduction[edit]

It is possible to do serial I/O on the Unix command line. However, the available control is limited. Reading and writing data can be done with the shell I/O redirections like <, >, and |. Setting basic configuration, like the baud rate, can be done with the stty (set terminal type) command.

There is also libserial for Linux. It's a simple C++ class whichhides some of the complexity of termios.

Configuration with stty[edit]

The Unix command stty allows one to configure a 'terminal'. Since all serial I/O under Unix is done via terminal I/O, it should be no surprise that stty can also be used to configure serial lines. Indeed, the options and parameters which can be set via stty often have a 1:1 mapping to termio/termios. If the explanations regarding an option in the stty(1) man page is not sufficient, looking up the option in the termio/termios man page can often help.

Php Serial Port Communication Linux

On 'modern' (System V) Unix versions, stty changes the parameters of its current standard input. On older systems, stty changes the parameters of its current standard output. We assume a modern Unix is in use here. So, to change the settings of a particular serial interface, its device name must be provided to stty via an I/O redirect:

On some systems, the settings done by stty are reverted to system defaults as soon as the device is closed again. This closing is done by the shell as soon as the stty parameters < /dev/com0 command has finished. So when using the above command, the changes will only be in effect for a few milliseconds.

One way to keep the device open for the duration of the communication is to start the whole communication in a sub shell (using, for example, '( ... )'), and redirecting that input. So to send the string 'ATI0' over the serial line, one could use:

Interweaving sending and receiving data is difficult from the command line. Two processes are needed; one reading from the device, and the other writing to the device. This makes it difficult to coordinate commands sent with the responses received. Some extensive shell scripting might be needed to manage this.

A common way to organize the two processes is to put the reading process in the background, and let the writing process continue to run in the foreground. For example, the following script configures the device and starts a background process for copying all received data from the serial device to standard output. Then it starts writing commands to the device:

If there is a chance that a response to some command might never come, and if there is no other way to terminate the process, it is advisable to set up a timeout by using the alarm signal and trap that signal (signal 14), or simply kill the process:

or

Permanent Configuration[edit]

Overview[edit]

It is possible to provide a serial line with a default configuration. On classic Unix this is done with entries in the /etc/ttytab configuration file, on newer (System V R4) systems with /etc/ttydefs.

The default configurations make some sense when they are used for setting up terminal lines or dialup lines for a Unix system (and that's what they are for). However, such default configurations are not of much use when doing some serial communication with some other device. The correct function of the communication program should better not depend on some operating system configuration. Instead, the application should be self-contained and configure the device as needed by it.

/etc/ttytab[edit]

The ttytab format varies from Unix to Unix, so checking the corresponding man page is a good idea. If the device is not intended for a terminal (no login), then the getty field (sometimes also called the program field, usually the 3rd field) for the device entry should be empty. The init field (often the 4th field) can contain an initialization command. Using stty here is a good idea. So, a typical entry for a serial line might look like:

/etc/ttydefs[edit]

Just some hints:

/etc/ttydefs provides the configuration as used by the ttymon program. The settings are similar to the settings possible with stty.

ttymon is a program which is typically run under control of the Service Access Controller (SAC), as part of the Service Access Facility (SAF).

TODO: Provide info to set up all the sac/sacadm junk.

/etc/serial.conf[edit]

Just some hints:

A Linux-specific way of configuring serial devices using the setserial program.

tty[edit]

tty with the -s option can be used to test if a device is a terminal (supports the termio/termios ioctl()'s). Therefore it can also be used to check if a given file name is indeed a device name of a serial line.

tip[edit]

It is a simple program for establishing a terminal connection with a remote system over a serial line. tip takes the necessary communication parameters, including the parameters for the serial communication, from a tip-specific configuration file. Details can be found in the tip(1) manual page.

Example:

To start the session over the first serial interface (here ttya):

To leave the session:

uucp[edit]

Overview[edit]

Uucp (Unix-to-Unix-Copy) is a set of programs for moving data over serial lines/modems between Unix computers. Before the rise of the Internet uucp was the heart and foundation of services like e-mail and Usenet (net news) between Unix computers. Today uucp is largely insignificant. However, it is still a good choice if two or more Unix systems should be connected via serial lines/modems.

The uucp suite also contains command line tools for login over a serial line (or another UUCP bearer to a remote system. These tools are cu and ct. They are e.g. useful when trying to access a device connected via a serial line and when debugging some serial line protocol.

cu[edit]

cu 'call another UNIX system', does what the name implies. Only, that the other system does not have to be a UNIX system at all. It just sets up a serial connection, possibly by dialing via a modem.

cu is the oldest Unix program for serial communication. It's the reason why some serial devices on classic Unix systems are called something like /dev/cul0 and /dev/cua0. Where cu of course stands for the cu program supposed to use the devices, l stands for line - the communication line, and a for acu (automatic call unit).

Note:
An ACU is kind of a modem. Modern modems work slightly different and don't provide separate serial interfaces for dialing and communicating with the remote side. Instead they do both over the same serial interface, using some kind of inband signaling. See Serial Programming:Modems and AT Commands.

ct[edit]

ct is intended to spawn a login to a remote system over a modem line, serial line, or similar bearer. It uses the uucp devices list to find the necessary dialing (modem) commands, and the serial line settings.

System Configuration[edit]

inittab, ttytab, SAF configuration

Other Serial Programming Articles[edit]

Linux Serial Port Communications

Serial Programming: Introduction and OSI Network Model-- RS-232 Wiring and Connections-- Typical RS232 Hardware Configuration-- 8250 UART-- DOS-- MAX232 Driver/Receiver Family-- TAPI Communications In Windows-- Linux and Unix-- Java-- Hayes-compatible Modems and AT Commands-- Universal Serial Bus (USB)-- Forming Data Packets-- Error Correction Methods-- Two Way Communication-- Packet Recovery Methods-- Serial Data Networks-- Practical Application Development-- IP Over Serial Connections

External links[edit]

Linux Serial Port Communication C++

  • pySerial helps Python programmers use the serial port.

C++ Serial Port Communication Linux Example

Retrieved from 'https://en.wikibooks.org/w/index.php?title=Serial_Programming/Serial_Linux&oldid=3421161'