Как передать данные по Modbus и вывести в Serial?

Не передаются данные с мастера на слейв и не выводятся в Serial. Обе платы Arduino Nano, стандарт передачи RS-485. Код мастера

#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,Serial,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(19200 );
  master.start(); // 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 = 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.poll() != 0) {
      Serial.println(master.poll());
    }
    */
    //Serial.println(master.poll());
    if (master.getState() == COM_IDLE) {
      u8state = 0;
      u32wait = millis() + 100; 
    } 

    for (int i=0; i <= telegram.u16CoilsNo; i++){
      if (au16data[i] != 0) {
        Serial.println(au16data[i]);
      }
    }
    
    break;
  }
}

Код слейва

#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(1, Serial, 2); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
  Serial.begin(19200);
  slave.start(); // baud-rate at 19200
}

void loop() {
  slave.poll( au16data, 16 );
}

Ответы (1 шт):

Автор решения: dusick150 Денис

Полный исправленный код:

#include <ModbusRtu.h>

uint16_t au16data[16];
uint8_t u8state;

Modbus master(0, Serial, 2); // this is master and RS-485

modbus_t telegram;
unsigned long u32wait;

void setup() {
  Serial.begin(19200);
  master.start(); // 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 = 0; // 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;
    }

    for (int i = 0; i < telegram.u16CoilsNo; i++) {
      Serial.println(au16data[i]);
    }
    break;
  }
}


#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 slave(1, Serial, 2); // this is slave @1 and RS-485

void setup() {
  Serial.begin(19200);
  slave.start(); // baud-rate at 19200
}

void loop() {
  slave.poll(au16data, 16);
}
→ Ссылка