Why characters received in serial connection only after pressing enter?

佐手、 提交于 2019-12-17 20:52:27

问题


I have a simple PC to board connection using serial (9600, no parity, 8 bits, no hw flow) I opened simple terminal *with teraterm) in PC and enter keys in teraterm and in board, I just do

 cat /dev/ttyO5

I see the pressed characters in scope, but I see the characters in the board console, only after pressing "enter" in teraterm (as if they are stored in some FIFO in Linux driver which only enter triggers out)

  1. why are the characters received in Linux driver only when pressing enter key ?
  2. Is there some way to receive the characters without pressing the enter key ? (we use some protocol of ascii so it does not make sense to send this as dummy)

Thanks for advise, Ran


回答1:


but I see the characters in the board console, only after pressing "enter" in teraterm

The behavior you describe is characteristic of canonical read (aka reading lines).
The behavior you seem to want is called non-canonical read (aka raw read or binary read).

  1. why are the characters received in Linux driver only when pressing enter key ?

No, the Linux serial port driver is receiving every character as it is appears on the wire.
Each character is buffered (typically in DMA-able memory), and then forwarded to a line discipline handler, which is also buffering the received data.
A canonical read() syscall by the userland program is blocked until the line discipline handler detects a line termination character.

  1. Is there some way to receive the characters without pressing the enter key ?

Yes, before issuing the cat command, configure the serial port to non-canonical mode:

stty -F /dev/tty05 raw

or more likely the correct device node is

stty -F /dev/ttyO5 raw

Or use the termios interface to configure the serial port to non-canonical mode in a userspace program on the board. Sample code is here.

Documentation on how to properly program a serial port are Serial Programming Guide for POSIX Operating Systems and Setting Terminal Modes Properly.



来源:https://stackoverflow.com/questions/27214759/why-characters-received-in-serial-connection-only-after-pressing-enter

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