sta*_*nri 26 linux devices serial-port
我经常使用cat
我的 FPGA 开发板通过串行连接在控制台中查看调试信息,但我从来没有告诉 linux 波特率是多少。cat 如何知道串口连接的波特率是多少?
cat
只使用端口已经配置的任何设置。使用这个小 C 代码片段,您可以看到当前为特定串行端口设置的波特率:
获取波特率.c
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
int main() {
struct termios tios;
tcgetattr(0, &tios);
speed_t ispeed = cfgetispeed(&tios);
speed_t ospeed = cfgetospeed(&tios);
printf("baud rate in: 0%o\n", ispeed);
printf("baud rate out: 0%o\n", ospeed);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行:
./get-baud-rate < /dev/ttyS0 # or whatever your serial port is
Run Code Online (Sandbox Code Playgroud)
您得到的数字可以在 中查找/usr/include/asm-generic/termios.h
,其中有#define
s 等B9600
。请注意,头文件和get-baud-rate
输出中的数字都是八进制的。
也许你可以试验一下,看看这些数字在新启动时是什么样的,以及它们以后是否会改变。