LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-01-2005, 11:36 AM   #1
RedDrake
LQ Newbie
 
Registered: Sep 2005
Location: Ljubljana / Slovenia
Distribution: Debian
Posts: 2

Rep: Reputation: 0
How to probe if a serial port has a valid connection?


To cut to the point.
I wrote a serial port testing program, which tests ports using a hardware loopback.

The problem I bumped into was that if the device "existed" in the /dev dir, but nothing was connected on it, I could open and write to the port, but trying to read or close it, would hang up the process. I resolved the read part by by doing a clone on the read function, and then terminating the child process after some delay. But that still leaves the close function, which still hangs up the program.

Now I would like to know if there is a way, either by using a termios flag, or something similar, that can be coded into a C program, that can probe to see if the port has a connection.

If needed I can provide the code (it's GPL ofc ^^).
 
Old 09-01-2005, 06:27 PM   #2
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Rep: Reputation: 15
Well, this is something that I happen to know a little bit about.

Opening and closing a serial port with no device connected should not hang. The open()/close() functions do not communicate with the connected device and are always unaware of the presence of something on the other end (the "other end" being the same machine in the case of a loopback adapter).

To avoid read() hangs when no data is being received, you can do something like the following:

1) After opening the serial port device, set it to non-blocking mode. This means that read() will return immediately (with the error EAGAIN) if no bytes were waiting in the buffer to be read, and write() will return immediately (with EAGAIN) if the device was busy and the bytes could not be sent:

Code:
int open_serialport (const char *device) {

  int fd, fl;

  /* open serial port device */
  fd = open(device, O_RDWR | O_NOCTTY);
  if (fd == -1)
    return -1;

  /* get current flags */
  fl = fcntl(fd, F_GETFL);
  if (fl == -1) {
    close(fd);
    return -1;
  }

  /* turn on the nonblocking bit and set the flags */
  if (fcntl(fd, F_SETFL, fl | O_NONBLOCK) == -1) {
    close(fd);
    return -1;
  }

  return fd;

}
The O_NOCTTY flag prevents the serial port device from becoming the process's terminal device if it doesn't have one already. I've actually never had a problem not specifying this flag but I could conceivable see something weird happen because of it (not related to your problem, though).

Now, I know, you could compress the above code into just a few lines, but I'm trying to keep it readable. You'd also want to add any termios configuration code to that function as well, of course (like setting the baud rate, flow control, linefeed translations, etc). Also don't forget to set CLOCAL in your tcattr c_cflags (see termios man page) if you aren't using a modem; that will cause the drivers to ignore all the modem control lines (although that one might be set by default, can't remember).

Setting nonblocking mode won't solve the problem completely by itself. Although read() won't hang, it is very possible that it will return before the number of bytes you asked for was read. Using select() combined with repeated read() calls will let you control the timeout time and also make sure you receive all the bytes you asked for, while using minimal system resources.

2) Now when you want to read() from the device, you may use select() to wait for data with a timeout, and return if no data was waiting to be read after some amount of time (thus preventing the infinite wait for data to come in). Yes, it adds some extra code, but you do what you have to do (and it's a lot less hacky and way more efficient than forking off child processes to read data):

Code:
int read_serialport (int fd, char *buf, int bytes, int timeout) {

  char *dest = buf;
  int remaining = bytes, got;
  struct timeval tv;
  fd_set fdr;

  /* while there are bytes left to read... */
  while (remaining) {

    /* reinit timeval structure each time through loop. */
    tv.tv_sec = timeout;
    tv.tv_usec = 0;

    /* wait for data to come in using select. */
    FD_ZERO(&fdr);
    FD_SET(fd, &fdr);
    if (select(fd + 1, &fdr, NULL, NULL, &tv) != 1) {
      /* this could happen on an i/o error too but most likely you timed out waiting
       * for incoming data. the way to tell the difference is select() returns -1 on error
       * but 0 if the time out expired. */
      fprintf(stderr, "timed out!\n");
      return -1;
    }

    /* read a response, we know there's data waiting. */
    got = read(fd, dest, remaining);

    /* handle errors. retry on EAGAIN, fail on anything else, ignore if no bytes read. */
    if (got == -1) {
      if (errno == EAGAIN)
        continue;
      else {
        perror("i/o error");
        return -1;
      }
    } else if (got > 0) {
      /* figure out how many bytes are left and increment dest pointer through buffer */
      dest += got;
      remaining -= got;
    }

  }

   /* return number of bytes read, might be 0 if 0 requested. */
   return bytes;

}
And you can add stuff to that, like maximum retry counts and more accurate error reporting. You should note that the above code will loop infinitely if read() keeps returning 0 and select() returns immediately every time (adding max retry counts will stop that). That case shouldn't happen but you never can be too safe, some serial port drivers are less than reliable when it comes to that kind of stuff.

The write() logic is similar to the above. However, write() is probably not going to timeout, so you don't necessarily need to use select() to wait for the descriptor to be available for writing; you can just get away with writing bytes and incrementing through the source buffer as above.

With regards to "can I check to see if there's a connection available"... not directly, I don't think. If your device sets one of the other lines high when it is connected (one of those modem lines, like CTS and crap, I don't know much about those) then you should be able to detect if the line is high or not; but chances are your device isn't like that.

However, assuming that your connected device gives some responses to the data you send to it, checking for it's presence is as simple as sending some data to it and seeing if you receive what you expect back from it without timing out. For example, with your loop back connector, all you have to do is send a few bytes. If you don't receive them back, then the connector was not attached. For something like say, a modem, or some other device, you could send some harmless command (like a status command or something) and make sure you get a valid response in a reasonable amount of time. More of a "You there? Yeah I'm here" approach than a "I know you're out there" approach.

Hope that helps,
Jason

Last edited by JCipriani; 09-01-2005 at 06:43 PM.
 
Old 09-01-2005, 07:05 PM   #3
RedDrake
LQ Newbie
 
Registered: Sep 2005
Location: Ljubljana / Slovenia
Distribution: Debian
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks, I did use NONBLOCKING, but must have misunderstood the part about select in the howto I have read.
This came in really handy
Keep up the good work.

Regards,
Borut
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux serial port to router console port connection? frankie_fix Linux - General 3 02-26-2007 09:32 PM
serial port network connection eantoranz Linux - Networking 2 10-31-2005 08:05 AM
setting up com port for serial connection to monitor network switch help murf562 Linux - Hardware 1 10-30-2004 09:46 AM
Detecting connection break on serial port listener on linux neelc20 Programming 6 12-04-2003 01:08 AM
Detecting connection break on serial port listener on linux neelc20 Linux - Newbie 3 11-29-2003 03:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration