2

I'm having some trouble reading an unknown amount of bytes (raw data, may contain NULLs) from a device using c in Linux. How can I read the device for "as long as it's ready to be read"? (hopefully without reading 1 byte at a time).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
LinkOfTime
  • 77
  • 5
  • Thanks for the idea. I haven't thought of taking that approach, but if I keep getting stuck with the same problem, I'll check if it's possible to map it like you say. – LinkOfTime Jan 07 '13 at 14:43

1 Answers1

4

Well if you're using read you'll either block if there's no data, or succeed if there was data to read. If there was data to read you'll get either the amount you wanted, or an amount less if there's no more to read. Look at the return value of read to determine how much you have. You could make it non-blocking but the approach of checking reads return value holds true regardless.

Just chunk through the data in whatever size chunks you want to deal with, one character at a time, or more, if you're more aware of the kind of bursts of data you'll have.

Joe
  • 7,378
  • 4
  • 37
  • 54
  • I remember trying the read function and running into a problem with it.. but I'll give it another try. Thanks for the help :) – LinkOfTime Jan 07 '13 at 14:42
  • 1
    No problem. In general all the approaches to reading allow you to ask for more than there is there and tell you how much you eventually got. – Joe Jan 07 '13 at 14:45