0

我正在尝试使用串行端口从自定义硬件中读取数据。我相信配置是正确的。问题是,我看到一些字节丢失了。在数据中,指定了数据的长度,因此我知道是否缺少某些数据。

我的配置如下:

int set_interface_attribs (int fd, int speed, int parity)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0) return -1;

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

    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
    tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw input
    tty.c_cc[VMIN]  = 1;                    // read does block
    tty.c_cc[VTIME] = 0;                    // 0 seconds read timeout

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

    //disable hw flow control
    #ifdef CRTSCTS
        tty.c_cflag &= ~CRTSCTS;
    #endif
    #ifdef CNEW_RTSCTS
        tty.c_cflag &= ~CNEW_RTSCTS;
    #endif

    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_iflag &= ~(ISTRIP | IGNCR | INLCR | ICRNL
        #ifdef IUCLC
             | IUCLC
        #endif
    );
    tty.c_oflag &= ~(OPOST
        #ifdef ONLCR
             | ONLCR
        #endif
        #ifdef OCRNL
             | OCRNL
        #endif
        #ifdef ONOCR
             | ONOCR
        #endif
        #ifdef ONLRET
             | ONLRET
        #endif
    );

    if (tcsetattr (fd, TCSANOW, &tty) != 0) return -1;

    return 0;
}

这就是我的阅读方式:

int fd = open (portname, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
char buf [10];
while(1) {
   int n = 0;
   while (n = read (fd, buf, sizeof buf) > 0) {
       write(STDOUT_FILENO, buf, n); //this printing
       .. other job ..
   }
}

以下是数据示例:

// correct data
#*69880001000800010007003F4530302403322402FE24080524000024012C2400142430303030302452B4FF

// example of lost data (lost data is signed by _ )
#*69880001000800010007003F4530302403322402FE2408092_000_24012C240014243030303030241674FF
#*69880001000800010007003F4530302403312402FF24080524_00024012C240014243030303030244EF9FF

对于配置,我需要的是:波特率:19200 数据位:8 个停止位:1 奇偶校验:无流量控制:无

仅供参考,我的硬件正在逐字节发送数据。所以上面代码中的n始终为 1(或 0)。

当我尝试使用腻子时,一切都很好。有什么我错过的吗?

谢谢

4

0 回答 0