I've started to play with the Modbus stuff and managed to get it working in my own fashion with reading and searching on the net.
At the moment I've one master reads an analogue channel from one slave, I would like to add more slaves (another 3), The part I'm not sure is how to get the master to read from the other 3 slaves.
I've searched and can only seem to find details on a master and slave type of thing.
Is it possible to add to the master to read from 3 other slaves by adding to the master code below or have I got it wrong or looking at totally wrong, My thinking is some sort of counter the display the incoming data.
Master:
#include <SimpleModbusMaster.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
//////////////////// Port information ///////////////////
#define baud 115200
#define timeout 1000
#define polling 200 // the scan rate
#define retry_count 10
// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 13
#define SlaveID 1
#define LED 11
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 1
// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1,
//PACKET2,
TOTAL_NO_OF_PACKETS // leave this last entry
};
// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];
void setup()
{
Serial.begin(115200);
lcd.begin(20, 4);
// Initialize each packet
modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 0, 1, 0); //(packet,SlaveId,Function,holdingregAdress,data,locanstartadress)
//modbus_construct(&packets[PACKET2], 1, PRESET_MULTIPLE_REGISTERS, 1, 1, 0);
// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
pinMode(LED, OUTPUT);
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print("MODBUS RTU");
modbus_update();
lcd.setCursor(0, 1);
lcd.print(regs[0]);
lcd.print(" ");
}
Slave:
#include <SimpleModbusSlave.h>
//#define LED 3
#define TxEnablePin 7
#define SlaveID 1
unsigned int holdingRegs[1]; // function 3 and 16 register array
////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
modbus_configure(&Serial, 90600, SERIAL_8N2, SlaveID, TxEnablePin , 1, 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(115200, SERIAL_8N2, 1);
//pinMode(LED, OUTPUT);
}
void loop()
{
// 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();
holdingRegs[0] = analogRead(A0); // update data to be read by the master to adjust the PWM
}
Any ideas or pointers would be grateful
Thanks
Steve