# 讀寫型記憶體-四個Register寫入指令(31)

{% hint style="info" %}
當數值較大時，若分別以不同資料格式（例如 float32 或 int64）進行讀取與換算，因內部計算方式不同，可能會出現非常小的差異，通常小於 1/10,000，屬於正常現象。

限使用在 Register 長度為 4 的記憶體，[請參考列表。](/qr/fu-lt-ultrasonic-flowmeter-manual-chinese-v1/communication/modbus-rtu-protocol/du-xie-xing-ji-yi-ti/du-xie-xing-ji-yi-ti-du-qu-lie-biao.md)
{% endhint %}

### Function Code 功能碼 31

讀寫型記憶體 四個 Register 寫入指令，功能碼 31 ，4 個暫存器 (64 bit)，可寫入 Int64 等 64 位元的數值範圍。 當您設定 **D11 累計目標 (Total Target 64)** 或 **C04 批量控制 (Dosing Target 64)** 大數值參數時，建議使用功能碼 31。

{% hint style="info" %}
一般的 32 位元浮點數 (功能碼 30) 有效位數僅約 7 位，也就是當設定值超過千萬位，使用浮點數寫入會產生精準度失真與捨入誤差，此時建議使用功能碼 31 透過 64-bit 長整數 (Int64) 格式進行傳輸，以確保數據在記憶體中完整對應，不產生任何四捨五入的誤差。
{% endhint %}

### 讀寫型記憶體-四個Register寫入指令(31)格式

<div align="left"><figure><img src="/files/p29cPo51tEqDg0uEHzhv" alt="" width="563"><figcaption></figcaption></figure></div>

### Modbus RTU 讀寫型記憶體-四個Register寫入指令 (功能碼 31)

本範例將演示如何以主站使用功能碼 31 ，向位置 "C04 Dosing Target  批量控制 " ，將目標值設定為 +999,999,999。

根據 [讀寫型記憶體-讀取列表](/qr/fu-lt-ultrasonic-flowmeter-manual-chinese-v1/communication/modbus-rtu-protocol/du-xie-xing-ji-yi-ti/du-xie-xing-ji-yi-ti-du-qu-lie-biao.md)  ，該參數的起始位址為02 83，Reg 長度為4 ，資料型式為int64。

#### **1. 數值轉換與資料準備 (Hex Data)**

* 目標數值： `999,999,999` (十進位)
* 轉換為十六進位 (Hex)： 經換算可得 `3B 9A C9 FF`。
* 封裝為 Int64 格式 (8 Bytes)： 由於功能碼 31 固定要求 8 個 Byte，前面需補零：

  完整 64-bit 資料： `00 00 00 00 3B 9A C9 FF`

#### **2. 主站發送寫入指令 (TX 訊框)**

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

* Slave Address: 01 (本流量計地址)
* Function Code: 31 (寫入指令)
* 起始暫存器位址:  02 83 (寫入C04 Dosing Target(64) 記憶體地址)
* 寫入資料: 00 00 00 00 3B 9A C9 FF (寫入資料C04 Dosing Target(64) )
* CRC: 0B 7A (依 CRC 算法計算出的糾錯碼)

#### **3.** 從站回覆數據 (RX 訊框)

寫入指令成功時，從站會將收到的 TX 訊框完整回傳，作為檢查（確認）功能。

```
RX 回覆數據：01 31 02 83 00 00 00 00 3B 9A C9 FF 0B 7A
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lorric.com/qr/fu-lt-ultrasonic-flowmeter-manual-chinese-v1/communication/modbus-rtu-protocol/du-xie-xing-ji-yi-ti/du-xie-xing-ji-yi-ti-si-ge-register-xie-ru-zhi-ling-31.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
