Arduino,解码位置N处的字符串

时间:2014-12-06 18:56:18

标签: string serial-port arduino

我在使用一些简单的代码时遇到了这个小问题,基本上我正在做的是通过串口通过我用Java编写的程序发送信息。这些信息正在获取他们的基本陈述(IE,可以打开灯光和东西),但是我没有错误地解码发送给它的数字值字符串。

例如,我发送的字符串看起来像这样

BS//:+000/+000/+000

和使用的解码方法看起来像这样。

通过以下方式添加字符串后:

 if (inputString.startsWith("BS//:")) //**fixed
    {
      inputInfoToBaseStepper(inputString);
      baseStepperRunAction(baseStepperRotCount, baseStepperRotStepSize, baseStepperTime);
    }

发送它......

void inputInfoToBaseStepper(String baseStepper) 
{
  baseStepperRotCount = baseStepper.substring(6,9).toInt(); 
  baseStepperRotStepSize = baseStepper.substring(10,13).toInt();
  baseStepperTime = baseStepper.substring(15,18).toInt();
}

哪个应解码并运行

void baseStepperRunAction (int rotations, int StepSize, int delayTime)
{
  for (int rotations; rotations >=0; rotations--)
  {
    baseStepper.step(StepSize);
    delay(delayTime);
  }
}

问题似乎是它没有解码......我在这个阶段失去了一些想法。 :/

(代码的总代码,我知道信息已经到达那里,只是没有按照它应该进行编译。)

#include <Stepper.h>
//#include <HardwareSerial.h>

// int intensity = 0;               // led intensity this is needed just as example for this sketch
String inputString = "";         // a string to hold incoming data (this is general code you can reuse)
boolean stringComplete = false;  // whether the string is complete (this is general code you can reuse)

int stepsPerRevolution = 64;  //at 5.625 degrees a step

// initialize the stepper library on pins 8 through 11:
Stepper baseStepper(stepsPerRevolution, 2,3,4,5); // protocols start with //BS:
Stepper shoulderStepper(stepsPerRevolution, 6,7,8,9); // protocols start with //SS:
Stepper armStepper(stepsPerRevolution, 10,11,12,13); // protocols start with //AS:

//--------baseStepper--------//
int baseStepperRotCount = 0; //how many rotations in the for loop is needed
int baseStepperRotStepSize = 0; // how large should the steps be...
int baseStepperTime = 0; //delay time needed between each step (delay); so the stepper can do it's work.
//--------shoulderStepper--------//
int shoulderStepperRotCount =0;



void setup() {
  // initialize serial: (this is general code you can reuse)
  Serial.begin(115200);

}

void loop() {
  // when a newline arrives:
  if (stringComplete) {

    //these are test if statements, they serve no purpose after the intial boot, but must be included to test the connectivity;
      if (inputString.startsWith("alpha"))
      {
        boolean msgRecognized = true;
        pinMode(13, OUTPUT);
        digitalWrite(13, HIGH);
        inputString = "";
        stringComplete = false;
      } 
      else if (inputString.startsWith("beta"))
      {
        boolean msgRecognized = true;
        pinMode(13, OUTPUT);
        digitalWrite(13, LOW);
        inputString = "";
        stringComplete = false;
      } 
      //---------------------///


     //these statements set the engines and prepare for running of the program.
     if (inputString.startsWith("//BS:")) //   "BS//:+000/+000/+000"
    {
      inputInfoToBaseStepper(inputString);
      baseStepperRunAction(baseStepperRotCount, baseStepperRotStepSize, baseStepperTime);
    }
    else if (inputString.startsWith("//SS:"))
    {
      //inputInfoToShoulderStepper();
      //outputConfirmed();
    }
    else if (inputString.startsWith("//AS:"))
    {
      //inputInfoToArmStepper();
     // outputConfirmed();
    }





    if(inputString.startsWith("alp://")) { // OK is a message I know (this is general code you can reuse)


      boolean msgRecognized = true;

      if(inputString.substring(6,10) == "kprs") { // KeyPressed }

        msgRecognized = false; // this sketch doesn't know other messages in this case command is ko (not ok)


      // Prepare reply message if caller supply a message id (this is general code you can reuse)
      int idPosition = inputString.indexOf("?id=");
      if(idPosition != -1) {
        String id = inputString.substring(idPosition + 4);
        // print the reply
        Serial.print("alp://rply/");
        if(msgRecognized) { // this sketch doesn't know other messages in this case command is ko (not ok)
          Serial.print("ok?id=");
        } else {
          Serial.print("ko?id=");
        }
        Serial.print(id);
        Serial.write(255); // End of Message
        Serial.flush();
      }
    }
        // clear the string:
    inputString = "";
    stringComplete = false;
  }
}
}
/*
   Send listen messages
  int index = 0;
  for (index = 0; index < digitalPinListeningNum; index++) {
    if(digitalPinListening[index] == true) {
      int value = digitalRead(index);
      if(value != digitalPinListenedValue[index]) {
        digitalPinListenedValue[index] = value;
        Serial.print("alp://dred/");
        Serial.print(index);
        Serial.print("/");
        Serial.print(value);
        Serial.write(255); 
        Serial.flush();
      }
    }
  }

  for (index = 0; index < analogPinListeningNum; index++) {
    if(analogPinListening[index] == true) {
      int value = analogRead(index);
      if(value != analogPinListenedValue[index]) {
        analogPinListenedValue[index] = value;
        Serial.print("alp://ared/");
        Serial.print(index);
        Serial.print("/");
        Serial.print(value);
        Serial.write(255); // End of Message
        Serial.flush();
      }
    }
  }
} */

//this method decodes and stores inputs
void inputInfoToBaseStepper(String baseStepper) 
{
  baseStepperRotCount = baseStepper.substring(6,9).toInt(); //       B S / / : + 0 0 0 / + 0 0 0 / + 0 0 0
  baseStepperRotStepSize = baseStepper.substring(10,13).toInt();//   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8
  baseStepperTime = baseStepper.substring(15,18).toInt();
}



//this method runs the base stepper off the decoded actions.
void baseStepperRunAction (int rotations, int StepSize, int delayTime)
{
  for (int rotations; rotations >=0; rotations--)
  {
    baseStepper.step(StepSize);
    delay(delayTime);
  }
}





/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 This is general code you can reuse.
 */



void serialEvent() {

  while (Serial.available() && !stringComplete) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

0 个答案:

没有答案