Trying to transfer byte array using ESP32-C3 Serial1

Hello, all,

I have a problem with delivering byte data over RS485 channel:

The hardware is costructed from two identical self-designed PCBs that contain ESP32 and SP3485. Ther ESP32 uses D6 as Tx, D7 as Rx (there is a voltage divider to reduce from 5V to 3.3V levels)) and D9 as DE/!RE. The target, after finishing the testing and integration, is to communicate with meteorological sensors, so the protocol is mandatory. I tried to terminate the RS485 lines but the low transmission rate (9600 baud) and the short distance (5cm – 2") implied that they are not needed.

The transmitter code was taken from an example:

bool tx485 (const uint8_t *buf, uint8_t len){

digitalWrite(rtsPin, true); // Enable transmitter, disable receiver

delayMicroseconds(_turnaround); // Allow the hardware RS485 transciever to stabilize

uint8_t txDataSize = len;

Serial1.write(buf, len); // Send the reading command

Serial1.flush();

digitalWrite(rtsPin, false); // Disable transmitter, Enable receiver

delayMicroseconds(_turnaround); // Allow the hardware RS485 transciever to stabilize

return (txDataSize == len);

}

Where buf is 8 bytes long and rs485txDir is boolean high. The transmitted data is { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xc4, 0x0b }. Using an osciloscope I verified that all 8 character are sent correctly.

The receiver code:

bool rx485 (char *buf, uint8_t lenLimit){

uint8_t rxPtr = 0;

while (Serial1.available() > 0){

***if(rxPtr < lenLimit){***

  ***buf\[rxPtr++\] = Serial1.read();      // Read the next character***

  ***rxPtr++;***

***}***

***else Serial1.read();  // "waist" extra characters, if any.***

}

return(lenLimit == rxPtr);

}

The receiver gets only 2 bytes instead of 8; all received bytes are positions 2 and 0. However, it looks like the receivber doesn’t synchronize on the package start.

I tried to add delays, split both sides into 1-byte messages and some other desparate ideas. Any hint will be warmly acdepted.

Regards

Yona