Problem sending byte variable over serial

I have shortened the sending code to make reading easier. I have a struct i send over serial from arduino mega to esp32 "ts1". They are both little endian and i checked and appears uint8_t is the same 1 bytes on esp32 as arduino mega.

My current code i post send okay if i remove vars[39]. Which is issue 1.

issue 2 is if i change any of those uint32_t in the structs to uint8_t then the data is not received correctly on the receiver esp32. i check if the size of incoming data == sizeof(ts1) but apparently its not the size it expects? why do i have trouble changing the uint32_t's to uint8_t. Not all the variable in the struct need to be uint32_t.

Sender arduiono mega,

struct testStruct {
  float T1 = 0;
  float T2 = 0;
  float T3 = 0;
  float T4 = 0;
  float T5 = 0;
  float mainAC = 0;
  uint32_t runtime = 0;
  uint32_t t1Cycles = 0;
  uint32_t t2Cycles = 0;
  uint32_t mainWaterFloat = 0;
  uint32_t t1U = 0;
  uint32_t t1L = 0;
  uint32_t t2U = 0;
  uint32_t t2L = 0;
  uint32_t MU = 0;
  uint32_t ML = 0;
  uint32_t RL = 0;
  uint32_t L1 = 0;
  uint32_t L2 = 0;
  uint32_t master = 1;
  uint32_t vars[39];
//  uint8_t vars2 [29];
  float Vin = 0;
};
testStruct ts1;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
delay(1000);
Write();
}
void Write() {
  //if (digitalRead(espSignalPin) == HIGH) {
   // Serial.println("write");
    Serial2.write((uint8_t*)&ts1, sizeof(ts1));
  }

To make looking at the code easier,

receiver esp32,

#include <EEPROM.h>
#include <WiFiClient.h>
#include <WiFi.h>
#include <WiFiUdp.h>

WiFiUDP Udp;

unsigned int ServerPort = 4210;
unsigned int localUdpPort = 4220;
char incomingPacket[255];  // buffer for incoming packets
int addr = 0;
unsigned long pairMarker = 0;
unsigned long pairCounter = 0;
unsigned long ledBlinkMillis = 0;
unsigned long rxBlinkMillis = 0;
char nodeID0[] = "ALARM1";
char nodeID1[] = "ALARM1";
//IPAddress (10, 0, 0, 124);
IPAddress custom (10, 0, 0, 124);
unsigned long now = 0;
unsigned long masterResetMillis = 0;

bool masterConnected = true;
bool noError = true;
bool blink = false;
bool enablePsuState = 0;
unsigned long   psuMillis = 0;

struct {
  byte alarmServer_IP[4];
  byte custom_IP[4];
} _EEPROM;

struct testStruct {
  float T1 = 0;
  float T2 = 0;
  float T3 = 0;
  float T4 = 0;
  float T5 = 0;
  float mainAC = 0;
  uint32_t runtime = 0;
  uint32_t t1Cycles = 0;
  uint32_t t2Cycles = 0;
  uint32_t mainWaterFloat = 0;
  uint32_t t1U = 0;
  uint32_t t1L = 0;
  uint32_t t2U = 0;
  uint32_t t2L = 0;
  uint32_t MU = 0;
  uint32_t ML = 0;
  uint32_t RL = 0;
  uint32_t L1 = 0;
  uint32_t L2 = 0;
  uint32_t master = 1;
  uint32_t vars[39];
  //  uint8_t vars2 [29];
  float Vin = 0;
};
testStruct ts1;
char messageBuffer[sizeof(ts1)];

//#include"handlePacket.h"

