如何在 Linux 上将波特率设置为 307,200?

新手上路,请多包涵

基本上我使用以下代码来设置串口的波特率:

 struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
tcsetattr(fd, TCSANOW, &options);

这很好用。但是知道我必须与使用波特率 307,200 的设备通信。我该如何设置? cfsetispeed(&options, B307200); 不起作用,没有定义 B307200

我使用 MOXA Uport 1150(实际上是一个 USB 转串口转换器)和英特尔主板的标准串口进行了尝试。我不知道后者的确切类型,setserial 只是将其报告为 16550A。

原文由 cairol 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 920
1 个回答

我使用 termios2ioctl() 命令完成了这项工作。

 struct termios2 options;
ioctl(fd, TCGETS2, &options);
options.c_cflag &= ~CBAUD;    //Remove current baud rate
options.c_cflag |= BOTHER;    //Allow custom baud rate using int input
options.c_ispeed = 307200;    //Set the input baud rate
options.c_ospeed = 307200;    //Set the output baud rate
ioctl(fd, TCSETS2, &options);

之后,您应该能够查询端口设置并查看您的自定义波特率以及其他设置(可能使用 stty 命令)。

原文由 Chef Pharaoh 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题