Hi, I have a project in making a Master-Slave Modbus Communication with RS485 using ArduinoIDE. The devices is a WeMos D1 Mini for Slave and a NodeMCU for Master. I use SimpleModbusMasterV2 and SimpleModbusSlaveV10.The code I'm using :
Master:
#include <SimpleModbusMaster.h>
//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate
#define retry_count 10
#define TxEnablePin 2 //D4
#define LED 4 //D2
#define TOTAL_NO_OF_REGISTERS 2
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()
{
// Initialize each packet
modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 0, 1, regs[0]);
modbus_construct(&packets[PACKET2], 1, PRESET_MULTIPLE_REGISTERS, 1, 1,regs[1]);
// 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()
{
modbus_update();
regs[1] = random(0,1024); // update data to be written to arduino slave
analogWrite(LED, regs[0]);
}
and For Slave :
#include <SimpleModbusSlave.h>
#define TxEnablePin 2 //D4
#define LED 4 //D2
//////////////// registers of your slave ///////////////////
enum
{
// just add or remove registers and your good to go...
// The first register starts at address 0
ADC_VAL,
PWM_VAL,
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)
*/
/* Valid modbus byte formats are:
SERIAL_8N2: 1 start bit, 8 data bits, 2 stop bits
SERIAL_8E1: 1 start bit, 8 data bits, 1 Even parity bit, 1 stop bit
SERIAL_8O1: 1 start bit, 8 data bits, 1 Odd parity bit, 1 stop bit
You can obviously use SERIAL_8N1 but this does not adhere to the
Modbus specifications. That said, I have tested the SERIAL_8N1 option
on various commercial masters and slaves that were suppose to adhere
to this specification and was always able to communicate... Go figure.
These byte formats are already defined in the Arduino global name space.
*/
modbus_configure(&Serial, 9600, SERIAL_8N2, 1, TxEnablePin, 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(9600, SERIAL_8N2, 1);
pinMode(LED, OUTPUT);
}
void loop()
{
modbus_update();
holdingRegs[ADC_VAL] = 512;
}
The problem is I just can't get it working. The plan is to have the master read the slave register and PWM a LED. However, the LED just won't turn on. I tried to add-in
Serial.println(regs[0]);
to the Master in order to see what it's reading, but when i connect it into my computer, my mouse start randomly clicking and dragging.
I really hope someone can help me in this. Or if anyone have an actual working library, please tell me.