0

have a program that does an open() on /dev/ttyACM0.

I know how to get bytes available using ioctl(), but I want to know what is the next available byte's value, without grabbing it. (A peek).

Is by using the recv() call? If so, how? Somehow I have missed this point in my googling.

I was trying to use:

FILE* f = fdopen(_fd,"r+");
int byte = getc(f);
ungetc(byte,f);

but I get a resource is temporarily unavailable, presumably because of the pre-existing open() call.

Any suggestions? thank you.

  • does it have to be a system call or could you implement a small put-back buffer in your own application? – thrig May 29 '18 at 18:51
  • 3
    fdopen() won't fail because the file has already been open()ed -- on the contrary, it needs that! But I can't tell where the error would come, and you didn't show the whole code. – ilkkachu May 29 '18 at 20:02
  • Guess: IIRC the stdio library implements ungetc by using an internal buffer in the FILE structure. This means you can't unget a character twice. This also won't work when the file is opened twice, because each FILE has its own buffer, while they should be shared. So I assume for either reason you get an error. Possible solution: Implement your own wrapper that allows peeking, allowing concurrent access as required. Another possible solution: Open it only once, don't use concurrent access. – dirkt May 30 '18 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.