“How to use TTY with User Terminal”的版本间的差异

来自百问网嵌入式Linux wiki
 
(未显示同一用户的5个中间版本)
第40行: 第40行:
 
  isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
 
  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
 
  {{Board$}} stty -F /dev/ttySTM1 speed
 
  {{highlight|# uart3 (aka ttySTM1) baud rate is set to 115200 bps #}}
 
  {{highlight|# uart3 (aka ttySTM1) baud rate is set to 115200 bps #}}
 
  115200
 
  115200
  
* Change the baud rate:
+
* 更改波特率:
 
: stty -F /dev/ttySTMx ''EXPECTED_BAUDRATE''<br/>
 
: stty -F /dev/ttySTMx ''EXPECTED_BAUDRATE''<br/>
 
<br/>
 
<br/>
第52行: 第52行:
 
  {{Board$}} stty -F /dev/ttySTM1 19200
 
  {{Board$}} stty -F /dev/ttySTM1 19200
  
The stty tool proposes many arguments allowing many operations on a tty terminal, such as:
+
stty工具提出了许多参数,允许在tty终端上进行许多操作,例如:
* 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
+
* 组合设置
  
 
<br/>
 
<br/>
'''Note''': If you want to go further, an interesting tutorial describes termios and stty <ref>[https://blog.nelhage.com/2009/12/a-brief-introduction-to-termios-termios3-and-stty/ A Brief Introduction to termios: termios(3) and stty] stty tutorial</ref>.
+
'''注意''': 如果您想进一步讲解,则有趣的教程介绍了termios和stty <ref>[https://blog.nelhage.com/2009/12/a-brief-introduction-to-termios-termios3-and-stty/ A Brief Introduction to termios: termios(3) and stty] stty tutorial</ref>.
  
 
==Send / Receive data (with stty, minicom, echo and cat tools)==
 
==Send / Receive data (with stty, minicom, echo and cat tools)==
  
[[Serial_TTY_overview#procfs_serialcounters|Serial counters]] can be very useful to debug the following use cases.
+
[[Serial_TTY_overview#procfs_serialcounters|Serial counters]] 对调试以下用例非常有用。
  
 
===Default configuration (8 data bits frame, no parity errors detection, no framing errors detection)===
 
===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. <br/>
+
规范模式,输入模式和输出模式termios设置对数据处理有重要影响。可以停用以下设置以进行测试。 <br/>
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 <ref>[https://www.mkssoftware.com/docs/man5/struct_termios.5.asp struct_termios man page]</ref>, where the following definitions are provided.
+
mkssoftware提出了termios手册的增强版本<ref>[https://www.mkssoftware.com/docs/man5/struct_termios.5.asp struct_termios man page]</ref>,其中提供了以下定义。
* echo: Enable echo. If ECHO is set input characters are echoed back to the terminal.<br/ >
+
* echo: 启用回显。 如果设置了ECHO,则将输入字符回显到终端。<br/ >
* 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.<br/ >
+
* icanon: 规范输入(擦除和终止处理)。 如果设置了ICANON,则启用规范处理。 在规范模式下,输入处理以行为单位进行处理。 一行由'\ n'字符或文件结尾(EOF)字符分隔。 直到从端口读取整行或接收到信号后,读取请求才会返回。<br/ >
* onlcr: Map NL to CR-NL on output. If ONLCR is set the NL character is transmitted as the CR-NL character pair.
+
* onlcr:在输出上将NL映射到CR-NL。 如果设置了ONLCR,则将NL字符作为CR-NL字符对发送。
  
Sending data can be simply done by opening the device as a file and writing data to it.
+
通过将设备打开为文件并将数据写入其中,可以简单地发送数据。
 
<br/>
 
<br/>
* Configure a port on ttySTM1 (aka usart3). echo, icanon and onlcr properties are deactivated to handle raw data.
+
* 在ttySTM1(也称为usart3)上配置端口。 echo, Icanon和onlcr属性被停用以处理原始数据。
 
  {{Board$}} stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr
 
  {{Board$}} stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr
  
* Display the current configuration on ttySTM1 (usart3):
+
* 在ttySTM1(usart3)上显示当前配置:
 
  {{highlight|# display the configuration of uart3 (aka ttySTM1) #}}
 
  {{highlight|# display the configuration of uart3 (aka ttySTM1) #}}
 
  {{Board$}} stty -a -F /dev/ttySTM1
 
  {{Board$}} stty -a -F /dev/ttySTM1
 
  speed 115200 baud; rows 45; columns 169; line = 0;
 
  speed 115200 baud; rows 45; columns 169; line = 0;
  
* Open a port on ttySTM1 (usart3) to receive data
+
* 在ttySTM1(Usart3)上打开一个端口以接收数据
 
  {{Board$}} cat /dev/ttySTM1 &
 
  {{Board$}} cat /dev/ttySTM1 &
  
* On the remote PC, identify the tty terminal associated to the RS232 card connected on STM32MPU USART3 pins
+
* 在远程PC上,识别与连接在STM32MPU USART3针脚上的RS232卡相关联的TTY终端
 
  {{highlight|# Command to execute from host terminal #}}
 
  {{highlight|# Command to execute from host terminal #}}
 
  {{PC$}} ls /dev/ttyUSB*
 
  {{PC$}} ls /dev/ttyUSB*
 
  /dev/ttyUSB0
 
  /dev/ttyUSB0
  
* Open a minicom in a second terminal on the remote device connected on USART3 pins
+
* 在通过USART3引脚连接的远程设备的第二个终端中打开一个小型通信
 
  {{PC$}} minicom -D /dev/ttyUSB0
 
  {{PC$}} minicom -D /dev/ttyUSB0
  
* Display the current configuration on ttyUSB0 (remote device):
+
* 显示ttyUSB0(远程设备)上的当前配置:
 
  {{highlight|# Display the configuration of host uart (aka ttyUSB0) #}}
 
  {{highlight|# Display the configuration of host uart (aka ttyUSB0) #}}
 
  {{PC$}} stty -a -F /dev/ttyUSB0
 
  {{PC$}} stty -a -F /dev/ttyUSB0
 
  speed 115200 baud; rows 45; columns 169; line = 0;
 
  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)
+
* 使用默认termios配置(8帧长,无奇偶校验)通过USART3将数据从远程PC发送到STM32MPU
 
  {{highlight|# Execute this command from host terminal #}}
 
  {{highlight|# Execute this command from host terminal #}}
 
  {{PC$}} echo "HELLO" > /dev/ttyUSB0
 
  {{PC$}} echo "HELLO" > /dev/ttyUSB0
  
* Send data from STM32MPU to remote PC over USART3  with default termios configuration (8 frames length, no parity)
+
* 通过USART3使用默认termios配置将数据从STM32MPU发送到远程PC(8帧长,无奇偶校验)
 
  {{highlight|# Execute this command from STM32 terminal #}}
 
  {{highlight|# Execute this command from STM32 terminal #}}
 
  {{Board$}} echo "HELLO" > /dev/ttySTM1
 
  {{Board$}} echo "HELLO" > /dev/ttySTM1
  
 
===Parity errors detection===
 
===Parity errors detection===
Some additional termios functions allow to enable parity errors detection:
+
一些附加的termios函数允许启用奇偶校验错误检测:
* parenb: Parity enable
+
* parenb: 启用奇偶校验
* parodd: Odd parity else even
+
* parodd: 奇偶校验或偶数
* inpck: Enable input parity or framing check
+
* inpck: 启用输入奇偶校验或成帧检查
* ignpar: Ignore characters with parity or framing errors
+
* ignpar: 忽略具有奇偶校验或成帧错误的字符
 
<br/>
 
<br/>
Exemples:
+
例子:
* Configure a port on ttySTM1 (usart3) with even parity enabling
+
* 在ttySTM1(Usart3)上配置启用偶数奇偶校验的端口
 
  {{highlight|# STM32 parity enabling #}}
 
  {{highlight|# STM32 parity enabling #}}
 
  {{Board$}} stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr parenb -parodd inpck ignpar
 
  {{Board$}} stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr parenb -parodd inpck ignpar
  
* Open a port on ttySTM1 (usart3) to receive data
+
* 在ttySTM1(usart3)上打开一个端口以接收数据
 
  {{Board$}} cat /dev/ttySTM1 &
 
  {{Board$}} cat /dev/ttySTM1 &
  
  Open a minicom in a second terminal on the remote device connected on USART3 pins
+
  在通过USART3引脚连接的远程设备的第二个终端中打开一个小型通信
 
  {{PC$}} minicom -D /dev/ttyUSB0
 
  {{PC$}} minicom -D /dev/ttyUSB0
  
* Configure a port on ttyUSB0 (remote device) with even parity enabling:
+
* 在ttyUSB0(远程设备)上配置端口,并启用偶数奇偶校验:
 
  {{highlight|# Remote device parity enabling #}}
 
  {{highlight|# Remote device parity enabling #}}
 
  {{PC$}} stty -F /dev/ttyUSB0 115200 -echo -icanon -onlcr parenb -parodd inpck ignpar
 
  {{PC$}} stty -F /dev/ttyUSB0 115200 -echo -icanon -onlcr parenb -parodd inpck ignpar
 
   
 
   
* Send data from remote PC to STM32MPU over USART3
+
* 通过USART3将数据从远程PC发送到STM32MPU
 
  {{highlight|# Execute this command from host terminal #}}
 
  {{highlight|# Execute this command from host terminal #}}
 
  {{PC$}} echo "HELLO" > /dev/ttyUSB0
 
  {{PC$}} echo "HELLO" > /dev/ttyUSB0
  
* Send data from STM32MPU to remote PC over USART3
+
* 通过USART3将数据从STM32MPU发送到远程PC
 
  {{highlight|#  Execute this command from STM32 terminal #}}
 
  {{highlight|#  Execute this command from STM32 terminal #}}
 
  {{Board$}} echo "HELLO" > /dev/ttySTM1
 
  {{Board$}} echo "HELLO" > /dev/ttySTM1
第141行: 第141行:
  
 
===Framing errors detection===
 
===Framing errors detection===
Some additional termios functions allow to enable framing errors detection:
+
一些附加的termios功能允许启用帧错误检测:
* csize: Number of bits per byte ([[Serial_TTY_overview#CSIZE_configurations|character size and parity bit configurations]])
+
* csize: 每字节位数 ([[Serial_TTY_overview#CSIZE_configurations|character size and parity bit configurations]])
* inpck: Enable input framing check
+
* inpck: 启用输入帧检查
* ignpar: Ignore characters with parity or framing errors
+
* ignpar: 忽略具有奇偶校验或成帧错误的字符
 
<br/>
 
<br/>
Exemples:
+
例子:
* Configure a port on ttySTM1 (usart3) with framing check enabling and 7 data bits length frames
+
* 在ttySTM1(usart3)上配置端口以启用帧检查和7个数据位长的帧
 
  {{highlight|# STM32 framing enabling #}}
 
  {{highlight|# STM32 framing enabling #}}
 
  {{Board$}} stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr cs7 inpck ignpar
 
  {{Board$}} stty -F /dev/ttySTM1 115200 -echo -icanon -onlcr cs7 inpck ignpar
  
* Open a port on ttySTM1 (usart3) to receive data
+
* 在ttySTM1(usart3)上打开一个端口以接收数据
 
  {{Board$}} cat /dev/ttySTM1 &
 
  {{Board$}} cat /dev/ttySTM1 &
  
Open a minicom in a second terminal on the remote device connected on USART3 pins
+
在通过USART3引脚连接的远程设备的第二个终端中打开一个小型通信
 
  {{PC$}} minicom -D /dev/ttyUSB0
 
  {{PC$}} minicom -D /dev/ttyUSB0
  
* Configure a port on ttyUSB0 (remote device) with framing check enabling and 7 data bits length frames
+
* 在ttyUSB0(远程设备)上配置端口,启用帧检查和7个数据位长的帧
 
  {{highlight|# Remote device parity enabling #}}
 
  {{highlight|# Remote device parity enabling #}}
 
  {{PC$}} stty -a -F /dev/ttyUSB0 115200 -echo -icanon -onlcr cs7 inpck ignpar
 
  {{PC$}} stty -a -F /dev/ttyUSB0 115200 -echo -icanon -onlcr cs7 inpck ignpar
 
  speed 115200 baud; rows 45; columns 169; line = 0;
 
  speed 115200 baud; rows 45; columns 169; line = 0;
  
* Send data from remote PC to STM32MPU over USART3
+
* 通过USART3将数据从远程PC发送到STM32MPU
 
  {{highlight|# Execute this command from host terminal #}}
 
  {{highlight|# Execute this command from host terminal #}}
 
  {{PC$}} echo "HELLO" > /dev/ttyUSB0
 
  {{PC$}} echo "HELLO" > /dev/ttyUSB0
  
* Send data from STM32MPU to remote PC over USART3
+
* 通过USART3将数据从STM32MPU发送到远程PC
 
  {{highlight|# Execute this command from STM32 terminal #}}
 
  {{highlight|# Execute this command from STM32 terminal #}}
 
  {{Board$}} echo "HELLO" > /dev/ttySTM1
 
  {{Board$}} echo "HELLO" > /dev/ttySTM1
第177行: 第177行:
 
==Link a tty serial device with a line discipline (with ldattach tool)==
 
==Link a tty serial device with a line discipline (with ldattach tool)==
  
Attach ttySTM1 with [[Serial TTY line discipline|line discipline]] number ''n'' :
+
[[Serial TTY line discipline|line discipline]]编号"n"连接ttySTM1:
 
  {{Board$}} ldattach ''n'' /dev/ttySTM1
 
  {{Board$}} ldattach ''n'' /dev/ttySTM1
  
 
==File transfer over serial console==
 
==File transfer over serial console==
Please see the dedicated article [[How to transfer a file over serial console]].
+
请参阅专题文章[[How to transfer a file over serial console]].
  
 
==References==
 
==References==
 
<references />
 
<references />
 
<noinclude>
 
[[Category:Serial TTY|3]]
 
{{PublicationRequestId | 15054 | 2020-02-21 | 8369 (AlainF - 2018-08-14)}}
 
</noinclude>
 

2020年11月8日 (日) 16:58的最新版本


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

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