-
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?
-
-
Linux kernel 2.6.8.1
Mandrake 10.1
-
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.
-
I do have a file descriptor for the serial port, but according to stat(), that file has size zero.
Any other suggestions?
-
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.
-
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).