After connecting up my Raspberry Pi to my laptop using a USB serial console cable I found that I kept missing the initial console output.
This is because to connect to the serial interface the device must be present on the system, so you have to plug the USB serial console cable in and then enter the command to connect to the device. Of course as soon as you plug it in Raspberry Pi it starts to boot so you miss the initial output.
Note – To be able to connect to a serial device (including a USB serial interface you need to be a member of the ‘dialout’ group.
$ sudo usermod -a -G dialout username
What I needed was a command that I could run before plugging in the USB cable that would wait for the device to appear and then connect automatically, and a way to delay the start of the boot process by just a few seconds to give the script time to connect.
To do this I wrote the following script that loops waiting for the USB serial interface to be created for 15 seconds and connects using the screen utility when it is available.
#!/bin/bash # # Waits for a USB serial device to become available then connects # to a console session using screen. # # Need a list of valid devices so we can check the device specified # by the user is correct, as it won't actually exist until it is # plugged in. devices="/dev/ttyUSB0|/dev/ttyUSB1" # Valid devices. fault="/dev/ttyUSB0" # Default device. timeout=15 command=screen # Command for console session. status=1 # Return an error status by default. device=$1 if [ -z "$2" ]; then # Check no extra parameters exist. command=$(command -v $command) >/dev/null 2>&1 # Command exists? if [ -x "$command" ]; then # Is command executable? if [ -z "$device" ]; then # Fall back to the default device. device=$default fi if [[ $device =~ ^($devices)$ ]]; then # Is the device 'valid'. count=$timeout string=".." while [ ! -c "$device" -a $count -gt 0 ] # Wait for device. do echo -ne "Waiting ($count""s) for $device $string \r" sleep 1 count=$((count - 1)) # Decrement counter. string="$string." done echo if [ $count -gt 0 ]; then $command $device status=$? # Update status fi else echo "Device $device is invalid." fi else echo "Cannot execute $command" fi else echo "Too many parameters." fi exit $status
To open a console window on the desktop I have a desktop shortcut with the following launcher properties
gnome-terminal --geometry=80x25 --window-with-profile="Console" --title="USB Console" --execute screen $HOME/sh-usb-console.sh
You could use this script to connect to a regular serial device as well by including the regular serial port devices in the list of ‘valid’ devices.
To get the Raspberry Pi to pause for a couple of second while the script connects I logged in as root and added an additional setting into config.txt.
# vi /boot/config.txt arm_freq=900 core_freq=350 sdram_freq=500 temp_limit=65 gpu_mem=64 boot_delay=3 disable_overscan=1 hdmi_force_hotplug=1 #
This will cause the Raspberry Pi to pause for three seconds while booting, this is enough to ensure that the USB device is detected and I can connect reliably but short enough to not be to intrusive when starting up normally or rebooting.
The other lines are specific to my setup and I suggest you ignore them.
Raspberry Pi is a trademark of the Raspberry Pi Foundation