Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
917 views
in Technique[技术] by (71.8m points)

linux - Why characters received in serial connection only after pressing enter?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...