0
点赞
收藏
分享

微信扫一扫

【BUG】串口通信问题

项目场景:

提示:这里简述项目相关背景:
例如:项目场景:示例:通过蓝牙芯片(HC-05)与手机 APP 通信,每隔 5s 传输一批传感器数据(不是很大)

从传感器中读出字节序列,然后每两个字节为一组,进行数据转换,一个简单的串口通信程序。

问题描述:

提示:这里描述项目中遇到的问题:
例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据
APP 中接收数据代码:

@Override
public void run() {
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(READ_DATA, bytes, -1, buffer).sendToTarget();
}
  1. 在x86架构和Arm架构下返回的数据不同。
  2. 从缓冲区中读取数据时,会发生字节错位现象,本应该为一组的两个字节数据各成一组。
  3. 每次去缓冲区取数据有时候会不满。

原因分析:

提示:这里填写问题的分析:
例如:Handler 发送消息有两种方式,分别是 Handler.obtainMessage()和 Handler.sendMessage(),其中 obtainMessage 方式当数据量过大时,由于 MessageQuene 大小也有限,所以当 message 处理不及时时,会造成先传的数据被覆盖,进而导致数据丢失。

  1. 可能是大小端数据流的问题。
  2. 不清楚。
  3. 不清楚,采样率是足够快的。

解决方案:

大小端问题

提示:这里填写该问题的具体解决方案:
例如:新建一个 Message 对象,并将读取到的数据存入 Message,然后 mHandler.obtainMessage(READ_DATA, bytes, -1, buffer).sendToTarget();换成 mHandler.sendMessage()。

大小端数据的存储格式:
【BUG】串口通信问题_串口通讯
网络传输一般采用 大端 序,也被称之为 网络字节序 ,或 网络序 ,即先发送高字节数据再发送低字节数据。

开发环境尽量和生产环境接近,尤其是对于跨架构开发。

sftp

  • 注意 sudo
  • ​!​​ 符号用于在本地使用命令;
  • 如果没有​​!​​符号,意味着在远程使用命令;
  • ​put​​​/​​get​​​​-r​​ 针对目录进行操作;
sftp> sftp -P 22 ubuntu@192.168.50.72
sftp> lls
sftp> get /home/ubuntu/Documents/tmp/gdb.txt # 获取文件
sftp> put boot.scr # 上传文件
sftp> get -r /home/ubuntu/Documents/ . # 下载目录
Fetching /home/ubuntu/Documents/ to ./Documents
Retrieving /home/ubuntu/Documents
Retrieving /home/ubuntu/Documents/tmp
/home/ubuntu/Documents/tmp/test 100% 12KB 3.9MB/s 00:00
/home/ubuntu/Documents/tmp/test_for.c 100% 92 51.2KB/s 00:00
remote open("/home/ubuntu/Documents/tmp/core"): Permission denied
...
sftp> !ls # !shellcmd
a.out Documents test.c
sftp> !mkdir test # 使用命令在本地创建目录
sftp> ls # 显示远程目录
Documents Download vimtutor
sftp> !ls # 显示本地目录
a.out Documents test test.c
sftp> put -r test/
Uploading test/ to /home/ubuntu/test
Entering test/
sftp> mkdir testpi # 创建远程目录
sftp> !ls # 查看本地目录
a.out Documents test test.c
sftp> ls # 查看远程目录
Documents Download test testpi vimtutor
put [-afPpr] local-path [remote-path]
Upload local-path and store it on the remote machine. If the
remote path name is not specified, it is given the same name it
has on the local machine. local-path may contain glob(3) charac‐
ters and may match multiple files. If it does and remote-path is
specified, then remote-path must specify a directory.

If the -a flag is specified, then attempt to resume partial
transfers of existing files. Note that resumption assumes that
any partial copy of the remote file matches the local copy. If
the local file contents differ from the remote local copy then
the resultant file is likely to be corrupt.

If the -f flag is specified, then a request will be sent to the
server to call fsync(2) after the file has been transferred.
Note that this is only supported by servers that implement the
"fsync@openssh.com" extension.

If either the -P or -p flag is specified, then full file permis‐
sions and access times are copied too.

If the -r flag is specified then directories will be copied
recursively. Note that sftp does not follow symbolic links when
performing recursive transfers.


get [-afPpr] remote-path [local-path]
Retrieve the remote-path and store it on the local machine. If
the local path name is not specified, it is given the same name
it has on the remote machine. remote-path may contain glob(3)
characters and may match multiple files. If it does and
local-path is specified, then local-path must specify a direc‐
tory.

If the -a flag is specified, then attempt to resume partial
transfers of existing files. Note that resumption assumes that
any partial copy of the local file matches the remote copy. If
the remote file contents differ from the partial local copy then
the resultant file is likely to be corrupt.

If the -f flag is specified, then fsync(2) will be called after
the file transfer has completed to flush the file to disk.

If either the -P or -p flag is specified, then full file permis‐
sions and access times are copied too.

If the -r flag is specified then directories will be copied
recursively. Note that sftp does not follow symbolic links when
performing recursive transfers.

遇到问题:

remote open("/home/ubuntu/Documents/gdb.txt"): Permission denied

解决方法:

可以先​​ssh​​​,改变目录所属权限,增加写权限。然后再 ​​sftp​​。

字节错位问题

判断两个字节是否为一组,如果不是那么顺延一个字节。

缓冲区数据较少的问题

判断个数是否符合要求,如果不符合,那么就重传。


举报

相关推荐

0 条评论