-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathModbusCrcTest.cs
More file actions
36 lines (29 loc) · 1.16 KB
/
ModbusCrcTest.cs
File metadata and controls
36 lines (29 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components.Utility;
namespace UnitTestTcpSocket;
public class ModbusCrcTest
{
[Fact]
public void Computer_Ok()
{
// 06 00 00 01 7F C9 BA
var data = new byte[] { 0x01, 0x06, 0x00, 0x00, 0x01, 0x7F };
var crc = ModbusCrc16.Compute(data);
Assert.Equal("BAC9", crc.ToString("X4"));
Assert.Equal("01060000017FC9BA", HexConverter.ToString(ModbusCrc16.Append(data), ""));
}
[Fact]
public void Validate_Ok()
{
var result = ModbusCrc16.Validate([0x01]);
Assert.False(result);
result = ModbusCrc16.Validate([0x01, 0x06, 0x00, 0x00, 0x01, 0x7F, 0xC9, 0xBA]);
Assert.True(result);
result = false;
var data = Enumerable.Range(0, 300).Select(i => (byte)Random.Shared.Next(0, 255));
result = ModbusCrc16.Validate(ModbusCrc16.Append(data.ToArray()));
Assert.True(result);
}
}