bash 从串口读取并以十六进制存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32822719/
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-18 13:39:03  来源:igfitidea点击:

Read from serial port and store in hexadecimal

bashshellserial-porthexdumpstty

提问by Latifa

I have a vhf radio which sent a status message continuosly through the serial port, and I need the messages that I got to be stored as hex data in a text file

我有一个 vhf 无线电,它通过串行端口连续发送状态消息,我需要将消息作为十六进制数据存储在文本文件中

I tried hexdump command as shown below, and the data that I've got from vhf radio is correct, but the problem with this script that when I execute it, it does not end until I press ctrl-c

我尝试了如下所示的 hexdump 命令,我从 vhf radio 获得的数据是正确的,但是这个脚本的问题是,当我执行它时,它直到我按下 ctrl-c 才结束

d -A n -t x1 -w128 /dev/ttyS0 > file.txt

so I've tried another command which is read command as follow:

所以我尝试了另一个命令,它是读取命令,如下所示:

COUNTER=0
while [ $COUNTER -lt 10 ]; do
read -r -t1 -N128 DATA < /dev/ttyS0 
echo $DATA >> file1.txt
od -A n -t x1 -w128 file1.txt >> file2.txt
let COUNTER=COUNTER+1
done

but the data stored in file2.txt is not correct.

但是file2.txt中存储的数据不正确。

the message that I got from the radio is not in a format that I could interprete it as per the radio prtocol document. so when I said that the data is not correct I mean that the message is could not be interpreted (it received randomly)

我从收音机收到的消息的格式不是我可以按照收音机协议文档来解释的。所以当我说数据不正确时,我的意思是无法解释消息(它是随机接收的)

note that I've setted the serial port before executing both scripts as follow:

请注意,我在执行两个脚本之前设置了串行端口,如下所示:

stty -g /dev/ttyS0 raw
stty -F /dev/ttyS0 9600

so, please help me to figure this out. or gave me aother way to read from serial port.

所以,请帮我解决这个问题。或者给了我另一种从串口读取的方法。

Regards,

问候,

回答by Latifa

The Problem is solved :)

问题已经解决了 :)

I tried to use hexdump command by setting -N to KB and it works successfuly

我尝试通过将 -N 设置为 KB 来使用 hexdump 命令并且它成功运行

it reads from serial port until 1000 bytes and it stops

它从串行端口读取直到 1000 字节,然后停止

od -A n -N KB -t x1 -w128 /dev/ttyS0 > /tmp/filename.txt

so, thank you guys for your cooperation. I really appriciated.

所以,谢谢你们的合作。我真的很感激。