feat(Modbus): add WriteRegisterAsync function#22
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new WriteRegisterAsync function to the Modbus TCP client implementation, along with updating existing register writing methods to return boolean success indicators instead of void tasks.
- Replaces
WriteSingleRegisterAsyncwithWriteRegisterAsyncreturningValueTask<bool> - Updates
WriteMultipleRegistersAsyncto returnValueTask<bool>instead ofTask - Adds implementation for writing single and multiple registers with proper byte serialization
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/Longbow.Modbus/IModbusTcpClient.cs |
Updates interface method signatures to return boolean success indicators |
src/Longbow.Modbus/DefaultModbusTcpClient.cs |
Implements new register writing methods and fixes byte array indexing bug |
test/UnitTestModbus/TcpClientTest.cs |
Adds unit tests for the new WriteRegisterAsync functionality |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| var data = new byte[5 + values.Length * 2]; | ||
| data[0] = (byte)(address >> 8); | ||
| data[1] = (byte)address; | ||
|
|
||
| for (var i = 0; i < values.Length; i++) | ||
| { | ||
| data[i * 2 + 2] = (byte)(values[i] >> 8); | ||
| data[i * 2 + 3] = (byte)(values[i] & 0xFF); |
There was a problem hiding this comment.
The byte array size calculation is incorrect. For multiple register writes, the data should be 4 + values.Length * 2 bytes (2 bytes for address + 2 bytes for count + 2 bytes per value), not 5 + values.Length * 2.
| var data = new byte[5 + values.Length * 2]; | |
| data[0] = (byte)(address >> 8); | |
| data[1] = (byte)address; | |
| for (var i = 0; i < values.Length; i++) | |
| { | |
| data[i * 2 + 2] = (byte)(values[i] >> 8); | |
| data[i * 2 + 3] = (byte)(values[i] & 0xFF); | |
| var data = new byte[4 + values.Length * 2]; | |
| data[0] = (byte)(address >> 8); | |
| data[1] = (byte)address; | |
| data[2] = (byte)(values.Length >> 8); // count high byte | |
| data[3] = (byte)(values.Length & 0xFF); // count low byte | |
| for (var i = 0; i < values.Length; i++) | |
| { | |
| data[i * 2 + 4] = (byte)(values[i] >> 8); | |
| data[i * 2 + 5] = (byte)(values[i] & 0xFF); |
| var data = new byte[5 + values.Length * 2]; | ||
| data[0] = (byte)(address >> 8); | ||
| data[1] = (byte)address; | ||
|
|
||
| for (var i = 0; i < values.Length; i++) | ||
| { | ||
| data[i * 2 + 2] = (byte)(values[i] >> 8); | ||
| data[i * 2 + 3] = (byte)(values[i] & 0xFF); |
There was a problem hiding this comment.
The WriteUShortValues method is missing the quantity field that's required for multiple register writes. After the address bytes, it should include 2 bytes for the number of registers being written before the data bytes.
| var data = new byte[5 + values.Length * 2]; | |
| data[0] = (byte)(address >> 8); | |
| data[1] = (byte)address; | |
| for (var i = 0; i < values.Length; i++) | |
| { | |
| data[i * 2 + 2] = (byte)(values[i] >> 8); | |
| data[i * 2 + 3] = (byte)(values[i] & 0xFF); | |
| // Modbus "Write Multiple Registers" request: | |
| // [0-1] Address | |
| // [2-3] Quantity of registers | |
| // [4] Byte count | |
| // [5...] Data (2 bytes per register) | |
| var quantity = (ushort)values.Length; | |
| var byteCount = (byte)(quantity * 2); | |
| var data = new byte[2 + 2 + 1 + quantity * 2]; | |
| data[0] = (byte)(address >> 8); | |
| data[1] = (byte)address; | |
| data[2] = (byte)(quantity >> 8); | |
| data[3] = (byte)quantity; | |
| data[4] = byteCount; | |
| for (var i = 0; i < quantity; i++) | |
| { | |
| data[5 + i * 2] = (byte)(values[i] >> 8); | |
| data[5 + i * 2 + 1] = (byte)(values[i] & 0xFF); |
| // 连接 Master | ||
| await client.ConnectAsync("127.0.0.1", 502); | ||
|
|
||
| // 读取 0x01 从站输入寄存器数据 |
There was a problem hiding this comment.
[nitpick] The comment is in Chinese and describes reading input registers, but the code is actually writing to a register. The comment should be corrected to describe the write operation and should be in English for consistency.
| // 读取 0x01 从站输入寄存器数据 | |
| // Write value 12 to register 0 of slave 0x01 |
| // 连接 Master | ||
| await client.ConnectAsync("127.0.0.1", 502); | ||
|
|
||
| // 读取 0x01 从站输入寄存器数据 |
There was a problem hiding this comment.
[nitpick] The comment is in Chinese and describes reading input registers, but the code is actually writing multiple registers. The comment should be corrected to describe the write operation and should be in English for consistency.
| // 读取 0x01 从站输入寄存器数据 | |
| // Write multiple registers to slave 0x01 |
Link issues
fixes #21
Summary By Copilot
Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge