Bash 从 ttyUSB0 读取并发送到 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4942502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:25:24  来源:igfitidea点击:

Bash read from ttyUSB0 and send to URL

bashserial-portwget

提问by afro360

I am a bash novice and I am struggling with putting it all together.

我是一个 bash 新手,我正在努力将它们放在一起。

What I am trying to do is:

我想做的是:

1) Set Port (stty)
2) Read from dev/ttyUSB0 - data should look like 000118110000101 (cat or Gawk?)
3) Set read data into a variable eg DATA and create a URL eg http://domain.com/get_data.php?data=$DATA
4) load the URL with wget?
5) Wait for more data from ttyUSB0 (polling or loop?)

1) 设置端口 (stty)
2) 从 dev/ttyUSB0 读取 - 数据应该看起来像 000118110000101(cat 或 Gawk?)
3)将读取的数据设置到一个变量中,例如 DATA 并创建一个 URL,例如http://domain.com/get_data .php?data=$DATA
4) 用 wget 加载 URL?
5) 等待来自 ttyUSB0 的更多数据(轮询或循环?)

I have tried the php DIO extention that does work but is not reliable because it stops/starts for some reason.

我已经尝试过确实有效但不可靠的 php DIO 扩展,因为它由于某种原因停止/启动。

ANY suggestions would be much appreciated, I will be very great-full if anyone could advise the best way to do this

任何建议将不胜感激,如果有人可以建议最好的方法来做到这一点,我将非常满意

Thanks

谢谢

Brent

布伦特

回答by afro360

This is what I used.

这是我用的。

#Set permisions
sudo chmod o+rwx /dev/ttyUSB0


#!/bin/bash

# Port setting
stty -F /dev/ttyUSB0 cs7 cstopb -ixon raw speed 1200

# Loop
while [ 1 ]; 
do
    echo 'LOADING...'
    READ=`dd if=/dev/ttyUSB0 count=22 | sed 's/ //g'`
    echo $READ
    wget http://localhost/BASHtest/test.php?signal=$READ
    echo '[PRESS Ctrl + C TO EXIT]'
done

回答by afro360

For the first step i would advise to read to a file and then use od to get an octal (there's no binary as far as i can see) representation, because standard awk doesn't cope with NULs (i think gawk too). So after you get the bytes, you pipe it through sed script to change octals to binaries, grab the output with $()(or apostrophs) and make an URL, which you feed to wget.

对于第一步,我建议读取文件,然后使用 od 获得八进制(据我所知没有二进制)表示,因为标准 awk 不能处理 NUL(我也认为 gawk)。因此,在获得字节后,您可以通过 sed 脚本将其通过管道将八进制更改为二进制文件,使用$()(或撇号)获取输出并创建一个 URL,然后将其提供给wget.

The only problem i can see is blocked/nonblocked read from usb. Please report if there will be one.

我能看到的唯一问题是从 USB 读取阻塞/非阻塞。有的话请汇报。