-
Notifications
You must be signed in to change notification settings - Fork 0
/
femtocom
executable file
·37 lines (27 loc) · 1.14 KB
/
femtocom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
# From https://unix.stackexchange.com/a/311680/173210
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
# Exit when any command fails
set -e
# Save settings of current terminal to restore later
original_settings="$(stty -g)"
# Kill background process and restore terminal when this shell exits
trap 'set +e; kill "$bgPid"; stty "$original_settings"' EXIT
# Remove serial port from parameter list, so only stty settings remain
port="$1"; shift
# 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
# 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=$!
# Redirect all keyboard input to serial port
cat >"$port"