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

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

问题描述

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

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

我看到了作用域中被按下的字符,但是只有在第三次按下"enter"(就像它们存储在Linux驱动程序中的某些FIFO中,它们仅输入会触发)之后,我才在板子控制台中看到这些字符

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. 为什么只有按Enter键才能在Linux驱动程序中接收到字符?
  2. 有什么方法可以不用按回车键来接收字符吗? (我们使用了一些ascii协议,因此将其作为虚拟对象发送是没有意义的)

感谢您的指教, 然

推荐答案

但是我只有在按下teraterm中的"enter"键之后,才能在板子控制台中看到字符

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. 为什么只有按Enter键才能在Linux驱动程序中接收到字符?

否,Linux串行端口驱动程序正在接收线路上显示的每个字符.
每个字符都被缓冲(通常在DMA可用的内存中),然后转发给行纪处理程序,该程序也缓冲接收到的数据.
userland程序会进行规范的 read()系统调用,直到线路规程处理程序检测到线路终止字符为止.

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. 有什么方法可以不用按回车键来接收字符吗?

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

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

或使用 termios 界面在主板上的用户空间程序中将串行端口配置为非规范模式.示例代码在此处.

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.

有关如何正确编程串行端口的文档为 POSIX操作系统串行编程指南

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

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

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