Sending data through Modbus

I connected the Max485 as following:
pin 1 of IC to Rx (pin 0 of Arduino)
pin 2 of IC - (RE_NEG) to pin 2 of Arduino
pin 3 of IC (DE) - to pin 3 of Arduino
pin 4 of IC (Tx) - to pin 1 of Arduino

I also modified the code:

//need to send the Modbus command "01 06 00 43 00 01 B9 DE"

#include <ModbusMaster.h>

#define MAX485_DE      3
#define MAX485_RE_NEG  2

// instantiate ModbusMaster object
ModbusMaster node;

uint16_t u16WriteAddress = 0x0043;
uint16_t u16WriteValue = 0x0001;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, HIGH);
  digitalWrite(MAX485_DE, HIGH);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, LOW);
  digitalWrite(MAX485_DE, LOW);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, HIGH);
  digitalWrite(MAX485_DE, LOW);

  // Modbus communication runs at 115200 baud
  Serial.begin(115200);

  // Modbus slave ID 1
  node.begin(1, Serial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);  //since we are going to transmit
//  node.postTransmission(postTransmission); 
}

  bool state = false;

void loop() 
{
  uint8_t result;
  
    if (state == false) {  //execute the function 'writeSingleRegister' only once
    Serial.println("Starting transmission");
    result = node.writeSingleRegister(u16WriteAddress, u16WriteValue);
    state = !state;
    
    if (result == 0) {
      Serial.println("Transmission done");
    }
    else 
      Serial.println("Error; press 'reset' button or restart serial to try again");
    }
}

What I am missing here? The sketch does not work - I am getting the error message, so the function does not return 0.

if you would print out the result (in HEX) you could get an idea what the the problem is. All "Error codes" (results) are in the .h file of the library.

I am getting an error 226, in HEX - E2. According to the .h file, 0xE2 - ModbusMaster response timed out exception. The entire response was not received within the timeout period.

so your module is not responding.

make good pictures of your setup so we can see each and every wire connection.

what's about the baudrate? You started with 9600, now you try 115200. What baudrate does your module need and how can you check it that it is set to the correct baudrate and address?

You could provide as a link to the datasheet describing your module so we can read about that thing.

The baudrate is defined by the DIP switches, and now it is 115200. The device address is also defined by the switches, it is set to 1. Attached is the manual.
Connections to the module are through RJ45 connector, accoridng to the manual. I double checked all the connections.
YKD2405PR.pdf (1.9 MB)

I tested the signals at the output of the MAX485 on the scope. I do see the differential signal, both A and lines.

Does your scope decode serial signals? Try decoding the message you are sending.
If not try using and usb/rs485 converter connected to the pc ( if you don't have one... well you need one if you are using modbus )

My scope is basic and does not decode serial signals. Finally I used USB to rs485 converter to configure the driver. I don't know while it did not work with Arduino.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.