Thread: How to check if there's something to read.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    How to check if there's something to read.

    Hello Everyone,

    I am a complete newbie to C, and I have been trying to do some serial programming.

    I opened a serial port in linux using:
    int fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK );

    I understand that to read the port I need to do this:
    read(fd, buffer, num_char);

    However my question is, is there a way to know if there is something to read from "fd" before calling read()? I tried pread(), but it just returns -1 on the serial port (maybe it is not seekable?).

    Thanks in advance,

    Andy


    (Please bear with me, I probably don't know what I am talking about...)

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    A common way is to use either the select() or poll() functions.

    You could also set the fd to nonblocking using fcntl, but this is probably a bad idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. fscanf function won't read my float value...?
    By qpsnhalfs in forum C Programming
    Replies: 8
    Last Post: 07-07-2009, 11:01 PM
  3. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 04:01 PM