首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino Gcode阅读器

Arduino Gcode阅读器
EN

Stack Overflow用户
提问于 2014-12-30 14:27:13
回答 1查看 1K关注 0票数 0

我在Arduino上做了G码阅读器,但它停止了阅读。它有很多“中断”,我也有很多when循环和开关,所以我想当我打开循环/开关时,它会破坏所有。

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

这是我的代码:

代码语言:javascript
复制
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.00Y-100.00 \n时,它只打印:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-31 12:56:20

你的一个大问题是,你的时间结束时,他们是名义上的拖车。这意味着如果循环使用缓冲区的速度比编写的快(记住:9600波特意味着960字节/秒,arduino即使速度慢,也可以计算16.000.000操作/s.)。

另一个大问题可能是缺乏ram,您的输出被截断。有一些函数可以检查ram的实时使用情况,查看http://playground.arduino.cc/Code/AvailableMemory,停止使用String,甚至char数组是一个更好的方法;代码并不难写!

因此pc会发送"G1 X10.00Y-100.00 \n“,但在X时,您的aruino会得到"G1 X10.00",如果您现在读取缓冲区的速度非常快(比1/960秒快,arduino是一种比这更快的方式!)

因此,理想情况下,您应该更改所有while条件,删除串行可用的条件,而是将您使用的条件放在if中,然后加上中断;因此,第一个时间从

代码语言:javascript
复制
while (Serial.available()>0)

变成了

代码语言:javascript
复制
while ( (k=Serial.read()) != ' ' && k != '\n') //yes it is a bit weird like this

也许更好一点,检查k是一个有效的字符,并且超时。

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27707186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档