0

我在 Arduino 上制作了 G 代码阅读器,但它停止阅读。它有许多“中断”;而且我也有很多while循环和开关,所以我想当我在循环/开关上中断时,它会全部中断。

另一个想法是它会进行某种循环,但我不知道它在哪里循环。

这是我的代码:

void Gcode(){
  String yy,xx,gg;
  char text[64];
  int number=1;
while(number!=3){
while (Serial.available()>0) {
  delay(3);  //delay to allow buffer to fill
  char c = Serial.read();
  Serial.println(c);
  switch(c){
    case 'G':
      //read_number()
      while (Serial.available()>0) {
        char k = Serial.read();
    if(k==' ' || k=='\n'){
          break; 
    }
        else{
          gg+=k;
    }
      }
      switch(gg.toInt()){
        case 1:
        Serial.println(gg);
      while (Serial.available()>0) {
        c = Serial.read();
            Serial.println(c);
        switch(c){
          case 'X':
            while (Serial.available()>0) {
          char k = Serial.read();
            if(k==' ' || k=='\n'){
              break; 
            }
            else{
              xx+=k;
            }
            }
                char buf[xx.length()];
                xx.toCharArray(buf,xx.length());
                x2=atof(buf);
                Serial.println(x2);
                break; 
              case 'Y':
            while (Serial.available()>0) {
              char k = Serial.read();
              if(k==' ' || k=='\n'){
                break;
                  }
              else{
                    yy+=k;
              }
            }
                Serial.println(yy);
                char buf2[yy.length()];
                yy.toCharArray(buf2,yy.length());
                y2=atof(buf2);
            break;
              case 'E':
            break;
              case 'F':
            break;
              default:
            Serial.print("the end");
          }
         Serial.print("out of switch");
        }
        break;
      case 2:
        break;
      default: 
    Serial.print("nothing");
      }
      break;
    case '\n':
      number=3;
      break;
    default:
      Serial.print("default");
  }

}
}
if(sizeof(yy)>0){
  yy="";
  xx="";
  gg="";
}
Serial.print("quit");
}

当我发送 G1 X10.00 Y-100.00 \n它只打印: G 1 X 10.00 out of s

4

1 回答 1

0

您的一个大问题是您的时间结束时不再是carachter。这意味着如果您的循环消耗缓冲区的速度比写入的速度快(记住:9600 波特意味着 960Byte/s,arduino 即使速度很慢但可以计算 16.000.000 操作/s...)。

另一个大问题可能是缺少 ram,os 你的输出被截断。有一些功能可以检查 ram 的实时使用情况,请参阅http://playground.arduino.cc/Code/AvailableMemory,停止使用 String 甚至 char 数组是一个更好的主意;代码并不难写!

所以电脑会发送“G1 X10.00 Y-100.00 \n”但是在时间 X 你的 aruino 得到“G1 X10.00”,如果你现在读得非常快(快于 1/960 秒,arduino 是比那更快!)

因此,理想情况下,您应该更改所有 while 条件,删除可用的序列号,而是将您使用的条件放在 if 中并带有中断;所以第一次从

while (Serial.available()>0)

变成了

while ( (k=Serial.read()) != ' ' && k != '\n') //yes it is a bit weird like this

可能会好一点,检查 k 是一个有效的字符,并且超时

unsigned long timeout_ms = 1000; //timeout after 1 seconds from NOW!
unsigned long start_ms = millis();
int k=Serial.read();
while ( k != ' ' && millis()-start_ms < timeout_ms){// because here we expect "Gx ", why are you was also using k != '\n'? removed, feel free to add it back
    if (k == -1){
        k=Serial.read(); //read the next char
        continue; //return to the beginning of the while
    }
    [... do thigs...]
    k=Serial.read(); //read the next char
    //here you may add "start_ms = millis();" if you want to reset the timeout
}
于 2014-12-31T12:56:20.230 回答