How can I read the terminal input on a tty device before a newline arrives?

一世执手 提交于 2019-12-11 01:14:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!