2

I'm working with a 32-Input Modbus RTU module using RS485. I'm trying to read/poll the inputs to check for ON(1) states. I have tested this module with Arduino UNO, Radzio! Modbus Master simulator, and Modbus Poll -- all worked as expected, (displaying 0=off, 1=on). Using an Arduino Nano with a TTL to RS485 Module like this.

So using the below code, the output is all 0s regardless of input state. It compiles and runs without errors.

#include <ModbusRtu.h>
#include <SoftwareSerial.h>

// data array for modbus network sharing
uint16_t au16data[32];
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); // this is master and RS-232 or USB-FTDI via software serial

/**
 * This is an structe which contains a query to an slave device
 */
modbus_t telegram;

unsigned long u32wait;

SoftwareSerial mySerial(2, 3);//Create a SoftwareSerial object

void setup() {
  Serial.begin(9600);
  master.begin( &mySerial, 9600 ); // begin ModBus object 9600baud
  master.setTimeOut( 2000 ); // if 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 = 128;// start address in slave 0x0080-0x009F(128-159)
    telegram.u16CoilsNo = 32; // number of elements to read  0x00 0x01
    telegram.au16reg = au16data; // pointer memory array in 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() + 2000; 
        Serial.println(au16data[0]);//Or do something else!
        Serial.println(au16data[1]);//Or do something else!
        Serial.println(au16data[2]);//Or do something else!
        Serial.println(au16data[3]);//Or do something else!
        Serial.println(au16data[4]);//Or do something else!
        Serial.println(au16data[5]);//Or do something else!
        Serial.println(au16data[6]);//Or do something else!
        Serial.println(au16data[7]);//Or do something else!
        Serial.println(au16data[8]);//Or do something else!
        Serial.println(au16data[9]);//Or do something else!
        Serial.println(au16data[10]);//Or do something else!
        Serial.println(au16data[11]);//Or do something else!
        Serial.println(au16data[12]);//Or do something else!
        Serial.println(au16data[13]);//Or do something else!
        Serial.println(au16data[14]);//Or do something else!
        Serial.println(au16data[15]);//Or do something else!
        Serial.println(au16data[16]);//Or do something else!
        Serial.println(au16data[17]);//Or do something else!
        Serial.println(au16data[18]);//Or do something else!
        Serial.println(au16data[19]);//Or do something else!
        Serial.println(au16data[20]);//Or do something else!
        Serial.println(au16data[21]);//Or do something else!
        Serial.println(au16data[22]);//Or do something else!
        Serial.println(au16data[23]);//Or do something else!
        Serial.println(au16data[24]);//Or do something else!
        Serial.println(au16data[25]);//Or do something else!
        Serial.println(au16data[26]);//Or do something else!
        Serial.println(au16data[27]);//Or do something else!
        Serial.println(au16data[28]);//Or do something else!
        Serial.println(au16data[29]);//Or do something else!
        Serial.println(au16data[30]);//Or do something else!
        Serial.println(au16data[31]);//Or do something else!
    }
    break;
  }
}

Could this be caused by the 'Number of elements" to read? Any suggestions greatly appreciated.

This is the 32-input module manual and programming guide

Code is based on this example.

8
  • Your code would be more readable with proper indentation. Can you clarify what you mean with "never displayed"? What is actually displayed? Is the actual amount of received bytes correct?
    – StarCat
    Mar 21, 2023 at 6:47
  • What is actually displayed is a 0, not a 1 when the input is ON.
    – Ashton
    Mar 21, 2023 at 12:55
  • If you change your code completely, you should added it as an UPDATE than total replace your original code.
    – hcheung
    Mar 22, 2023 at 7:21
  • What RS485/Modbus interface board you have? other than the Pin2 for RX, and P3 for TX, How do you wire the RE(Receiver Enable) and DE(Driver Enable) pins?
    – hcheung
    Mar 22, 2023 at 8:12
  • 2
    "Code courtesy of: [He who shall not be named]" No, what that guy does is leech off these sites with an automatic video generation system. Real credit goes to (or more to) one of the authors here of an ESE Q&A.
    – timemage
    Mar 22, 2023 at 13:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.