How to check if there's something to read.

This is a discussion on How to check if there's something to read. within the C Programming forums, part of the General Programming Boards category; Hello Everyone, I am a complete newbie to C, and I have been trying to do some serial programming. I ...

  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
    cwr
    cwr is offline
    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-06-2010, 12:42 AM
  2. fscanf function won't read my float value...?
    By qpsnhalfs in forum C Programming
    Replies: 8
    Last Post: 07-08-2009, 12:01 AM
  3. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 03:06 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21