Two way serial , bluepill

Hello,
I want to establish two way serial comunication between bluepill and nano , could also bluepill to bluepill, to exchange values of defined variables.
I have working stuff with UNO and nano , using struct data.
would arduino struct data libriris work on stm32 bluepill?

On A side i have stm32 conected to tft display, it uses tx rx 1 and 2 pins , so i need to use tx rx 3 pins, but i am not familiar to set up this with cube MX, also dont have enough knowledge to use HAL libraries. On B side i have nano or can use also bluepill, conected sensors to collect data.

distance is abot 12 meters so i want use serial , maybe also rs 485 converter between.
so , i need to setup tx rx 3 pins on bluepill and want use struct data ( it is eaay to do with struct data, and as i can see, it is reliable transmitting,
better than..if serial aaliable and ent , stop caracters.)

here is my code



// sending UNO to STM32 Bluepill


#include "SerialTransfer.h"


int VTmin;
int VTmax;
int ZelVlaga;
int Vzracenje;


// data to send from UNO to STM32
struct SensorData {
  float sensorValue1;
  float sensorValue2;
  float sensorValue3;
  float sensorValue4;
  int VodaAl;
};


struct TFTdata {
  int ZelVlaga;
  int VTmin;
  int VTmax;
  int Vzracenje;
};


SerialTransfer stream;
SensorData TXdata;
TFTdata RXdata;


// data to send from UNO to STM32 for testing
float sensorValue1=1.1;
float sensorValue2=2.1;
float sensorValue3=3.1;
float sensorValue4=4.1;
bool VodaAl=false;



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

}

void loop() {
  
   // Update TXdata with sensor values
  TXdata.sensorValue1 = sensorValue1;
  TXdata.sensorValue2 = sensorValue2;
  TXdata.sensorValue3 = sensorValue3;
  TXdata.sensorValue4 = sensorValue4;

 // Send data
  sndtx();

   // Receive data
  recrx();


ZelVlaga = RXdata.ZelVlaga;
VTmin=RXdata.VTmin;
VTmax=RXdata.VTmax;
Vzracenje=RXdata.Vzracenje;

// Print received sensor data
  Serial.println("Received Sensor Data:");
  Serial.print("Zeljena vlaga: ");
  Serial.println(RXdata.ZelVlaga);
  Serial.print("Minimalna temperatura ");
  Serial.println(RXdata.VTmin);
  Serial.print("Maximalna temperatura ");
  Serial.println(RXdata.VTmax);
  Serial.print("Prezracevanje ");
  Serial.println(RXdata.Vzracenje);

}


void recrx() {
  if (stream.available()) {
    stream.rxObj(RXdata);
  }
}

void sndtx() {
  stream.txObj(TXdata, sizeof(TXdata));
  stream.sendDatum(TXdata);
  Serial.flush();
}





and STM to UNO ( I use pins A9 and A10 on bluepill)


// sending STM32 to UNO
#include "SerialTransfer.h"

// variables for receiving from UNO
 float sensorValue1;
  float sensorValue2;
  float sensorValue3;
  float sensorValue4;
  int VodaAl;


// variables to send from STM32 to UNO for testing

int ZelVlaga = 1;
int VTmin = 2;
int VTmax = 3;
int Vzracenje =4;


struct SensorData {
  float sensorValue1;
  float sensorValue2;
  float sensorValue3;
  float sensorValue4;
  int VodaAl;  
};

struct TFTdata {
  int ZelVlaga;
  int VTmin;
  int VTmax;
  int Vzracenje;
};


SerialTransfer stream;
SensorData RXdata;  // Use RXdata to store received data
TFTdata TXdata;


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


void loop() {


// Receive data
  recrx();


    // Print received sensor data
  Serial.println("Received Sensor Data:");
  Serial.print("SensorValue1: ");
  Serial.println(RXdata.sensorValue1);
  Serial.print("SensorValue2: ");
  Serial.println(RXdata.sensorValue2);
  Serial.print("SensorValue3: ");
  Serial.println(RXdata.sensorValue3);
  Serial.print("SensorValue4: ");
  Serial.println(RXdata.sensorValue4);
  Serial.print("VodaAl: ");
  Serial.println(RXdata.VodaAl);

  
  
TXdata.ZelVlaga = ZelVlaga;
TXdata.VTmin = VTmin;
TXdata.VTmax = VTmax;
TXdata.Vzracenje = Vzracenje;


// Send data
  sndtx();

}



void recrx() {
  if (stream.available()) {
    stream.rxObj(RXdata);
  }
}


void sndtx() {
  stream.txObj(TXdata, sizeof(TXdata));
  stream.sendDatum(TXdata);
  Serial.flush();
}

thank you for your help
joe

Hello

You have to make sure the size of the struct is the same on both platforms. int size may be different on uno and bluepill. Use sizeof to verify.

You can specify the size of the struct member variables by using types such as int16_t , uint32_t etc, so the structs will have the same size on both platform.

And make sure the alignment and packing of the structs is also the same, by using __attribute__((packed)) (search for it).

thx, I will try.

i change to this but doesnt work, all values are 0.

~�ցReceived Sensor Data:

SensorValue1: 0.00

SensorValue2: 0.00

SensorValue3: 0.00

SensorValue4: 0.00

struct __attribute__((packed)) SensorData {
  float sensorValue1;
  float sensorValue2;
  float sensorValue3;
  float sensorValue4;
  int16_t VodaAl;
};




struct __attribute__((packed)) TFTdata {
  int16_t ZelVlaga;
  int16_t VTmin;
  int16_t VTmax;
  int16_t Vzracenje;
};

size of data on stm and uno are not the same

Size of SensorData: 18
Size of TFTdata: 8

Size of SensorData: 20
Size of TFTdata: 16

i made this working. it was problems due to delays in loop function.

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