Hello everyone, I'm faced with a problem, there is a master on arduino mega2560 and a few slaves on arduino nano, all are connected in parallel and at the beginning and at the end of the line the resistors are 120m, when working in mode 1 master -1 slave everything is fine, it is necessary to connect the second slave not both work, which is the problem with the whole head broke, when working on the library easytransfer everything was fine, switched to modbus and there were problems, now I use this GitHub - smarmengol/Modbus-Master-Slave-for-Arduino: Modbus Master-Slave library for Arduino
master
/**
* Modbus master example 1:
* The purpose of this example is to query an array of data
* from an external Modbus slave device.
* The link media can be USB or RS232.
*
* Recommended Modbus slave:
* diagslave http://www.modbusdriver.com/diagslave.html
*
* In a Linux box, run
* "./diagslave /dev/ttyUSB0 -b 19200 -d 8 -s 1 -p none -m rtu -a 1"
* This is:
* serial port /dev/ttyUSB0 at 19200 baud 8N1
* RTU mode and address @1
*/
#include <ModbusRtu.h>
// data array for modbus network sharing
uint16_t au16data[16];
uint8_t u8state;
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0,1,2); // this is master and RS-232 or USB-FTDI
/**
* This is an structe which contains a query to an slave device
*/
modbus_t telegram;
unsigned long u32wait;
void setup() {
Serial.begin(9600);
master.begin( 9600 ); // baud-rate at 19200
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
u32wait = millis() + 1000;
u8state = 0;
}
void loop() {
switch( u8state ) {
case 0:
if (millis() > u32wait) u8state++; // wait state
break;
case 1:
telegram.u8id = 2; // slave address
telegram.u8fct = 3; // function code (this one is registers read)
telegram.u16RegAdd = 1; // start address in slave
telegram.u16CoilsNo = 4; // number of elements (coils or registers) to read
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
master.query( telegram ); // send query (only once)
u8state++;
break;
case 2:
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + 100;
}
break;
}
Serial.println("");
Serial.print("Value = ");
Serial.print(au16data[0]);
}
Slaves
/**
* Modbus slave example 1:
* The purpose of this example is to link a data array
* from the Arduino to an external device.
*
* Recommended Modbus Master: QModbus
* http://qmodbus.sourceforge.net/
*/
#include <ModbusRtu.h>
// data array for modbus network sharing
uint16_t au16data[16] = {
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus slave(2,0,10); // this is slave @1 and RS-232 or USB-FTDI
void setup() {
slave.begin( 9600 ); // baud-rate at 19200
}
void loop() {
slave.poll( au16data, 16 );
}
Yes, I know that the master code is designed for 1 slave, but the problem is that with 2 slaves it stops responding even 1 slave.
a similar code with a cycle under a few slave is too cumbersome because of another code. the essence remains the same. I give an individual id for slaves)