xreef / LoRa_E32_Series_Library

Arduino LoRa EBYTE E32 device library complete and tested with Arduino, esp8266, esp32, STM32 and Raspberry Pi Pico (rp2040 boards). sx1278/sx1276
https://www.mischianti.org
Other
355 stars 73 forks source link

Problem getting structure #68

Closed Oleg-Perevyshin closed 1 month ago

Oleg-Perevyshin commented 1 month ago

Hello! I have a transmitter that needs to transmit a structure

void Task_LoRaSender(void) {
    struct LoraData {
      char type[3] = "ND";
      uint8_t dev_id = config.DevID;
      int32_t latitude = gnss_latitude;
      int32_t longitude = gnss_longitude;
      int32_t altitude = gnss_altitude;
      uint8_t crc8;
    } lora_data;
    lora_data.crc8 = get_crc_8((const char *)&lora_data, sizeof(lora_data) - sizeof(lora_data.crc8));

    ResponseStatus response_status = LORA_E32.sendFixedMessage(BASE_ADDR_H, BASE_ADDR_L, BASE_RF_CHANNEL, &lora_data, sizeof(lora_data));

    char buffer_navigation_data[256];
    snprintf(buffer_navigation_data, sizeof(buffer_navigation_data),
             "Type: %s, ID: %d | LTD: %ld, LNG: %ld, ALT: %ld | CRC: 0x%02X",
             lora_data.type, lora_data.dev_id,
             lora_data.latitude, lora_data.longitude, lora_data.altitude,
             lora_data.crc8);
    SEND_DEBUG_MSG(buffer_navigation_data);
}

In the serial port I see (Its Ok): Type: ND, ID: 3 | LTD: 59916757, LNG: 33590709, ALT: 127690 | CRC: 0xE0

I am successfully sending data over the air.

=================================

On the receiving side my code is:

struct LoRaNavData {
  char type[3]; 
  uint8_t dev_id;
  int32_t latitude;
  int32_t longitude;
  int32_t altitude;
  uint8_t crc8;
};

void Task_LoRa(void) {
  if (LORA_E32.available() > 1) {
    char type[3];
    ResponseContainer rs = LORA_E32.receiveInitialMessage(sizeof(type));

    String type_str = rs.data;
    if (type_str == "ND") {
      SEND_DEBUG_MSG("Navigation Data");
      ResponseStructContainer rsc = LORA_E32.receiveMessage(sizeof(LoRaNavData));
      struct LoRaNavData nav_data = *(LoRaNavData *)rsc.data;

      char buffer_navigation_data[256];
      snprintf(buffer_navigation_data, sizeof(buffer_navigation_data),
               "Type: %s, ID: %d | LTD: %ld, LNG: %ld, ALT: %ld | CRC: 0x%02X",
               type_str, nav_data.dev_id,
               nav_data.latitude, nav_data.longitude, nav_data.altitude,
               nav_data.crc8);
      SEND_DEBUG_MSG(buffer_navigation_data);
      rsc.close();
    } else {
      Serial.println("Something Goes Wrong!");
    }
  }
}

In the serial port I see something like this (not what I expected): Type: ND, ID: 54 | LTD: -1526807549, LNG: 57776641, ALT: 0 | CRC: 0x00

I did everything according to the example, the structures are the same, but I get something completely different from what I send.

How to correctly receive structures?

Oleg-Perevyshin commented 1 month ago

Need Add pragma

#pragma pack(push, 1)
  struct LoRaNavData {
    char type[3];  // delete (used receiveInitialMessage)
    uint8_t dev_id;
    int32_t latitude;
    int32_t longitude;
    int32_t altitude;
    uint8_t crc8;
  };
#pragma pack(pop)