简体   繁体   English

Serial.println(Serial.available()); 改变Arduino代码的行为

[英]Serial.println(Serial.available()); changing behavior of Arduino code

I have this piece of code on my Arduino: 我的Arduino上有以下代码:

These variables are defined as global, like this: 这些变量被定义为全局变量,如下所示:

 #define arraySize 32        
 char inChar = '0';
 char inVariable[arraySize];
 byte index = 0;  
 byte inDigit;     
 int inNumber;

A function is defined like this: 函数定义如下:

void checkSeriale(){
     while(Serial.available() > 0){
       Serial.println(Serial.available());
       inChar = Serial.read();
        if(inChar != ':' && index < arraySize-1 && inChar != ';'){
           inVariable[index] = inChar;
           index++;
           inVariable[index] = '\0';
        }else{
           break;
        }
     }

     if(inChar == ':'){ 
       index = 0;
       while(Serial.available() >0 && index < 10 && inChar != ';'){
         inDigit = Serial.read() - '0';
         inNumber = inNumber * 10;
         inNumber = inNumber + inDigit; 
         index++; 
       }
       cambioVariabiliSeriale(inVariable);
     }

     if(inChar == ';'){
       cambioVariabiliSeriale(inVariable);
       inNumber=0;
       inChar='0';
       index=0;
     }
}

The checkSeriale() function loops in the Arduino loop() with some others functions that run some other pieces of code that do not affect any of these variables. checkSeriale()函数与其他一些函数一起在Arduino loop() ,这些函数运行一些不影响任何这些变量的代码。

This function should get commands from the serial port in two forms: 此函数应以两种形式从串行端口获取命令:

  1. variablename:variablevalue - This should change the the value of the variable called variablename to the new received value. variablename:variablevalue这应将名为variablename的变量的值更改为新的接收值。
  2. command; - This should simply run a external function to do something else. -这应该只运行外部函数以执行其他操作。

The function reads the received data until it reaches a : or a ; 该函数读取接收到的数据,直到到达:;

If it receives a : , it keeps on reading to get the value. 如果收到: ,它将继续读取以获取该值。 The value is then stored in the inNumber variable and inVariable gets passed to a second function cambioVariabiliSeriale(String inVariable) that checks what variable name the inVariable string equals to and assigns inNumber to the variable that has the same name as inVariable . 然后该值被存储在inNumber可变和inVariable被传递到第二函数cambioVariabiliSeriale(String inVariable) ,检查什么变量名inVariable串等于并分配inNumber到具有相同名称的变量inVariable

The code as it is seems to work perfectly. 该代码似乎可以正常工作。 Every received signal(with the right syntax) gets handled well. 每个接收到的信号(使用正确的语法)都会得到很好的处理。

But if I try and remove the Serial.println(Serial.available()); 但是如果我尝试删除Serial.println(Serial.available()); line, the code stops working. 行,代码停止工作。 inNumber now always contains 0 . inNumber现在始终包含0

I don't mind keeping that line in the code, but I think it is strange that executing that command keeps the code working. 我不介意在代码中保留该行,但我认为执行该命令使代码保持正常工作很奇怪。

Does someone know why that line affects the reading of the received value? 有人知道为什么该行会影响接收值的读取吗?

I've tried to run your code with some dummy values in (see below), and it seems to run ok with the line commented out, ie innumber does contain values. 我试图用一些虚拟值运行您的代码(请参见下文),并且在注释掉该行的情况下似乎可以正常运行,即innumber确实包含值。 However after working through it I kinda sense I'm not understanding your logic. 但是,经过研究之后,我觉得我不了解您的逻辑。 Is this roughly the flow: 这大致是流程:

  1. check characters are in the serial receive buffer with Serial.available 检查字符是否在带有Seri​​al.available的串行接收缓冲区中

  2. If so, do one of three things: 如果是这样,请执行以下三种操作之一:

a) if the char isn't a ';' a)如果字符不是';' or ':' or the last char in the array, add it to the inVariable array, increment the index counter and add a zero to the array 或':'或数组中的最后一个字符,将其添加到inVariable数组中,递增索引计数器,并将零添加到数组中

b) if the char is a ':', do a couple of checks and read the next char from the buffer as a number, then do something with that, then run cambioVariabiliSeriale b)如果字符为“:”,请进行两次检查,并从缓冲区中读取下一个字符为数字,然后执行一些操作,然后运行cambioVariabiliSeriale

c) if the char is a ';', run cambioVariabiliSeriale and reset some indexes. c)如果char是';',请运行cambioVariabiliSeriale并重置一些索引。

(Also, I don't think you need check if inChar != ';' when you're inside an if statement checked for inChar == ':') (此外,当您在if语句中检查inChar ==':'时,我认为您不需要检查inChar!=';')

Couldn't this be run as the following, so Serial.println(Serial.available()); 无法按以下方式运行,所以Serial.println(Serial.available()); can be commented out without effect? 可以被注释掉而没有效果吗?

