Search code examples
serial-port64-bituartubuntu-13.04

Read some bytes via serial port


I can't read byte 0x11 and 0x13 via serial port. Source:

    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 and Rx is connected. In console outputs:

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

And no more. Can't receive transmitted byte 0x13. At oscilloscope i see transmitted 0x13, but i can't understand why this byte is not receiving. Same thing with 0x11 byte. Other bytes is ok.


Solution

  • 0x11 and 0x13 are the XON/XOFF flow control characters (Control-Q and Control-S). You will need to disable XON/XOFF flow control if you want to be able to send and receive these characters, e.g.

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