I'm having trouble getting information from a Modbus slave running on a UNO. using Modbus Master running on a Leonardo.
Slave Code
#include <SimpleModbusSlave.h>
/*
SimpleModbusSlaveV10 supports function 3, 6 & 16.
*/
#define TxRxPin 2
#define LED 9
int i;
int j;
// Using the enum instruction allows for an easy method for adding and
// removing registers. Doing it this way saves you #defining the size
// of your slaves register array each time you want to add more registers
// and at a glimpse informs you of your slaves register layout.
//////////////// registers of your slave ///////////////////
enum
{
// just add or remove registers and your good to go...
// The first register starts at address 0
VALx00,
VALx01,
VALx02,
VALx03,
VALx04,
VALx05,
VALx06,
VALx07,
VALx08,
VALx09,
VALx0a,
VALx0b,
VALx0c,
VALx0d,
VALx0e,
VALx0f,
VALx10,
VALx11,
VALx12,
VALx13,
VALx14,
VALx15,
VALx16,
VALx17,
VALx18,
VALx19,
VALx1a,
VALx1b,
VALx1c,
VALx1d,
VALx1e,
VALx1f,
HOLDING_REGS_SIZE // leave this one
// total number of registers for function 3 and 16 share the same register array
// i.e. the same address space
};
unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
////////////////////////////////////////////////////////////
void setup()
{
/* parameters(HardwareSerial* SerialPort,
long baudrate,
unsigned char byteFormat,
unsigned char ID,
unsigned char transmit enable pin,
unsigned int holding registers size,
unsigned int* holding register array)
*/
modbus_configure(&Serial, 19200, SERIAL_8N2, 1, TxRxPin, HOLDING_REGS_SIZE, holdingRegs);
// modbus_update_comms(baud, byteFormat, id) is not needed but allows for easy update of the
// port variables and slave id dynamically in any function.
modbus_update_comms(19200, SERIAL_8N2, 1);
pinMode(LED, OUTPUT);
//set dummy values
holdingRegs[VALx00] = 0;
holdingRegs[VALx01] = 0x01;
holdingRegs[VALx02] = 0x02;
holdingRegs[VALx03] = 0x03;
holdingRegs[VALx04] = 0x04;
holdingRegs[VALx05] = 0x05;
holdingRegs[VALx06] = 0x06;
holdingRegs[VALx07] = 0x07;
holdingRegs[VALx08] = 0x08;
holdingRegs[VALx09] = 0x09;
holdingRegs[VALx0a] = 0x0a;
holdingRegs[VALx0b] = 0x0b;
holdingRegs[VALx0c] = 0x0c;
holdingRegs[VALx0d] = 0x0d;
holdingRegs[VALx0e] = 0x0e;
holdingRegs[VALx0f] = 0x0f;
holdingRegs[VALx10] = 0x10;
holdingRegs[VALx11] = 0x11;
holdingRegs[VALx12] = 0x12;
holdingRegs[VALx13] = 0x13;
holdingRegs[VALx14] = 0x14;
holdingRegs[VALx15] = 0x15;
holdingRegs[VALx16] = 0x16;
holdingRegs[VALx17] = 0x17;
holdingRegs[VALx18] = 0x18;
holdingRegs[VALx19] = 0x19;
holdingRegs[VALx1a] = 0x1a;
holdingRegs[VALx1b] = 0x1b;
holdingRegs[VALx1c] = 0x1c;
holdingRegs[VALx1d] = 0x1d;
holdingRegs[VALx1e] = 0x1e;
holdingRegs[VALx1f] = 0x1f;
}
void loop()
{
//make up some data which will slowly vary the output of the LEDs in the slave and master
i++;
if (i >254) {
i=0;
j++;
if (j>254) j=0;
}
holdingRegs[VALx00] = j*j; // update data to be read by the master to adjust the PWM
holdingRegs[VALx01] = j;
// modbus_update() is the only method used in loop(). It returns the total error
// count since the slave started. You don't have to use it but it's useful
// for fault finding by the modbus master.
modbus_update();
analogWrite(LED, j);
}
Master Code
/*
RS485_HalfDuplex.pde - example using ModbusMaster library to communicate
with EPSolar LS2024B controller using a half-duplex RS485 transceiver.
This example is tested against an EPSolar LS2024B solar charge controller.
See here for protocol specs:
http://www.solar-elektro.cz/data/dokumenty/1733_modbus_protocol.pdf
Library:: ModbusMaster
Author:: Marius Kintel <marius at kintel dot net>
Copyright:: 2009-2016 Doc Walker
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <ModbusMaster.h>
/*!
We're using a MAX485-compatible RS485 Transceiver.
Rx/Tx is hooked up to the hardware serial port at 'Serial'.
The Data Enable and Receiver Enable pins are hooked up as follows:
*/
#define MAX485_DE 7
// instantiate ModbusMaster object
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_DE, 0);
// Modbus communication runs at 115200 baud
Serial.begin(9600);
// while the serial stream is not open, do nothing:
while (!Serial) ;
Serial1.begin(19200);
// Modbus slave ID 1
node.begin(1, Serial1);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
bool state = true;
void loop()
{
uint8_t result;
// Read 16 registers starting at 0x0000)
result = node.readInputRegisters(0x0000, 16);
Serial.print("x00: ");
Serial.println(node.getResponseBuffer(0x00));
Serial.print("x01: ");
Serial.println(node.getResponseBuffer(0x01));
Serial.print("x02: ");
Serial.println(node.getResponseBuffer(0x02));
Serial.println(result,HEX);
Serial.println(node.ku8MBSuccess);
if (result == node.ku8MBSuccess)
{
Serial.print("Vbatt: ");
Serial.println(node.getResponseBuffer(0x04)/100.0f);
Serial.print("Vload: ");
Serial.println(node.getResponseBuffer(0xC0)/100.0f);
Serial.print("Pload: ");
Serial.println((node.getResponseBuffer(0x0D) +
node.getResponseBuffer(0x0E) << 16)/100.0f);
}
delay(500);
}
As you can see these are both fairly simple modified versions of the examples in the available libraries.
The reason for using ModbusMaster rather than the Simple Modbus Master is that ultimately ther will be another device on the bus and I will need to sensibly and individually address some 16 registers in the 0x2000 address space on that device.
The Uno and the Leonardo are wired together using simple MAX485 chips on commercially available 485 boards. A to A, B to B and DO/DI to the arduinos Rx/Tx pins.
this is what is coming out on the Leonardo's Serial monitor
"17:56:33.925 -> x00: 0
17:56:33.925 -> x01: 0
17:56:33.925 -> x02: 0
17:56:33.925 -> E2
17:56:33.925 -> 0
17:56:36.397 -> x00: 0
17:56:36.397 -> x01: 0
17:56:36.397 -> x02: 0
17:56:36.397 -> E2
17:56:36.397 -> 0"
Sometimes I also get the E0 and E1 error codes.