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
  • 仅显示当前波特率:
Board $> stty -F /dev/ttySTM1 speed
# uart3 (aka ttySTM1) baud rate is set to 115200 bps #
115200
  • 更改波特率:
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

stty工具提出了许多参数,允许在tty终端上进行许多操作,例如:

  • 特殊设置(各种参数,例如速度,行纪律,完成读取的最小字符数,大小,超时等)
  • 控制设置
  • 输入设置
  • 输出设置
  • 本地设置
  • 组合设置


注意: 如果您想进一步讲解,则有趣的教程介绍了termios和stty [2].

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

Serial counters 对调试以下用例非常有用。

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

规范模式,输入模式和输出模式termios设置对数据处理有重要影响。可以停用以下设置以进行测试。
如果发生意外行为,则必须检查所有规范模式,输入模式和输出模式设置。 mkssoftware提出了termios手册的增强版本[3],其中提供了以下定义。

  • echo: 启用回显。 如果设置了ECHO,则将输入字符回显到终端。
  • icanon: 规范输入(擦除和终止处理)。 如果设置了ICANON,则启用规范处理。 在规范模式下,输入处理以行为单位进行处理。 一行由'\ n'字符或文件结尾(EOF)字符分隔。 直到从端口读取整行或接收到信号后,读取请求才会返回。
  • onlcr:在输出上将NL映射到CR-NL。 如果设置了ONLCR,则将NL字符作为CR-NL字符对发送。

通过将设备打开为文件并将数据写入其中,可以简单地发送数据。

  • 在ttySTM1(也称为usart3)上配置端口。 echo, Icanon和onlcr属性被停用以处理原始数据。
Board $> stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr
  • 在ttySTM1(usart3)上显示当前配置:
# display the configuration of uart3 (aka ttySTM1) #
Board $> stty -a -F /dev/ttySTM1
speed 115200 baud; rows 45; columns 169; line = 0;
  • 在ttySTM1(Usart3)上打开一个端口以接收数据
Board $> cat /dev/ttySTM1 &
  • 在远程PC上,识别与连接在STM32MPU USART3针脚上的RS232卡相关联的TTY终端
# Command to execute from host terminal #
PC $> ls /dev/ttyUSB*
/dev/ttyUSB0
  • 在通过USART3引脚连接的远程设备的第二个终端中打开一个小型通信
PC $> minicom -D /dev/ttyUSB0
  • 显示ttyUSB0(远程设备)上的当前配置:
# Display the configuration of host uart (aka ttyUSB0) #
PC $> stty -a -F /dev/ttyUSB0
speed 115200 baud; rows 45; columns 169; line = 0;
  • 使用默认termios配置(8帧长,无奇偶校验)通过USART3将数据从远程PC发送到STM32MPU
# Execute this command from host terminal #
PC $> echo "HELLO" > /dev/ttyUSB0
  • 通过USART3使用默认termios配置将数据从STM32MPU发送到远程PC(8帧长,无奇偶校验)
# Execute this command from STM32 terminal #
Board $> echo "HELLO" > /dev/ttySTM1

Parity errors detection

一些附加的termios函数允许启用奇偶校验错误检测:

  • parenb: 启用奇偶校验
  • parodd: 奇偶校验或偶数
  • inpck: 启用输入奇偶校验或成帧检查
  • ignpar: 忽略具有奇偶校验或成帧错误的字符


例子:

  • 在ttySTM1(Usart3)上配置启用偶数奇偶校验的端口
# STM32 parity enabling #
Board $> stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr parenb -parodd inpck ignpar
  • 在ttySTM1(usart3)上打开一个端口以接收数据
Board $> cat /dev/ttySTM1 &
在通过USART3引脚连接的远程设备的第二个终端中打开一个小型通信
PC $> minicom -D /dev/ttyUSB0
  • 在ttyUSB0(远程设备)上配置端口,并启用偶数奇偶校验:
# Remote device parity enabling #
PC $> stty -F /dev/ttyUSB0 115200 -echo -icanon -onlcr parenb -parodd inpck ignpar

  • 通过USART3将数据从远程PC发送到STM32MPU
# Execute this command from host terminal #
PC $> echo "HELLO" > /dev/ttyUSB0
  • 通过USART3将数据从STM32MPU发送到远程PC
#  Execute this command from STM32 terminal #
Board $> echo "HELLO" > /dev/ttySTM1


Framing errors detection

一些附加的termios功能允许启用帧错误检测:


例子:

  • 在ttySTM1(usart3)上配置端口以启用帧检查和7个数据位长的帧
# STM32 framing enabling #
Board $> stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr cs7 inpck ignpar
  • 在ttySTM1(usart3)上打开一个端口以接收数据
Board $> cat /dev/ttySTM1 &

在通过USART3引脚连接的远程设备的第二个终端中打开一个小型通信

PC $> minicom -D /dev/ttyUSB0
  • 在ttyUSB0(远程设备)上配置端口,启用帧检查和7个数据位长的帧
# 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;
  • 通过USART3将数据从远程PC发送到STM32MPU
# Execute this command from host terminal #
PC $> echo "HELLO" > /dev/ttyUSB0
  • 通过USART3将数据从STM32MPU发送到远程PC
# 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)

line discipline编号"n"连接ttySTM1:

Board $> ldattach n /dev/ttySTM1

File transfer over serial console

请参阅专题文章如何通过串行控制台传输文件.