Thread: read()ing larger chucks

  1. #1
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    read()ing larger chucks

    Is there any way to read larger chunks with read()? Normally, it will return any number of bytes it pleases less than or equal to the number provided. Currently, when reading from a serial port at 38400 baud, that means I'm only reading five or fewer bytes at a time.

    I'd prefer to read() less often. I've checked the read and fcntl man pages, but I couldn't find anything to do this.

    I know it's possible when recv()ing from a socket, but I don't have a socket. Or is there some way to open up a serial port as a socket?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    What OS are you on?

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Linux kernel 2.6.8.1
    Mandrake 10.1
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    Im not sure how it would work with a serial port, but most likely, its a file descriptor. With that you can call stat (see man stat page) and then get a struct, which contains the size of the data which is readable. From there you can read in bytes all at once, or a portion of the size at a time.

    I hope thats what you were getting at. A good book for Unix I/O is Advanced UNIX Programming. Lots of useful system calls in there.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I do have a file descriptor for the serial port, but according to stat(), that file has size zero.

    Any other suggestions?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    I would try posting in a linux forum if that was the result. Cause your dealing with system calls here.

    I would also check if the file descriptor is valid.

    EDIT........

    Actually try using fsta....

    Code:
    #include <sys/stat.h>
    
    int fsta( int fd, struct stat *buf);
    first allocate some memory for a struct stat, then call this function with the file descriptor. Then with the struct use buf->st_size to get the size in bytes.

    EDIT AGAIN: Im an idiot, I edited after you already replied.
    Last edited by Lateralus; 08-05-2005 at 09:14 AM.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Eureka!
    Code:
    struct termios newtio;
    newtio.c_cc[VMIN]=7;	// Minimum number of characters for non-canonical read.
    newtio.c_cc[VTIME]=2;	// Timeout in deciseconds for non-canonical read.
    I found that out in the man page for termios(3).
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Resetting a ifstream object after reading the whole file
    By Zeeshan in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2008, 08:03 AM
  3. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  4. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM