Difference between revisions of "How to use TTY with User Terminal"

[quality revision] [unchecked revision]
m
imported>Frq08988
m

Template:ArticleMainWriter Template:ArticleApprovedVersion


1 Purpose[edit]

This article describes how to use TTY with a user terminal. The TTY overview is described in Serial TTY overview article.

The use case of the following examples is a data transfer between a STM32 MPU board and PC, over a USB to a RS232 adapter cable.
The setup of this use case is described in details in the How to get Terminal article.


For the following examples:

  • uart4 is activated by default (for the Linux console)
  • usart3 is enabled by device tree
  • The usart3 pins are connected to a RS232 card
  • The RS232 card is connected to the PC over the USB to RS232 adapter cable.

Note: Some TTY tools are used in this article. A list of TTY tools is defined a dedicated article [TTY Tools ].

2 Print the file name of the terminal connected to standard input (with tty tool)[edit]

 tty
/dev/ttySTM0                                                                                 /* the console is connected to uart4, aka ttySTM0 */


3 Change serial port configuration (with stty tool)[edit]

Many serial port properties can be displayed and changed with the stty tool. The full feature list is available in stty user manual pages [1] .

 stty --help
  • Display the current configuration:
 stty -a -F /dev/ttySTM1                                                             /* display the configuration of uart3 aka ttySTM1*/
speed 115200 baud; rows 45; columns 169; line = 0;


  • Display only the current baud rate:
 stty -F /dev/ttySTM1 speed
115200                                                                                     /* uart3 aka ttySTM1 baud rate is set to 115200 bps */


  • Change the baud rate:

stty -F /dev/ttySTMx EXPECTED_BAUDRATE

Example: change the baud rate to 19200

 stty -F /dev/ttySTM1 19200                                                          /* uart3 aka ttySTM1 baud rate has been changed to 19200 bps */


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].

4 Send / Receive data (with stty, minicom, echo and cat tools)[edit]

4.1 Default configuration (8 data bits frame, no parity errors detection, no framing errors detection)[edit]

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

  • Configure a port on ttySTM1 (usart3)
 stty -F /dev/ttySTM1 115200 -echo


  • Display the current configuration on ttySTM1 (usart3):
 stty -a -F /dev/ttySTM1                                                             /* display the configuration of uart3 aka ttySTM1*/
speed 115200 baud; rows 45; columns 169; line = 0;

  • Open a port on ttySTM1 (usart3) to receive data
 cat /dev/ttySTM1 &


  • On the remote PC, identify the tty terminal associated to RS232 card connected on STM32MPU USART3 pins
 ls /dev/ttyUSB*                                                                         /* command to execute from host terminal*/
/dev/ttyUSB0
  • Open a minicom in a second terminal on the remote device connected on USART3 pins
 minicom -D /dev/ttyUSB0
  • Display the current configuration on ttyUSB0 (remote device):
 stty -a -F /dev/ttyUSB0                                                                 /* display the configuration of host uart aka 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)
 echo "HELLO" > /dev/ttyUSB0                                                             /* command to execute from host terminal*/

  • send data from STM32MPU to remote PC over USART3 with default termios configuration (8 frames length, no parity)
 echo "HELLO" > /dev/ttySTM1                                                          /* command to execute from STM32 terminal*/



4.2 Parity errors detection[edit]

Some additional termios funtions allow to enable parity errors detection:

  • parenb: Parity enable.
  • parodd: Odd parity else even.
  • inpck: Enable input parity check.


Exemples:

  • Configure a port on ttySTM1 (usart3) with even parity enabling
 stty -F /dev/ttySTM1 115200 -echo parenb -parodd inpck                               /* STM32 parity enabling*/

  • Open a port on ttySTM1 (usart3) to receive data
 cat /dev/ttySTM1 &

Open a minicom in a second terminal on the remote device connected on USART3 pins
 minicom -D /dev/ttyUSB0

  • Configure a port on ttyUSB0 (remote device) with even parity enabling:
 stty -a -F /dev/ttyUSB0 115200 -echo parenb -parodd inpck                               /* remote device parity enabling*/
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)
 echo "HELLO" > /dev/ttyUSB0                                                             /* command to execute from host terminal*/

  • send data from STM32MPU to remote PC over USART3 with default termios configuration (8 frames length, no parity)
 echo "HELLO" > /dev/ttySTM1                                                          /* command to execute from STM32 terminal*/


4.3 Framing errors detection[edit]

Some additional termios funtions allow to enable framing errors detection:

  • csize: Number of bits per byte (CS5, CS6, CS7, or CS8).
  • inpck: Enable input framing check.


Exemples:

  • Configure a port on ttySTM1 (usart3) with framing check enabling and 7 data bits length frames
 stty -F /dev/ttySTM1 115200 -echo cs7 inpck                                          /* STM32 framing enabling*/

  • Open a port on ttySTM1 (usart3) to receive data
 cat /dev/ttySTM1 &

Open a minicom in a second terminal on the remote device connected on USART3 pins
 minicom -D /dev/ttyUSB0

  • Configure a port on ttyUSB0 (remote device) with framing check enabling and 7 data bits length frames
 stty -a -F /dev/ttyUSB0 115200 -echo cs7 inpck                                          /* remote device parity enabling*/
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)
 echo "HELLO" > /dev/ttyUSB0                                                             /* command to execute from host terminal*/

  • send data from STM32MPU to remote PC over USART3 with default termios configuration (8 frames length, no parity)
 echo "HELLO" > /dev/ttySTM1                                                          /* command to execute from STM32 terminal*/

5 Identify processes using a tty serial device (with fuser tool)[edit]

 fuser /dev/ttySTM0
395 691 3872                                                                               /* the process numbered 395, 691 and 3872 are using a tty serial device*/


6 Link a tty serial device with a line discipline (with ldattach tool)[edit]

Attach ttySTM1 with line discipline number n :

 ldattach n /dev/ttySTM1


7 File transfer over serial console[edit]

Please see the dedicated article How to transfer a file over serial console.

8 References[edit]

<noinclude>

{{ArticleMainWriter | ErwanLR}}
{{ArticleApprovedVersion | ErwanLR | FabriceG (Passed), AlexandreT (Passed) , NathalieS | No previous approved version | AlainF - 14Aug'18 - 8369 | 28Aug'18}}
{{UpdateNeededForNewRelease}}

[[Category:Serial TTY|3]]</noinclude>

__TOC__<br />


==Purpose==
This article describes how to use TTY with a user terminal. The TTY overview is described in [[Serial TTY overview]] article. <br/>


The use case of the following examples is a data transfer between a STM32 MPU board and PC, over a USB to a RS232 adapter cable. <br/>

