如何波特率在Linux上设置为307200? [英] How to set baud rate to 307200 on Linux?

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

问题描述

基本上我用下面的code设置串口的波特率:

Basically I'm using the following code to set the baud rate of a serial port:

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

这工作得很好。但是知道我有与使用的307200波特率如何设置一个设备进行通信? 之后,cfsetispeed(安培;选项,B307200); 不工作,也没有 B307200 定义

This works very well. But know I have to communicate with a device that uses a baud rate of 307200. How can I set that? cfsetispeed(&options, B307200); doesn't work, there is no B307200 defined.

我试了一下使用MOXA UPORT 1150(这实际上是一个USB转串口转换器)和Intel主板的标准串行端口。我不知道确切的那种后者,只是setserial的报告为16550A。

I tried it using a MOXA Uport 1150 (that's actually a USB-to-serial converter) and the standard serial port of an Intel motherboard. I don't know the exact kind of the latter, setserial just reports it as 16550A.

推荐答案

Linux使用非标准波特率一个肮脏的方法,称为波特率走样。基本上,你告诉串口驱动程序间preT值 B38400 不同。这与 ASYNC_SPD_CUST 标志 serial_struct 控制对象标记

Linux uses a dirty method for non-standard baud rates, called "baud rate aliasing". Basically, you tell the serial driver to interpret the value B38400 differently. This is controlled with the ASYNC_SPD_CUST flag in serial_struct member flags.

您需要手动计算除数为自定义速度,如下所示:

You need to manually calculate the divisor for the custom speed as follows:

// configure port to use custom speed instead of 38400
ioctl(port, TIOCGSERIAL, &ss);
ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
ss.custom_divisor = (ss.baud_base + (speed / 2)) / speed;
closestSpeed = ss.baud_base / ss.custom_divisor;

if (closestSpeed < speed * 98 / 100 || closestSpeed > speed * 102 / 100) {
    sprintf(stderr, "Cannot set serial port speed to %d. Closest possible is %d\n", speed, closestSpeed));
}

ioctl(port, TIOCSSERIAL, &ss);

cfsetispeed(&tios, B38400);
cfsetospeed(&tios, B38400);

当然,你需要用合适的 baud_base 和除数设置的串口驱动程序。在preceding片段允许2%的偏差,这应该是好于大多数的目的。

Of course, you need a serial driver with suitable baud_base and divisor settings. The preceding snippet allows for 2% deviation, which should be ok for most purposes.

和告诉司机间preT B38400 再次为38400波特:

And to tell the driver to interpret B38400 as 38400 baud again:

ioctl(mHandle, TIOCGSERIAL, &ss);
ss.flags &= ~ASYNC_SPD_MASK;
ioctl(mHandle, TIOCSSERIAL, &ss);

作为一个忠告:我不知道,如果这种方法其他的* nix口味之间移植

As a word of caution: I'm not sure if this method is portable between other *nix flavors.

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

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