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!!!