最後更新: 2023-09-29
stty 介紹
change and print terminal line settings
Usage
i.e. Get Setting Serial Port Settings
# -F, --file=DEVICE open and use the specified DEVICE instead of stdin
stty -F /dev/ttyUSB0
speed 9600 baud; line = 0; -brkint -imaxbel
# -a, --all print all current settings in human-readable form
stty -a -F /dev/ttyUSB0
# Part 1 speed 9600 baud; rows 0; columns 0; line = 0; # Part 2 intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; # Part 3 min = 1; time = 0; # Part 4 -parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
說明
Part 1
rows 0; columns 0; line = 0;
- rows N # tell the kernel that the terminal has N rows
- columns N # same as cols N
- line N # use line discipline N
Part 2
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O;
- intr = ^C; SIGINT signal is usually triggered by Control+C
Part 3
min = 1; time = 0
The reading program will read until at least one character has been read and there is no timeout.
With "-icanon"
- set N characters minimum for a completed read
- set read timeout of N tenths of a second
generate parity bit in output and expect parity bit in input
set odd parity
use "stick" (mark/space) parity
set character size to N bits, N in [5..8]
i.e. cs8
same as hup
hup send a hangup signal when the last process closes the tty
use two stop bits per character
allow input to be received
disable modem control signals
crtscts
enable RTS/CTS handshaking
translate carriage return to newline
i.e.
\r\n -> \n\n
enable interrupt, quit, and suspend special characters
enable special characters: erase, kill, werase, rprnt
enable non-POSIX special characters
echo input characters
same as crterase
echo erase characters as backspace-space-backspace
echok
echo a newline after a kill character
echo newline even if not echoing other characters
disable flushing after interrupt and quit special characters
with icanon, escape with '\' for uppercase characters
stop background jobs that try to write to the terminal
echo erased characters backward, between '\' and '/'
same as [-]ctlecho
echo control characters in hat notation ('^c')
same as crtkill
kill all line by obeying the echoprt and echoe settings
discard output
enable "LINEMODE"; useful with high latency links
應用
i.e. Set baud rate
stty -F /dev/ttyS0 speed 9600
9600
i.e. stty reset to default
# sane 相當於一堆 settings 的 default value (cread -ignbrk brkint ...)
# 不包括 (baud rate, parity, ... )
stty -F /dev/ttyUSB0 sane
i.e. 讓 cat 會自動中止
i.e. ch340+STM8S103 控制的 relay switch
stty -F /dev/ttyUSB0 -icanon min 0 time 10
stty -F /dev/ttyUSB0
speed 9600 baud; line = 0; min = 0; time = 10; -icanon
echo -n -e '\xff' > /dev/ttyUSB0
cat /dev/ttyUSB0