我正在设计一个应用程序,该应用程序通过蓝牙连接从Arduino获取值,它可以获取数据,但有时可以获取数据,但第一个数字错误。 像它得到840,564,0,0,0,0而不是540,564,0,0,0,0。
这仅发生在我的应用中。通过USB可以完美运行。
这是arduino的代码。
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(38400);
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
}
void loop() {
// read the input on analog pin 1:
int pot1 = analogRead(A1);
// read the input on analog pin 0:
int pot2 = analogRead(A0);
int switch1 = digitalRead(7);
int switch2 = digitalRead(6);
int switch3 = digitalRead(5);
int switch4 = digitalRead(4);
// print out the value you read:
Serial.print(pot1);
Serial.print(",");
Serial.print(pot2);
Serial.print(",");
Serial.print(switch1);
Serial.print(",");
Serial.print(switch2);
Serial.print(",");
Serial.print(switch3);
Serial.print(",");
Serial.println(switch4);
delay(100); // delay in between reads for stability
}
这是我的应用程序的一些代码
h = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECIEVE_MESSAGE: // if receive massage
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
sb.append(strIncom); // append string
int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
if (endOfLineIndex > 0) { // if end-of-line,
String sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear
// update TextView
dataFromArd=sbprint;
String segments[]=dataFromArd.split(",");
//angle1= -90 + (Integer.getInteger(segments[0]) - -333) * (90 - -90) / (333 - -333);
if (segments[0].length()>2 && segments[0].length()<4) {
int segmentInt1= Integer.parseInt(segments[0]);
int segmentInt2= Integer.parseInt(segments[1]);
if (segmentInt1 > 180 || segmentInt1 < 900) {
//angle1 = Integer.parseInt(segments[0]) - 529;
//angle1= -90 + (Integer.parseInt(segments[0]) - (515-333)) * (90 - -90) / (515+333) - (515-333);
int data1= (int) (0.27 * segmentInt1 - 139);
int data2=(int) (0.27 * segmentInt2 - 151);
if (data1!=angle1 && Math.abs(data1-angle1)<10){
angle1 = data1;
speedView.speedTo(angle1,5);
Log.d("int", segments[0] + "-" + Math.abs(data1-angle1) + "-" + angle1 + " " + segments[1] + "-" + Math.abs(data2-angle2) + "-" + angle2);
}
if (data2!=angle2 && Math.abs(data2-angle2)<25){
speedView2.speedTo(angle2,5);
angle2 = data2;
Log.d("int2", segments[0] + "-" + Math.abs(data1-angle1) + "-" + angle1 + " " + segments[1] + "-" + Math.abs(data2-angle2) + "-" + angle2);
}
//Log.d("int", Math.abs(data1-angle1) + "-" + angle1 + " " + Math.abs(data2-angle2) + "-" + angle2);
teste.setText("Data from Arduino: " + segments[0] + segments[1]);
}
}
}
//Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "...");
break;
}
};
};
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
}
public void run() {
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer); // Get number of bytes and message in "buffer"
h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler
} catch (IOException e) {
break;
}
}
}
}
答案 0 :(得分:0)
问题似乎出在内存的过度使用上,导致应用跳过帧而无法获取正确的数据。
我通过为数据的第一段添加一个虚拟值作为字符串并过滤掉损坏的数据(如果第一段!=到字符串没有得到数据)来解决此问题。
之后,我进行了很多性能改进,从而解决了该问题。