Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to do a blocking read from a serial device?




On Jul 10, 2006, at 10:34 AM, Sean McBride wrote:
The read() man page says "The system guarantees to read the number of
bytes requested if the descriptor references a normal file that has that
many bytes left before the end-of-file, but in no other case." Indeed
with my serial device, if I say to read 15 bytes it sometimes returns
after reading 10 bytes.


Is there anyway to have read() block until the requested length is
ready? Is my only other choice some kind of loop that calls read() and
usleep() over and over? If so, how do I choose a sleep duration?

Read(2) will block if there is no data so there's no need to sleep. Using select(2) is one way to go about it but with only one file descriptor/socket, it seems pointless to me.


unsigned char buffer[15];
size_t len = 0;
int fd = /* something to get your descriptor */

do {
	ssize_t ret = read( fd, &buffer[0] + len, sizeof(buffer) - len );

	if( ret == -1 )
		break;
	len += ret;
} while( len < sizeof(buffer) );

Now you've read len bytes of data.

--
Steve Checkoway



Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden

References: 
 >How to do a blocking read from a serial device? (From: "Sean McBride" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.