Using screen for serial connections

Add your user to the dialout group so you don't have to do sudo screen:

$ sudo adduser $USER dialout

Then either reboot, or start a new shell with the new group applied

$ newgrp

Then connect to the TTY with

$ screen /dev/ttyUSB0 115200

Note that the 115200 baud rate is a good default, but is device configuration dependent, so you may need to provide a different value.

There's a few helpful keybinds

You can set the size of the scrollback buffer with screen -h <lines>. The default is 100 lines, but Ubuntu ups that to 1024 lines in /etc/screenrc.

You can set a custom session name with screen -S <session>. But if you're only a passing screen user like me, and only ever have one screen session open at a time, you can use screen -r to resume a detached session. Otherwise, you can use screen -list to list sessions, and screen -r <session> to resume a specific session.

$ screen -list
There is a screen on:
    16366.pts-1.bedlam    (01/17/2023 05:21:36 PM)    (Detached)
1 Socket in /run/screen/S-nots.
$ screen -r 16366.pts-1.bedlam

screen is pretty customizable; there's guaranteed to be more powerful workflows that you can use, but this is all I needed to be productive at a serial console.

An aside on PuTTY

I can't remember the details, but at one point I came to the conclusion that I should use PuTTY for serial connections because it "Just Worked". But when I tried it today, it'd crash because it couldn't load a font

$ putty
(putty:10718): Gtk-CRITICAL **: 16:01:12.674: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GtkScrollbar
(putty:10718): Gtk-CRITICAL **: 16:01:12.675: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GtkScrollbar
(putty:10718): Gtk-CRITICAL **: 16:01:12.676: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GtkScrollbar
PuTTY: unable to load font "server:fixed"
$ echo $?
1

If I set the font to something I have installed locally, it works. This seemed suboptimal, so I'm back to screen now that I know how to use the scrollback mode.

Transmitting a file over serial

Sometimes I've wanted to transmit a file to a remote device over serial (usually because its network is broken).

On the receiving end, and use base64 -d - to decode base64 data from stdin until EOF, and write it to /tmp/file.bin

$ screen /dev/ttyUSB0 115200
$ base64 -d - >/tmp/file.bin

Then exit the screen session with <ctrl-a k>. If your tmux prefix is configed as <ctrl-a>, you'll need to press it twice to pass the second one through to screen.

On the transmitting side, run

$ base64 file.bin >/dev/ttyUSB0

Finally, open a new screen session and press <ctrl-d> to send EOF to the base64 -d - running on the receiver. Verifying the file MD5 hash is likely a good idea

$ md5sum file.bin

This trick was taught to me by my friend Dustin Richards. Thanks Dustin!

Saving screen scrollback history to a file

Sometimes, usually when investigating something broken, I want to save all of my serial console output to a file, normally so I can paste it into a bug ticket. You can do this by

<ctrl-a :>  # enters command mode
hardcopy -h output.txt

If you leave off the -h it will copy only the current screen's contents.