【问题标题】:Why is my Arduino sending a message infinitely?为什么我的 Arduino 会无限发送消息?
【发布时间】:2021-11-05 17:46:54
【问题描述】:

我不明白为什么我的 Arduino 会无限地向串行监视器发送相同的消息。

该项目的目标是监视和控制一种基本的开关算法,以便在检测到某个特定分支的故障时通过电力“电网”重新引导电力,从而最大限度地减少停电期间受影响的客户数量。这两个模块(每个模块都在“网格”中监控自己的分支)都不是设计为主或客户端。他们每个人都会收到完全相同的代码(“草图”)。谁最先发现故障,谁就会开始对话并开始重新分配电源的算法。

基本前提是让一个模块检测到故障并询问其邻居是否也有故障。如果没有,它会询问是否可以将电源从邻居的分支重定向到该模块的分支。故障检测模块将向相邻模块发送其最后已知的潮流信息。邻居将确定其“输电线路”是否可以处理增加的电力流,然后相应地重定向电力(通过继电器)。

但是,我无法通过第一条消息。一旦检测到“故障”,故障检测模块就会无限发送它的消息。我需要的是它发送一条消息,然后等待响应,然后做出相应的反应。

Serial Monitor Message Display

以下是代码的故障检测模块部分。

我是 Arduino 和 StackOverflow 的新手。如果我违反了社区的任何规范或准则,我深表歉意。但是,我找不到任何明确解决此问题的论坛,并且没有一个“修复”解决了该问题。

谢谢

int StatusPin = 13; //Assigns Pin 13 to be called "StatusPin"
int Relay = 12;     //Relay Signal Pin
int Feeder = 11;    //Feeder Control Switch Signal Pin
int readPin = A0;   //Assigns Pin A) to be called "readPin"
int msg = 0;        //Used for case declaration
//Default = 0
//Yes = 1
//No = 2
//Are You OK? = 3
//Switching Possible? = 4
int Pcheck = 0;       //Used for Power Calculation
float val = 0;        //Creates an empty variable to store future readings
float volts = 0;      //Same as above
char statTX = "Good"; //Current Status for this module

void setup() {
  char statTX = "Good"; //Default Status for this module
  pinMode(StatusPin, OUTPUT);
  pinMode(Relay, OUTPUT);
  pinMode(Feeder, OUTPUT);
  Serial.begin(9600);
}
  
void loop() {
  val = analogRead(readPin); //Stores the input value from the A0 pin into a variable
  volts = (val/1024)*5;      //Converts ADC reading to volts
  digitalWrite(Feeder, LOW); //Feeder Switch is Default OPEN

  if (val <= 676){
   digitalWrite(StatusPin, HIGH); //Green light status turns on when greater than 3.3V
   digitalWrite(Relay, HIGH); //Breaker switch is closed
  }

 //Transmission Code for Fault Detection Side
  else{
  statTX = "Fault";
  Serial.println("Fault");      //Sent Fault Status Message
  digitalWrite(Relay, LOW);     //Breaker switch is opened
  digitalWrite(StatusPin, LOW); //Green light status turns off when greater than 3.3V
  Serial.write(3);              //Send "Are You Okay?" message
  if (Serial.available() == 0){ //Creates a pause to wait for a response
  }
  else{
    msg = Serial.read();
      if (msg == 1) {
        statTX = "Isolated";           //Declares fault isolated
        Serial.println("Isolated");
        Serial.write(4);               //Send Message to Request Switch
        if (Serial.available() == 0){ //Creates a pause to wait for a response
        }
        else{
          msg = Serial.read();
           if (msg == 1){
              Serial.write(20);       //Sends tentative current draw
              if (Serial.available() == 0){
              }
              else {
                  msg = Serial.read();
                  if (msg == 1 && val < 676){
                  statTX = "SystemRestored";
                  Serial.println("SystemRestored");
                  digitalWrite(StatusPin, HIGH); //Green light status turns on when greater than 3.3V
                   }
              else {
                  statTX = "Failure";
                  Serial.println("Failure");
                   }
                }
           }
                else {
                  statTX = "Failure";
                  Serial.println("Failure");
                }
            }
          }
      }
  }

【问题讨论】:

    标签: arduino serial-communication serial-monitor


    【解决方案1】:

    问题是,void loop() {...} 过程被重复调用。因此,如果没有任何条件发生变化,则输出保持不变。

    在你的情况下,

    if (val <= 676){...
    

    不被占用。因此,else 块执行以下命令并打印到serial.

    Serial.println("Fault");      //Sent Fault Status Message
    ...
    Serial.write(3);              //Send "Are You Okay?" message
    

    这在您的输出图像中不可见,但我猜该行开头的 (U+25A1) 来自之前的迭代 - 来自 Serial.write(3);。但这不能通过给定的输出图像来证实。而如果if (Serial.available() == 0){...永远不会被占用,上述命令将永久循环执行。

    而且这个输出会无限重复,因为void loop()函数不会重复执行任何其他代码路径。

    【讨论】:

    • 您对如何实现“发送消息,等待响应,根据响应响应”的预期结果有什么建议吗?或者你知道我做错了什么,所以我的代码不会卡在我应该由 'if (serial.available() == 0) { }' 触发的“什么都不做”循环中?我需要在我的模块等待另一个模块的响应时暂停编程。
    • 而且,我什至不一定要求您提供完整的代码行。由于我是 Arduino 新手,也许有一个我可以使用的有用函数或库,我只是通过我的谷歌搜索还没有发现。但是,我很好奇为什么 Arduino 没有完成 Serial.write(3); 行,然后挂断并在 if (Serial.available() == 0) { } 处暂停,直到它在 RX 引脚上收到一条消息。
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多