Results 1 to 2 of 2
Hello,
I have one application that reads and writes from the serial port.
Sometimes the function read():
"read(fd,buffer,numbytes)" returns a number < numbytes. (Because there are not more data in ...
- 10-21-2008 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 4
to make "read()" no return until read all the bytes requiered
Hello,
I have one application that reads and writes from the serial port.
Sometimes the function read():
"read(fd,buffer,numbytes)" returns a number < numbytes. (Because there are not more data in the serial port buffer)
I mean. if numbytes is 32, sometimes read returns 12, and in the next read returns the 20 remaining.
I want to avoid that and I want that "read()" does not return until reads all the numbytes. I want that "read()" waits until read all the bytes in the third parameter.
Some ideas?
I have tried to "open()" with different configurations but all the time "read()" does not wait until reads everything.
Also I have thought about to change the mode from the default one "byte-stream" to " message-nondiscard" but when I try to use the functions:
ioctl(fd,I_SRDOPT,param)
ioctl(fd,I_GRDOPT,param)
both functions returns -1 with errno = EINVAL and I do not know if it is bercause I set badly the parameters or is because "Stream is linked under a multiplexing driver" (REALLY I DO NOT WHAT THIS MEANS).
Here I write how I use ioctl:
int a;
ioctl(fd,I_GRDOPT,&a); -> //fails -1 and errno = EINVAL
ioctlfd,I_SRDOPT,RMSGN) > //fails -1 and errno = EINVAL.
So if I am using correctly both functions, could somebody explain me the error:
"[EINVAL] fd refers to a Stream that is linked under a multiplexing driver. If a Stream is linked under a multiplexing driver, all ioctl(2s) commands other than I_UNLINK or I_PUNLINK will return [EINVAL]. "?
How can I do that "read()" waits until all the bytes are read as requiered?
Thank you.
- 10-21-2008 #2
Certain ioctl() calls are used for certain kinds of devices. It's possible to instruct the C compiler to use inappropriate ioctl() calls for serial devices, for example. You can usually expect an EINVAL error in such a situation.
So don't try to do that. It just won't work. If you want a read() function which doesn't return until the required number of bytes has been returned, do that by writing a "wrapper function". A wrapper function is called that because (in this case) it is wrapped around the actual read() call. Call your function my_read() if you like.
This function that you write should take the same paramaters that the real read() function does. It should sit in a loop until all the required bytes have been read. In the example that you give where 12 bytes are returned from the first read() call and 20 bytes are returned from the second read() call, your wrapper function will notice after the first time through the loop that only 12 bytes have been returned, so the second time your code calls read(), it will ask for only the correct number of bytes to complete your request, and will ask them to be placed not at the beginning of your buffer, but 12 bytes after the beginning.
This isn't as hard as it sounds. It is the ordinary approach in code which uses serial devices and wants a fixed number of bytes.
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote