Skip to content

feat(Modbus): add WriteRegisterAsync function#22

Merged
ArgoZhang merged 2 commits into
mainfrom
feat-write
Sep 4, 2025
Merged

feat(Modbus): add WriteRegisterAsync function#22
ArgoZhang merged 2 commits into
mainfrom
feat-write

Conversation

@ArgoZhang

Copy link
Copy Markdown
Member

Link issues

fixes #21

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Copilot AI review requested due to automatic review settings September 4, 2025 06:43
@ArgoZhang ArgoZhang merged commit f7545b8 into main Sep 4, 2025
1 check passed
@ArgoZhang ArgoZhang deleted the feat-write branch September 4, 2025 06:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 WriteSingleRegisterAsync with WriteRegisterAsync returning ValueTask<bool>
  • Updates WriteMultipleRegistersAsync to return ValueTask<bool> instead of Task
  • 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.

Comment on lines +115 to +122
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);

Copilot AI Sep 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
Comment on lines +115 to +122
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);

Copilot AI Sep 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
// 连接 Master
await client.ConnectAsync("127.0.0.1", 502);

// 读取 0x01 从站输入寄存器数据

Copilot AI Sep 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Suggested change
// 读取 0x01 从站输入寄存器数据
// Write value 12 to register 0 of slave 0x01

Copilot uses AI. Check for mistakes.
// 连接 Master
await client.ConnectAsync("127.0.0.1", 502);

// 读取 0x01 从站输入寄存器数据

Copilot AI Sep 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Suggested change
// 读取 0x01 从站输入寄存器数据
// Write multiple registers to slave 0x01

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(Modbus): add WriteRegisterAsync function

2 participants