void enablePSU() { //sending scpi commands
  Serial2.println(":VOLTage 18.3");
  delay(100);
  Serial2.println(":OUTPut:STATe CH1,ON");
}
void setup() {
  Serial.begin(115200);
  //  Serial2.begin(115200, SERIAL_8N1, 16, 17);
  Serial2.begin(115200);
  pinMode(25, OUTPUT); //D26
  pinMode(26, OUTPUT); //D27

  pinMode(21, OUTPUT); //
  pinMode(18, OUTPUT); //
  pinMode(19, OUTPUT); //
  pinMode(5, OUTPUT); //

  digitalWrite(5, LOW);// RX/TX
  digitalWrite(21, LOW);// WIFI
  digitalWrite(18, LOW);//MASTER
  digitalWrite(19, LOW); //ERROR
  //digitalWrite(27, HIGH);
  digitalWrite(26, LOW);
  digitalWrite(25, HIGH); // TO ARDUINO STRUCT SIGNAL
  WiFi.persistent(0);
  WiFi.mode(WIFI_STA);

  Udp.begin(4210);
  for (byte j = 0; j < 4; j++) {
    Serial.print(_EEPROM.alarmServer_IP[j]);
  }
  //update_ip(_EEPROM.custom_IP, 10, 0, 0, 124, "ALNODE");
}

void loop() {
  if (enablePsuState) {
    if (millis() -  psuMillis >= 5000) {
      enablePSU();
      enablePsuState = 0;
      psuMillis = millis();
    }
  }
  if (ts1.Vin < 15.0) {
    enablePsuState = 1;
  } else {
    enablePsuState = 1;
    psuMillis = millis();
  }
  if (blink) {
    digitalWrite(5, HIGH);// RX/TX
    if (millis() - rxBlinkMillis >= 30) {
      digitalWrite(5, LOW);// RX/TX
      blink = false;
    }
  }
  if (millis() - masterResetMillis >= 5000) {
    ts1.master = 0;
    delay(30);

    delay(30);
  }
  if (ts1.master && noError) {
    digitalWrite(19, LOW);
    digitalWrite(18, HIGH);
  } else {
    digitalWrite(19, HIGH);
    digitalWrite(18, LOW);
  }

  if (WiFi.status() == WL_CONNECTED) {
    digitalWrite(21, HIGH);
  } else {
    digitalWrite(21, LOW);
    if (millis() - ledBlinkMillis >= 250) {
      digitalWrite(21, HIGH);
      if (millis() - ledBlinkMillis >= 500) {
        ledBlinkMillis =  millis();
      }
    }
  }
  ReadData();
  recvPacket();
  pairMe();
  if (millis() - now >= 500) {
    //  Serial2.println(":VOLTage 17.3");
    // Serial2.println(":OUTPut:STATe CH1,OFF");
    //  Serial2.println(":OUTPut:STATe CH1,OFF");
    // Serial.println(WiFi.localIP());
    if (ts1.master) {
      Udp.beginPacket(custom, 4210);
      Udp.print("MASTER");
      Udp.print("MASTEt");
      Udp.endPacket();

    } else {
      sendError("ERRORMMaster Offline!!");
    }
    delay(30);

    Udp.beginPacket(custom, 4210);
    Udp.print("OPERAT");
    Udp.print("TPERAT");
    Udp.endPacket();
    now = millis();
    delay(30);
    sendStruct();

    delay(30);
    // Serial.println(ts1.runtime);
  }
}

void sendError(char message[]) {
  Udp.beginPacket(custom, 4210);
  Udp.print(message);
  //Udp.print("TPERAT");
  Udp.endPacket();
}


void ReadData() {
  if (Serial2.available() == sizeof(ts1)) {
    blink = true;
    rxBlinkMillis = millis();
    Serial2.readBytes(messageBuffer , sizeof(ts1));
    Serial2.flush();
    memcpy(&ts1, &messageBuffer, sizeof(ts1));
    Serial.println(ts1.master);
    if (ts1.master) {
      digitalWrite(18, HIGH);
      masterResetMillis = millis();
    } else
      digitalWrite(18, LOW);
  }
}



void pairMe() {
  if (WiFi.status() == WL_CONNECTED) {
    if (millis() - pairMarker >= 100) {

      IPAddress myaddress = WiFi.localIP();
      myaddress[3] = 1;
      pairCounter++;
      myaddress[3] = pairCounter;
      Udp.beginPacket(myaddress, 4220);
      Udp.print("PAIRMEEMRIAP");
      Udp.endPacket();
      if (pairCounter >= 255) {
        pairCounter = 0;
      }
      pairMarker = millis();
    }
  }
}
void _writeEEPROM () {
  EEPROM.put(addr, _EEPROM);
  EEPROM.commit();
  Serial.println("-------------eeprom write!");
}

void update_ip(byte to[], IPAddress ip, char name[]) {
  if (!(ip == to)) {
    for (byte j = 0; j < 4; j++) {
      to[j] = ip[j];
    }
    Serial.print("----------------PAIRED ");
    Serial.println(name);
    _writeEEPROM();
  } else {
    Serial.print("----------------no need to pair ");
    Serial.println(name);
  }
}
void recvPacket() {

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    char incomingPacket[1000];
    int len = Udp.read(incomingPacket, 1000);
    char verifyStart[7]; //NODEC1 plus null
    char verifyEnd[7]; //C1NODE plus null
    strncpy (verifyStart, (char*)incomingPacket, 6);//6 bytes
    strncpy (verifyEnd, (char *)incomingPacket + len - 6 , 6 );//6 bytes
    verifyStart[6] = 0; //null terminate
    verifyEnd[6] = 0; //null terminate

    Serial.println(verifyStart);
    Serial.println(verifyEnd);
    if (strcmp(verifyStart, "ALPAIR") == 0) {
      update_ip(_EEPROM.alarmServer_IP, Udp.remoteIP(), "ALNODE");
      for (byte j = 0; j < 4; j++) {
        Serial.print(_EEPROM.alarmServer_IP[j]);
      }
      //  Serial.println("larms submodule pair");
    }
  }
}

void sendStruct() {
  Udp.beginPacket(custom, 4210);
  Udp.print("ALDATA");
  Udp.write((uint8_t*)&ts1, sizeof(ts1)); //cast to bytes
  //Udp.print("DATAAL");
  Udp.endPacket();
}

To make looking at the code easier, this is the function on the esp32 that is responsible to handling incoming bytes on serial2

void ReadData() {
  if (Serial2.available() == sizeof(ts1)) {
    blink = true;
    rxBlinkMillis = millis();
    Serial2.readBytes(messageBuffer , sizeof(ts1));
    Serial2.flush();
    memcpy(&ts1, &messageBuffer, sizeof(ts1));
    Serial.println(ts1.master);
    if (ts1.master) {
      digitalWrite(18, HIGH);
      masterResetMillis = millis();
    } else
      digitalWrite(18, LOW);
  }
}

For some reason, if (Serial2.available() == sizeof(ts1)) { is never true when sending uint8_t

Okay, on the esp32 this struct is 244 bytes,

On the arduino the same struct is 241 bytes.

what is the problem?

struct testStruct {
  float T1 = 0;
  float T2 = 0;
  float T3 = 0;
  float T4 = 0;
  float T5 = 0;
  float Temp = 0;
  float RoomTemp = 0;
  float Temp0 = 0;
  float mainAC = 0;
  uint32_t runtime = 0;
  uint32_t t1Cycles = 0;
  uint32_t t2Cycles = 0;
  uint32_t Cycles = 0;
  uint32_t mainWaterFloat = 0;
  uint32_t t1U = 0;
  uint32_t t1L = 0;
  uint32_t t2U = 0;
  uint32_t t2L = 0;
  uint32_t MU = 0;
  uint32_t ML = 0;
  uint32_t RL = 0;
  uint32_t L1 = 0;
  uint32_t L2 = 0;
  uint32_t master = 1;
  uint32_t vars[35];
  uint8_t vars2 = 1;
  float Vin = 0;
};

Okay i find on esp32 when i add byte OR uint8_t to the struct and Serial.println(sizeof(ts1)); it reports the struct is 4 bytes larger "244", byte or uint8_t, without the variable the struct is 240 bytes????

In serial on esp32 sizeof says what i expect,

  Serial.print("size of bool");
  Serial.println(sizeof(bool));
  Serial.print("size of byte");
  Serial.println(sizeof(byte));
  Serial.print("size of uint8_t");
  Serial.println(sizeof(uint8_t));
  Serial.print("size of int");
  Serial.println(sizeof(int));
size of bool1
size of byte1
 size of uint8_t1
 size of int4

but when added to the struct is 4 ??

Ive read this could have something to do with alignment and the compiler inserting padding to the struct??

I figure out what to do to disable padding, to make it work

typedef struct __attribute((packed)) testStruct {
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.