- Working with the serial console
- Contents
- Configure console access on the target machine
- Boot loader
- GRUB Legacy
- rEFInd
- Syslinux
- Kernel
- getty
- Making Connections
- Connect using a terminal emulator program
- Command line
- And, for Windows
- Graphical front-ends
- Installing Arch Linux using the serial console
- Debugging an unresponsive machine using a serial console
- Troubleshooting
- Ctrl+c and Minicom
- Resizing a terminal
- Missing ports on multi-port expansion cards
- Serial Programming/Serial Linux
- Contents
- The Classic Unix C APIs for Serial Communication [ edit | edit source ]
- Introduction [ edit | edit source ]
- Scope [ edit | edit source ]
- Basics [ edit | edit source ]
- Serial I/O via Terminal I/O [ edit | edit source ]
- Basics [ edit | edit source ]
- Line Discipline [ edit | edit source ]
- Unix V6/PWB [ edit | edit source ]
- Unix V7 [ edit | edit source ]
- termios [ edit | edit source ]
- termio / ioctl(2) [ edit | edit source ]
- Serial I/O on the Shell Command Line [ edit | edit source ]
- Introduction [ edit | edit source ]
- Configuration with stty [ edit | edit source ]
- Permanent Configuration [ edit | edit source ]
- Overview [ edit | edit source ]
- /etc/ttytab [ edit | edit source ]
- /etc/ttydefs [ edit | edit source ]
- /etc/serial.conf [ edit | edit source ]
- tty [ edit | edit source ]
- tip [ edit | edit source ]
- uucp [ edit | edit source ]
- Overview [ edit | edit source ]
- cu [ edit | edit source ]
- ct [ edit | edit source ]
Working with the serial console
An Arch Linux machine can be configured for connections via the serial console port, which enables administration of a machine even if it has no keyboard, mouse, monitor, or network attached to it.
Installation of Arch Linux is possible via the serial console as well.
A basic environment for this scenario is two machines connected using a serial cable (9-pin connector cable). The administering machine can be any Unix/Linux or Windows machine with a terminal emulator program (PuTTY or Minicom, for example).
The configuration instructions below will enable boot loader menu selection, boot messages, and terminal forwarding to the serial console.
Contents
Configure console access on the target machine
Boot loader
When using GRUB with a generated grub.cfg , edit /etc/default/grub and enable serial input and output support:
Next add the GRUB_SERIAL_COMMAND variable and set the options for the serial connection. For COM1 ( /dev/ttyS0 ) with baud rate of 115200 bit/s:
Read GRUB’s manual on Using GRUB via a serial line and the serial command for detailed explanation of the available options.
GRUB Legacy
Edit the GRUB Legacy configuration file /boot/grub/menu.lst and add these lines to the general area of the configuration:
rEFInd
rEFInd supports serial console only in text mode. Edit refind.conf and uncomment textonly .
Syslinux
To enable serial console in Syslinux, edit syslinux.cfg and add SERIAL as the first directive in the configuration file.
For COM1 ( /dev/ttyS0 ) with baud rate of 115200 bit/s:
The serial parameters are hardcoded to 8 bits, no parity and 1 stop bit.[1]. Read Syslinux Wiki:Config#SERIAL for the directive’s options.
Kernel
Kernel’s output can be sent to serial console by setting the console= kernel parameter. The last specified console= will be set as /dev/console .
getty
At boot, systemd-getty-generator(8) will start a getty instance for each console specified in the kernel command line.
If you have not configured console= in kernel command line start serial-getty@device.service . For /dev/ttyS0 (COM1) that would be serial-getty@ttyS0.service . Enable the service to start it at boot.
Unless specified otherwise in the kernel command line, getty will be expecting 38400 bit/s baud rate, 8 data bits, no parity and one stop bit-times.
Making Connections
Connect using a terminal emulator program
Perform these steps on the machine used to connect the remote console.
Command line
dterm
dterm AUR is a tiny serial communication program. If you invoke it without parameters, it will connect to /dev/ttyS0 at 9600 baud by default. The following example connect to /dev/ttyS0 at 115200 baud, with 8 data bits, no parity bit and 1 stop bit-times:
See its homepage[2] for more examples.
Minicom
minicom can be obtained from the official repositories. Start Minicom in setup mode:
Using the textual navigation menu, change the serial port settings to the following:
Press Enter to exit the menus (pressing Esc will not save changes). Remove the modem Init and Reset strings, as we are not connecting to a modem. To do this, under the Modem and Dialing menu, delete the Init and Reset strings. Optionally save the configuration by choosing save setup as dfl from the main menu. Restart minicom with the serial cable connected to the target machine. To end the session, press Ctrl+A followed by Ctrl+X .
picocom
picocom is a tiny dumb-terminal emulation program that is very like minicom, but instead of mini, it is pico. The following example connect to ttyS0 at 9600 bps:
See its manual for detailed usage.
Screen
GNU Screen is able to connect to a serial port. It will connect at 9600 baud by default:
A different baud rate (e.g. 115200) may be specified on the command line.
To end the session, press Ctrl+a followed by K . Alternatively, press Ctrl+a , type :quit and confirm it by pressing Enter .
Serialclient
Serialclient[3] is a CLI client for serial connection written in ruby. Install ruby package, then install it with the following:
Then, you can use like this:
And, for Windows
On Windows machines, connect to the serial port using programs like PuTTY[4] or Terminalbpp[5].
Graphical front-ends
cutecom AUR is another gui enabled serial monitor.
putty is also available for Linux.
moserial is a gtk-based serial terminal, primarily intended for technical users and hardware hackers who need to communicate with embedded systems, test equipment, and serial consoles.
Installing Arch Linux using the serial console
- Connect to the target machine using the method described above.
- Boot the target machine using the Arch Linux installation CD.
- When the bootloader appears, select Boot Arch Linux () and press Tab to edit
- Append console=ttyS0,115200 and press Enter .
- Now systemd should detect ttyS0 and spawn a serial getty on it. Login as root and start the installation as usual.
Debugging an unresponsive machine using a serial console
Even though [7] has only raw and terse instructions, it presents the full scene. It is important to note that here, the machine under test got unresponsive in a reproducible manner. And that it happened during normal operation. So it could be accessed normally before it needed debugging. However, in general, the serial console is also useful for debugging boot issues. Perhaps by configuring the boot loader by hand at machine startup time. Also note the mentioned netconsole within the P.S paragraph of the external link from this section.
Troubleshooting
Ctrl+c and Minicom
If you are having trouble sending a Ctrl+c command through minicom you need to switch off hardware flow control in the device settings ( minicom -s ), which then enables the break.
Resizing a terminal
Unlike ssh, serial connections do not have a mechanism to transfer something like SIGWINCH when a terminal is resized. This can cause weird problems with some full-screen programs (e.g. less ) when you resize your terminal emulator’s window.
Resizing the terminal via stty is a workaround:
However, this requires you to manually input the proper geometry. The following methods should be simpler.
1. There is a lesser-known utility called resize , shipped with xterm , that can solve this problem. Invoke it without parameters after you resize the terminal emulator’s window:
2. If you do not want to install xterm, it is possible to do the same work via a shell function. Put the following function into your zshrc and invoke it without parameters after resizing the terminal emulator’s window:
Missing ports on multi-port expansion cards
This article or section needs expansion.
The number of serial ports using the generic 8250 driver on the default kernel configuration is set to 4 at runtime with a maximum of 32. This will prevent the creation of /dev/ttyS4 and above. Counting the typical built in serial port on the motherboard this prevents the use of the 4th serial port on a 4 port expansion card.
Источник
Serial Programming/Serial Linux
Contents
The Classic Unix C APIs for Serial Communication [ edit | edit source ]
Introduction [ edit | edit source ]
Scope [ edit | edit source ]
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 Unix version’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 | edit source ]
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
- low-level drivers for mapping the device into the file system (/dev and/or /device/ file system entries),
- the standard system calls for opening, reading, writing, and closing the device, and
- the standard system call for controlling a device, and/or
- 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 | edit source ]
Basics [ edit | edit source ]
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:
- V7, 4BSD, XENIX style device-specific ioctl-based API,
- An old one called termio
- 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:
- Terminals/teletypes were the first, and apparently very important, serial devices which were connected to Unix. So that API was created first.
- 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.
Line Discipline [ edit | edit source ]
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 | edit source ]
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 | edit source ]
termios [ edit | edit source ]
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:
termio / ioctl(2)
[ edit | edit source ]
Serial I/O on the Shell Command Line
[ edit | edit source ]
Introduction [ edit | edit source ]
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 which hides some of the complexity of termios.
Configuration with stty [ edit | edit source ]
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.
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 trap that signal (signal 14), or simply kill the process:
Permanent Configuration [ edit | edit source ]
Overview [ edit | edit source ]
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 | edit source ]
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 | edit source ]
You can help Wikibooks by expanding it.
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 | edit source ]
You can help Wikibooks by expanding it.
Just some hints:
A Linux-specific way of configuring serial devices using the setserial program.
tty [ edit | edit source ]
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 | edit source ]
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.
To start the session over the first serial interface (here ttya):
To leave the session:
uucp [ edit | edit source ]
Overview [ edit | edit source ]
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 | edit source ]
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 | edit source ]
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.
Источник