0

I'm testing an UART application on the BeagleBone Black and when I try to read from the device it blocks forever. A write works fine. I hooked up a logic analyzer to inspect the line and I can see the data being transmitted and received, but it just always blocks on a read.

int main() {
  int res;
  struct termios tty;
  memset(&tty, 0, sizeof(tty));
  int serial = open("/dev/ser2", O_RDWR | O_NOCTTY);
  if (!isatty(serial))
    return false;
  // Setting the Baud rate
  cfsetispeed(&tty, B9600);
  cfsetospeed(&tty, B9600);
  // 8N1 Mode
  tty.c_cflag &= ~PARENB;
  tty.c_cflag &= ~CSTOPB;
  tty.c_cflag &= ~CSIZE;
  tty.c_cflag |= CS8;
  tty.c_cflag &= ~CRTSCTS;
  tty.c_cflag |= CREAD | CLOCAL;
  tty.c_iflag &= ~(IXON | IXOFF | IXANY);

  tty.c_iflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ISIG);
  tty.c_oflag &= ~OPOST;

  tty.c_cc[VMIN] = 1;
  tty.c_cc[VTIME] = 0;
  if ((tcsetattr(serial, TCSANOW, &tty)) != 0)
    return false;
  if ((tcflush(serial, TCIOFLUSH)) != 0)
    return false;

  while (1)
  {
    uint8_t wrBuff[3] = {0xAA, 0xBB, 0xCC};
    uint8_t res = write(serial, wrBuff, 3);
    printf("Write = %d", res);
    res = read(serial, wrBuff, 3);
    printf("Read = %d", res);
  }
  return 0;
}

I attempted the fix mentioned in the comments below regards the incorrect flag set but this did not fix the issue.

  • Use `O_SYNC` with `open`. Also see [How to open, read, and write from serial port in C?](https://stackoverflow.com/q/6947413/608639) and [Reading and writing to serial port in C on Linux](https://stackoverflow.com/q/18108932/608639). – jww May 12 '19 at 22:52
  • @jww I had tried `O_SYNC` flag before but open returns an error for some reason with that flag on QNX – test12341990 May 12 '19 at 22:56
  • @jww Using those examples provided above my read is still hanging? – test12341990 May 12 '19 at 23:12
  • 2
    Possible duplicate of [Ubuntu Serial Communication: reads failing and then coming in all at once](https://stackoverflow.com/questions/51195829/ubuntu-serial-communication-reads-failing-and-then-coming-in-all-at-once). Exact same bug, except your input does not have any EOLs, so read does not terminate. – sawdust May 13 '19 at 01:42
  • @sawdust I fixed that bug but unfortunately the read is still hanging. In raw mode I dont need to send any EOL correct? Just sending the bytes shoudl suffice. – test12341990 May 13 '19 at 09:26
  • 1
    The initialization of the termios structure `tty` is improper, and then the code is illogical. You zero-out the structure instead of a proper initialization by calling **tcgetattr()**. See [Setting Terminal Modes Properly](http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_12.html#SEC237). Then the code illogically clears attribute bits as if the structure had been properly initialized but was actually cleared. – sawdust May 13 '19 at 19:11

0 Answers0