- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个问题。我有 2 个硬件(类似于 pi),我正在尝试测试它们之间通过串行电缆进行的通信。两者都是基于 Linux 的,但工具有限。我写了一个脚本来发送和接收文件。当我发送一个包含一些文本的 txt 文件时,一切正常。当我尝试发送一个二进制文件时,它的数据不一样,有时我得到更大的文件,有时更小,有时只是一些字节被改变。我想了好几个小时为什么会这样,我将设备设置为 raw
模式(对于二进制文件)...
这是我写的脚本:
#!/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
我的想法告诉我,在解释某些字符时存在问题,并且可能会通过更改上面的某些选项来解决。我尝试了几个,但我无法让它发挥作用。如果有人看到我没有看到的东西,我将不胜感激。
问候
编辑:发现问题但未解决。 Read 和 cat 不喜欢字符 NULL
和 \n
。我如何读取这 2 个字符?
最佳答案
在读取前添加IFS=
,允许读取空格、制表符
while IFS= read -t 5 -r -n 1 c; do
编辑:'\n' 和 '\0' 仍然无法读取。
比较以下两个输出
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 = '' ]]
;所以在这种情况下它是 '\0' 字符,例如使用 printf '\0' 来写它
以下命令证明可以复制所有字符
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
关于linux - 串行数据损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44304933/
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我目前正在尝试制作一个非常简单的应用程序,它会根据一天中的时间问候。我的代码是: open System let read() = Console.Read() let readLine() = Co
我已经运行Elasticsearch服务很长时间了,但是突然遇到了以下情况 由以下原因导致:org.elasticsearch.index.translog.TranslogCorruptedExce
我对执行以下操作的 php 重定向脚本有一个奇怪的问题: 在用户的浏览器中植入 Cookie,或者读取现有 Cookie(如果有)。 将用户重定向到另一个网址(重定向的网址是原始网址中的参数,例如 h
我正在使用 iText 7.0.0(Java 风格),似乎表格单元格 HorizontalAlignment 被忽略,因为 CENTER 和 RIGHT 都不起作用。你能重现这个吗? see th
简而言之: 我有一个可以从多个线程访问的计数器变量。尽管我已经实现了多线程读/写保护,但该变量似乎仍然以不一致的方式同时写入,导致计数器结果不正确。 深入杂草: 我使用的“for 循环”会在后台触发大
我有一个 REST 项目,在访问控制服务类中保存用户的ArrayList。一切都工作正常,直到 REST Web 服务突然抛出 java.util.NoSuchElementException。单步查
已关闭。此问题不符合Stack Overflow guidelines 。它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
当我刷新页面时,我无法显示 voteUp/Down,因为如果我执行 voteUp/Down(+1 或 -1) 并刷新页面,这会再次返回 voteUp/Down (0)。过去我使用 JSON,但社区推荐
我正在为离散时间 CPU 调度模拟器编写代码。它只是生成流程并相应地安排它们。我目前正在实现 FCFS 计划。我理解离散时间模拟器的本质,但我在用 C++ 实现时遇到了麻烦。 问题出现在handleN
尝试使用 yum 部署包时出现错误: 2016-07-07 14:14:31,296 - ERROR - error: rpmdb: BDB0113 Thread/process 6723/1
我有一个简单的同步队列 template class SynchronisedQueue { public: void Enqueue(const T& d
我正在使用 hadoop 0.20.append 和 hbase 0.90.0。我将少量数据上传到 Hbase,然后出于评估目的杀死了 HMaster 和 Namenode。在此之后,我向 Hbase
我使用 symfony 框架 1.4 创建了一个网站。我正在使用 sfguard 进行身份验证。 现在,这在 WAMP (windows) 上运行良好。我可以在不同的浏览器上登录多个帐户并使用该网站。
目前我已经实现了 HashMap private static Map cached = new HashMap(); 和 Item 是一个具有属性的对象 Date expireTime 和 byte
我试图将 2 个不同的 WPF 控件绑定(bind)到 ViewModel 中的同一属性,即 CheckBox.IsChecked 和 Expander.IsExpanded。我想要实现的行为是让 C
我希望这是一个简单的问题,但我没有找到答案。 我想让 build.gradle 文件通过替换某些变量来设置我的 Spring Boot 应用程序中的版本。这与广告一样有效: def tokens =
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
这个问题在这里已经有了答案: In a fragment shader, why can't I use a flat input integer to index a uniform array o
我已经下载了 OSM 世界地图。解析时出现异常: osm bound changeset (...) changeset Exception in thread "main" org.xml.sax.
我是一名优秀的程序员,十分优秀!