#include <HardwareSerial.h>
#define TXPIN 2
HardwareSerial RS485(TXPIN);
void setup() {
Serial.begin(115200);
RS485.begin(9600, SERIAL_8N1);
}
void loop() {
uint8_t header = 0xFF; // byte 0
//set different address
uint8_t address1 = 0xFA; // byte 1
uint8_t address2 = 0xFB;
uint8_t address3 = 0xFC;
uint8_t address4 = 0xFD;
uint8_t byte2= 0x10;
uint8_t byte3= 0x12;
uint8_t byte4= 0x34;
uint8_t byte5= 0x56;
uint8_t byte6= 0x00;
uint8_t checksum1= address1 + byte2 + byte3 + byte4 + byte5 + byte6;
uint8_t checksum2 = address2 + byte2 + byte3 + byte4 + byte5 + byte6;
uint8_t checksum3 = address3 + byte2 + byte3 + byte4 + byte5 + byte6;
uint8_t checksum4 = address4 + byte2 + byte3 + byte4 + byte5 + byte6;
uint8_t data1[] = {header, address1, byte2, byte3, byte4, byte5, byte6, checksum1};
uint8_t data2[] = {header, address2, byte2, byte3, byte4, byte5, byte6, checksum2};
uint8_t data3[] = {header, address3, byte2, byte3, byte4, byte5, byte6, checksum3};
uint8_t data4[] = {header, address4, byte2, byte3, byte4, byte5, byte6, checksum4};
RS485.write(data1, sizeof(data1));
RS485.flush(); // Wait for all bytes to be sent
RS485.write(data2, sizeof(data2));
RS485.flush(); // Wait for all bytes to be sent
RS485.write(data3, sizeof(data3));
RS485.flush(); // Wait for all bytes to be sent
RS485.write(data4, sizeof(data4));
RS485.flush(); // Wait for all bytes to be sent
delay(1000); // wait for 1 second before sending the next batch
}
Is this correct? I wanted to try first doing it like sending 1 data at a time but I also wanted to send 32bytes in one go just to check if maybe it will work that way.
Will this second option of code might work too?
void setup() {
Serial.begin(115200);
Serial1.begin(9600, SERIAL_8N1, GPIO_NUM_2); // using UART1 on pin 2 only
}
uint8_t checksum1= address1 + byte2 + byte3 + byte4 + byte5 + byte6;
uint8_t checksum2 = address2 + byte2 + byte3 + byte4 + byte5 + byte6;
uint8_t checksum3 = address3 + byte2 + byte3 + byte4 + byte5 + byte6;
uint8_t checksum4 = address4 + byte2 + byte3 + byte4 + byte5 + byte6;
void loop() {
uint8_t data[32] = {
0xFF, 0xFA, 0x10, 0x12, 0x34, 0x56, 0x00, checksum1,
0xFF, 0xFB, 0x10, 0x12, 0x34, 0x56, 0x00, checksum2,
0xFF, 0xFC, 0x10, 0x12, 0x34, 0x56, 0x00, checksum3,
0xFF, 0xFD, 0x10, 0x12, 0x34, 0x56, 0x00, checksum4,
};
Serial1.write(data, sizeof(data));
Serial1.flush(); // wait until all data has been sent
delay(100); // wait for 100 millisecond before sending the next batch
}
I will be using Serial1 because I have read it from here Reference — ESP8266 Arduino Core 3.1.2 documentation