简体   繁体   English

Linux读取表单串行端口-fork解决方法

[英]Linux read form serial port - fork workaround

I'm trying to send data via serial port and receive the response. 我正在尝试通过串行端口发送数据并接收响应。 Currently I'm looping the interface physically to emulate the device. 目前,我正在物理上循环接口以模拟设备。 first I do: 首先,我要做:

# stty -F /dev/ttyUSB0 -echo

if I try to read after write, it doesn't print anything: 如果我尝试在写入后阅读,它不会打印任何内容:

# echo "foo" > /dev/ttyUSB0; cat /dev/ttyUSB0

I have to fork the read before write to get the response. 我必须在写入之前先分叉读取,以获取响应。

# cat /dev/ttyUSB0 &
[1] 7881

# echo -ne "foo\xA\0"
foo

Is there a work around? 有没有解决的办法?

Thank you! 谢谢!

Assuming that you've got the correct read and write privileges for /dev/ttyUSB0. 假设您具有对/ dev / ttyUSB0的正确读写权限。 You simply need to have the echo running on one terminal: 您只需要在一个终端上运行echo

echo -ne 'foo\xA\0' >  /dev/ttyUSB0 

and the cat running on another terminal (use the -v option to make all characters readable): 和在另一终端上运行的cat (使用-v选项使所有字符可读):

cat -v /dev/ttyUSB0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM