我有问题。我有2个硬件(类似于pi),我试图通过它们之间的串行电缆测试通信。两者都是基于Linux的,但是工具有限。我写了一个脚本来发送和接收文件。当我发送带有一些文本的txt文件时,一切正常。当我尝试发送二进制文件时,数据不一样,有时我会得到更大的文件,有时更小,有时只会改变一些字节。我想知道为什么会发生这种情况,我将设备设置为raw
模式(对于二进制文件)......
这是我写的scrpit:
#!/bin/bash
FILE=$2
send()
{
if [ ! -f $FILE ]; then
echo "File $2 doesn not exist, please introduce a valid file"
fi
content=`cat "$FILE"` #Dump the file content into variable
echo -E "$content" > /dev/ttyO5 #send the whole content to the other device
}
receive()
{
if [ -f $FILE ]; then
echo "The file already exists. Do you want to overwrite it? (y/n
read opc
if [ "$opc" == "n" ]; then
exit 1
fi
rm "$FILE"
fi
while read -t 5 -r -n 1 c; do # read char by char -r to avoid backslashes to be scaped
echo -E -n "$c" >> $FILE # append char on file -n(to avoid creation of new lines and -E to avoid interpretation of backslashes.
done < /dev/ttyO5
}
case $1 in
's')
send
;;
'r')
receive
;;
*)
echo "Usage $0 [s | r] [FILE]"
;;
esac
要将设备置于raw
模式,我使用stty -F /dev/ttyO5 raw
这是设备的选项:
speed 9600 baud;stty: /dev/ttyO5
line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon
-ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
ff0
-isig -icanon iexten -echo -echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
我的想法告诉我,在解释某些字符时会出现问题,并且可能会修改上面的某些选项。我尝试了几个,但我不能让它工作。如果有人看到我不喜欢的东西,我会非常感激。
此致
编辑:发现问题但未解决。阅读并且cat不喜欢字符NULL
和\n
。我怎么能读出那两个字符?
答案 0 :(得分:1)
在读取之前添加IFS=
以允许读取空格,制表符
while IFS= read -t 5 -r -n 1 c; do
编辑:&#39; \ n&#39;和&#39; \ 0&#39;仍然无法阅读。
比较以下两个输出
for i in {{0..9},a,b,c,d,e,f}{{0..9},a,b,c,d,e,f};do printf '\x'"$i"; done | od -c
for i in {{0..9},a,b,c,d,e,f}{{0..9},a,b,c,d,e,f};do printf '\x'"$i"; done | { while IFS= read -r -n1 c; do echo -E -n "$c";done;} | od -c
编辑:要阅读换行符,请添加选项-d ''
while IFS= read -t 5 -r -n 1 -d '' c; do
如果[[ $c = '' ]]
;所以它是&#39; \ 0&#39;在这种情况下,使用printf&#39; \ 0&#39;例如写它
以下命令证明可以复制所有字符
for i in {{0..9},a,b,c,d,e,f}{{0..9},a,b,c,d,e,f};do printf '\x'"$i"; done | { while IFS= read -r -n1 -d '' c; do if [[ $c = '' ]]; then printf '\0'; else echo -E -n "$c";fi;done;} | od -c
编辑:
考虑到性能read
很慢,请考虑使用另一个unix工具:perl
perl -e '
# $/ end of line of input, special variable
# read one byte at time
$/=\1;
my $outfilename=shift;
open $outfile,">>",$outfilename or die $!;
while (<>) {
# do something on output (for example print ascii number)
print ord($_),"\n";
# write to out file
print $outfile "$_";
}
' "$FILE" < /dev/ttyO5