A
A
Andrey Kovalchuk2016-05-31 13:41:22
Arduino
Andrey Kovalchuk, 2016-05-31 13:41:22

RS485, MODBUS, why can messages not reach the slave device?

Good day.
There are two Arduinos, Nano (slave) and Mega (master). The master sends something (RX blinks as expected), the slave does not receive anything (tx, rx are silent).
Here are the sketches.
Master.

#include <ModbusRtu.h>

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

/**
 *  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,0,TXEN); // 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() {
  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 query (only once)
    u8state++;
    break;
  case 2:
    master.poll(); // check incoming messages
    if (master.getState() == COM_IDLE) {
      u8state = 0;
      u32wait = millis() + 100; 
      
    }
    break;
  }
}

Slave.
#include <ModbusRtu.h>
#define ID   1
#define TXEN  10

Modbus slave(ID, 0, TXEN); // 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];

/**
 *  Setup procedure
 */
void setup() {
  io_setup(); // I/O settings

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

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

  
  io_poll();
} 


void io_setup() {
  // define i/o
  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() {

  // read analog inputs
  au16data[4] = analogRead( 4 );
  //au16data[5] = analogRead( 1 );

  // diagnose communication
  au16data[6] = slave.getInCnt();
  au16data[7] = slave.getOutCnt();
  au16data[8] = slave.getErrCnt();
}

What could go wrong? The circuit is assembled using Max485
XVmJT1jMYQM.jpg6d6b773d547e4eb18dd179a35669d6df.pngBonus modules
1MxSO0S2Crg.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Ocelot, 2016-05-31
@mrkovalchuk

The slave has TXD and RXD interchanged.

I
Ivan, 2016-05-31
@LiguidCool

If the slave's reception is silent, and the master's transmission is flashing ... "Uncle, I know what's broken with you."
Either they connected incorrectly, or the adapters are not working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question