Note that there are some explanatory texts on larger screens.

plurals
  1. POSerial port not reading
    primarykey
    data
    text
    <p>I've got this strange issue with reading from a serial port on Linux. About 50% of the time, I won't get any data back from the serial port when I'm reading from it, yet if I use GTKTerm I get data back 100% of the time. I've double-checked my settings, and the terminal settings that I'm using are the exact same that GTKTerm uses, so I'm mystified as to why this would occur. The only thing that I could think of is that the port is set in canonical mode, not raw mode, but I set it properly - the device that I'm reading from does not send back newlines with all of the commands. However, everything is working properly when the device sends back commands with a newline character.</p> <p>Here's the code I'm using:</p> <pre><code>int fd = open(serial_port, O_RDWR ); if( fd &lt; 0 ){ perror("open"); } //Set the serial port settings struct termios newio; if( tcgetattr( fd, &amp;newio ) &lt; 0 ) perror("tcgetattr"); if( cfsetospeed( &amp;newio, B9600 ) &lt; 0 ) perror("cfsetospeed"); newio.c_cflag &amp;= ~CSTOPB; newio.c_cflag &amp;= ~CSIZE; newio.c_cflag |= CS8; newio.c_cflag |= CREAD; newio.c_iflag |= IGNPAR; newio.c_iflag |= IGNBRK; newio.c_iflag &amp;= ~BRKINT; newio.c_iflag &amp;= ~ICRNL; newio.c_iflag &amp;= ~IXON; newio.c_cflag |= CLOCAL; newio.c_oflag = 0; newio.c_lflag = 0; newio.c_cc[VTIME] = 0; newio.c_cc[VMIN] = 1; if( tcsetattr( fd, TCSANOW, &amp;newio ) &lt; 0 ) perror("tcsetattr"); tcflush( fd, TCOFLUSH ); tcflush( fd, TCIFLUSH ); </code></pre> <p><strong>Reading code (in separate thread)</strong></p> <pre><code>void* thr(void* ign){ char buffer[10]; while( 1 ){ int got = read(fd, buffer, 10); buffer[got] = 0; printf("got %s\n", buffer); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload