[Tools] Linux serial device debugging skills (sftty)



Preface

  Serial device is one of the most basic devices in the Linux system, and it is almost indispensable in the development of embedded Linux. Because the serial port is simple and widely used, in addition to using a serial port as a debugging terminal output, the serial port is also used as a connection bus for external devices.


  Although the serial port is one of the simplest bus devices, in the actual development process, more or less problems such as failure of serial port communication, data disorder, and unexpected disconnection will be encountered. The linux system provides complete equipment management and debugging tools, through the command terminal can monitor the serial data flow and adjust the serial device parameters in real time. Making good use of these tools can quickly locate serial port-related issues and achieve twice the result with half the effort.


Read serial device data

cat [tty device]

Example:

root@ubuntu:~# cat /dev/ttyUSB0 
Hello Word
Hello Word

Write data to serial device

echo [data] > [tty device]

Example:

root@ubuntu:~# echo ABCD > /dev/ttyUSB0 

Serial device parameter access

  • Baud rate, 9600, 57600, 115200, etc.

  • Data bits, usually 8 bits

  • Stop bit, related to specific equipment, commonly used stop bit 1, 1.5, 2

  • Check, including odd check, even check, no check

  • Flow control, generally set to none


View serial device parameters

stty -F [tty device] -a

Example:

root@ubuntu:~# stty -F /dev/ttyUSB0 -a
speed 9600 baud; rows 0; columns 0; line = 0;
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; min = 1; time = 0;
-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

  In addition to the baud rate parameter, it “-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts”is the actual serial port parameter we care about.

  • speed, baud rate
  • cs8, 8 data bits
  • parenb, even parity
  • parodd, odd parity
  • cstopb, 2 stop bits
  • crtscts, hardware flow control

  Which "-"means that the state is not set, so the default parameters of the serial device are: 9600bps baud rate, 8 data bits, no parity, 1 stop bit, no hardware flow control.


Set serial device parameters

stty -F [tty device] [pararm0] [param1] […]

  The serial port parameters to be set after the command are in no particular order

  • Set 115200 baud rate, 8 data bits, even parity, 2 stop bits, no flow control
root@ubuntu:~# stty -F /dev/ttyUSB0 115200 cs8 parodd cstopb
root@ubuntu:~# stty -F /dev/ttyUSB0 -a
speed 115200 baud; rows 0; columns 0; line = 0;
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; min = 1; time = 0;
-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

  • Remove the setting, add before the parameter "-"; remove even parity, 2 stop bit settings
root@ubuntu:~# stty -F /dev/ttyUSB0 115200 cs8 -parodd -cstopb
root@ubuntu:~# stty -F /dev/ttyUSB0 -a
speed 115200 baud; rows 0; columns 0; line = 0;
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; min = 1; time = 0;
-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

Note:
When changing optional parameters, you need to disable the set parameters, and then enable the new parameters. For example, if even parity is set first, and then need to be changed to odd parity, even parity must be disabled, otherwise the setting of odd parity will be invalid.

stty -F /dev/ttyUSB0 115200 cs8 -parodd parenb


  • Enter unsupported parameters, an error message will be prompted
root@ubuntu:~# stty -F /dev/ttyUSB0 115200 cs9 parodd
stty: invalid argument ‘cs9’
Try 'stty --help' for more information.

Guess you like

Origin blog.csdn.net/qq_20553613/article/details/104733792