Android 应用程序与 Arduino 串行通信不同步 [英] Android app not synchronized with Arduino Serial communication
问题描述
我有一个简单的声纳 arduino 项目,以便它每秒打印一次距离.我已经使用 UsbSerial 实现了一个 android 应用程序来与我的 arduino 通信.到目前为止一切顺利,我能够接收数据并且我收到的数据是正确的,但问题是有时无法正确发送值.这是我收到的示例输出:
I have a simple sonar arduino project so that it prints the distance every second. I have implemented an android app using UsbSerial to communicate with my arduino. So far so good, I am able to receive data and the data I receive is correct, but the problem is that the values are sometimes not properly sent. Here is the sample output I receive:
data: 7
data: 1
data:
data: 71
这里是生成输出的代码:
and here is the code that generates output:
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
@Override
public void onReceivedData(byte[] arg0)
{
try {
String data = new String(arg0, "UTF-8");
System.out.println("data: " + data);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
};
所以我认为这里有两个问题:
So in my opinion there is 2 problems here:
- 第 1 行和第 1 行2 必须是
71
值的一行 - 第 3 行不应该存在,因为我的应用程序正在侦听
onReceivedData
并且 arduino 总是发送一些东西.
- Lines 1 & 2 must be just one line with the value of
71
- Line 3 should not exists as my application is listening
onReceivedData
and arduino always send something.
任何帮助将不胜感激.
推荐答案
我已经找到了该问题的解决方案.通过阅读此链接,我注意到我需要对收到的数据进行一些操作在 onReceivedData
方法中.所以我改变了 mCallBack
如下:
I have found a solution for the issue. by reading this link I noticed that I need to do some manipulation on the data I receive in the onReceivedData
method.
So I changed the mCallBack
as follow:
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
@Override
public void onReceivedData(byte[] arg0)
{
if(arg0!= null && arg0.length > 0){
if (isStartByte(arg0[0])) {
printData();
clearBytes();
}
appendBytes(arg0);
}
}
};
这是我添加的其他方法:
and here is the other methods I added:
private void clearBytes(){
buffer=new byte[8];
bufferSize = 0;
}
private void appendBytes(byte[] buf){
System.arraycopy(buf, 0, buffer, bufferSize, buf.length);
bufferSize += buf.length;
}
private void printData() {
if (bufferSize == 0) {
return;
}
byte[] buf = new byte[bufferSize];
System.arraycopy(buffer, 0, buf, 0, bufferSize);
String data = null;
try {
data = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (null != data && !data.isEmpty()) {
System.out.println("data: " + data);
}
}
public boolean isStartByte(byte firstChar){
return firstChar=='A';
}
我还修改了 Arduino 代码并在串行输出的开头添加了字符 A
.这解决了问题,但是我认为这不是最佳实践.我认为 UsbSerial 库应该提供更好的输出处理(或者我可能错了,这就是使用串行通信的本质).
And also I modified the Arduino code and added character A
to the beginning of the serial output.
This solves the issue, however I think this is not the best practice. I think the UsbSerial library should provide better output handling( or maybe I am wrong and this is the nature of working with serial communication ).
这篇关于Android 应用程序与 Arduino 串行通信不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!