Modbus RS485 - Library issues

Hi! I have a MAX13487e connected to a ESP32. I program using PlatformIO and using Arduino based coding. Now I have Modbus RTU (server) device on address 1.

I have tried to use several librrary but all of them are giving me no response or error codes like 226 and 227. While my RAW implementation of reading the serial does give me the right response from the device.

Now I am stuck how I need to proceed further to obtain the values from the response. Anyone can help me?

Current response from the serial:

Response: 01 03 01 66 00 02 25 E8 01 03 04 38 1B 43 6C B6 49

More information /code:

#define BOARD_485_TX                39
#define BOARD_485_RX                38
#define BOARD_485_EN                42
#define Serial485   Serial2

void sendModbusRequest() {
  uint8_t request[] = {0x01, 0x03, 0x01, 0x66, 0x00, 0x02}; // Device 1, Function 3, Starting Address 0, Read 1 register
  uint16_t crc = calculateCRC(request, sizeof(request));
  // Append CRC to request
  uint8_t crcLow = crc & 0xFF;
  uint8_t crcHigh = (crc >> 8) & 0xFF;
  Serial2.write(request, sizeof(request));
  Serial2.write(crcLow);
  Serial2.write(crcHigh);
}

void readModbusResponse() {
  int availableBytes = Serial2.available();
  if (availableBytes > 0) {
    Serial.print("Response: ");
    while (availableBytes--) {
      int byteReceived = Serial2.read();
      if (byteReceived == -1) break; // Just in case
  
      // Print each byte in HEX format
      if (byteReceived < 16) {
        
        Serial.print("0"); // Print leading zero for single digit hex values
      }
      Serial.print(byteReceived, HEX);
      Serial.print(" "); // Add space between bytes for readability
    }
    Serial.println(); // Newline after the complete message
  } else {
    Serial.println("No response");
  }
}

an UART is asynchronous. Data will come in over time. You should not consider that the whole message is ready for you to read right away.

Hi, I'm using this library for ESP32 RTU master RS485 and it works fine:
https://github.com/4-20ma/ModbusMaster

Thanks, this keeps giving me CRC mismatch error (227).

node.clearResponseBuffer();

  result = node.readHoldingRegisters(0x0166,2);

  //readModbusResponse();
  
   if (result == node.ku8MBSuccess) {
    Serial.println("Request successful");
    // If successful, read the data
    for (uint8_t i = 0; i < 2; i++) { // Assuming 2 registers were requested
      uint16_t data = node.getResponseBuffer(i);
      Serial.print("Register ");
      Serial.print(i);
      Serial.print(": ");
      Serial.println(data, HEX);
    }
  } else {
    Serial.print("Request failed with error code: ");
    Serial.println(result);
  }

Are you sure that 0x0166 is an address of your first holding register you want to read?

Where are you monitoring this response from? The "01 03 01 66 00 02 25 E8" sequence is the request sent to the remote device and the "01 03 04 38 1B 43 6C B6 49" is the response back.

Serial Monitor, see readModbusResponse function.

I would say that you have something wrong in your setup. You shouldn't normally "receive" the transmitted message unless you have enabled both the transmitter and receiver sections of your RS485 line driver at the same time - effectively causing a loopback of the transmitted message.

Check that you are controlling the RE & DE signals correctly.

It's not the first address if the device. But first float I want to read.

Thanks for the suggestion. Since I have a MAX13487e it should control itself. Will check if I can do something with the EN line.

Schermafbeelding 2024-03-22 193547

For example this means, read 2 holding registers, starting from number 10.

With your MAX13487e are you running it from 5V?

What level are the RE and SHDN pins set to - presumably high?

The datasheet for the MAX13487e implies that it disables the RX when in TX mode - via the internal autoswitching circuitry - but the output from your code would suggest that this is not the case as you are receiving your own transmission, which will likely be the cause.

Another thought - I wonder if you were to use the ModbusMaster library and then in the postTransmission function make a call to the clearResponseBuffer function.

In theory this would empty the response buffer at the end of transmission which might remove the echoed request message leaving the buffer empty ready for the remote device response message.

1 Like

RE is set to Low. After al my tries I am think that it might be the MAX13487e which might be faulty.

Still the same :frowning:

The datasheet says:

Receiver Output Enable. Drive RE low to enable the RO. Drive RE high to let the AutoDirection circuit
control the receiver. RE is a hot-swap input (see the Hot-Swap Capability section for more details).

Yeah, there must be something wrong in the circuit. Drive RE to High will give no response at all.

Has your RS485 bus got the resistors fitted? If not, then I think the MAX13487e chip may be getting confused over bus activity and not switching correctly.

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