0

In C/C++

How can I read from serial port, without removing the info I have already read from the buffer?

I want to do something like the Peek function of arduino (http://arduino.cc/en/Serial/Peek) in a linux machine.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Anjz
  • 197
  • 1
  • 1
  • 11
  • 1
    Maybe [this question](http://stackoverflow.com/questions/5900216/get-the-count-of-bytes-waitting-on-a-serial-port-before-reading-linux) can help. – Eitan T Aug 01 '12 at 12:03
  • 1
    Usually you would do this with your own buffer, not the one provided by the OS and used in kernel mode. – Ben Voigt Aug 01 '12 at 12:53
  • How are you reading from the serial port, now? A code snippet of the implemented buffer reading mechanism, could help you get more useful answers. – askmish Aug 01 '12 at 13:18
  • it is already answered here: [read from serail without remove](http://stackoverflow.com/questions/5478160/is-there-a-way-to-peek-a-value-from-a-tty-device) – elhadi dp ıpɐɥןǝ Aug 02 '12 at 10:42

1 Answers1

1

The serial port is accessed as though it is a file using a file descriptor so a fgetc ungetc method should work.

Community
  • 1
  • 1
DrYap
  • 6,525
  • 2
  • 31
  • 54
  • 2
    Thank you! I also needed to use fdopen() in order to obtain a pointer to a FILE struct from the file descriptor (an integer). – Anjz Aug 06 '12 at 17:19