# 讀寫型記憶體-雙Register寫入指令(30)

{% hint style="info" %}
當數值較大時，若以不同資料格式（例如 float32 與 int32）進行讀取與換算，因內部計算與表示方式不同，可能產生極小的量化差異，通常小於 1/10,000，屬於正常現象。

本指令僅適用於 資料長度為 32-bit（2 registers） 的記憶體項目，[請參考列表。](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-du-qu-lie-biao)
{% endhint %}

### Function Code 功能碼 30

讀寫型記憶體雙 Register 寫入指令，功能碼 30，用於寫入 2 個連續暫存器（共 32-bit / 4 bytes）的資料。適用於帶小數或正負數之參數設定Float32 範圍約為 1.18×10⁻³⁸ 至 3.4×10³⁸。 例如：設定A18 強制歸零 (Low Flow Cutoff ) 為 +5.5 、設定 A23 零點偏差校正 (Zero Offset) 為 -0.08 。

### 讀寫型記憶體-雙Register寫入指令(30) 格式

<div align="left"><figure><img src="https://1153145047-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cwNfn5rF1lQNDdmFBmA%2Fuploads%2Fat3DKBOBTnuMyqhuRJS5%2Fmodbus-30-TX-diagram.png?alt=media&#x26;token=41d1d644-9339-443a-a50b-06bae034a61c" alt="" width="563"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1153145047-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cwNfn5rF1lQNDdmFBmA%2Fuploads%2F2aDAqvo7iq4YkONEa1O0%2Fmodbus-30-RX-diagram.png?alt=media&#x26;token=c8d7ccbe-17fb-43cc-aa52-1e76b689d024" alt="" width="563"><figcaption></figcaption></figure></div>

### Modbus RTU 讀寫型記憶體-雙 Register 寫入指令 (功能碼 30)

以主站使用功能碼 30 ，目標向位置 "A18 Low Flow Cutoff  強制歸零" ，寫入 +5.5。

根據 [du-xie-xing-ji-yi-ti-du-qu-lie-biao](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-du-qu-lie-biao "mention")  ，該參數的起始位址為00 15，Reg 長度為 2   (二個暫存器)，資料型式為 float32。

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

將十進制數 +5.5 轉換為 IEEE 754 Float32 (Big Endian) 格式：

$$
5.5 ₁₀ = 40B00000 ₁₆
$$

這 4 個位元組的數據是：`40` `B0` `00` `00`。

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

主站使用功能碼 30，攜帶 4 個數據位元組（Byte Count 04），寫入從位址 `00 15` 開始的 2 個暫存器。

```
TX 請求指令： 01 30 00 15 00 02 04 40 B0 00 00 27 D1 
```

* Slave Address = 01
* Function Code = 30
* 讀取數據的啟始地址 = 00 15 (A18 Low Flow Cutoff 的記憶體地址)
* 寫入暫存器數量 = 00 02 (寫入 2 個暫存器 (Float32))
* 寫入位元組計數 = 04 (寫入 4 個數據位元組)
* 寫入資料 (Float32) 寫入 `40 B0 00 00`  (寫入 +5.5 的 Hex 數據)
* CRC 校驗碼 = 27 D1   (依 CRC 算法計算出的糾錯碼)

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

寫入指令成功時，主站將會收到從站回傳數據，作為檢查（確認）功能。

```
RX 回覆數據：01 30 00 15 00 02 D1 CB
```

* Slave Address = 01
* Function Code = 30
* 讀取數據的起始地址 = 00 15  (A18 Low Flow Cutoff 的記憶體地址)
* 寫入資料 = 00 02 (確認寫入 2 個暫存器)
* CRC 校驗碼 = D1 CB  (依 CRC 算法計算出的糾錯碼)

主站接收到 RX 回覆後，確認功能碼Function Code、位址、寫入資料暫存器數量都正確無誤，即可確認A18 Low Flow Cutoff 已成功設定為 +5.5 。
