bash 在bash中将十六进制字节发送到串行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22511225/
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
sending hex bytes to serial in bash
提问by Pavan K
I am able to send hec bytes to a serial port using
我能够使用发送 hec 字节到串行端口
stty -F /dev/ttyUSB0 speed 115200 cs8 -cstopb -parenb -echo
echo -en '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' > /dev/ttyUSB0
But when I try to do this in a loop reading test from a file, it doesnt want to work
但是当我尝试从文件中循环读取测试时,它不想工作
#!/bin/bash
stty -F /dev/ttyUSB0 speed 115200 cs8 -cstopb -parenb -echo
while read line
do
name=$line
echo -en $name | tr -d ' ' > /dev/ttyUSB0
sleep 0.04
done <
I call the script like this
我这样称呼脚本
./sendData.sh file.txt
file.txt has some simple content like this
file.txt 有一些像这样的简单内容
Try 1
尝试 1
\ xFF\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00\ x00
Try 2
尝试 2
\xFF\xF2\x00\xFF\xF2\x00\xFF\xF2\x00\xFF\xF2\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF
Could someone point to me what is missing.
有人可以指出我缺少什么。
回答by that other guy
The problem is that read
interprets escape sequences by default, effectively removing your backslashes. Make your file contain e.g. \x01\x02\x03
and use read -r
:
问题是read
默认情况下解释转义序列,有效地删除了反斜杠。使您的文件包含 eg\x01\x02\x03
并使用read -r
:
while read -r line
do
echo -en "$line" > /dev/ttyUSB0
done < ""