Has anybody gotten a RS485 click to work with Modbus?
is there any sample code?
What is another good RS485 board?
I'm trying to read a Banner GPS50M.
Any help would be appreciated.
This is some code that I've tried.
// This #include statement was automatically added by the Particle IDE.
#include <ModbusMaster.h>
#include "Particle.h"
SYSTEM_THREAD(ENABLED);
//instantiate ModbusMaster object as slave ID 1
ModbusMaster node(1);
unsigned long interval = 0;
unsigned long base_T = 10000;
void idle() {
delay(10); // in case slave only replies after 10ms
Particle.process(); // avoids letting the connection close if open
}
void setup() {
// initialize Modbus communication baud rate
node.begin(19200);
node.enableTXpin(D7); //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(19200);
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.readHoldingRegisters(0x65,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(" ");
Particle.publish("Modbus", String(data[j], HEX), PRIVATE);
}
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!
}
}
}