Arduino-串行通信不起作用

时间:2019-02-28 20:07:34

标签: c arduino

我在进入Package中的代码的第二个Serial.available循环时遇到问题。如果您发现代码有任何问题,请告诉我。 Arduino Code

输入:  1.“ Package”->我从串行监视器获得+1  2.“ 5&12!23!34!59!70!” ->我从串行监视器得到0。对于这个,我应该看到5 12 23 34 59 70

请让我知道,如果您在此代码中发现任何错误,以及为什么Serial.available在Package中不起作用?

String incoming;
String readIncoming; 
int i=0;
typedef struct {
  int npins;
  int addr[512];
}
measureList;
measureList list; 
void setup() {
    Serial.begin(9600);
    Serial.println("Serial conection started");
    incoming = ""; list.npins = 6; 
}
void loop() {
   while(Serial.available() > 0) {
   incoming = Serial.readString();

  if(incoming == "HI")        {  Serial.println("Steven"); }
  if(incoming == "SampleAll") {  SampleAll();             }
  if(incoming == "Package")   { Serial.println("+1"); Package(); }
  }
}
void Package(void)
{
  while(1)
{ 

if(Serial.available()>0 & i<= list.npins)

 {
        char recieved = Serial.read(); 

 if (recieved != '&' & i == 0){incoming = incoming + recieved;}

 if (recieved == '&' & i == 0){list.npins = incoming.toInt(); Serial.println(list.npins); incoming=""; i = 1;}

  if (recieved != '!' & recieved != '&' & i > 0){incoming = incoming + recieved; }

  if (recieved == '!' & recieved != '&' & i > 0){ list.addr[i] = incoming.toInt(); Serial.println(list.addr[i]);incoming = "";  i = i + 1;} 

 if (i == list.npins) {break;}
  }
}
}

0 个答案:

没有答案