我正嘗試從Android應用程序向Arduino發送諸如「c」或「s」之類的命令。設備已被識別,但我不確定是否正確發送/接收數據。我正在使用USBSerialforAndroid庫。USB Serial for Android - 用Arduino Uno R3發送/接收數據的問題
當我點擊我的按鈕來開始和停止測量時,吐司通知彈出來表示他們正在經歷,但是當數據「收到」時,我收回「7.00」或「0.00」,我知道這是錯誤的。我覺得它可能在我的Arduino代碼中,或者我是如何從應用程序發送字節的。
下面是相關的Android代碼:
startMeasurement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectedSensorType.equals("grade")) {
//do nothing
} else if (selectedSensorType.equals("height")) {
startCode = "c";
heightStart = serialUser(startCode); //get heightFloor from Arduino
} else if (selectedSensorType.equals("distance")) {
startCode ="s";
distanceStart = 0;
}
}
});
saveMeasurement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectedSensorType.equals("grade")) {
check.setText(String.format("%.2f", outputGrade));
} else if (selectedSensorType.equals("height")) {
endCode = "c";
heightEnd = serialUser(endCode); //get heightCurb from Arduino
double measuredHeight = heightEnd - heightStart;
check.setText(String.format("%.2f", measuredHeight));
} else if (selectedSensorType.equals("distance")) {
endCode = "e";
distanceEnd = serialUser(endCode); //get distanceEnc from Arduino
double measuredDistance = distanceEnd - distanceStart;
check.setText(String.format("%.2f", measuredDistance));
}
}
});
這裏是我的serialUser()方法:
private double serialUser(String command) {
double output = 0;
if (mSerialDevice != null) {
byte[] commandArray = command.getBytes();
byte[] returnArray = new byte[8];
try {
mSerialDevice.setBaudRate(115200);
mSerialDevice.write(commandArray, 1000);
output = mSerialDevice.read(returnArray, 2500);
} catch (IOException e) {
e.printStackTrace();
}
}
return output;
}
這裏是我的Arduino代碼:
void setup()
{
Serial.begin(115200);
Dist.begin(2,3);
}
const double Pi = 3.14159265359; // 3.141593, set to 1 for testing purposes
double encoderPosition = -999;
void loop()
{
double newPosition = abs(myEnc.read()); // gets raw data, 1440 ticks = 1 rev
double revs = newPosition/(double)960; // calculates number of revs
double diameter = 15; // specify wheel diameter
double distanceEnc = diameter*Pi*revs; // calculates distance
double heightDiff;
int timesum = 0;
distanceEnc = round(distanceEnc*1000.0l)/1000.0l; // round distance to 3 decimal places
if (newPosition != encoderPosition) { // write new position to serial
encoderPosition = newPosition;
//Serial.println(distanceEnc,3);
//Serial.println(revs,3);
}
while (Serial.available()) { // if a character is sent to the device, the encoder reading resets to zero
byte incomingCommand = Serial.read();
if (incomingCommand == 's'){
myEnc.write(0);
revs = 0;
}
if (incomingCommand == 'e'){
Serial.println(distanceEnc,3);
}
if (incomingCommand == 'f'){
time = Dist.getDistanceTime();
heightFloor = (float) time/(148.00);
Serial.println(heightFloor);
}
if(incomingCommand =='c'){
for(int i=1; i<11; i++){
times[i] = Dist.getDistanceTime();
delay(100);
timesum = timesum+times[i];}
time = timesum/(int)10;
heightCurb = (float) time/(148.00);
heightDiff = heightFloor - heightCurb;
Serial.println(heightCurb);
}
}
}
你的困難似乎來自於發送arduino println()的值的字符串表示形式,但期望在android上接收二進制雙精度值。相反,讓你的android代碼將接收到的數據作爲文本字節或字符串處理,記錄下來進行調試驗證,然後再解析它。發送二進制雙選擇的替代方案可能會讓工作變得更具挑戰性,而且這裏沒有性能優勢。 – 2014-12-06 20:13:10