如何解决Arduino 和 Raspberry Pi 之间的高效通信串行
对于我的学士论文,我正在为电动汽车开发双向充电系统。我现在处于一个阶段,一切都像我希望的那样工作,但现在我遇到了代码效率问题。我使用 Arduino Mega 2560 通过串行对电力电子设备进行编程并在我的 GUI 中显示数据我使用通过 USB 连接到 Arduino 的 RaspBerry Pi 4,协议也是串行的。
在我的这个版本的软件中,当我将数据写入电力电子设备时,我在 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,16);
}else if (antwort.substring(10,13) == "VZB"){
blindstrom = (antwort.substring(8,14) == "VZB"){
blindstrom = (antwort.substring(8,11)).toInt();
vz_blind = antwort.substring(14,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,17);
}else if (antwort.substring(11,14) == "VZB"){
blindstrom = (antwort.substring(9,15) == "VZB"){
blindstrom = (antwort.substring(9,12)).toInt();
vz_blind = antwort.substring(15,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,18);
}else if (antwort.substring(12,15) == "VZB"){
blindstrom = (antwort.substring(10,16) == "VZB"){
blindstrom = (antwort.substring(10,13)).toInt();
vz_blind = antwort.substring(16,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 中,在那里处理字符串或字符向量要容易得多。 我的问题是,我必须为另一个 linux 系统提供一个接口,在那里我必须提供更改所有可写参数的机会,大约 25 到 30。
是否有可能处理来自输入数组的数据,该数组具有动态长度并遍历条目? 例如在数组中搜索一个字符串并处理该字符串之后的数字,直到出现下一个字符串?
对不起,问题太长,代码太少,但整个程序有 1400 多行 c 代码和 2100 多行 python 代码。 谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。