为什么只有在按回车后才能在串行连接中收到字符? [英] Why characters received in serial connection only after pressing enter?

查看:9
本文介绍了为什么只有在按回车后才能在串行连接中收到字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 PC 到板使用串行连接(9600,无奇偶校验,8 位,无硬件流)我在 PC 中打开了简单的终端 *with teraterm) 并在 teraterm 和板上输入密钥,我只是这样做

 cat/dev/ttyO5

我在范围内看到按下的字符,但我在板控制台中看到字符,只有在 teraterm 中按输入"后(好像它们存储在 Linux 驱动程序中的某些 FIFO 中,只有输入触发)

  1. 为什么Linux驱动只有按回车键才能接收到字符?
  2. 有什么方法可以在不按回车键的情况下接收字符吗?(我们使用一些 ascii 协议,因此将其作为虚拟发送是没有意义的)

感谢您的建议,然

解决方案

但我在板控制台中看到字符,只有在按输入"后才能看到.暂时

您描述的行为是规范阅读(又名阅读行)的特征.
您似乎想要的行为称为非规范读取(也称为原始读取或二进制读取).

<块引用>

  1. 为什么Linux驱动只有按回车键才能接收到字符?

不,Linux 串行端口驱动程序正在接收网络上出现的每个字符.
每个字符都被缓冲(通常在支持 DMA 的内存中),然后转发到行规则处理程序,该处理程序也在缓冲接收到的数据.
用户态程序的规范 read() 系统调用被阻止,直到行规则处理程序检测到行终止字符.

<块引用>

  1. 有什么方法可以在不按回车键的情况下接收字符吗?

是的,在发出cat命令之前,将串口配置为非规范模式:

stty -F/dev/tty05 raw

或更可能是正确的设备节点

stty -F/dev/ttyO5 raw

或者使用termios接口在板上的用户空间程序中将串口配置为非规范模式.示例代码是 这里.

有关如何正确编程串行端口的文档是POSIX 操作系统串行编程指南正确设置终端模式.

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

解决方案

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.

这篇关于为什么只有在按回车后才能在串行连接中收到字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