gpt4 book ai didi

java - Arduino - Android 蓝牙连接有时会出现第一个字符错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:21:36 25 4
gpt4 key购买 nike

我正在设计一个应用程序,它通过蓝牙连接从 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;
}
}
}

}

最佳答案

问题似乎是内存使用过多,导致应用程序跳帧并且无法获取正确的数据。

我通过为第一段数据添加一个虚拟值作为字符串并在过滤掉损坏的数据后解决了这个问题(如果第一段!=到字符串没有获取数据)。

之后我做了很多性能改进来解决这个问题。

关于java - Arduino - Android 蓝牙连接有时会出现第一个字符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51083476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com