Skip to content

Instantly share code, notes, and snippets.

@securelyfitz
Created August 26, 2021 18:41
Show Gist options
  • Save securelyfitz/5938392a6cd00288a7add1d4a1f3c427 to your computer and use it in GitHub Desktop.
Save securelyfitz/5938392a6cd00288a7add1d4a1f3c427 to your computer and use it in GitHub Desktop.
Use screen with custom baud rates on FTDI devices
#!/bin/bash
# since screen doesn't know how to set custom baud rates, this does it for FTDI devices.
# it works by using setserial to set the custom speed (spd_cust) mode for baud rate 38400
# it calculates the proper clock divider by dividing the device's base clock by the desired rate
# then, it starts screen at 38400 - which is not longer 38400 but your new clock rate.
# tested on FT232H, FT2232H, FT-X, and FT232RL
# does not work on CH341 or PL2303
# todo: confirm FTDI before running setserial and report error
# todo: validate uart range
# default baud and port
baud=115200
port=/dev/ttyUSB0
#parse params
for param in "$@"
do
# if it starts with / its a device
if [[ $param == /* ]]; then port=$param
# else if its all digits, its baud rate
elif [[ $param =~ ^-?[0-9]+$ ]]; then baud=$param
# otherwise error
else echo "$0 [port] [baud]
parameters optional and ok in any order, and default to 115200 and /dev/ttyUSB0"l; exit 1
fi
done
# get base clock via setserial, and calculate baud
baud_base=`setserial $port -G | sed "s/.*baud_base \([0-9]*\).*/\1/"`
divisor=$(($baud_base/$baud))
# debug output
#echo baud = $baud port = $port baud_base = $baud_base divisor = $divisor
# set baud rate and run screen if successful
setserial $port spd_cust divisor $divisor && screen $port 38400 && exit 0
# if we made it here, setserial probably failed
echo custom baud rates only work on FTDI devices. Double check your device and port.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment