Template:ArticleMainWriter Template:ArticleApprovedVersion
1. Purpose[edit source]
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 source]
# The console is connected to uart4 (aka ttySTM0) # /dev/ttySTM0tty
3. Change serial port configuration (with stty tool)[edit source]
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:
# Display the configuration of uart3 (aka ttySTM1) # speed 115200 baud; rows 45; columns 169; line = 0;stty -a -F /dev/ttySTM1
- Display only the current baud rate:
# uart3 (aka ttySTM1) baud rate is set to 115200 bps # 115200stty -F /dev/ttySTM1 speed
- 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 # 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].
4. Send / Receive data (with stty, minicom, echo and cat tools)[edit source]
4.1. Default configuration (8 data bits frame, no parity errors detection, no framing errors detection)[edit source]
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)
stty -F /dev/ttySTM1 115200 -echo
- Display the current configuration on ttySTM1 (usart3):
# display the configuration of uart3 (aka ttySTM1) # stty -a -F /dev/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
# Command to execute from host terminal # ls /dev/ttyUSB*
/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):
# Display the configuration of host uart (aka ttyUSB0) # 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 # 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 # echo "HELLO" > /dev/ttySTM1
4.2. Parity errors detection[edit source]
Some additional termios funtions 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 # stty -F /dev/ttySTM1 115200 -echo parenb -parodd inpck ignpar
- 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:
# Remote device parity enabling # stty -a -F /dev/ttyUSB0 115200 -echo parenb -parodd inpck ignpar 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 # 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 # echo "HELLO" > /dev/ttySTM1
4.3. Framing errors detection[edit source]
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
- ignpar: Ignore characters with parity or framing errors
Exemples:
- Configure a port on ttySTM1 (usart3) with framing check enabling and 7 data bits length frames
# STM32 framing enabling # stty -F /dev/ttySTM1 115200 -echo cs7 inpck ignpar
- 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
# Remote device parity enabling # stty -a -F /dev/ttyUSB0 115200 -echo cs7 inpck ignpar 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 # 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 # echo "HELLO" > /dev/ttySTM1
5. Identify processes using a tty serial device (with fuser tool)[edit source]
# The process numbered 395, 691 and 3872 are using a tty serial device # 395 691 3872fuser /dev/ttySTM0
6. Link a tty serial device with a line discipline (with ldattach tool)[edit source]
Attach ttySTM1 with line discipline number n :
ldattach n /dev/ttySTM1
7. File transfer over serial console[edit source]
Please see the dedicated article How to transfer a file over serial console.