Linux串口监听器和解释器? [英] Linux serial port listener and interpreter?
问题描述
我正在为一个项目使用串行设备,而我试图在 PC 端完成的是侦听串行设备发送的命令,解释查询,根据查询运行一些代码,然后传输返回结果.
I'm using a serial device for a project, and what I'm trying to accomplish PC side, is listening for a command sent by the serial device, interpreting the query, running some code depending on the query, and transmitting back the result.
老实说,我尝试使用 PHP 作为侦听器,它可以工作,不幸的是,使脚本充当接收器所需的无限循环将 CPU 加载到 25%.所以这并不是最好的选择.
To be honest I tried out using PHP as the listener, and it works, unfortunately the infinite loop required to make the script act as a receiver, loads the CPU to 25%. So it's not really the best option.
我现在正在使用 cygwin,我想使用 linux 原生命令创建一个 bash 脚本.
I'm using cygwin right now, I'd like to create a bash script using linux native commands.
我可以通过以下方式接收数据:
I can receive data by using:
cat /dev/ttyS2
并发送响应:
echo "command to send" > /dev/ttyS2
我的问题是,如何使自动侦听器能够接收和发送数据?我遇到的主要问题实际上是如何在收到信息后停止 cat/dev/ttyS2 命令,将其放入变量中,然后我可以与开关或一系列 if 进行比较否则然后阻塞.然后发回响应并重新开始循环?
My question is, how do I make an automated listener to be able to receive and send data? The main issue I have, is actually how do I stop the cat /dev/ttyS2 command once information was received, put it into a variable which then I could compare with a switch, or a series of if else then blocks. Afterwards send back a response and start the cycle all over again?
谢谢
推荐答案
这不是你要找的吗?
while read -r line < /dev/ttyS2; do
# $line is the line read, do something with it
# which produces $result
echo $result > /dev/ttyS2
done
这篇关于Linux串口监听器和解释器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!