我在termios.h
基础上编写了一个基于Linux的串口程序(应该支持B921600
),这里是代码:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h> //O_RDWR | O_NOCTTY
int main (void)
{
int hd;
struct termios setting;
speed_t speed_i, speed_o;
hd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
setting.c_cflag &= ~CBAUD;
setting.c_cflag &= ~CBAUDEX;
if(tcgetattr(hd, &setting) != 0)
{
printf("open error\n");
}
cfsetispeed(&setting, B921600);
cfsetospeed(&setting, B921600);
setting.c_cflag |= B921600;
if(tcsetattr(hd, TCSANOW, &setting) != 0)
{
//ERROR;
}
speed_i = cfgetispeed(&setting);
speed_o = cfgetospeed(&setting);
printf("out baudrate: %x\n",speed_o);
printf("in baudrate: %x\n",speed_i);
if(tcgetattr(hd, &setting) != 0)
{
//ERROR;
}
speed_i = cfgetispeed(&setting);
speed_o = cfgetospeed(&setting);
printf("out baudrate: %x\n",speed_o);
printf("in baudrate: %x\n",speed_i);
return 0;
}
之前所有都正确:
if(tcsetattr(hd, TCSANOW, &setting) != 0)
{
//ERROR;
}
speed_i = cfgetispeed(&setting);
speed_o = cfgetospeed(&setting);
printf("out baudrate: %x\n",speed_o);
printf("in baudrate: %x\n",speed_i);
然后我做tcgetattr
,之后,速度被读作所有1002
,这是我的测试日志:
out baudrate: 1007
in baudrate: 1007
out baudrate: 1002
in baudrate: 1002
有什么问题?似乎波特率高于115200
无法设置?