Created
August 6, 2022 10:30
-
-
Save mnl/89b63be6ed5bdd3f3102c6a412262f91 to your computer and use it in GitHub Desktop.
femtocom - super simple serial port handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Adapted from https://unix.stackexchange.com/a/311680/81195 | |
# I added tput, fuser and cleanup | |
if [[ $# -lt 1 ]]; then | |
echo "Usage:" | |
echo " femtocom <serial-port> [ <speed> [ <stty-options> ... ] ]" | |
echo " Example: $0 /dev/ttyS0 9600" | |
echo " Press Ctrl+Q to quit" | |
fi | |
cleanup() { | |
set +e | |
[[ -n "$bgPid" ]] && kill "$bgPid" | |
stty "$original_settings" | |
} | |
set -e # Exit when any command fails | |
# Save settings of current terminal to restore later | |
original_settings="$(stty -g)" | |
# Kill background process and restore terminal when this shell exits | |
trap cleanup EXIT | |
# Remove serial port from parameter list, so only stty settings remain | |
port="$1"; shift | |
# kill any other processes using the port, if any | |
fuser -k "$port" || true | |
# Set up serial port, append all remaining parameters from command line | |
stty -F "$port" raw -echo "$@" | |
# Set current terminal to pass through everything except Ctrl+Q | |
# * "quit undef susp undef" will disable Ctrl+\ and Ctrl+Z handling | |
# * "isig intr ^Q" will make Ctrl+Q send SIGINT to this script | |
stty raw -echo isig intr ^Q quit undef susp undef | |
echo "Connecting to $port. Press Ctrl+Q to exit." | |
# Let cat read the serial port to the screen in the background | |
# Capture PID of background process so it is possible to terminate it | |
cat "$port" & bgPid=$! | |
# Move cursor to the beginning | |
tput cr | |
cat >"$port" # Redirect all keyboard input to serial port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment