为什么Union不能通过Serial发送从字符串转换的字节?

时间:2017-05-25 16:22:48

标签: arduino embedded type-conversion unions

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

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

我试过了:

  • 使用atol代替strtol来转换字符串,但丢弃了该字符串 看完之后 缺点。 atol() v/s. strtol()
    无论如何它都没用。
  • 我尝试使用lowByte((int) int1)转换字节。

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

#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

1 个答案:

答案 0 :(得分:1)

Arduino上的

intlong以及ESP8266的大小不同。

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

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