Using Modbus Master & Simple Modbus slave

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.

  modbus_configure(&Serial, 19200, SERIAL_8N2, 1, TxRxPin, HOLDING_REGS_SIZE, holdingRegs);

Your slave is running with 8N2 while the master uses the default 8N1.

Thanks for that I've changed the slave to 'SERIAL_8N1.' I now get an E0 error which is an invalid response and occasionally get a error 1 which is an Illegal function!

I've reduced the baud on master and slave to 9600 which stops the error 1 but there is still no data.

The TX led on the slave seams to be on continuously rather than flashing like the one on the master.

   SimpleModbusSlaveV10 supports function 3, 6 & 16.

but

  result = node.readInputRegisters(0x0000, 16);

readInputRegisters is function number 4. That won't work. You either have to use another library for the slave that supports the functions you need or use the correct functions on the master.

Thanks for that. I've changed the line in the master to :

result = node.readHoldingRegisters(0x0000, 16);

and I now get an output some of the time Other times the system just locks up, either in a E0/ zero data state or in a valid output which does not change even thought the data on the slave is changing.

Are Lock ups a problem, I cant see how to clear them from the master or the slave.

1 Like

and I now get an output some of the time Other times the system just locks up, either in a E0/ zero data state or in a valid output which does not change even thought the data on the slave is changing.

Define that in more detail. How does it lock up, where does it lock up? How often? Post example output!

Are the GNDs of the two systems connected?

Got it working at last! turns out the 100ohm resistor I was using in the ground link between the two boards was actually open circuit!!

Thanks for pointing out the two silly mistakes.

Having got the basics working I can now get on with making a Uno/Mega slave with 48 registers and getting the master to read 80 registers on a piece of commercial kit that I want to read the data from.