Note that there are some explanatory texts on larger screens.

plurals
  1. POClear data at serial port in Linux in C?
    primarykey
    data
    text
    <p>I am testing the sending and receiving programs with the code as</p> <p>The main() function is below:</p> <pre> <code>#include "lib.h" int fd; int initport(int fd) { struct termios options; // Get the current options for the port... tcgetattr(fd, &options); // Set the baud rates to 19200... cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); // Enable the receiver and set local mode... options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; // Set the new options for the port... tcsetattr(fd, TCSANOW, &options); return 1; } int main(int argc, char **argv) { fd = open("/dev/pts/2", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open_port: Unable to open /dev/pts/1 - "); return 1; } else { fcntl(fd, F_SETFL, 0); } printf("baud=%d\n", getbaud(fd)); initport(fd); printf("baud=%d\n", getbaud(fd)); char sCmd[254]; sCmd[0] = 0x41; sCmd[1] = 0x42; sCmd[2] = 0x43; sCmd[3] = 0x00; if (!writeport(fd, sCmd)) { printf("write failed\n"); close(fd); return 1; } printf("written:%s\n", sCmd); usleep(500000); char sResult[254]; fcntl(fd, F_SETFL, FNDELAY); if (!readport(fd,sResult)) { printf("read failed\n"); close(fd); return 1; } printf("readport=%s\n", sResult); close(fd); return 0; } </code> </pre> <p>The lib.h contains read and write code as at:</p> <p><a href="https://stackoverflow.com/questions/2500567/parse-and-read-data-frame-in-c">Parse and read data frame in C?</a></p> <p>and got the issue:</p> <p>In order to test with serial port, I used the socat (<a href="https://help.ubuntu.com/community/VirtualSerialPort" rel="nofollow noreferrer">https://help.ubuntu.com/community/VirtualSerialPort</a> ) to create a pair serial ports on Linux and test my program with these port.</p> <p>The first time the program sends the data and the program receives data is ok. However, if I read again or even re-write the new data into the serial port, the return data is always null until I stop the virtual serial port and start it again, then the write and read data is ok, but still, only one time.</p> <p>(In the real case, the sending part will be done by another device, I am just taking care of the reading data from the serial port. I wrote both parts just to test my reading code.)</p> <p>Does anyone have any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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