问题
I am reading a serial device using embedded linux (angstrom) using open(), tcget/setattr(), read(). I can read and write data just fine. However, the device I am reading displays a prompt that is not terminated with a newline. This means I can't read the prompt until after I cause the device to send some other output that has a newline at the end. Basically, I know the prompt string ("COM3>") is sitting in the tty buffer waiting for the device to send a newline. read() won't access the "COM3>" prompt string until the newline arrives. Is there any way to change the force read() to get the data in the buffer before the newline arrives?
Thanks
回答1:
In canonical mode, which is the default, input is line-buffered.
What you probably want is to set the terminal into "raw" mode with cfmakeraw(), which will (among other things) disable canonical mode.
See the tcsetattr() man page for more information.
回答2:
One of the benefits of C is that there's a hundred ways to do anything.
I wouldn't use ioctl if it were I.
A simple cMyChar = getc(STDIN) would do nicely.
来源:https://stackoverflow.com/questions/9222231/how-can-i-read-the-terminal-input-on-a-tty-device-before-a-newline-arrives