# Read/Write Memory – Four Register Write Command (31)

{% hint style="info" %}
When large values are processed using different data formats (e.g., **float32** vs. **int64**), minor differences may occur due to internal calculation methods, typically less than **1/10,000**, and are considered normal.

This command applies **only to memory items with a register length of 4**. Refer to the parameter list for details.
{% endhint %}

#### Function Code 31

The Four Register Write command (Function Code 31) writes 4 consecutive registers (64-bit), supporting Int64 or other 64-bit values. It is recommended for writing large parameters such as: D11 Total Target (64-bit), C04 Dosing Target (64-bit)

{% hint style="info" %}
A standard 32-bit floating-point number (Function Code 30) has a precision of about **7 significant digits**. When the value exceeds ten million, using 32-bit floats may cause **accuracy loss and rounding errors**.

In such cases, it is recommended to use **Function Code 31** with **64-bit integers (Int64)** to ensure the data is accurately stored in memory without any rounding errors.
{% endhint %}

####

![](https://3404778090-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi4ECt41Kyy9211OWazj4%2Fuploads%2F7R7pUOAfrPPfR486Hoji%2Fmodbus-31-TX-diagram-en.png?alt=media\&token=959a1d8e-e89e-444d-b1cf-dbd9314851fe)

Function Code 31 uses a mirror-response mechanism. When the write is successful, the RX response frame from the slave will exactly match the TX request in both format and content.

#### Read/Write Memory – Four Register Write Command (Function Code 31) Format

This example demonstrates how the **master** uses **Function Code 31** to write a target value of **+999,999,999** to the parameter **C04 Dosing Target**.

* **Register Address:** 02 83
* **Register Length:** 4 (4 consecutive registers, 64-bit)
* **Data Type:** Int64

#### 1. Value Conversion and Data Preparation (Hex Data)

* Target Value: `999,999,999` (decimal)
* Hex Conversion: `3B 9A C9 FF`。
* Packed in Int64 Format (8 Bytes):\
  Since Function Code 31 requires 8 bytes, prepend zeros:\
  Complete 64-bit Data:  `00 00 00 00 3B 9A C9 FF`

#### **2.** Master Sends Write Command (TX Frame)

```
TX： 01 31 02 83 00 00 00 00 3B 9A C9 FF 0B 7A
```

* Slave Address: `01`  (Flow meter address)
* Function Code: `31` (Write 4 Registers)
* Starting Register Address: `02 83` (C04 Dosing Target 64-bit memory address)
* Data to Write: `00 00 00 00 3B 9A C9 FF` (C04 Dosing Target 64-bit value)
* CRC: `0B 7A`  (Calculated CRC for error checking)

#### 3. Slave Response (RX Frame)

When the write is successful, the slave echoes the TX frame for verification.

```
RX: 01 31 02 83 00 00 00 00 3B 9A C9 FF 0B 7A
```