The setup of this use case is described in details in the [[How_to_get_Terminal#Remote Terminal via serial link (UART/USB)|How to get Terminal]] article.

For the following examples:

* uart4 is activated by default (for the Linux console)
* usart3 is enabled by [[Serial TTY_device_tree_configuration#Activation_of_a_USART_or_UART_instance|device tree]]
* The usart3 pins are connected to a RS232 card
* The RS232 card is connected to the PC over the USB to RS232 adapter cable.

Note: Some TTY tools are used in this article. A list of TTY tools is defined a dedicated article [TTY Tools ].

== Print the file name of the terminal connected to standard input (with tty tool)==
 {{Board$}} tty
 {{Green|'''/dev/ttySTM0'''}}                                                                                 {{highlight|/* the console is connected to uart4, aka ttySTM0 */}}

{{InternalInfo|<br/>

With Beta1 DV and earlier, UART4 is linked to TTYS3<br/>

 {{Green|'''/dev/ttyS3'''}}                                                                                 {{highlight|/* the console is connected to uart4, aka ttyS3 */}}
}}

==Change serial port configuration (with stty tool)==
Many serial port properties can be displayed and changed with the stty tool. The full feature list is available in stty user manual pages <ref>[https://linux.die.net/man/1/stty stty manual page]</ref> .
 {{Board$}} stty --help

* Display the current configuration:
 {{Board$}} stty -a -F /dev/ttySTM1                                                             {{highlight|/* display the configuration of uart3 aka ttySTM1*/}}
 speed 115200 baud; rows 45; columns 169; line = 0;

{{InternalInfo|<br/>

With Beta1 DV and earlier, USART3 is linked to TTYS2<br/>

 {{Board$}} stty -a -F /dev/ttyS2                                                             {{highlight|/* display the configuration of uart3 aka ttyS2*/}}
}} 

* Display only the current baud rate:
 {{Board$}} stty -F /dev/ttySTM1 speed
 115200                                                                                     {{highlight|/* uart3 aka ttySTM1 baud rate is set to 115200 bps */}}

{{InternalInfo|<br/>

With Beta1 DV and earlier, USART3 is linked to TTYS2<br/>

 {{Board$}} stty -F /dev/ttyS2 speed
 115200                                                                                     {{highlight|/* uart3 aka ttyS2 baud rate is set to 115200 bps */}}
}} 

* Change the baud rate: 
stty -F /dev/ttySTMx ''EXPECTED_BAUDRATE''<br/>
<br/>

Example: change the baud rate to 19200
 {{Board$}} stty -F /dev/ttySTM1 19200                                                          {{highlight|/* uart3 aka ttySTM1 baud rate has been changed to 19200 bps */}}

{{InternalInfo|<br/>

With Beta1 DV and earlier, USART3 is linked to TTYS2<br/>

 {{Board$}} stty -F /dev/ttyS2 19200                                                          {{highlight|/* uart3 aka ttyS2 baud rate has been changed to 19200 bps */}}
}}

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
<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>.

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

===Default configuration (8 data bits frame, no parity errors detection, no framing errors detection)===Sending data can be simply done by opening the device as a file and writing data to it:
.<br/>

* Configure a port on ttySTM1 (usart3)
 {{Board$}} stty -F /dev/ttySTM1 115200 -echo

{{InternalInfo|<br/>

With Beta1 DV and earlier, USART3 is linked to TTYS2<br/>

 {{Board$}} stty -F /dev/ttyS2 115200 -echo
}}

* Display the current configuration on ttySTM1 (usart3):
 {{Board$}} stty -a -F /dev/ttySTM1                                                             {{highlight|/* display the configuration of uart3 aka ttySTM1*/}}
 speed 115200 baud; rows 45; columns 169; line = 0;

* Open a port on ttySTM1 (usart3) to receive data
 {{Board$}} cat /dev/ttySTM1 &

{{InternalInfo|<br/>

With Beta1 DV and earlier, USART3 is linked to TTYS2<br/>

 {{Board$}} cat /dev/ttyS2 &
}}

* On the remote PC, identify the tty terminal associated to RS232 card connected on STM32MPU USART3 pins
 {{PC$}} ls /dev/ttyUSB*  /                                                                       {{highlight|/* command to execute from host terminal*/}}
 /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):
 {{PC$}} stty -a -F /dev/ttyUSB0                                                                 {{highlight|/* display the configuration of host uart aka 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){{PC$}} echo "HELLO" > /dev/ttyUSB0  *                                                            {{highlight|/* command to execute from host terminal*/}}

* send data from STM32MPU to remote PC over USART3  with default termios configuration (8 frames length, no parity){{Board$}} echo "HELLO" > /dev/ttySTM1  {{                                                        {{highlight|/* command to execute from STM32 terminal*/}}

{{InternalInfo|<br/>

With Beta1 DV and earlier, USART3 is linked to TTYS2<br/>

 {{Board$}} echo "HELLO" > /dev/ttyS2
}}

==<br/>


===Parity errors detection===
Some additional termios funtions allow to enable parity errors detection:
* parenb: Parity enable.
* parodd: Odd parity else even.
* inpck: Enable input parity check.<br/>

Exemples:
* Configure a port on ttySTM1 (usart3) with even parity enabling
 {{Board$}} stty -F /dev/ttySTM1 115200 -echo parenb -parodd inpck                               {{highlight|/* STM32 parity enabling*/}}

* 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:
 {{PC$}} stty -a -F /dev/ttyUSB0 115200 -echo parenb -parodd inpck                               {{highlight|/* remote device parity enabling*/}}
 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)
 {{PC$}} echo "HELLO" > /dev/ttyUSB0                                                             {{highlight|/* command to execute from host terminal*/}}

* send data from STM32MPU to remote PC over USART3  with default termios configuration (8 frames length, no parity)
 {{Board$}} echo "HELLO" > /dev/ttySTM1                                                          {{highlight|/* command to execute from STM32 terminal*/}}<br/>


===Framing errors detection===
Some additional termios funtions allow to enable framing errors detection:
* csize: Number of bits per byte (CS5, CS6, CS7, or CS8).
* inpck: Enable input framing check.<br/>

Exemples:
* Configure a port on ttySTM1 (usart3) with framing check enabling and 7 data bits length frames
 {{Board$}} stty -F /dev/ttySTM1 115200 -echo cs7 inpck                                          {{highlight|/* STM32 framing enabling*/}}

* 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
 {{PC$}} stty -a -F /dev/ttyUSB0 115200 -echo cs7 inpck                                          {{highlight|/* remote device parity enabling*/}}
 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)
 {{PC$}} echo "HELLO" > /dev/ttyUSB0                                                             {{highlight|/* command to execute from host terminal*/}}

* send data from STM32MPU to remote PC over USART3  with default termios configuration (8 frames length, no parity)
 {{Board$}} echo "HELLO" > /dev/ttySTM1                                                          {{highlight|/* command to execute from STM32 terminal*/}}

==Identify processes using a tty serial device (with fuser tool)==
 {{Board$}} fuser /dev/ttySTM0
 395 691 3872                                                                               {{highlight|/* the process numbered 395, 691 and 3872 are using a tty serial device*/}}

{{InternalInfo|<br/>

With Beta1 DV and earlier, UART4 is linked to TTYS3<br/>

 {{Board$}} fuser /dev/ttyS3
}}

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

Attach ttySTM1 with [[Serial TTY line discipline|line discipline]] number ''n'' :
 {{Board$}} ldattach ''n'' /dev/ttySTM1

{{InternalInfo|<br/>

With Beta1 DV and earlier, USART3 is linked to TTYS2<br/>

 {{Board$}} ldattach ''n'' /dev/ttyS2
}}

==File transfer over serial console==
Please see the dedicated article [[How to transfer a file over serial console]].

==References==<references />
Line 80: Line 80:
   
 
==Send / Receive data (with stty, minicom, echo and cat tools)==
 
==Send / Receive data (with stty, minicom, echo and cat tools)==
Sending data can be simply done by opening the device as a file and writing data to it:
 
   
  +
===Default configuration (8 data bits frame, no parity errors detection, no framing errors detection)===
  +
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 (usart3)
 
* Configure a port on ttySTM1 (usart3)
Line 90: Line 91:
 
  {{Board$}} stty -F /dev/ttyS2 115200 -echo
 
  {{Board$}} stty -F /dev/ttyS2 115200 -echo
 
}}
 
}}
  +
  +
* Display the current configuration on ttySTM1 (usart3):
  +
{{Board$}} stty -a -F /dev/ttySTM1                                                            {{highlight|/* display the configuration of uart3 aka ttySTM1*/}}
  +
speed 115200 baud; rows 45; columns 169; line = 0;
   
 
* Open a port on ttySTM1 (usart3) to receive data
 
* Open a port on ttySTM1 (usart3) to receive data
Line 100: Line 105:
   
 
* On the remote PC, identify the tty terminal associated to RS232 card connected on STM32MPU USART3 pins
 
* On the remote PC, identify the tty terminal associated to RS232 card connected on STM32MPU USART3 pins
  {{PC$}} ls /dev/ttyUSB*
+
  {{PC$}} ls /dev/ttyUSB*                                                                         {{highlight|/* command to execute from host terminal*/}}
 
  /dev/ttyUSB0
 
  /dev/ttyUSB0
   
* Open a minicom terminal on the remote device connected on USART3 pins
+
* Open a minicom in a second terminal on the remote device connected on USART3 pins
 
  {{PC$}} minicom -D /dev/ttyUSB0
 
  {{PC$}} minicom -D /dev/ttyUSB0
   
* Send data from remote PC to STM32MPU over USART3
+
* Display the current configuration on ttyUSB0 (remote device):
  {{PC$}} echo "HELLO" > /dev/ttyUSB0
