UART serial port related under linux

The RS232 serial device will be recognized as /dev/ttyS * or ttymxc * on linux

1. Introduction to Serial Port

To operate the serial port, we generally use the following instructions:

1. Check the serial port baud rate and other information:

stty -F /dev/ttyS0 -a  #ttyS0为要查看的串口 

2. Set serial port parameters:

stty -F /dev/ttyS0 ispeed 115200 ospeed 115200 cs8 

This command sets serial port 1 (/dev/ttyS0) to 115200 baud rate, 8-bit data mode.
In general, it is enough to set these two parameters. If the data is garbled, you may need to set other parameters. Use man to view other setting options of stty.

1、显示某个串口参数信息:stty -F /dev/ttySTM6  -a
        
2、设置某个串口参数信息:
 
    2.1      7位数据位,无校验,1位停止位,无回显
 
       stty -F /dev/ttySTM6 speed 115200 cs7 -parenb -cstopb  -echo
        
    2.2     串口ttySTM6 波特率为115200,8位数据位,1位停止位,偶数校验位
  
       stty -F /dev/ttyS0 speed 115200 cs8 -cstopb parenb -parodd
 
3、串口发送数据:echo "1234456"  >  /dev/ttySTM6
 
4、显示串口接收数据:cat /dev/ttySTM6 &(后台)
 
5、stty命令功能参数
 
   5.1 校验位
    parenb:使终端进行奇偶校验,-parenb则是禁止校验;
    -parodd 偶数校验
    parodd 奇数校验  
 
   5.2 数据位
    cs5、cs6、cs7和cs8分别将字符大小设为5、6、7和8比特;
 
   5.3 波特率
    speed 波特率:设置波特率 
 
   5.4 停止位
    cstopb和-cstopb分别设置两个或一个停止位;

3. View the data received by the serial port:

cat /dev/ttyS0 

4. View the data received by the serial port in hexadecimal:

hexdump -C /dev/ttyO1

5. Send data to the serial port:

echo "111111" > /dev/ttyS0

2. Serial port test

You can use virtualbox virtual machine and virtual serial port tool VPSD (Configure Virtual Serial Port Driver) for testing
Reference: https://blog.csdn.net/zxw1473474655/article/details/126102955

1. First use VPSD to virtualize two serial ports

insert image description here
insert image description here

2. The virtual machine also configures the serial port

insert image description here
The name of the serial port of the path should be the same as that on Windows, and open the virtual machine after configuration

3. To check whether the computer has its own serial port, use the following command:
sudo cat /proc/tty/driver/serial

The option with tx, rx is the actual serial port, 0 corresponds to ttyS0, generally ttyS0 corresponds to COM1 under the window, and so on. According to the actual serial port, use ttyS0 or ttyS1 to test.

insert image description here
insert image description here

4. Use the serial port tool of windows and the serial port communication under linux

The default baud rate of ttyS0 under linux is 9600, corresponding to COM1 on windows

insert image description here

The serial ports com1 and com2 virtualized by VPSD are connected, that is, send data to com1, com2 will receive

Open the ttyS0 serial port on linux and wait for data

cat /dev/ttyS0

send data on win

insert image description here

The serial port receives data under linux

insert image description here

Configure ttyS0 under linux

#串口ttySTM6 波特率为9600,8位数据位,1位停止位,无校验位
stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb

Send data using ttyS0

echo "888888"  > /dev/ttyS0

The serial port under windows receives data

insert image description here

You can also use hexdump to display, the specific usage of hexdump, you can Baidu

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44618297/article/details/131881635