# First Project - Struggling with modbus write function

**URL:** <https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770>\
**Category:** Programming\
**Created:** [August 9, 2023, 7:42pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770 "2023-08-09T19:42:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kennet\_s](https://avatars.discourse-cdn.com/v4/letter/k/9dc877/32.png) [@kennet\_s](https://forum.arduino.cc/u/kennet_s)\
**Post date:** [August 9, 2023, 7:42pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/1 "2023-08-09T19:42:45Z")

</div>

Hi,

I am working on my first project. Pretty simple project. But I struggled several days with the modbus write function. Reading from the slave is no problem.

I use this library [GitHub - 4-20ma/ModbusMaster: Enlighten your Arduino to be a Modbus master](https://github.com/4-20ma/ModbusMaster)

Hardware: ESP8266 and MAX485 board.  
Slave device: ID 1, address 145. I know that 0x00 is value off. And writing 0x01 is value on

My Slave device only support read 0x03 FC and write 0x10 FC.

I have tested with qModbus that it works from my PC and to the slave device.  
I get the following raw modbus RTU message in the log from qModbus:  
Tx -\> 01 10 00 91 00 01 02 00 00 BA D1 = value 0 in slave device  
Tx -\> 01 10 00 91 00 01 02 00 01 7B 11 = value 1 in slave device.

When I break down messages I get the following, if correct:  
01 = Slave ID  
10 = Function code  
00 = Address of the first register Hi bytes  
91 = Address of the first register Lo bytes  
00 = Number of registers to write Hi bytes  
01 = Number of registers to write Lo bytes  
02 = Number of bytes  
00 = Value Hi  
00 = Value Lo (This one is 01 when raw message value is 1)

Please help me, I am totally out of my mind and think I have tried everything.

```arduino
#include <SoftwareSerial.h>
#include <ModbusMaster.h>

// Define the pins for SoftwareSerial
#define RX_PIN 5 // Max485 Rx connected to D5 (GPIO 5)
#define TX_PIN 4 // Max485 Tx connected to D4 (GPIO 4)
#define RE_DE_PIN 16 // RE/DE control pin for MAX485 (GPIO 16)

// Create a SoftwareSerial object
SoftwareSerial mySerial(RX_PIN, TX_PIN); // RX, TX

// Create a ModbusMaster object
ModbusMaster node;

void preTransmission() {
  digitalWrite(RE_DE_PIN, HIGH); // Set RE/DE pin HIGH for transmit mode
}

void postTransmission() {
  digitalWrite(RE_DE_PIN, LOW); // Set RE/DE pin LOW for receive mode
}

void setup() {
  Serial.begin(9600); // Initialize the Serial monitor
  mySerial.begin(9600, SWSERIAL_8N1); // Initialize SoftwareSerial
  pinMode(RE_DE_PIN, OUTPUT); // Set RE/DE pin as OUTPUT
  digitalWrite(RE_DE_PIN, LOW); // Initialize the pin as LOW
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
  node.begin(1, mySerial); // Initialize ModbusMaster with slave ID 1
}

void loop() {
  if (Serial.available()) {
    int command = Serial.parseInt(); // Read the command from Serial monitor
    switch (command) {
      case 0:
        writeModbusMessageOff();
        break;
      case 1:
        writeModbusMessageOn();
        break;
      case 2:
        readModbusMessage();
        break;
      default:
        Serial.println("Invalid command.");
        break;
    }
    delay(10000); // Add delay here, after the switch block
  }
}

void writeModbusMessageOff() {
  node.setTransmitBuffer(0, 0000);
  node.setTransmitBuffer(1, 0000);

  uint8_t result = node.writeMultipleRegisters(0x0091, 1);

  if (result == node.ku8MBSuccess) {
    Serial.println("Write Sell Off successful!");
  } else {
    Serial.println("Write Sell Off failed!");
  }
}

void writeModbusMessageOn() {
  node.setTransmitBuffer(0, 0000);
  node.setTransmitBuffer(1, 0001);

  uint8_t result = node.writeMultipleRegisters(0x0091, 1);

  if (result == node.ku8MBSuccess) {
    Serial.println("Write Sell On successful!");
  } else {
    Serial.println("Write Sell On failed!");
  }
}

void readModbusMessage() {
  // Read the Modbus message
  uint8_t result = node.readHoldingRegisters(0x0091, 1);

  if (result == node.ku8MBSuccess) {
    uint16_t value = node.getResponseBuffer(0x00);
    Serial.print("Solarsell: ");
    Serial.print(value, HEX); // Display the value in hexadecimal
    Serial.println();
  } else {
    Serial.println("Read failed!");
  }
}

```

---

<div class="post-metadata">

**Author:** ![qubits-us](https://avatars.discourse-cdn.com/v4/letter/q/5daacb/32.png) [@qubits-us](https://forum.arduino.cc/u/qubits-us)\
**Post date:** [August 9, 2023, 8:24pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/2 "2023-08-09T20:24:00Z")

</div>

Hi @kennet_s ,  
Welcome to the forum..

Modbus, yummy..  
Careful with [integer constants..](https://www.arduino.cc/reference/en/language/variables/constants/integerconstants/)..  
got some octal going on..

Try using writeSingleRegister as you're really only setting one and beacuase of this you only ever set it to 0, or drop one of you setTransmitBuffers..

good luck.. ~q

---

<div class="post-metadata">

**Author:** ![kennet\_s](https://avatars.discourse-cdn.com/v4/letter/k/9dc877/32.png) [@kennet\_s](https://forum.arduino.cc/u/kennet_s)\
**Post date:** [August 10, 2023, 5:49am UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/3 "2023-08-10T05:49:45Z")

</div>

> [@kennet\_s](#):
>
> My Slave device only support read 0x03 FC and write 0x10 FC.

Unfortunately, it does not work because the slave only supports the following: readHoldingRegisters (0x03) and writeMultipleRegisters (0x10).

I have tried almost everything, without success.

If anyone knows of another library where it's easier to set up. so feel free to shout out loud

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [August 10, 2023, 6:15am UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/4 "2023-08-10T06:15:32Z")

</div>

print the error code you receive from your device and check in the library files the meaning of that error code

```arduino
if (result == node.ku8MBSuccess) {
    Serial.println("Write Sell Off successful!");
  } else {
    Serial.print("Write Sell Off failed! ");
    Serial.println(result, HEX); // print error code in HEX
  }

```

read again answer #2 and correct your numbers.

If you don't get better results

1. post your new code.
2. post the serial output of your code.
3. post the datasheet of your device so we can check the registers and functions.

---

<div class="post-metadata">

**Author:** ![kennet\_s](https://avatars.discourse-cdn.com/v4/letter/k/9dc877/32.png) [@kennet\_s](https://forum.arduino.cc/u/kennet_s)\
**Post date:** [August 10, 2023, 7:13am UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/5 "2023-08-10T07:13:22Z")

</div>

I don't get a error code. it write successful. But the value never change on my slave device.

I'll change from int 0 to 3.

The strange thing is that, when I put sales "on" in settings of my slave device. Then I can write "0x00" from this code to the slave device and sales are turned off. But I can't do the other way around.

I have attached the modbus datasheet for the device

[Modbus.Deye.amit.posun.pdf](https://forum.arduino.cc/uploads/short-url/tgjQXBFgoJMU17nqFac8YqxkFGf.pdf) (1.4 MB)

---

<div class="post-metadata">

**Author:** ![qubits-us](https://avatars.discourse-cdn.com/v4/letter/q/5daacb/32.png) [@qubits-us](https://forum.arduino.cc/u/qubits-us)\
**Post date:** [August 10, 2023, 1:51pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/6 "2023-08-10T13:51:29Z")

</div>

Drop one of your setTransmitbuffers..

> [@kennet\_s](#):
>
> ```arduino
> node.setTransmitBuffer(0, 0000);
> node.setTransmitBuffer(1, 0001);
> 
> ```

change too..

```arduino
//set the one reg in the buffer..  
node.setTransmitBuffer(0, 1);
  //node.setTransmitBuffer(1, 1);

```

you are only setting one reg in your call to writeMultipleRegisters, that's the second param of 1 you pass.. in your current code you always set this register to octal 0000 which is dec 0, off..

good luck.. ~q

---

<div class="post-metadata">

**Author:** ![qubits-us](https://avatars.discourse-cdn.com/v4/letter/q/5daacb/32.png) [@qubits-us](https://forum.arduino.cc/u/qubits-us)\
**Post date:** [August 10, 2023, 1:57pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/7 "2023-08-10T13:57:42Z")

</div>

Your doc does say it supports function code x06 which is preset single register.  
Would be less code as you don't have to put register data into buffer it's part of the call to writeSingleRegister(address, value)..  
But either way should work, you just always set it to off now..

~q

---

<div class="post-metadata">

**Author:** ![kennet\_s](https://avatars.discourse-cdn.com/v4/letter/k/9dc877/32.png) [@kennet\_s](https://forum.arduino.cc/u/kennet_s)\
**Post date:** [August 10, 2023, 2:37pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/8 "2023-08-10T14:37:30Z")

</div>

![03-10-only](https://europe1.discourse-cdn.com/arduino/original/4X/e/e/1/ee1d6ad571d321aac3f6810359b6b020d8d3584b.png)

I have already tried writeSingleRegister. It just fails there. Only 03 and 10 work.

---

<div class="post-metadata">

**Author:** ![kennet\_s](https://avatars.discourse-cdn.com/v4/letter/k/9dc877/32.png) [@kennet\_s](https://forum.arduino.cc/u/kennet_s)\
**Post date:** [August 10, 2023, 2:50pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/9 "2023-08-10T14:50:04Z")

</div>

omg... now it works with this change of the code:  
`node.setTransmitBuffer(0, 1);`

I was sure that the first line was a low word and the second line was a high word in my code:

```arduino
  node.setTransmitBuffer(0, 0000);
  node.setTransmitBuffer(1, 0001);

```

imagine that I have spent several days on this and then it is "just" the small correction. Thank you very much!!! qubits-us!

---

<div class="post-metadata">

**Author:** ![qubits-us](https://avatars.discourse-cdn.com/v4/letter/q/5daacb/32.png) [@qubits-us](https://forum.arduino.cc/u/qubits-us)\
**Post date:** [August 10, 2023, 3:40pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/10 "2023-08-10T15:40:31Z")

</div>

Yeah, read that too after i posted..  
But, should work with set multiple too..  
Glad you got it.. 🙂

have fun.. ~q

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 6, 2024, 3:40pm UTC](https://forum.arduino.cc/t/first-project-struggling-with-modbus-write-function/1156770/11 "2024-02-06T15:40:54Z")

</div>

Closed on February 6, 2024, 3:40pm UTC

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