+
  {{PC$}} stty -a -F /dev/ttyUSB0                                                                 {{highlight|/* display the configuration of host uart aka ttyUSB0*/}}
  +
speed 115200 baud; rows 45; columns 169; line = 0;
   
* send data from STM32MPU to remote PC over USART3
+
* Send data from remote PC to STM32MPU over USART3 with default termios configuration (8 frames length, no parity)
  {{Board$}} echo "HELLO" > /dev/ttySTM1
+
{{PC$}} echo "HELLO" > /dev/ttyUSB0                                                            {{highlight|/* command to execute from host terminal*/}}
  +
 
  +
* send data from STM32MPU to remote PC over USART3 with default termios configuration (8 frames length, no parity)
  +
  {{Board$}} echo "HELLO" > /dev/ttySTM1                                                         {{highlight|/* command to execute from STM32 terminal*/}}
   
 
{{InternalInfo|<br/>
 
{{InternalInfo|<br/>
Line 116: Line 125:
 
  {{Board$}} echo "HELLO" > /dev/ttyS2
 
  {{Board$}} echo "HELLO" > /dev/ttyS2
 
}}
 
}}
  +
<br/>
  +
  +
===Parity errors detection===
  +
Some additional termios funtions allow to enable parity errors detection:
  +
* parenb: Parity enable.
  +
* parodd: Odd parity else even.
  +
* inpck: Enable input parity check.
  +
<br/>
  +
Exemples:
  +
* Configure a port on ttySTM1 (usart3) with even parity enabling
  +
{{Board$}} stty -F /dev/ttySTM1 115200 -echo parenb -parodd inpck                              {{highlight|/* STM32 parity enabling*/}}
  +
  +
* 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:
  +
{{PC$}} stty -a -F /dev/ttyUSB0 115200 -echo parenb -parodd inpck                              {{highlight|/* remote device parity enabling*/}}
  +
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)
  +
{{PC$}} echo "HELLO" > /dev/ttyUSB0                                                            {{highlight|/* command to execute from host terminal*/}}
  +
  +
* send data from STM32MPU to remote PC over USART3  with default termios configuration (8 frames length, no parity)
  +
{{Board$}} echo "HELLO" > /dev/ttySTM1                                                          {{highlight|/* command to execute from STM32 terminal*/}}
  +
<br/>
  +
  +
===Framing errors detection===
  +
Some additional termios funtions allow to enable framing errors detection:
  +
* csize: Number of bits per byte (CS5, CS6, CS7, or CS8).
  +
* inpck: Enable input framing check.
  +
<br/>
  +
Exemples:
  +
* Configure a port on ttySTM1 (usart3) with framing check enabling and 7 data bits length frames
  +
{{Board$}} stty -F /dev/ttySTM1 115200 -echo cs7 inpck                                          {{highlight|/* STM32 framing enabling*/}}
  +
  +
* 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
  +
{{PC$}} stty -a -F /dev/ttyUSB0 115200 -echo cs7 inpck                                          {{highlight|/* remote device parity enabling*/}}
  +
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)
  +
{{PC$}} echo "HELLO" > /dev/ttyUSB0                                                            {{highlight|/* command to execute from host terminal*/}}
  +
  +
* send data from STM32MPU to remote PC over USART3  with default termios configuration (8 frames length, no parity)
  +
{{Board$}} echo "HELLO" > /dev/ttySTM1                                                          {{highlight|/* command to execute from STM32 terminal*/}}
   
 
==Identify processes using a tty serial device (with fuser tool)==
 
==Identify processes using a tty serial device (with fuser tool)==