Arduino和Raspberry Pi之间的高效通信(串行)

2024-06-02 08:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的学士学位论文中,我正在为电动汽车开发一种双向充电系统。我现在正处于这样一个时刻,所有的东西都像我希望的那样工作,但是现在我在代码的效率方面遇到了问题。我使用Arduino Mega 2560通过串行对电力电子设备进行编程,并在GUI中显示数据。我使用Raspberry Pi 4,通过USB连接到Arduino,协议也是串行的

在这个版本的软件中,当我将数据写入电力电子设备时,我在arduino中使用预构造的硬编码数据,当我从模块中读取数据时,我读取硬编码参数。我使用substring()函数实现了一个函数,用于传输当前值(活动/反应),因为我想从我构建的GUI中更改它们

函数处理的接收电报示例如下所示:W0VZW-B0VZB-M22 W(有功功率)+值(0-1000)+VZW(电流方向符号)+B(无功功率)+值(0-1000)+VZW(电流方向符号)+M(模块号)+值 字符串“antwort”包含此电报,并被分成若干部分,以将值传递给函数“strom_stellen”,该函数将参数写入模块

  void strom_ubergabe(){
  Serial.println("strom_ubergabe");
  while(true){
    if (Serial.available()){
      String antwort = Serial.readString();
      
      if (antwort.substring(2,5) == "VZW"){
        wirkstrom = (antwort.substring(1,2)).toInt();
        vz_wirk = antwort.substring(5,6);
        
        if (antwort.substring(8,11) == "VZB"){
          blindstrom = (antwort.substring(7,8)).toInt();
          vz_blind = antwort.substring(11,12);
          Modul = antwort.substring(13,15);
          
        }else if (antwort.substring(9,12) == "VZB"){
          blindstrom = (antwort.substring(7,9)).toInt();
          vz_blind = antwort.substring(12,13);
          Modul = antwort.substring(14,16);
  
        }else if (antwort.substring(10,13) == "VZB"){
          blindstrom = (antwort.substring(7,10)).toInt();
          vz_blind = antwort.substring(13,14);
          Modul = antwort.substring(15,17);
  
        }else if (antwort.substring(11,14) == "VZB"){
          blindstrom = (antwort.substring(7,11)).toInt();
          
          if (blindstrom > 1000){
            blindstrom = 0; 
          }
          
          vz_blind = antwort.substring(14,15);
          Modul = antwort.substring(16,18);
  
        }else{
          Serial.println("Fehler bei der Blindstromübergabe!");
          blindstrom = 0;
        }
        
      }else if(antwort.substring(3,6) == "VZW"){
        wirkstrom = (antwort.substring(1,3)).toInt();
        vz_wirk = antwort.substring(6,7);
        
        if (antwort.substring(9,12) == "VZB"){
          blindstrom = (antwort.substring(8,9)).toInt();
          vz_blind = antwort.substring(12,13);
          Modul = antwort.substring(14,16);
          
        }else if (antwort.substring(10,13) == "VZB"){
          blindstrom = (antwort.substring(8,10)).toInt();
          vz_blind = antwort.substring(13,14);
          Modul = antwort.substring(15,17);
  
        }else if (antwort.substring(11,14) == "VZB"){
          blindstrom = (antwort.substring(8,11)).toInt();
          vz_blind = antwort.substring(14,15);
          Modul = antwort.substring(16,18);
  
        }else if (antwort.substring(12,15) == "VZB"){
          blindstrom = (antwort.substring(8,12)).toInt();
  
          if (blindstrom > 1000){
            blindstrom = 0; 
          }
          
          vz_blind = antwort.substring(15,16);
          Modul = antwort.substring(17,19);
  
        }else{
          Serial.println("Fehler bei der Blindstromübergabe!");
          blindstrom = 0;
        }
        
      }else if(antwort.substring(4,7) == "VZW"){
        wirkstrom = (antwort.substring(1,4)).toInt();
        vz_wirk = antwort.substring(7,8);
  
        if (antwort.substring(10,13) == "VZB"){
          blindstrom = (antwort.substring(9,10)).toInt();
          vz_blind = antwort.substring(13,14);
          Modul = antwort.substring(15,17);
          
        }else if (antwort.substring(11,14) == "VZB"){
          blindstrom = (antwort.substring(9,11)).toInt();
          vz_blind = antwort.substring(14,15);
          Modul = antwort.substring(16,18);
  
        }else if (antwort.substring(12,15) == "VZB"){
          blindstrom = (antwort.substring(9,12)).toInt();
          vz_blind = antwort.substring(15,16);
          Modul = antwort.substring(17,19);
  
        }else if (antwort.substring(13,16) == "VZB"){
          blindstrom = antwort.substring(9,13).toInt();
  
          if (blindstrom > 1000){
            blindstrom = 0; 
          }
          
          vz_blind = antwort.substring(16,17);
          Modul = antwort.substring(18,20);
  
        }else{
          Serial.println("Fehler bei der Blindstromübergabe!");
          blindstrom = 0;
        }
        
      }else if(antwort.substring(5,8) == "VZW"){
        wirkstrom = (antwort.substring(1,5)).toInt();
  
        if (wirkstrom > 1000){
            wirkstrom = 0; 
          }
          
        vz_wirk = antwort.substring(8,9);
  
        if (antwort.substring(11,14) == "VZB"){
          blindstrom = (antwort.substring(10,11)).toInt();
          vz_blind = antwort.substring(14,15);
          Modul = antwort.substring(16,18);
          
        }else if (antwort.substring(12,15) == "VZB"){
          blindstrom = (antwort.substring(10,12)).toInt();
          vz_blind = antwort.substring(15,16);
          Modul = antwort.substring(17,19);
  
        }else if (antwort.substring(13,16) == "VZB"){
          blindstrom = (antwort.substring(10,13)).toInt();
          vz_blind = antwort.substring(16,17);
          Modul = antwort.substring(18,20);
  
        }else if (antwort.substring(14,17) == "VZB"){
          blindstrom = antwort.substring(10,14).toInt();
  
          if (blindstrom > 1000){
            blindstrom = 0; 
          }
          
          vz_blind = antwort.substring(17,18);
          Modul = antwort.substring(19,21);
  
        }else{
          Serial.println("Fehler bei der Blindstromübergabe!");
          blindstrom = 0;
        }
        
      }else{
        Serial.println("Fehler bei der Wirkstromübergabe!");
        wirkstrom = 0;
      }
      
      if (vz_wirk == "-"){
        stromrichtung_w = "Laden (Wirk)";
        
      }else if (vz_wirk == "+"){
        stromrichtung_w = "Entladen (Wirk)";
        
      }else{
        stromrichtung_w = "Fehler in der Stromrichtungsübergabe!";
      }
  
      if (vz_blind == "-"){
        stromrichtung_b = "Laden (Blind)";
        
      }else if (vz_blind == "+"){
        stromrichtung_b = "Entladen (Blind)";
        
      }else{
        stromrichtung_b = "Fehler in der Stromrichtungsübergabe!";
      }

      strom_wirk = String(wirkstrom);
      strom_blind = String(blindstrom);
      int modulpin = Modul.toInt();
      
      strom_stellen(vz_wirk, vz_blind, strom_wirk, strom_blind, modulpin);
      
      Serial.println(String(float(wirkstrom)/100) + " A " + stromrichtung_w + " " + String(float(blindstrom)/100) + " A " + stromrichtung_b + " Modul" + Modul);
      break;
    }
  }
}

正如你所看到的,这是非常好的编程,也不是非常有效,特别是当你考虑到,整个函数发送超过5个值。但它是有效的

我的代码的另一部分是python,在python中处理字符串或字符向量要容易得多。 我的问题是,我必须给另一个linux系统提供一个接口,在这里我必须给修改所有可写参数的机会,这些参数大约是25到30

是否有可能处理来自输入数组(具有动态长度)的数据并在入口上迭代? 例如,在数组中搜索一个字符串,并处理该字符串之后的数字,直到出现下一个字符串

很抱歉,问题太长,代码太少,但整个程序有1400多行c代码和2100多行python代码。 多谢各位


Tags: ifserialsubstringelseblindvzmodultoint