String Ardunio,在N位置解码字符串

String Ardunio,在N位置解码字符串,string,serial-port,arduino,String,Serial Port,Arduino,我在一些简单的代码中遇到了一个小问题,基本上我所做的就是通过我用Java编写的程序通过串口发送信息。这些信息是为基本语句(即,可以打开灯和其他东西)获取的,但我在让它解码发送给它的带有数字值的字符串时出错 例如,我发送的字符串如下所示 BS//:+000/+000/+000 我使用的解码方法是这样的 通过以下方式添加字符串后: if (inputString.startsWith("BS//:")) //**fixed { inputInfoToBaseStepper(i

我在一些简单的代码中遇到了一个小问题,基本上我所做的就是通过我用Java编写的程序通过串口发送信息。这些信息是为基本语句(即,可以打开灯和其他东西)获取的,但我在让它解码发送给它的带有数字值的字符串时出错

例如,我发送的字符串如下所示

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);
  }
}
问题似乎是它无法解码。。。我在这个阶段有点迷茫/

(代码完全过时了,我知道信息已经到了那里,只是没有像应该的那样编译。)

#包括
//#包括
//整数强度=0;//led强度这是需要的,就像这个草图的示例一样
字符串inputString=“”;//保存传入数据的字符串(这是可以重用的常规代码)
布尔stringComplete=false;//字符串是否完整(这是可以重用的常规代码)
int-stepsperevolution=64//以每级5.625度的速度
//在引脚8至11上初始化步进器库:
基于步进器的步进器(步进再进化,2,3,4,5);//协议以//BS开始:
步进机肩步进机(步进再进化,6,7,8,9);//协议以//SS开头:
步进器臂步进器(步进再进化,10,11,12,13);//协议以//开头,如下所示:
//--------基本步进器--------//
int basestepErrorCount=0//for循环中需要多少次旋转
int baseStepErrorStepSize=0;//台阶应该有多大。。。
int baseStepperTime=0//每个步骤之间所需的延迟时间(延迟);所以步进机可以完成它的工作。
//--------肩步进机--------//
int-shoulderStepErrorCount=0;
无效设置(){
//初始化串行:(这是可以重用的常规代码)
序列号开始(115200);
}
void循环(){
//当换行符到达时:
如果(完成){
//这些是testif语句,它们在初始引导后没有任何用途,但必须包含在测试连接性中;
if(inputString.startsWith(“alpha”))
{
布尔值msgrerecognized=true;
pinMode(13,输出);
数字写入(13,高);
inputString=“”;
stringComplete=false;
} 
else if(inputString.startsWith(“beta”))
{
布尔值msgrerecognized=true;
pinMode(13,输出);
数字写入(13,低);
inputString=“”;
stringComplete=false;
} 
//---------------------///
//这些语句设置引擎并准备运行程序。
if(inputString.startsWith(“//BS:”)//“BS//:+000/+000”
{
InputFotoBaseStepper(inputString);
BaseStepErrorNaction(BaseStepErrorCount、BaseStepErrorStepSize、BaseStepTime);
}
else if(inputString.startsWith(“//SS:”)
{
//InputFotoShoulderStepper();
//输出已确认();
}
else if(inputString.startsWith(“//AS:”)
{
//InputFoToArmStepper();
//输出已确认();
}
if(inputString.startsWith(“alp://”)是我知道的消息(这是可以重用的通用代码)
布尔值msgrerecognized=true;
if(inputString.substring(6,10)=“kprs”){//KeyPressed}
msgrerecognized=false;//此草图不知道本例中的其他消息命令为ko(不确定)
//如果呼叫者提供消息id,则准备回复消息(这是可以重用的常规代码)
int idPosition=inputString.indexOf(“?id=”);
如果(idPosition!=-1){
字符串id=inputString.substring(idPosition+4);
//打印答复
序列号。打印(“alp://rply/");
如果(msgrerecognized){//此草图不知道其他消息,则在这种情况下,命令为ko(不确定)
Serial.print(“ok?id=”);
}否则{
Serial.print(“ko?id=”);
}
序列号。打印(id);
Serial.write(255);//消息结束
Serial.flush();
}
}
//清除字符串:
inputString=“”;
stringComplete=false;
}
}
}
/*
发送侦听消息
int指数=0;
对于(索引=0;索引=0;旋转--)
{
基本步进器。步长(步长);
延迟(延迟时间);
}
}
/*
每当新数据进入时,都会发生SerialEvent
硬件串行接收。此例程在每个
time loop()运行,因此在循环中使用delay可以延迟
答复。可能有多个字节的数据可用。
这是可以重用的通用代码。
*/
void serialEvent(){
while(连载)
#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;
    }
  }
}