I have a couple of devices that are powered from a USB port that is also used as a serial console, including my Raspberry Pi which use with a USB serial console cable as it is more convenient than connecting a keyboard and monitor.

I quickly became frustrated by the fact that you can’t connect to the device before plugging it in (as it doesn’t exist) which meant that more often than not I missed the initial output. What I wanted was a script that would prompt me to select the correct baud rate and then loop waiting for the device to be plugged in before connecting automatically.

#!/bin/bash## sh-usb-console.sh [<device>]## Waits for a USB serial device to become available then connects to a# console session using 'screen'. ## Note - If you close the window without quiting 'screen' it will continue# to run in the background and prevent new connections being made. Not sure# how to fix that - but killing the offending process using 'kill -9' works!## This program is free software: you can redistribute it and/or modify it# under the terms of the GNU General Public License as published by the Free# Software Foundation, either version 3 of the License, or (at your option)# any later version.## This program is distributed in the hope that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License# formore details.## You should have received a copy of the GNU General Public License along# with this program. If not, see <http://www.gnu.org/licenses/>### We use a list of devcies to check the device specified by the user, as it# won't actually exist until it is plugged in. You may need to modify this# list to include your serial device as different USB to serial devices have# different device names.#
devices="/dev/ttyUSB0|/dev/ttyUSB1|/dev/ttyACM0"# Valid devices.
speeds="9600 19200 38400 115200"# Valid baud rates.#
device="/dev/ttyUSB0"# Default USB serial device.
baudrate=115200 # Default baudrate
timeout=15 # Default time to wait for the USB device to be plugged in.#
command=screen # Command for console session.
status=1 # Return an error by default.if [[ ! -z "$1" ]]; then device=$1; fi# Get device.if [[ -z "$2" ]]; then# Check no extra parameters exist.
command=$(command -v $command) >/dev/null 2>&1 # Check the comamnd exists.if [[ -x "$command" ]]; then# Display manager exists and is executable.if [[ $device =~ ^($devices)$ ]]; then# Is the device 'valid'.if[ -x "$(command -v zenity)"]; then
baudrate=$(zenity --list --title="" --column="Baud Rate" $speeds \
--ok-label="Connect" --height=201 --width=301 2>/dev/null)
status=$? # Update statusfiif [[ ! -z "$baudrate" ]]; then# Connect
count=$timeout
string=".."while[! -c "$device" -a $count -gt 0]# Wait for device.doecho -ne "Waiting ($count""s) for $device $string \r"
sleep 1
count=$((count - 1)) # Decrement counter.
string="$string."doneechoif [[ $count -gt 0 ]]; then
$command $device $baudrate
status=$? # Update statusfifielseecho"Device $device is invalid."fielseecho"Cannot execute $command"fielseecho"Too many parameters."fiexit $status

Note – To be able to connect to a serial device (including a USB serial interface) the user will need to be a member of the ‘dialout’ group. You can use the following command to add a user to this group, replacing $lt;username> with the user’s actual username, they will then be able to access any serial device the NEXT time they login.

$ sudo usermod -a -G dialout username

To use this script you just need to run it from the command line optionally specifying a device (if you don’t it will use ‘/dev/ttyUSB0’ by default), it will then prompt you to select the baud rate (if you don’t have ‘zenity’ installed it will not prompt you and just default to ‘115200’ baud), before waiting 15 seconds for the device to be connected.

$ ./sh-usb-console.sh /dev/ttyACM0
Waiting (9s) for /dev/ttyACM0 ……..

When the device is plugged in the screen will clear and you should see the console output. (In this case it is the console output from my WiPy 2.0).

MicroPython v1.8.6-849-eff7d712 on 2018-03-15; WiPy with ESP32
Type “help()” for more information.
>>>

For convenience I also use a desktop shortcut which I created with the following launcher properties.

This allows me to being up a new console window just by double clicking on the icon, which is very convenient – don’t forget that you could use this script to connect to a regular serial device by including ‘/dev/ttyS0’ and ‘/dev/ttyS1’ the list of allowed devices.