Modbus RTU how to send and process the request?

Good time of day. Is the following scenario:

Three Android devices: 1 master Arduino Mega, 2 - the slave Arduino Nano. Everything needs to communicate via Modbus Rtu Protocol, why do it? Part of the graduation project is a prerequisite and all that.

The RS485 interface.

That's simple-master

#include <Modbus RTU.h>

// data array for modbus network sharing
uint16_t au16data[10];
uint8_t u8state;
#define TXEN 4 


Modbus master(0,0,TXEN); // this is master and RS-232 or USB-FTDI

modbus_t telegram;

unsigned long u32wait;

void setup() {
 master.begin( 19200 ); // 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 = 1; // slave address
 telegram.u8fct = 3; // function code (this one is registers read)
 telegram.u16RegAdd = 4; // start address in slave
 telegram.u16CoilsNo = 1; // number of elements (coils or registers) to read
 telegram.au16reg = au16data; // pointer to a memory array in the Arduino

 master.query( telegram ); // send the query (only once)
u8state++;
break;
 case 2:
 master.poll(); // check incoming messages
 if (master.getState() == COM_IDLE) {
 u8state = 0;
 u32wait = millis() + 100; 

}
break;
}
}</Modbus RTU.h>


That's simple-slave

#include <Modbus RTU.h>
#define ID 1

Modbus slave(ID, 0, 0); // this is slave ID and RS-232 or USB-FTDI
boolean led;
int8_t state = 0;
unsigned long tempus;

// data array for modbus network sharing
uint16_t au16data[9];

void setup() {
 io_setup(); // I/O settings

 // start communication
 slave.begin( 19200 );
 tempus = millis() + 1000;
 digitalWrite(13, HIGH );
}


void loop() {
 // poll messages
 // blink led pin on each valid message
 state = slave.poll( au16data, 9 );
 digitalWrite( 6, HIGH);
 if (state > 4) {
 tempus = millis() + 50;
 digitalWrite(13, HIGH);
}
 if (millis() > tempus) digitalWrite(6, LOW );

 // link the Arduino pins to the Modbus array
io_poll();
} 

void io_setup() {

 pinMode(2, INPUT);
 pinMode(3, INPUT);
 pinMode(4, INPUT);
 pinMode(5, INPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(13, OUTPUT);

 digitalWrite(6, LOW );

 digitalWrite(13, LOW ); // this is for the UNO led pin

}


void io_poll() {
 au16data[4] = analogRead( 4 );


 // diagnose communication
 au16data[6] = slave.getInCnt();
 au16data[7] = slave.getOutCnt();
 au16data[8] = slave.getErrCnt();
}</Modbus RTU.h>


I see that the wizard generates modbus_t telegram type. Why is that slave? More precisely, what does the line:

slave.poll( au16data, 16 );

And this block:

if (master.getState() == COM_IDLE) {
 u8state = 0;
 u32wait = millis() + 100; 

 }


When working with the master client some all somehow easier.

Upd. Updated master and slave 31.05 10:19 GMT
July 9th 19 at 10:41
1 answer
July 9th 19 at 10:43
Solution
The master sends packets. The slave listens to the line and when it sees the packet, processes it and responds. slave.poll() is responsible for receiving, processing and sending the response. This method you need to periodically pull in the main loop, timer or just. If the master didn't send anything, slave.poll() simply exits. Modbase all registers appear in the memory of the slave (solid au16data[]), you can read/write at any time. Well, not exactly any, and when not called slave.poll().

Unit
if (master.getState() == COM_IDLE) {
 u8state = 0;
 u32wait = millis() + 100; 
 }
gives 100ms delay before the next poll Modbus. In the code wizard implements a simple state machine with three States.
1) u8state == 1 request
2) u8state == 2 Receiving response
3) u8state == 0 Delay
How long slave.pool()? Is there any chance of a banal crossing signal from the master?

Is there a way to process what is received from the master?

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

Still, can you please explain these three lines? - alene.Roberts commented on July 9th 19 at 10:46
>And how long is the slave.pool()?
On exactly your hardware, with your settings? No one can say, it is easier to measure by yourself.

>Is there any chance of a banal crossing signal from the master?
If the library was written not quite a woodpecker (and it probably is), the data coming through UART are written to any buffer. Immediately on termination. And then the slave.pool() processes them slowly. Thus, if you call slave.pool() more often than the polling interval is the master, then passes the packets will not.

>Is there a way to process what is received from the master?
How? All payload values (modbus registers) available through au16data [] and your code should be completely still, how it got there, it's a personal thing library Modbus RTU. To dig deeper into the raw package? Climb into the library code to slave.pool(), set handlers there. But why?

>Also, can you please explain these three lines?
After you learn about the modbus specification such issues should arise. Master this example uses modmenu function 3 (Read Holding Registers, that is, reading multiple registers slave)
telegram.u16RegAdd = 1; // at what address it will read
telegram.u16CoilsNo = 4; // how much read at a time
telegram.au16reg = au16data; // and where to put read. - Sherwood_Larson commented on July 9th 19 at 10:49
: Updated the sketches in question. For A4 hanging temperature sensor, and the diode D6. After compiling the sketch, 1 time blinks D6 diode and turns on diode D13 meet. I don't know what could be the problem? - alene.Roberts commented on July 9th 19 at 10:52
UPD. Lit only diode D6, what was expected. And that's all. - alene.Roberts commented on July 9th 19 at 10:55
:
Modbus slave(ID, 0, 0);
there is a mistake. This is the pin layout for RS-232. For RS-485 third parameter specifies the pin that will control the direction of transmission. - Sherwood_Larson commented on July 9th 19 at 10:58
: Fixed does not work. I noted that your decision is correct because the initial questions you have answered and created a new branch. If not difficult, I would be very grateful: RS485, MODBUS, why can not get messages to the slave device? - alene.Roberts commented on July 9th 19 at 11:01

Find more questions by tags ModbusArduino