modbus
modbus copied to clipboard
Add unit tests for modbus core
Adds comprehensive test coverage for the modbus core library to ensure correctness and stability.
Changes
- Migrated Core to .NET Standard 2.0 - Converted from .NET Framework 4.5 to enable modern tooling and cross-platform compatibility
-
Added 59 xUnit tests covering:
- CRC16 checksum computation (edge cases, offsets, empty ranges)
- Big-endian byte conversions for Int16/UInt16 types
- Struct serialization with endianness attributes and arrays
- RTU request builder (CRC validation, data appending, error cases)
- TCP request builder (MBAP header, auto-length computation)
- Constants validation
-
Exposed internal types via
InternalsVisibleTofor test access
Example Usage
[Fact]
public void Build_CrcValidation_ProducesCorrectCrc()
{
var builder = new RtuRequest.Builder()
.SetSlaveAddress(0x01)
.SetFunctionCode(0x03)
.SetInt16(0x0000)
.AddInt16(0x0002);
var request = builder.Build();
// For message: 01 03 00 00 00 02, CRC should be C4 0B
Assert.Equal(0xC4, request.RequestBytes[6]);
Assert.Equal(0x0B, request.RequestBytes[7]);
}
Original prompt
This section details on the original issue you should resolve
<issue_title>Add unit test for modbus core</issue_title> <issue_description>Ensure the core modbus work correctly and stable with bestpractice</issue_description>
Comments on the Issue (you are @copilot in this section)
- Fixes sontx/modbus#6
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.