RS-485 and MODBUS Master

Hi folks,

I am trying to communicate with a battery charge controller via RS-485 ModBus RTU half-duplex communication. I am using an Argon and this RS-485 transciever. The charge controller is powered separately so only the A,B, and GND lines are connected.

Does anyone experience using this transceiver or debugging the RS-485 bus?

The firmware I am running is below. It is compiled for the Argon on device 5.8

#include "Particle.h"

SYSTEM_MODE(SEMI_AUTOMATIC); 
SYSTEM_THREAD(ENABLED);

//instantiate ModbusMaster object as slave ID 1
ModbusMaster node(1);
unsigned long interval = 0;
unsigned long base_T = 1000;
const int addr = 0x30A0;

void idle() {
    delay(10); // in case slave only replies after 10ms
}

void setup() {
	// initialize Modbus communication baud rate
	node.begin(9600);
	node.enableTXpin(D4); //D7 is the pin used to control the TX enable pin of RS485 driver
	node.enableDebug();  //Print TX and RX frames out on Serial. Beware, enabling this messes up the timings for RS485 Transactions, causing them to fail.
	node.idle(idle);      //function gets called in the idle time between transmission of data and response from slave

	Serial.begin(9600);
	// while(!Serial.available()) Particle.process();
	Serial.println("Starting Modbus Transaction:");
}


void loop() {
	uint8_t j, result;
	uint16_t data[10];

        if (millis() - interval > base_T) {
                interval = millis();
		result = node.readInputRegisters(addr,1);
    
	
	// do something with data if read is successful
	if (result == node.ku8MBSuccess) {
		Serial.print("Success, Received data: ");
		for (j = 0; j < 2; j++) {
			data[j] = node.getResponseBuffer(j);
			Serial.print(data[j], HEX);
			Serial.print(" ");
		}
		Serial.println("");
        base_T = 1000;
	} else {
		Serial.print("Failed, Response Code: ");
		Serial.print(result);
		Serial.print(" - ");
		Serial.print(result, HEX); 
		Serial.println("");
		base_T = 5000; //if failed, wait for bit longer, before retrying!
	}
    }
}
type or paste code here

What issue are you encountering with this?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.