Hello There
im using max485 and Arduino ATmega2560 and a battery BMS board connected to a huge battery(TP-BMS48100-LT-41_V1)
the A and B come from Rs485 socket on BMS board from a lan cable , unwired and connected to A,B on MAX485
the DE and RE together on breadboard together and wired them to pin pwm 3 now (tried on digital pin, sepeprated them from each and connected them to digital pin and it didnt work, just like now)
connected the Ro to Rx and Di to Tx0(tried Rx1 and Tx1 and 2 did not work)
gnd to gnd on arduino and Vcc to 5v this mmax485 module is receiving data from battery bms which i tried with a usb to rs485 converter(UT-890A USB to RS485 RS422 Converter & USB 2.0 - UOTEK) and showed me data in modboss app but now my arduino does not read any data at all
this is the code
#include <ModbusMaster.h>
// Define RS-485 control pin
#define DE_RE_PIN 3 // DE/RE pin connected to pin 3 (PWM)
// Create ModbusMaster object
ModbusMaster node;
// RS-485 Transmission Control
void preTransmission() {
digitalWrite(DE_RE_PIN, HIGH); // Enable transmission
delayMicroseconds(100); // Allow time for RS-485 transceiver to settle
Serial.println("DE/RE Pin: HIGH (Transmit Mode)");
}
void postTransmission() {
delayMicroseconds(100); // Allow time for RS-485 transceiver to settle
digitalWrite(DE_RE_PIN, LOW); // Enable reception
Serial.println("DE/RE Pin: LOW (Receive Mode)");
}
// Function to check if the RS-485 device is connected
bool isDeviceConnected() {
// Attempt to read a single register (e.g., register 4096)
uint8_t result = node.readInputRegisters(4096, 1);
if (result == node.ku8MBSuccess) {
Serial.println("Device is connected and responding.");
return true;
} else {
Serial.print("Device not responding. Modbus error: 0x");
if (result < 16) Serial.print("0"); // Pad single-digit hex values with a leading zero
Serial.println(result, HEX);
return false;
}
}
void setup() {
pinMode(DE_RE_PIN, OUTPUT);
digitalWrite(DE_RE_PIN, LOW); // Set to receive mode
Serial.begin(9600); // Baud rate for RS-485 (using hardware serial)
// Initialize Modbus communication
node.begin(1, Serial); // Slave ID = 1
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
Serial.println("Modbus Communication Initialized on Serial (RX0/TX0)");
// Check if the device is connected
if (!isDeviceConnected()) {
Serial.println("Error: No device connected or device not responding.");
while (1); // Halt the program if no device is connected
}
}
void loop() {
uint8_t result;
uint16_t data[15];
// Read 15 registers from address 4096
result = node.readInputRegisters(4096, 15);
if (result == node.ku8MBSuccess) {
Serial.println("Modbus data received:");
Serial.println("Raw Payload (Decimal):");
for (int i = 0; i < 15; i++) {
data[i] = node.getResponseBuffer(i);
Serial.print(data[i]);
if (i < 14) Serial.print(", ");
}
Serial.println();
// Decoded values
Serial.print("Pack Voltage (V): "); Serial.println(data[0] / 100.0, 2);
Serial.print("Current Pack (A): "); Serial.println(data[1] / 100.0, 2);
Serial.print("Remaining Capacity (Ah): "); Serial.println(data[2] / 100.0, 2);
Serial.print("Average Cell Temp (C): "); Serial.println(data[3] / 10.0, 1);
Serial.print("Environment Temp (C): "); Serial.println(data[4] / 10.0, 1);
Serial.print("SOC (%): "); Serial.println(data[8] / 10.0, 1);
Serial.print("SOH (%): "); Serial.println(data[9] / 10.0, 1);
Serial.print("Full Charge Capacity (Ah): "); Serial.println(data[10] / 100.0, 2);
Serial.print("Cycle Count: "); Serial.println(data[11]);
Serial.print("Max Charging Current (A): "); Serial.println(data[12] / 100.0, 2);
Serial.print("Max Cell Voltage (V): "); Serial.println(data[13] / 1000.0, 3);
Serial.print("Min Cell Voltage (V): "); Serial.println(data[14] / 1000.0, 3);
Serial.println("-------------------------");
} else {
Serial.print("Modbus error: 0x");
if (result < 16) Serial.print("0"); // Pad single-digit hex values with a leading zero
Serial.println(result, HEX);
// Print additional debugging information
Serial.print("Last error: ");
Serial.println(node.getResponseBuffer(0), HEX);
// Retry mechanism
static uint8_t retryCount = 0;
retryCount++;
if (retryCount >= 3) {
Serial.println("Max retries reached. Halting program.");
while (1); // Halt the program after max retries
} else {
Serial.println("Retrying...");
delay(1000); // Wait 1 second before retrying
return; // Skip the rest of the loop and retry
}
}
delay(500); // Wait 500 ms before next poll
}```