Need Help for Sending Structs with HC-12 Modules

Hello, I'm building a custom fixed-wing UAV and I couldn't figure out data transmission with structs. When I try to send something that changes over time or not, I just get a corrupted value on the other side. Any ideas to fix that issue? I've tried so many things but none of them worked.

Corrupted lines looks like this in serial monitor:
data 168 235 24 249 68 0 0
data 31 0 0 0 255 168 235

Thanks.`

Sender Code

#include <SoftwareSerial.h>
#define RXD PC11
#define TXD PC10

SoftwareSerial HC12B = SoftwareSerial(RXD, TXD);

struct RxStruct {
  uint8_t aileronData;
  uint8_t elevatorData;
  uint8_t throttle;
  uint8_t yaw;
  uint8_t flaps;
  bool navLights;
  bool uavTxE;
  byte padding[10];
};
RxStruct rxData ={ 168, 235, 24, 249, 68, 0, 0 }; // fixed values for testing purposes




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




void loop() {

 HC12B.write((byte *)&rxData, rxDataLen);

 delay(10);
}

Reciever

#define RXD2 16  //Connect to the TX pin of the HC-12
#define TXD2 17  //Connect to the RX pin of the HC-12
#define HC12E Serial2

struct RxStruct {
  uint8_t aileronData;
  uint8_t elevatorData;
  uint8_t throttle;
  uint8_t yaw;
  uint8_t flaps;
  bool navLights;
  bool uavTxE;
  byte padding[10];
};
RxStruct rxData;


void setup() {
  HC12E.begin(9600);
  Serial.begin(115200);

  delay(4000);
  Serial.println("serial active");
}



void loop() {

  if (HC12E.available()) {
    HC12E.readBytes((byte*)&rxData, sizeof(rxData));

    Serial.print("data   ");
   
    Serial.print(' ');
    Serial.print(rxData.aileronData);
    Serial.print(' ');
    Serial.print(rxData.elevatorData);
    Serial.print(' ');
    Serial.print(rxData.throttle);
    Serial.print(' ');
    Serial.print(rxData.yaw);
    Serial.print(' ');
    Serial.print(rxData.flaps);
    Serial.print(' ');
    Serial.print(rxData.navLights);
    Serial.print(' ');
    Serial.println(rxData.uavTxE);
  }
}

Which Arduino board are you using and which pins are being used for SoftwareSerial ?

Please post your full sketches rather than just part of them

The problem with this approach is that there is no way to synchronize (determine the start and end of) the data packets.

void loop() {

 HC12B.write((byte *)&rxData, rxDataLen);

 delay(10);
}

At a serial Baud rate of 9600, it takes 1 millisecond to transmit a single character, so the delay(10) is useless.

Full sketches have been uploaded. On the receiver side, an ESP32 Wroom DA module is used and configured at 80MHz cpu frequency, while the sender is an STM32F103, which is a Nucleo64 board. However, my problems still occur despite using the same microcontroller architecture. I tested both of the HC12's with an Arduino Uno and moved on to those boards due to lack of peripherals.

I'm currently trying random approaches in hopes of finding a solution, but so far, nothing seems to work. I've also attempted various handshake protocols to address the synchronization issue, but none of them have been successful. Do you have any ideas on how to overcome this?

I would suggest to study Serial Input Basics to handle this

adding a start or end marker would be useful too (but more complicated in general binary transmissions)

also given the heterogeneous platforms you should pack and align your structure.

1 Like

+1 for Serial Input Basics, send the data in text form with start and end markers.

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