c - Linux C串口读取

标签 c linux serial-port tty

我正在使用串行端口从设备接收数据。通信正常,但读取数据时出现问题。我在 Linux (Ubuntu) 上工作。

这是我的代码:

int OpenPort(char *PortName, int *FileDesc) {
    *FileDesc = open (PortName, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0) {
            perror("fakap");
            return (P_OPEN_ERROR);
    }
    else return(P_OPEN_SUCCESS);

};

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

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

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag |= (IGNBRK | IGNPAR);,

        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_iflag &= ~(ISTRIP);
        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD | HUPCL);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;
        tty.c_lflag = 0;
        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                perror("error %d from tcsetattr");
                return -1;
        }
        return 0;
}

阅读部分:

void PortRead(int *FileDesc) {

    memset(&recBuff[0], 0, sizeof(recBuff)); //clear buff

    int n = read (*FileDesc, recBuff, sizeof(recBuff));  // read
    printf("n: %d \n", n);
    int i = 0;
    for(i = 0;i<n;i++) {
        recData.buf[i].b_int = recBuff[i]; // put rec buff to ANS_u type variable
    }
};

一切正常,直到我收到大于 8 字节的消息。 read() 读取的数据不超过 8 个字节,所以我必须第二次使用 read() 来读取所有数据。 当我使用 GtkTerm 时一切正常,但在 C 实现过程中出现问题。

最佳答案

so I have to use read() second time to read to read all data.

您必须准备好在任何情况下第二次、第三次、第四次等调用 read()始终。仅仅因为您知道对方发送了 16 个字节,您不能假设所有 16 个字节都会在一次操作中从 read() 中出来。它可能是 8+8、16*1、3+3+10 或任何其他总和为 16 的组合(尽管其中大多数组合的可能性极小)。

来自 read(2) 的手册页:

RETURN VALUE

On success, the number of bytes read is returned ... It is not an error if this number is smaller than the number of bytes requested


您还应该准备像已经评论的那样处理 EINTR。


在函数 OpenPort 中,open 的结果被分配给 *FileDesc 但一些其他的 fd 变量被检查,这也应该被修复。

关于c - Linux C串口读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22527415/

相关文章:

linux - nfs mount shell 脚本中需要一元运算符

linux - 为什么 "mycommand >>file1 >>file2"不附加到两个文件?

c++ - 等待 COM 端口上的数据?

language-agnostic - 用于协议(protocol)开发/调试的 RS-232 串行监听工具

c - C 中用于定义 3D 指针数组的正确语法

c - 我是 C 新手。第二个 for 循环永远不会结束为什么会这样

java - Windows 上的 Groovy URL UnknownHostException

Bash、串行 I/O 和 Arduino

c - 如何从函数中的变量获取值并在 C 中的另一个函数中使用它?

c - 位图字体渲染问题