SerialTransfer Library, Nano>>>ESP32, can send char but can't or dont know how to send int or float

Hi all, Long time reader but never posted. I can normally find my answer from others that have posted.

I am trying to communicate between a Nano and an esp32 div board using a pair of max485 modules. Tx with the Nano, and Rx with the ESP32

I can draw up a schematic if it is necessary

I am using software serial on the Nano and serial2 on the esp32.

I have read through Serial Input Basics and Serial input Advanced articles. some of it makes sense to me... I will admit some of it is over my head...

I have tried multiple code from different examples and suggestions from other post given by @Power_Broker and others, but still run into issues when trying to read anything but char.

I have tried the example data code sending a char and a float and only receive $,0

Here is my Tx code:

#include "SerialTransfer.h"
SerialTransfer myTransfer;

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);  // RX, TX

struct __attribute__((packed)) STRUCT {  //added __attribute__((packed)) 
  char a;
  char b;
  char c;
  char d;
  char e;
  char f;
} testStruct;

char arr[] = "hello";


void setup() {
  Serial.begin(115200);
  mySerial.begin(115200);
  myTransfer.begin(mySerial);

  testStruct.a = 'a';
  testStruct.b = 'b';
  testStruct.c = 'c';
  testStruct.d = 'd';
  testStruct.e = 'e';
  testStruct.f = 'f';
  Serial.println(sizeof(testStruct));
}


void loop() {
  // use this variable to keep track of how many
  // bytes we're stuffing in the transmit buffer
  uint16_t sendSize = 0;

  ///////////////////////////////////////// Stuff buffer with struct
  sendSize = myTransfer.txObj(testStruct, sendSize);

  ///////////////////////////////////////// Stuff buffer with array
  sendSize = myTransfer.txObj(arr, sendSize);

  ///////////////////////////////////////// Send buffer
  myTransfer.sendData(sendSize);
  Serial.println(sizeof(testStruct));  //code pulled from the form
  delay(500);
}

here is my Rx code:

#include "SerialTransfer.h"


SerialTransfer myTransfer;

struct __attribute__((packed)) STRUCT { //added __attribute__((packed)) 
  char a;
  char b;
  char c;
  char d;
  char e;
  char f;
} testStruct;

char arr[6];


void setup() {
  Serial.begin(115200);
  Serial2.begin(115200);
  myTransfer.begin(Serial2);
  }


void loop() {
  if (myTransfer.available()) {
    // use this variable to keep track of how many
    // bytes we've processed from the receive buffer
    uint16_t recSize = 0;

    recSize = myTransfer.rxObj(testStruct, recSize);
    Serial.print(testStruct.a);
    Serial.print(testStruct.b);
    Serial.print(testStruct.c);
    Serial.print(testStruct.d);
    Serial.print(testStruct.e);
    Serial.print(testStruct.f);
    Serial.print(" | ");

    recSize = myTransfer.rxObj(arr, recSize);
    Serial.println(arr);
    Serial.println(sizeof(testStruct)); //code pulled from the forum
    //delay(1000);
  }
}

right now the serial data out of esp32 reads

14:46:56.516 -> abcde3 | hello
14:46:56.516 -> 6
it was reading abcdef | hello , but i have been messing with code changing the value of (char f) to a number...so not sure whats up with that.

I added this line { Serial.println(sizeof(testStruct)); } because of a post @gfvalvo made in another topic....Both the Nano and the esp32 give a response of 6.

I added this line to the code { attribute((packed)) } because of a comment @guix made in another topic... it was not in original example code.(it did not seem to make a difference.)

**** if i change to char f; to a int f; in the nano (tx) code and make testStruct.f = 4;

and change char f; to int f; in the rx code....

i get on the serial monitor of ESP32 (Rx)

14:56:32.442 -> abcde1701314564 | llo
14:56:32.442 -> 9

So i get a 9 in responce to Serial.println(sizeof(testStruct)); on the ESP32(rx)
and i get a 7 on the nano serial monitor for the Serial.println(sizeof(testStruct));
So the testStruct is different on the tx and rx... but i don't know what that means, or how to fix it...if that is even related to my problem.