void checkSeriale(){
  while(Serial.available() > 0){
    Serial.println(Serial.available());
    inChar = Serial.read();
    if(inChar != ':' && index < arraySize-1 && inChar != ';'){
      inVariable[index] = inChar;
      index++;
      inVariable[index] = '\0';
    }

    else if(inChar == ':'){ 
      index = 0;
      while(Serial.available() >0 && index < 10){
        inDigit = Serial.read() - '0';
        inNumber = inNumber * 10;
        inNumber = inNumber + inDigit; 
        index++; 
      }
      cambioVariabiliSeriale(inVariable);
    }

    else if if(inChar == ';'){
      cambioVariabiliSeriale(inVariable);
      inNumber=0;
      inChar='0';
      index=0;
    }
  }
}

I plugged a bunch of Serial Monitor prints into your original code to see what was going on - it's here if it's any use to you. 我在自己的原始代码中插入了一些串行监视器打印内容,以查看发生了什么-如果对您有用,就在这里。 Hope it helps! 希望能帮助到你!

#define arraySize 32        
char inChar = '0';
char inVariable[arraySize];
byte index = 0;  
byte inDigit;     
int inNumber;
int i; //counter

String repeatstring;

void setup(){
  Serial.begin(9600);
}

void loop(){
  checkSeriale();
}

void checkSeriale(){
  while(Serial.available() > 0){
    pr("Start of 'checkSeriale' function");
    pr("Characters available in serial receive buffer: " + String(Serial.available()));
    //Serial.println(Serial.available());
    inChar = Serial.read();
    pr("Read a char into InChar: " + String(inChar));
    pr("Characters available after Serial.read(): " + String(Serial.available()));
    if(inChar != ':' && index < arraySize-1 && inChar != ';'){
      pr("First 'if' statement (inChar not equal to colon)");
      inVariable[index] = inChar;
      index++;
      inVariable[index] = '\0';
      for(i=0;i<sizeof(inVariable);i++){
        Serial.print(String(inVariable[i]));
      }
    }
    else{
      pr("Else statement for first 'if', inChar equals colon hence break");
      break;
    }
  }
  if(inChar == ':'){ 
    pr("Second 'if' statement (InChar is equal to colon)");
    index = 0;
    pr("Characters available in serial receive buffer: " + String(Serial.available()));
    while(Serial.available() >0 && index < 10 && inChar != ';'){
      inDigit = Serial.read() - '0';
      pr("Characters available after Serial.read(): " + String(Serial.available()));
      pr("inDigit: " + String(inDigit));
      pr("inNumber: " + String(inNumber));
      inNumber = inNumber * 10;
      pr("inNumber * 10: " + String(inNumber));
      inNumber = inNumber + inDigit; 
      pr("InNumber + inDigit : " + String(inNumber));
      index++; 
    }
    cambioVariabiliSeriale(inVariable);
  }

  if(inChar == ';'){
    pr("third 'if' statement (inChar is equal to semicolon)");
    cambioVariabiliSeriale(inVariable);
    inNumber=0;
    inChar='0';
    index=0;
  }

}

void cambioVariabiliSeriale(char test[]){
}

//simple debug technique - use pr("something"); in your code - askchipbug
void pr(String txt){
  if(repeatstring != txt){
    //if the debug text is different, print it
    Serial.println(txt); //prints the text and adds a newline
    delay(1000); //just pauses the scrolling text for 1 second, make bigger if you want a longer pause
    repeatstring = txt;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Arduino IDE-按下按钮时停止重复“ Serial.println” - Arduino IDE - Stopping repeat 'Serial.println' when button pressed 如果不在loop()中,则Arduino Serial.println()输出空白行 - Arduino Serial.println() outputs a blank line if not in loop() Serial.println()定义在哪里..? 我可以看到它的源代码吗? - where is the Serial.println() defined ..? can i see source code for it? 如何在串行时退出``for&#39;&#39;循环.Arduino中可用 - How to exit a 'for' loop while serial.available in Arduino 仅当我在循环中放入Serial.println(“”)行时,Arduino串行端口才打印所有行 - Arduino serial port printing all lines only when I put a Serial.println(“ ”) line in the loop serial.println(String) 打印“#”而不是字符串 - serial.println(String) prints "#" instead of the string 为什么 Serial.println(<char[]> ); 返回随机字符? - Why Does Serial.println(<char[]>); Return Random Characters? 为什么调试语句Serial.println(i)时for循环行为会发生变化; 存在与注释掉 - Why does for loop behavior change when debug statement Serial.println(i); is present vs. commented out 我正在尝试通过蓝牙发送数据,但是Serial.available()总是返回0 - I am trying to send data via bluetooth but Serial.available() always return 0 Arduino 代码未处理串行 Output - Arduino Code Not Processing Serial Output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM