通过串口读取一些字节

时间:2014-06-09 10:00:21

标签: serial-port 64-bit uart ubuntu-13.04

我无法通过串口读取字节0x11和0x13。 来源:

    int fd; /* File descriptor for the port */
    fd = open(PORT_PATH, O_RDWR | O_NOCTTY);// ); | O_NDELAY
    if (fd == -1){//Could not open the port.
        fprintf(stderr, "open_port: Unable to open %s - %s\n", PORT_PATH, strerror(errno));
        return fd;
    }

    struct termios settings;
    tcgetattr(fd, &settings);
    cfsetispeed(&settings, B38400); // baud rate
    cfsetospeed(&settings, B38400); // baud rate
    settings.c_cflag &= ~PARENB; // no parity
    settings.c_cflag &= ~CSTOPB; // 1 stop bit
    settings.c_cflag &= ~CSIZE;
    settings.c_cflag &= ~CRTSCTS;
    settings.c_cflag |= (CS8 | CLOCAL | CREAD);
    settings.c_cc[VMIN] = 1;
    settings.c_cc[VTIME] = 0;
    tcflush(fd, TCIOFLUSH);
    tcsetattr(fd, TCSANOW, &settings);// apply the settings

    char data, rcv;
    data = 0x10;
    write(fd, &data, 1);
    read(fd, &rcv, 1);
    printf("rcv_data = 0x%02X\n", rcv);

    data = 0x12;
    write(fd, &data, 1);
    read(fd, &rcv, 1);
    printf("rcv_data = 0x%02X\n", rcv);

    data = 0x88;
    write(fd, &data, 1);
    read(fd, &rcv, 1);
    printf("rcv_data = 0x%02X\n", rcv);

    data = 0x13;
    write(fd, &data, 1);
    read(fd, &rcv, 1);
    printf("rcv_data = 0x%02X\n", rcv);

    data = 0x11;
    write(fd, &data, 1);
    read(fd, &rcv, 1);
    printf("rcv_data = 0x%02X\n", rcv);

    close(fd);

Tx和Rx已连接。在控制台输出中:

rcv_data = 0x10
rcv_data = 0x12
rcv_data = 0x88

不多了。无法接收发送的字节0x13。在示波器上,我看到发送的0x13,但我无法理解为什么这个字节没有接收。与0x11字节相同。其他字节都可以。

1 个答案:

答案 0 :(得分:2)

0x110x13XON/XOFF flow control个字符( Control-Q Control-S )。如果您希望能够发送和接收这些字符,则需要禁用XON / XOFF流控制,例如

setting.c_iflag &= ~(IXOFF | IXON);