How to use TTY with User Terminal

来自百问网嵌入式Linux wiki


Purpose

本文介绍如何在用户终端上使用TTY。Serial TTY overview 文章中介绍了TTY概述。

以下示例的用例是STM32 MPU板和PC之间通过USB到RS232适配器电缆的数据传输。
如何获得终端 中详细描述了此用例的设置。

请参阅以下示例:

  • uart4默认情况下处于激活状态(对于Linux控制台)
  • Usart3由 device tree启用
  • usart3引脚连接到RS232卡
  • RS232卡通过USB转RS232适配器电缆连接到PC。

注意: 本文中使用了一些TTY工具。 TTY工具列表定义为专用文章 [TTY Tools ].

Print the file name of the terminal connected to standard input (with tty tool)

Board $> tty
# The console is connected to uart4 (aka ttySTM0) #
/dev/ttySTM0

Change serial port configuration (with stty tool)

可以使用stty工具显示和更改许多串行端口属性。 完整的功能列表可在stty用户手册页面中找到[1].

Board $> stty --help
  • 显示当前配置:
termios默认配置特定于每个Linux发行版。 开始在两个设备之间进行串行通信之前,建议检查Termios配置在两个设备上是否兼容。 termios配置需要首先对齐。
Board $> stty -a -F /dev/ttySTM1
# Display the configuration of uart3 (aka ttySTM1) #
speed 115200 baud; rows 45; columns 169; 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
  • Display only the current baud rate:
Board $> stty -F /dev/ttySTM1 speed
# uart3 (aka ttySTM1) baud rate is set to 115200 bps #
115200
  • Change the baud rate:
stty -F /dev/ttySTMx EXPECTED_BAUDRATE


Example: change the baud rate to 19200
# Change uart3 (aka ttySTM1) baud rate to 19200 bps #
Board $> stty -F /dev/ttySTM1 19200

The stty tool proposes many arguments allowing many operations on a tty terminal, such as:

  • special settings (various arguments such as speed, line discipline, minimum number of characters for a completed read, size, timeout, etc...)
  • control settings
  • input settings
  • output settings
  • local settings
  • combination settings


Note: If you want to go further, an interesting tutorial describes termios and stty [2].

Send / Receive data (with stty, minicom, echo and cat tools)

Serial counters can be very useful to debug the following use cases.

Default configuration (8 data bits frame, no parity errors detection, no framing errors detection)

Canonical mode, input modes and output modes termios settings have a major influence on data processing. The following settings can be deactivated for testing.
In case of unexpected behavior, all canonical mode, input modes and output modes settings must be checked. mkssoftware proposes an enriched version of termios manual [3], where the following definitions are provided.

  • echo: Enable echo. If ECHO is set input characters are echoed back to the terminal.
  • icanon: Canonical input (erase and kill processing). If ICANON is set canonical processing is enabled. In canonical mode input processing is processed in units of lines. A line is delimited by a '\n' character or and end-of-file (EOF) character. A read request does not return until an entire line is read from the port or a signal is received.
  • onlcr: Map NL to CR-NL on output. If ONLCR is set the NL character is transmitted as the CR-NL character pair.

Sending data can be simply done by opening the device as a file and writing data to it.

  • Configure a port on ttySTM1 (aka usart3). echo, icanon and onlcr properties are deactivated to handle raw data.
Board $> stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr
  • Display the current configuration on ttySTM1 (usart3):
# display the configuration of uart3 (aka ttySTM1) #
Board $> stty -a -F /dev/ttySTM1
speed 115200 baud; rows 45; columns 169; line = 0;
  • Open a port on ttySTM1 (usart3) to receive data
Board $> cat /dev/ttySTM1 &
  • On the remote PC, identify the tty terminal associated to the RS232 card connected on STM32MPU USART3 pins
# Command to execute from host terminal #
PC $> ls /dev/ttyUSB*
/dev/ttyUSB0
  • Open a minicom in a second terminal on the remote device connected on USART3 pins
PC $> minicom -D /dev/ttyUSB0
  • Display the current configuration on ttyUSB0 (remote device):
# Display the configuration of host uart (aka ttyUSB0) #
PC $> stty -a -F /dev/ttyUSB0
speed 115200 baud; rows 45; columns 169; line = 0;
  • Send data from remote PC to STM32MPU over USART3 with default termios configuration (8 frames length, no parity)
# Execute this command from host terminal #
PC $> echo "HELLO" > /dev/ttyUSB0
  • Send data from STM32MPU to remote PC over USART3 with default termios configuration (8 frames length, no parity)
# Execute this command from STM32 terminal #
Board $> echo "HELLO" > /dev/ttySTM1

Parity errors detection

Some additional termios functions allow to enable parity errors detection:

  • parenb: Parity enable
  • parodd: Odd parity else even
  • inpck: Enable input parity or framing check
  • ignpar: Ignore characters with parity or framing errors


Exemples:

  • Configure a port on ttySTM1 (usart3) with even parity enabling
# STM32 parity enabling #
Board $> stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr parenb -parodd inpck ignpar
  • Open a port on ttySTM1 (usart3) to receive data
Board $> cat /dev/ttySTM1 &
Open a minicom in a second terminal on the remote device connected on USART3 pins
PC $> minicom -D /dev/ttyUSB0
  • Configure a port on ttyUSB0 (remote device) with even parity enabling:
# Remote device parity enabling #
PC $> stty -F /dev/ttyUSB0 115200 -echo -icanon -onlcr parenb -parodd inpck ignpar

  • Send data from remote PC to STM32MPU over USART3
# Execute this command from host terminal #
PC $> echo "HELLO" > /dev/ttyUSB0
  • Send data from STM32MPU to remote PC over USART3
#  Execute this command from STM32 terminal #
Board $> echo "HELLO" > /dev/ttySTM1


Framing errors detection

Some additional termios functions allow to enable framing errors detection:


Exemples:

  • Configure a port on ttySTM1 (usart3) with framing check enabling and 7 data bits length frames
# STM32 framing enabling #
Board $> stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr cs7 inpck ignpar
  • Open a port on ttySTM1 (usart3) to receive data
Board $> cat /dev/ttySTM1 &
Open a minicom in a second terminal on the remote device connected on USART3 pins
PC $> minicom -D /dev/ttyUSB0
  • Configure a port on ttyUSB0 (remote device) with framing check enabling and 7 data bits length frames
# Remote device parity enabling #
PC $> stty -a -F /dev/ttyUSB0 115200 -echo -icanon -onlcr cs7 inpck ignpar
speed 115200 baud; rows 45; columns 169; line = 0;
  • Send data from remote PC to STM32MPU over USART3
# Execute this command from host terminal #
PC $> echo "HELLO" > /dev/ttyUSB0
  • Send data from STM32MPU to remote PC over USART3
# Execute this command from STM32 terminal #
Board $> echo "HELLO" > /dev/ttySTM1

Identify processes using a tty serial device (with fuser tool)

Board $> fuser /dev/ttySTM0
# The process numbered 395, 691 and 3872 are using a tty serial device #
395 691 3872

Link a tty serial device with a line discipline (with ldattach tool)

Attach ttySTM1 with line discipline number n :

Board $> ldattach n /dev/ttySTM1

File transfer over serial console

Please see the dedicated article 如何通过串行控制台传输文件.

References

<securetransclude src="ProtectedTemplate:PublicationRequestId" params="15054 | 2020-02-21 | 8369 (AlainF - 2018-08-14)"></securetransclude>