Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6139849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:02:35+00:00 2026-05-23T18:02:35+00:00

This is a followup to this question: How to wait for input from the

  • 0

This is a followup to this question: How to wait for input from the serial port in the middle of a program

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 select() command to wait for this input.

However, I have run into some difficulty with this approach. Initially, select() 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.

 //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(&fds);
FD_SET(fd, &fds);
struct timeval timeout = { 10, 0 }; /* 10 seconds */
//int ret = select(fd+1, &fds, NULL, NULL, &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<(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, &fds, NULL, NULL, &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, &fds, NULL, NULL, &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++;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:02:36+00:00Added an answer on May 23, 2026 at 6:02 pm

    You aren’t using the same file handle for read/write/select, which is somewhat strange.

    You are not resetting your fd_sets, which are modified by select and would have all of your fds unset in the case of a timeout, making the next call timeout by default (as you are asking for no fds).

    you are also using buffered IO, which is bound to create headaches in this case. eg. fgets waits for either EOF (which won’t occur), or a newline, reading all the while. It will block until it gets its newline, so may keep you hanging indefinitely if that never occurs.
    It may also read more than it needs into the buffer, messing up your select read signal (you have data in the buffer, but select will time out, since there’s nothing to read on the filehandle).

    Bottom line is this:

    • use FD_SET in the loop to set/reset your fd sets, also reset your timeout, as select may modify it.
    • use a single handle for read/write/select, instead of multiple handles, eg. open file with fopen(..., "w+") or open(..., O_RDWR)
    • if still using fopen, try disabling buffering using setvbuf with the _IONBF buffering option.
    • otherwise, use open/read/write instead of fopen etc.

    I will note that part of this was mentioned in this answer to your previous question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup question of How to encode characters from Oracle to Xml?
THis is a followup to my previous question Font-dependent control positioning. It's an attempt
Followup question from this one: Swing font names do not match? (Making a font
I have a followup question to this question . I'm writing a web service
this is a followup question to Remove default apps from Django-admin I am trying
This is a followup question from a previous question, which is hopefully a little
This is a followup question to my question , that was left unanswered EDIT
This is a followup to my previous question. Parsing file names from a character
This is a followup question to one I previously asked: start-program-if-not-already-running-in-java I didn't get
This is a followup question to my other widget-related question . I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.