简体   繁体   English

如何在串行时退出``for''循环.Arduino中可用

[英]How to exit a 'for' loop while serial.available in Arduino

Background: I am using an XBee connected with a PC and a XBee + Arduino (sensor too). 背景:我正在使用的XBee与PC和的XBee + Arduino的(传感器太)连接。 I want to send a command from the PC to the Arduino side. 我想从PC向Arduino端发送命令。 The Arduino side will get the command and send sensor data. Arduino端将获取命令并发送传感器数据。

Now the problem: I want the Arduino to keep sending the data (current sensor data - so it will may change in value), suppose 20 times if there is no command from the PC side. 现在的问题是:我想让Arduino继续发送数据(当前传感器数据-这样它的值可能会发生变化),假设PC端没有命令,则发送20次。 What I mean is that if after sending 5 sensor data, the Arduino sees that there is another command from PC, it has to stop sending the sensor data; 我的意思是,如果在发送5个传感器数据后,Arduino看到PC发出了另一条命令,它必须停止发送传感器数据。 it will get the new command from the PC, read it and then start sending again. 它将从PC获得新命令,将其读取,然后再次开始发送。

My requirement is that the Arduino will send sensor data after getting a command from the PC side and will keep sending for 20 times, but in this meantime, if new command comes, it will stop sending the sensor data, will read the command and then start once again to send the sensor data 20 times. 我的要求是Arduino将在从PC端收到命令后发送传感器数据,并将继续发送20次,但是在此期间,如果出现新命令,它将停止发送传感器数据,将读取命令,然后再次开始发送传感器数据20次。 How can I write the code so that it will check in every loop whether there is any command from the PC or not? 如何编写代码,以便在每个循环中检查PC是否有任何命令?

I know the below code is not even close for what I am looking for: 我知道以下代码与我正在寻找的内容甚至还不接近:

void loop()
{
    while (Serial.available()) {
        delay(2);
        char c = Serial.read();
        readString += c;
    }
    if (readString.length() >0) {
        Serial.println(readString);
        for (int i=0; i<20; i++) {
            //Send the temperature, or send a simple message
            Serial.println("The command has been received");
        }
    }
}

The while loop and the delay() call don't help. while循环和delay()调用无济于事。 Say that the PC sends the START command to enable temperature sampling and STOP to tell it to stop updating them. 假设PC发送了START命令以启用温度采样,并发送STOP通知其停止更新温度。 You'll need a bool variable to indicate state. 您将需要一个bool变量来指示状态。 Something like this: 像这样:

bool sendTemperatures;
String readString;

void executeCommand(String cmd)
{
    if (cmd == "START") sendTemperatures = true;
    else if (cmd == "STOP") sendTemperatures = false;
    // etc...
    else Serial.println("Bad command");
}

void checkSerial()
{
    if (!Serial.available()) return;
    char c = Serial.read();
    if (c == '\n') {
        executeCommand(readString);
        readString = "";
    }
    else readString += c;
}

void sendSensorData() 
{
    // etc..
}


void loop() 
{
    checkSerial();
    if (sendTemperatures) sendSensorData();
}
String recievedData;  //readString

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

 void loop() {

  if (Serial.available() > 0) {
   recievedData = Serial.readString();//read full recieved data 
   Serial.println(recievedData);

    for (int i = 0; i < 20 && Serial.available() <= 0; i++) {
     //Send the temperature, or send a simple message
    }
   }
 }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM