Hello,
I actually I want to Communicate my Teensy 4.1 with Modbus RS485 using MAX 485 Chip. I want this System to work as a Slave in my master network on MODBUS RS485/RTU.
So can any one help me with it like this is the code I am burning on my teensy device.
I actually I want to Communicate my Teensy 4.1 with Modbus RS485 using MAX 485 Chip. I want this System to work as a Slave in my master network on MODBUS RS485/RTU.
So can any one help me with it like this is the code I am burning on my teensy device.
Code:
#include <ModbusRTUSlave.h>
// Modbus Configuration
bool coils[1];
uint16_t holdingRegisters[9];
int Slave_ID = 10;
const uint8_t dePin = 19;
ModbusRTUSlave modbus(Serial5, dePin); // ModbusRTUSlave object
void setup() {
Serial.begin(9600);
Serial.println("-------------------------------------------------SETUP Started ------------------------------------------------- ");
Serial5.begin(9600);
// Pin configurations
pinMode(LED_BUILTIN, OUTPUT);
// Modbus initialization
modbus.configureHoldingRegisters(holdingRegisters, 14);
modbus.configureCoils(coils, 1);
modbus.begin(Slave_ID, 9600);
}
void loop() {
Serial.println("-------------------------------------------------LOOP Started ------------------------------------------------- ");
modbus.poll(); // Poll Modbus communication
// Control LED based on coil value
digitalWrite(LED_BUILTIN, coils[0] ? LOW : HIGH);
// Update holding registers
holdingRegisters[0] = 7;
holdingRegisters[1] = 12;
holdingRegisters[2] = 8;
holdingRegisters[3] = 85;
holdingRegisters[4] = 60;
holdingRegisters[5] = 25;
holdingRegisters[6] = 45;
holdingRegisters[7] = 50;
holdingRegisters[8] = 80;
delay(1000);
Serial.flush();
}