I am trying to send data (test integer) to S2100 and received on LoRa
This is program :
/* ESP32-C3 -> RS485 (MAX3485/MAX485) XIAO_RS485_Expansion_Board
https://files.seeedstudio.com/wiki/rs485_ExpansionBoard/Seeed_Studio_XIAO_RS485_Expansion_Board.pdf
— RS485 DI <- ESP32C3 TX
— RS485 RO -> ESP32C3 RX
— RS485 DE+RE -> ESP32C3 TXEN (SAME GPIO)
Alim: 3V3/5V.
Protocol: Modbus RTU SLAVE
Address slave: 1
Baud: 9600 8N1
Register Holding 0x0000 : integer no sign 0..100
*/
#include <ModbusRTU.h>
#define RS485_TX_PIN 4 // (XIAO ESP32-C3: D4)
#define RS485_RX_PIN 5 // (XIAO ESP32-C3: D5)
#define RS485_TXEN_PIN 2 // DE/RE same GPIO D2
// id Modbus
static const uint8_t SLAVE_ID = 1;
static const uint32_t BAUD = 9600;
// Registers
static const uint16_t HREG_BIKE_COUNT = 0x0000; // value 0..100
HardwareSerial RS485(1);
ModbusRTU mb;
// value from to 0..100
uint16_t clamp100(int v) {
if (v < 0) return 0;
if (v > 100) return 100;
return (uint16_t)v;
}
void setup() {
Serial.begin(115200);
delay(500);
pinMode(RS485_TXEN_PIN, OUTPUT);
digitalWrite(RS485_TXEN_PIN, LOW);
RS485.begin(BAUD, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN);
mb.begin(&RS485, RS485_TXEN_PIN); // control DE/RE auto
mb.setBaudrate(BAUD);
mb.slave(SLAVE_ID);
// define (Holding Register) 0x0000 with initiale value 0
mb.addHreg(HREG_BIKE_COUNT, 0);
Serial.println(F("Modbus RTU slave ready."));
Serial.println(F("the S2100 (master RS485) must read HREG 0x0000 (fonction 03)."));
}
void loop() {
// Service Modbus (response to S2100's queries )
mb.task();
int v = random(100);
uint16_t bounded = clamp100(v);
mb.Hreg(HREG_BIKE_COUNT, bounded);
Serial.print(F("New value (0..100) = "));
Serial.println(bounded);
delay(60*1000);
}
The S2100 config follow