简体   繁体   English

以100波特率处理串口

[英]Dealing with serial port at 100 baud rate

I am looking to know if it is possible to read from a serial port at 100 baud rate. 我想知道是否可以从100波特率的串行端口读取。 As per termio.h there is no provision to set 100 as baud rate. 根据termio.h ,没有规定将100设置为波特率。 I am working in Linux. 我在Linux工作。 The communicating device on the other end is sending data at 100 baud rate and it is fixed. 另一端的通信设备以100波特率发送数据并且是固定的。 I would like to know if my baud rate is set to 110, would it guarantee that the data I am receiving is correct? 我想知道我的波特率是否设置为110,它能保证我收到的数据是否正确? or is there any solution for this? 或者有什么解决方案吗?

Kindly guide. 请指导。

You're actually in luck. 你真的很幸运。 100 baud is low enough that you can compute a divisor that will do it (1,152) with typical 16450-compatible serial ports (which is pretty much what everything is) and linux supports custom divisors with the spd_cust parameter to setserial . 100波特是不够,你可以计算除数,会做它(1152)具有典型的16450兼容串行端口(这是几乎就是一切)和Linux的低支持定制除数spd_cust参数setserial

Hmmm.... 110 bps is unique among serial port speeds in that it conventionally has two stop bits (all other speeds use one stop bit), so that sending one character requires 10 bits for 7-bit data, or 11 bits for 8-bit data. 嗯.... 110 bps在串行端口速度中是独一无二的,因为它通常有两个停止位(所有其他速度使用一个停止位),因此发送一个字符需要10位用于7位数据,或11位用于8位位数据。

If the communication protocol was communicated as ten characters per second and someone ignorant of 1950s protocols might convert cps to baud by assuming only one stop bit and 8-bit data, they would conclude that 100 baud is the result. 如果通信协议以每秒十个字符的形式传送,并且不知道20世纪50年代协议的人可能通过假设只有一个停止位和8位数据将cps转换为波特,那么他们会得出100波特的结果。

If the custom setting for a true 100 baud doesn't work, try setting standard 110 baud. 如果真正的100波特的自定义设置不起作用,请尝试设置标准110波特。

Excerpted from a related answer : 摘录自相关答案

#include <errno.h>
#include <termios.h>
#include <unistd.h>

int
set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tcgetattr", errno);
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        if (speed == B110)
            tty.c_cflag |= CSTOPB;      // 2 stop bits for 110

        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // ignore break signal
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                error_message ("error %d from tcsetattr", errno);
                return -1;
        }
        return 0;
}

void
set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tggetattr", errno);
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                error_message ("error %d setting term attributes", errno);
}


...
char *portname = "/dev/ttyUSB1"
 ...
int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
        error_message ("error %d opening %s: %s", errno, portname, strerror (errno));
        return;
}

set_interface_attribs (fd, B110, 0);  // set speed to 115,200 bps, 8n2 (no parity)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM