如何在 Linux 上设置自定义波特率? [英] How can I set a custom baud rate on Linux?

查看:48
本文介绍了如何在 Linux 上设置自定义波特率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我在 Linux 上的串行端口与具有 termios.h 中未定义的非标准波特率的设备通信.

I want to communicate over my serial port on Linux to a device with a non-standard-baud rate that is not defined in termios.h.

我尝试了 这篇文章中的波特率别名"方法,但是当我执行我的 C 程序时(我把它命名为testprogram"),Linux 说 testprogram 在 ttyS0 上设置自定义速度.这已被弃用."

I tried the "baud rate aliasing"-method from this post, but when I execute my C-program (I’ve named it "testprogram"), Linux says "testprogram sets custom speed on ttyS0. This is deprecated."

我在 Google 上进行了一些搜索,似乎还有另一种(更新的?)方法可以将波特率更改为非标准值:在 http://sourceware.org/ml/libc-help/2009-06/msg00016.html 作者说struct termiosc_flag 必须与 BOTHER (=CBAUDEX | B0) 进行 OR'd.

I did some search on Google, and it seems that there is another (newer?) method to change the baud rate to a non-standard-value: On http://sourceware.org/ml/libc-help/2009-06/msg00016.html the author says that the c_flag of struct termios must be OR’d with BOTHER (=CBAUDEX | B0).

使用这种方法,波特率直接在 struct termiosc_ispeedc_ospeed 成员中设置.但是,我不知道如何在我的 C 程序中使用这种方法.正如作者所说,当我包含 termios.h 时,没有定义/可用的 BOTHER,那么应该如何设置波特率呢?

With this method the baud rates are set directly in the c_ispeed and c_ospeed-members of the struct termios. However, I don’t know how I use this method in my C program. Like the author said, there is no BOTHER defined/available when I include termios.h, so what should be done to set the baud rate this way?

如何在不更改内核的情况下将波特率设置为非标准值?

How can I set the baud rate to a non-standard-value without changing the kernel?

推荐答案

我注意到关于 BOTHER 未定义的同样情况.正如 Jamey Sharp 所说,您可以在 <asm/termios.h> 中找到它.只是一个警告,我想我同时遇到了问题,包括它和常规 <termios.h> 文件.

I noticed the same thing about BOTHER not being defined. Like Jamey Sharp said, you can find it in <asm/termios.h>. Just a forewarning, I think I ran into problems including both it and the regular <termios.h> file at the same time.

除此之外,我发现我拥有的 glibc 仍然无法正常工作,因为 glibc 的 tcsetattr 正在为不注意速度设置的旧式 struct termios 版本执行 ioctl.我可以通过使用新样式的 termios2 结构手动执行 ioctl 来设置自定义速度,包括 <asm/termios.h>:

Aside from that, I found with the glibc I have, it still didn't work because glibc's tcsetattr was doing the ioctl for the old-style version of struct termios which doesn't pay attention to the speed setting. I was able to set a custom speed by manually doing an ioctl with the new style termios2 struct, which should also be available by including <asm/termios.h>:

struct termios2 tio;

ioctl(fd, TCGETS2, &tio);
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= BOTHER;
tio.c_ispeed = 12345;
tio.c_ospeed = 12345;
ioctl(fd, TCSETS2, &tio);

这篇关于如何在 Linux 上设置自定义波特率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