为什么 Union 不适用于通过串行发送从字符串转换而来的字节?

Why does a Union not work for sending bytes converted from a string via Serial?

在我的 NodeMCU V3 上,我得到一个字符串,将其转换为两个 long、一个 int 和两个字节,将它们保存在一个联合中并将整个联合发送到 Arduino Mega,它应该以相等的方式读取它工会.

当 NodeMCU 将字节打印到我的笔记本电脑的串行监视器时,它显示了它们的确切值,但在发送它们之后我只得到两个 0。我仍然认为它与字节的转换有关,但无法弄清楚是什么。

我试过了:

这是我的代码的相关部分。我编了一个字符串,而不是收到一个用于测试目的。

#include <ESP8266WiFi.h>

union Serial {          //Union to place the numbers in.
  byte by[12];              
  struct  {
    long vsollneu;         
    long ssollneu;        
    int phisollneu;         
    byte priosollneu;       
    byte typsollneu;        
  } Befehlssammlung;        
} Befehle;

long a1,pr1,ty1;

char str[]="2049714 -1927461000 17 80 4"; //String to be seperated

void convert(char*);

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

void loop(){
  convert(str);
  //Serial.write(Befehle.by,12);  //Send the Bytes of the Union to the Arduino MEGA
  delay(5555);
}

  void convert(char* str){     //Converting the String with strtol
    char* ende;

    Befehle.Befehlssammlung.vsollneu =strtol(str,&ende,10);
    Befehle.Befehlssammlung.ssollneu =strtol(ende,&ende,10);
    a1 = strtol(ende,&ende,10);   
    pr1= strtol(ende,&ende,10);
    ty1= strtol(ende,NULL,10);

   Befehle.Befehlssammlung.phisollneu=(int) a1;     //Converting the long to an int
   Befehle.Befehlssammlung.priosollneu=(byte) pr1;  //Probably that's somehow wrong???
   Befehle.Befehlssammlung.typsollneu=(byte) ty1;

      // Serial.println(Befehle.Befehlssammlung.vsollneu);
      // Serial.println(Befehle.Befehlssammlung.ssollneu);
      // Serial.println(Befehle.Befehlssammlung.phisollneu);
      // Serial.println(Befehle.Befehlssammlung.priosollneu);
      // Serial.println(Befehle.Befehlssammlung.typsollneu);
}

这是Arduino Mega的接收部分:

union IZweiCkontainer {
  byte by[12];              
  struct  {                 
    long vsollneu;          
    long ssollneu ;         
    int phisollneu;         
    byte priosollneu;       
    byte typsollneu;        
  } Befehlssammlung;        
} Befehle;

void setup(){
Serial.begin(115200);   //Serial for debugging
Serial3.begin(9600);    //Serial for conncetion the the Mcu
}

void loop(){

  if(Serial3.available()>0){
      while(Serial3.available()){
      Serial3.readBytes(Befehle.by,12);     //receiving the Bytes and writing them in the "same" union
    }
  }
      Serial.println(Befehle.Befehlssammlung.vsollneu);
      Serial.println(Befehle.Befehlssammlung.ssollneu);
      Serial.println(Befehle.Befehlssammlung.phisollneu);
      Serial.println(Befehle.Befehlssammlung.priosollneu);
      Serial.println(Befehle.Befehlssammlung.typsollneu);
}

让我失望的是,在 NodeMCU 上一切正常,但发送后我从 Arduino Mega 得到以下输出:

2049714
-1927461000
17
0
0

ints 和 longs 在 Arduino 和 ESP8266 上的尺寸不同。

使用 int16_tint32_t 确保它们在不同架构中的大小相同。

union {
  byte by[12];              
  struct  {                 
    int32_t vsollneu;          
    int32_t ssollneu;         
    int16_t phisollneu;         
    byte priosollneu;       
    byte typsollneu;        
  } Befehlssammlung;        
} Befehle;