适用于Android的USB Serial - 使用Arduino Uno R3发送/接收数据时出现问题

时间:2014-12-02 18:11:48

标签: java android serial-port arduino bytearray

我试图发送一个命令,如" c"或" s"从我的Android应用程序到Arduino。设备已被识别,但我不确定我是否正确发送/接收数据。我正在使用USBSerialforAndroid库。

当我点击我的按钮开始和停止测量时,会弹出Toast通知以表示它们正在通过,但是当数据被接收时#34;我要回到" 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);  
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我使用了开发人员示例文件夹中的示例来构建一个有效的应用程序。

https://github.com/mik3y/usb-serial-for-android/tree/master/usbSerialExamples

检查这些,看看你能做些什么。