all I really want to do is take the data from six sensors (all int's or maybe floats with a value from 0 to 2000) and transmit them to my exp32 and display them on a Oled.

The serial communication is the last piece of the puzzle....once I figure this out I can put all my code together. my two boards are 10-15' apart and my research tells me I can't/shouldn't use ISP or I2C (well some claim they can but it seems it is not recommended )
I would really like to learn how to use serial communication anyway for future projects. SerialTransfer seams to be a really simplistic solution. I really want it to work... just need help figuring out what I'm doing wrong.
Thanks in advance!!!

The objects that you transfer (whether they're ints, structs, classes, strings, etc) should always exactly match between transmitter and receiver. If you change something on one side, you need to apply the same changes to the other.

This is a rather odd request on this forum, but try using test sketches that are less like the lib examples and more like what you'd actually want in your project. Most likely, you can fit everything you need into a single struct with member variables with the desired types and not have to worry about the array. This actually simplifies things a lot and will be easier to debug.

If that doesn't work, post the updated TX and RX sketches along with the debug printouts and we can go from there.

1 Like

Welcome

int is 2 bytes on arduino nano, and 4 bytes on esp32, hence why you see 7 and 9

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

1 Like

Thank you both for your quick reply!!!
I could have solved this 4 days ago by asking, but I like to try and figure things out on my own... I retain them better.

That's it! I knew it was something simple. "int16_t" It works!!!!

I will post some code soon, hopefully it will help others working with the same platforms.... I was tempted to switch to a pair of Nanos just to proof concept... It would have worked... but that still wouldn't have helped me after changing back to the ESP32. (this is my first Project with ESP32... always learning!!)

Thank you!!!!

1 Like

Just wanted to post some working code that might help out the next person.

Tx Code:

#include "SerialTransfer.h"
SerialTransfer myTransfer;

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);  // RX, TX

struct __attribute__((packed)) STRUCT {
  int16_t temp1;
  float temp2;
  int16_t temp3;
  float pressure1;
  int16_t pressure2;
  int16_t pressure3;
} testStruct;



void setup() {
  Serial.begin(115200);
  mySerial.begin(115200);
  myTransfer.begin(mySerial);

//test values for code...these will be replaced with sensor data
  testStruct.temp1 = 212;
  testStruct.temp2 = 2.254;
  testStruct.temp3 = 400;
  testStruct.pressure1 = 4.5;
  testStruct.pressure2 = 45;
  testStruct.pressure3 = 12345;
}


void loop() {
  // use this variable to keep track of how many
  // bytes we're stuffing in the transmit buffer
  uint16_t sendSize = 0;

  ///////////////////////////////////////// Stuff buffer with struct
  sendSize = myTransfer.txObj(testStruct, sendSize);

  ///////////////////////////////////////// Send buffer
  myTransfer.sendData(sendSize);
  Serial.println(sizeof(testStruct));   //code from forum,used to check size of testStruct...can be deleated by user if not needed
  delay(500);
}

Rx Code:

#include "SerialTransfer.h"


SerialTransfer myTransfer;

struct __attribute__((packed)) STRUCT {
  int16_t temp1;
  float temp2;
  int16_t temp3;
  float pressure1;
  int16_t pressure2;
  int16_t pressure3;
} testStruct;

void setup() {
  Serial.begin(115200);
  Serial2.begin(115200);
  myTransfer.begin(Serial2);
}


void loop() {
  if (myTransfer.available()) {
    // use this variable to keep track of how many
    // bytes we've processed from the receive buffer
    uint16_t recSize = 0;

    recSize = myTransfer.rxObj(testStruct, recSize);
    Serial.print(testStruct.temp1);
    Serial.print(" | ");
    Serial.print(testStruct.temp2);
    Serial.print(" | ");
    Serial.print(testStruct.temp3);
    Serial.print(" | ");
    Serial.print(testStruct.pressure1);
    Serial.print(" | ");
    Serial.print(testStruct.pressure2);
    Serial.print(" | ");
    Serial.print(testStruct.pressure3);
    Serial.print(" | ");

    Serial.println(sizeof(testStruct)); //code from forum,used to check size of testStruct...needs to be the same size as Tx testStruct
    //delay(1000);
  }
}

This is what comes out of serial monitor on the Rx side:

08:38:50.064 -> 212 | 2.25 | 400 | 4.50 | 45 | 12345 | 16

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