Skip to Content

Modbus register 40001 or address 0? 6 checks

Short answer: in a Modbus frame, addresses start at 0, so holding register 40001 of a manual usually travels as address 0. Find out which convention your manual and your software use, subtract 40001 when the manual numbers registers from 1, and test with a known value before you write.

Download the checklist (PDF, 2 pages)

Descarga la lista de comprobación (PDF, 2 páginas)

These checks apply to Modbus RTU and Modbus TCP, with any master and slave. The symptom: you read the register number from the manual and get the value of the register next to it, or exception 02, Illegal Data Address. If the slave does not answer at all, start with the lesson on Modbus RTU timeouts.

Six checks, in this order

CheckWhat to doGood result
1. Register number40001 is a register number of the data model, not the address sent in the frame. The first digit names the table: 4 is holding registers, read with function 03. This 4xxxx numbering is a Modicon convention, used by Schneider Electric and by many manuals; the Modbus specification does not use it.The function code matches the table: 4xxxx, function 03.
2. FrameThe specification addresses each data item from 0 to 65535, and data numbered X is addressed as X-1 in the PDU. Holding register 40001 travels as address 0 (0x0000). In the example of the specification, registers 108 to 110 start at address 107 (0x6B).The first holding register is requested as 0x0000.
3. Mathsaddress = register - 40001. Register 40010 is address 9. Subtracting 40000 leaves an offset of one.40010 gives 9.
4. Six digitsSome software writes 400001 to 465536: the same table, with six digits and room for all 65536 addresses. Subtract 400001. With five digits the highest number is 49999 (address 9998). Many devices may not support the whole range.address = register - 400001.
5. ConventionManuals differ: some list protocol addresses from 0, others register numbers from 1, others addresses in hexadecimal. Some SCADA and OPC tools accept 40001 or 400001 and convert it. The Industrial Shields Modbus library 1.1.0 writes the address you pass into the frame as it is, so it expects the protocol address, from 0.The manual and the software use the same convention.
6. TestRead address N and N-1 from a register with a known value: a serial number, a firmware version or a setpoint shown on the display. The one that matches tells you the convention. Exception 02, Illegal Data Address, is a clue too, but it also appears when the quantity goes past the map of the slave. Test with reads only, not with writes, until you know the convention.One of the two reads matches the known value.

Symptom, cause and what to do

  • You get the value of the register next to it: an offset of one. Subtract 40001, not 40000, or test N-1.
  • Exception 02 when you read the last register of the map: an offset of one, or a quantity that is too large. Test N-1 and reduce the quantity.
  • It works from the SCADA and not from the PLC, or the other way round: one converts the number and the other does not. Check whether each field asks for a register number or for an address.
  • The manual gives 0x0000 or "address 0": it is already the protocol address. Subtract nothing.
  • The manual gives 400001: six digits. Subtract 400001.
  • Coils, discrete inputs or input registers: the same rule, with their own table. See the conversion table below.

Conversion table

0xxxx: coils, function 01, subtract 1. 1xxxx: discrete inputs, function 02, subtract 10001. 3xxxx: input registers, function 04, subtract 30001. 4xxxx: holding registers, function 03, subtract 40001. The same subtraction applies when the manual uses 1-based register numbers; when it gives addresses from 0, subtract nothing. The slave address (1 to 247 on a serial line) has nothing to do with the register address.

On Arduino PLC (M-Duino, and Ardbox HF+ with hardware RS-485) and ESP32 PLC, the Industrial Shields Modbus library takes protocol addresses; its examples are in File > Examples > Modbus. For Raspberry PLC (Python, Node-RED, C), check the convention of the library you use with the test of check 6.

Example configuration

Reference frame for reading register 40001 of slave 31:

Modbus RTU request: read register 40001 of slave 31 (the library adds the CRC)

Slave  Function  Start address  Quantity   CRC
0x1F   0x03      0x00 0x00      0x00 0x01  (2 bytes)
       ^ holding registers      ^ 1 register
                 ^ 40001 - 40001 = 0

Reference code with the Industrial Shields Modbus library 1.1.0, based on the example ModbusRTUMasterReadHoldingRegisters: it converts the manual number and reads N-1 and N in one request.

#include <RS485.h>
#include <ModbusRTUMaster.h>

ModbusRTUMaster master(RS485);

const uint8_t  SLAVE_ID      = 31;
const uint16_t MANUAL_REG    = 40010;             // number in the manual (4xxxx, 1-based)
const uint16_t ADDR          = MANUAL_REG - 40001; // protocol address: 9

void setup() {
  Serial.begin(9600);
  RS485.begin(38400, HALFDUPLEX, SERIAL_8E1);
  master.begin(38400);
}

void loop() {
  static unsigned long last = 0;
  if (millis() - last > 1000 && !master.isWaitingResponse()) {
    // Test N-1 and N at once: ask for 2 registers starting at ADDR - 1
    if (!master.readHoldingRegisters(SLAVE_ID, ADDR - 1, 2)) {
      Serial.println("Request not sent");
    }
    last = millis();
  }
  if (master.isWaitingResponse()) {
    ModbusResponse response = master.available();
    if (response) {
      if (response.hasError()) {
        Serial.print("Error: ");           // e.g. exception 02, Illegal Data Address
        Serial.println(response.getErrorMessage());
      } else {
        Serial.print("N-1 = "); Serial.println(response.getRegister(0));
        Serial.print("N   = "); Serial.println(response.getRegister(1));
        // The one that matches the known value tells you the convention of the manual
      }
    }
  }
}

If ADDR is 0, you cannot ask for ADDR - 1: read addresses 0 and 1 instead. The code compiles without errors or warnings for ESP32 PLC with the Industrial Shields ESP32 boards 2.7.1 and the Modbus library 1.1.0; it has not been compiled here for Arduino PLC. Set the baud rate and parity of your slave.

Next steps

Sources

Modbus Organization: MODBUS Application Protocol Specification V1.1b3, sections 4.3, 4.4, 6.3 and 7. Schneider Electric: FAQ FA168406 on Modbus register numbers and addresses, and the Schneider Electric Community article Helpful Modbus register information. PTC Kepware: Modbus addressing. Industrial Shields: Modbus RTU with Arduino and ESP32 based PLCs, and the Modbus library 1.1.0 on GitHub (Industrial-Shields/arduino-Modbus).

Seis comprobaciones para el offset de registros Modbus: por qué el holding register 40001 de un manual suele viajar como dirección 0.

Commenting is not enabled on this course.