- Writing to the serial port from the Linux command line
- 3 Answers 3
- Using Screen:
- echo
- printf
- Working with the serial console
- Configure console access on the target machine
- Boot loader
- GRUB
- GRUB Legacy
- rEFInd
- Syslinux
- Kernel
- getty
- Making Connections
- Connect using a terminal emulator program
- dterm
- Minicom
- picocom
- Screen
- Serialclient
- tinyserial
- tio
- Graphical front-ends
- Windows clients
- 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
Writing to the serial port from the Linux command line
The device starts the requested operation. When I try to accomplish the same operation from a stand-alone Debian box or from a Debian VirtualBox instance of the same Windows machine, I had no luck so far. Here are equivalent Linux commands (at least I think so):
stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb echo '\x12\x02' > /dev/ttyS0
3 Answers 3
If you want to use hexadecimal codes, you should add the -e option to enable interpretation of backslash escapes by echo (but the result is the same as with echo Ctrl + R , Ctrl + B ). And as wallyk said, you probably want to add -n to prevent the output of a newline:
Also make sure that /dev/ttyS0 is the port you want.
thanks, I am sure that it is the port that I want, is there a simpler way of testing serial connection, echo always returns success.
will not be interpreted, and will literally write the string \x12\x02 (and append a newline) to the specified serial port. Instead use
which you can construct on the command line by typing Ctrl V Ctrl R and Ctrl V Ctrl B . Or it is easier to use an editor to type into a script file.
The stty command should work, unless another program is interfering. A common culprit is gpsd which looks for GPS devices being plugged in.
Thanks so much for your response, I tried the echo -n CTRL+VCTRL+RCTRRL+VCTRL+B from command line but I couldn’t make it work, I want to try other commands, therefore I want to know how you come up with these translations(0x12 = ^R , 0x02 = ^B), these are not ascii translations I suppose.
@erincarikan: use man ascii to see how 0x12 relates to Ctrl-R. They are pure ascii. It is possible that some ctrl combinations won’t work, like ctrl-@ (NUL). It could be easier to write a program to do a binary protocol like this.
thanks I totally got it, but unfortunately it doesn’t work, I am suspecting that something is interfering with stty , I don’t have gpsd running. I got to look into this more.
Using Screen:
Note: Screen is actually not able to send hexadecimal, as far as I know. To do that, use echo or printf .
I was using the suggestions in this post to write to a serial port, and then using the information from another post to read from the port, with mixed results. I found that using Screen is an «easier» solution, since it opens a terminal session directly with that port. (I put easier in quotes, because Screen has a really weird interface, IMO, and takes some further reading to figure it out.)
You can issue this command to open a screen session, and then anything you type will be sent to the port, plus the return values will be printed below it:
(Change the above to fit your needs for speed, parity, stop bits, etc.) I realize Screen isn’t the «Linux command line» as the post specifically asks for, but I think it’s in the same spirit. Plus, you don’t have to type echo and quotes every time.
echo
It follows praetorian droid’s answer. However, this didn’t work for me until I also used the cat command ( cat < /dev/ttyS0 ) while I was sending the echo command.
printf
I found that one can also use printf’s ‘%x’ command:
c="\x"$(printf '%x' 0x12) printf $c >> $SERIAL_COMM_PORT
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.
Configure console access on the target machine
Boot loader
GRUB
When using GRUB with a generated grub.cfg , edit /etc/default/grub and enable serial input and output support:
. GRUB_TERMINAL_INPUT="console serial" . GRUB_TERMINAL_OUTPUT="gfxterm serial" .
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:
. GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200"
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:
serial --unit=0 --speed=9600 terminal --timeout=5 serial console
Note: When the terminal —timeout=5 serial console line is added to your menu.lst , your boot sequence will now show a series of Press any key to continue messages. If no key is pressed, the boot menu will appear on whichever (serial or console) appears first in the terminal configuration line.
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 .
console=tty0 console=ttyS0,115200
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
Note: Before making a connection, it is recommended to add yourself to the uucp user group. Otherwise you will need root’s permission to make a connection.
Perform these steps on the machine used to connect the remote console.
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] [dead link 2023-05-06 ⓘ] 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:
Serial Device: /dev/ttyS0 Bps/Par/Bits: 9600 8N1
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:
tinyserial
tinyserial AUR is a minicom replacement for accessing serial ports on Linux inspired by FreeBSD ‘tip’.
tio
tio AUR is a simple serial device tool which features a straightforward command-line and configuration file interface to easily connect to serial TTY devices for basic I/O operations. It has less focus on classic terminal/modem features and more focus on the needs of embedded developers and hackers. tio was originally created to replace screen for connecting to serial devices when used in combination with tmux.
Graphical front-ends
- cutecom — A GUI enabled serial monitor.
- PuTTY — A terminal integrated SSH/Telnet client.
- moserial — 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.
Windows clients
On Windows machines, connect to the serial port using programs like PuTTY or Terminalbpp.
Installing Arch Linux using the serial console
Note: The Arch Linux monthly release(i.e. the installation medium)’s boot loader has been configured[4] to listen on 0 port( ttyS0 /COM1) at 115200 bps, with 8 data bits, no parity bit and 1 stop bit-times.
- Connect to the target machine using the method described above.
- Boot the target machine using the Arch Linux installation medium.
- 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.
- After setup is complete, the console settings will not be saved on the target machine; in order to avoid having to connect a keyboard and monitor, configure console access on the target machine before rebooting.
- While a port speed of 9600 is used in most of the examples in this document, working with higher values is recommended (List of available speeds is displayed in Minicom by pressing Ctrl+a and then p )
Debugging an unresponsive machine using a serial console
Even though [5] 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:
$ stty rows lines cols columns
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 bash/zshrc and invoke it without parameters after resizing the terminal emulator’s window:
rsz() < if [[ -t 0 && $# -eq 0 ]];then local IFS='[;' escape geometry x y echo -ne '\e7\e[r\e[999;999H\e[6n\e8' read -t 5 -sd R escape geometry || < echo unsupported terminal emulator. >&2 return 1 > x="$" y="$" if [[ $ -eq "$" && $ -eq "$" ]];then echo "$ $x$" elif [[ "$x" -gt 0 && "$y" -gt 0 ]];then echo "$x$ -> $x$" stty cols $ rows $ else echo unsupported terminal emulator. >&2 return 1 fi else echo 'Usage: rsz' fi >
Missing ports on multi-port expansion cards
This article or section needs expansion.
Reason: Can the kernel module option be set in modprobe.d(5) ? (Discuss in Talk:Working with the serial console)
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.
This can be overridden with the kernel parameter 8250.nr_uarts . E.g.: