【问题标题】:Arduino Gcode readerArduino Gcode 阅读器
【发布时间】:2014-12-31 21:04:28
【问题描述】:

我在 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 出s

【问题讨论】:

    标签: arduino g-code


    【解决方案1】:

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

    另一个大问题可能是缺少 ram,因为你的输出被截断了。有一些功能可以检查 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 和 break 中;所以第一次从

    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
    }
    

    【讨论】:

    • 谢谢!现在我明白为什么它不起作用了。但为什么 ”&&”?应该是“||”吗?如果只有一个 while 循环,代码会完美运行,它会遍历输入的值并且有很多“if”语句。
    • 虽然 k 不是空格并且没有超时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多