Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing select() and fgets() to access information from a serial port
    primarykey
    data
    text
    <p>This is a followup to this question: <a href="https://stackoverflow.com/questions/6656579/how-to-wait-for-input-from-the-serial-port-in-the-middle-of-a-program">How to wait for input from the serial port in the middle of a program</a></p> <p>I am writing a program to control an Iridium modem that needs to wait for a response from the serial port in the middle of the program in order to verify that the correct response was given. In order to accomplish this, a user recommended I use the <code>select()</code> command to wait for this input.</p> <p>However, I have run into some difficulty with this approach. Initially, <code>select()</code> would return the value indicated a timeout on the response every time (even though the modem was sending back the correct responses, which I verified with another program running at the same time). Now, the program stops after one iteration, even with the correct response sent back from the modem.</p> <pre><code> //setting the file descriptor to the port int fd = open(portName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { /* * Could not open the port. */ perror("open_port: Unable to open /dev/ttyS0 - "); } else fcntl(fd, F_SETFL, 0); FILE *out = fopen(portName.c_str(), "w");//sets the serial port FILE *in = fopen(portName.c_str(), "r"); fd_set fds; FD_ZERO(&amp;fds); FD_SET(fd, &amp;fds); struct timeval timeout = { 10, 0 }; /* 10 seconds */ //int ret = select(fd+1, &amp;fds, NULL, NULL, &amp;timeout); /* ret == 0 means timeout, ret == 1 means descriptor is ready for reading, ret == -1 means error (check errno) */ char buf[100]; int i =0; while(i&lt;(sizeof(messageArray)/sizeof(messageArray[0]))) { //creates a string with the AT command that writes to the module std::string line1("AT+SBDWT="); line1+=convertInt( messageArray[i].numChar); line1+=" "; line1+=convertInt(messageArray[i].packetNumber); line1+=" "; line1+=messageArray[i].data; line1+=std::string("\r\n"); //creates a string with the AT command that initiates the SBD session std::string line2("AT+SBDI"); line2+=std::string("\r\n"); fputs(line1.c_str(), out); //sends to serial port //usleep(7000000); int ret =select(fd+1, &amp;fds, NULL, NULL, &amp;timeout); /* ret == 0 means timeout, ret == 1 means descriptor is ready for reading, ret == -1 means error (check errno) */ if (ret ==1){ fgets (buf ,sizeof(buf), in); //add code to check if response is correct } else if(ret == 0) { perror("timeout error "); } else if (ret ==-1) { perror("some other error"); } fputs(line2.c_str(), out); //sends to serial port //usleep(7000000); //Pauses between the addition of each packet. int ret2 = select(fd+1, &amp;fds, NULL, NULL, &amp;timeout); /* ret == 0 means timeout, ret == 1 means descriptor is ready for reading, ret == -1 means error (check errno) */ if(ret2 == 0) { perror("timeout error "); } else if (ret2 ==-1) { perror("some other error"); } i++; } </code></pre>
    singulars
    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.
 

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