|
- using DryIoc;
- using Microsoft.EntityFrameworkCore;
- using ModbusDemo.Device;
- using ModbusDemo.Model;
- using Moq;
- using Prism.Ioc;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ModbusTest
- {
- public class ModbusRTUTest
- {
-
- private SerialPort _serialport;
- private Mock<ModbusDbContext> _mockDbContext;
- private ModbusRTU _modbusRtu;
- [SetUp]
- public void Setup()
- {
- _serialport = new SerialPort();
- _serialport.PortName = "COM3";
- _serialport.DataBits = 8;
- _serialport.BaudRate = 9600;
- _serialport.Parity = Parity.Even;
- _serialport.StopBits = StopBits.One;
- _serialport.ReadTimeout = 500;
- _serialport.WriteTimeout = 500;
- _serialport.Open();
- _mockDbContext = new Mock<ModbusDbContext>();
- _modbusRtu = new ModbusRTU(_serialport, _mockDbContext.Object);
- }
- [TearDown]
- public void TearDown()
- {
- // 确保在测试结束后释放资源
- _serialport.Dispose();
- _modbusRtu.Dispose();
- }
- /// <summary>
- /// 测试线圈读取
- /// </summary>
- [Test]
- public void ReadCoil1()
- {
-
- var result = _modbusRtu.ReadCoil(1, 300, 5);
- var expected = new bool[5];
- Array.Fill(expected, false);
- CollectionAssert.AreEqual(expected, result);
- }
-
- [Test]
- public void ReadCoil2()
- {
- var result = _modbusRtu.ReadCoil(1, 305, 5);
-
- var expected = new bool[5];
- Array.Fill(expected, true);
- CollectionAssert.AreEqual(expected, result);
-
- }
-
- /// <summary>
- /// 测试写入线圈
- /// </summary>
- [Test]
- public void WriteCoil1()
- {
- var data = new bool[5];
- Array.Fill(data, true);
- _modbusRtu.WriteCoil(1, 310, data);
-
- var expected = _modbusRtu.ReadCoil(1, 310, 5);
- CollectionAssert.AreEqual(data, expected);
-
- }
-
-
-
- [Test]
- public void WriteCoil2()
- {
- var data = new bool[5];
- Array.Fill(data, false);
- _modbusRtu.WriteCoil(1, 315, data);
-
- var expected = _modbusRtu.ReadCoil(1, 315, 5);
- CollectionAssert.AreEqual(data, expected);
-
- }
-
- /// <summary>
- /// 测试读取寄存器
- /// </summary>
- [Test]
- public void ReadRegister1()
- {
- ushort[] data = new ushort[5];
- Array.Fill(data, (ushort)0);
- var expected = _modbusRtu.ReadRegister(1, 300, 5);
- CollectionAssert.AreEqual(data, expected);
- }
-
-
-
- [Test]
- public void ReadRegister2()
- {
- ushort[] data = new ushort[5];
- Array.Fill(data, (ushort)1);
-
- var expected = _modbusRtu.ReadRegister(1, 305, 5);
- CollectionAssert.AreEqual(data, expected);
- }
-
-
- /// <summary>
- /// 测试写入寄存器
- /// </summary>
- [Test]
- public void WriteRegister1()
- {
- ushort[] data = new ushort[5];
- Array.Fill(data, (ushort)1);
-
- _modbusRtu.WriteRegister(1, 310, data);
- var expected = _modbusRtu.ReadRegister(1, 310, 5);
- CollectionAssert.AreEqual(data, expected);
- }
-
-
- [Test]
- public void WriteRegister2()
- {
- ushort[] data = new ushort[5];
- Array.Fill(data, (ushort)2);
-
- _modbusRtu.WriteRegister(1, 315, data);
- var expected = _modbusRtu.ReadRegister(1, 315, 5);
- CollectionAssert.AreEqual(data, expected);
- }
-
-
- [Test]
- public void CRC1()
- {
- byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };
- int length = data.Length;
-
- byte[] crc = ModbusRTU.CalculateCRC(data, length);
- // Assert
- Assert.That(crc[0], Is.EqualTo(0x77));
- Assert.That(crc[1], Is.EqualTo(0xF3));
- }
-
- [Test]
- public void CRC2()
- {
- byte[] data = { 0x49, 0x66, 0x51, 0x6C, 0x7F };
- int length = data.Length;
-
- byte[] crc = ModbusRTU.CalculateCRC(data, length);
- // Assert
- Assert.That(crc[0], Is.EqualTo(0xDA));
- Assert.That(crc[1], Is.EqualTo(0x77));
- }
-
-
- [Test]
- public void CRC3()
- {
- byte[] data = { 0x01, 0x90, 0x03, 0x0C, 0x01 };
-
-
- bool isTrue = ModbusRTU.ValidateCRC(data);
- // Assert
- Assert.IsTrue(isTrue);
- }
- [Test]
- public void CRC4()
- {
- byte[] data = { 0x01, 0x90, 0x03, 0x0C, 0x00 };
-
-
- bool isFalse = ModbusRTU.ValidateCRC(data);
- // Assert
- Assert.IsFalse(isFalse);
- }
- }
- ///// <summary>
- ///// 解析线圈的返回值
- ///// </summary>
- //[Test]
- //public void ParseCoilresponse1()
- //{
- // var data = new bool[5];
- // Array.Fill(data, true);
- // byte[] response = new byte[] { 0x01, 0x01, 0x01, 0x7f, 0x10, 0x68 };
- // bool[] bools = _modbusRtu.ParseCoilresponse(new byte[] { }, response, 5);
-
- // CollectionAssert.AreEqual(data, bools);
- //}
-
- //[Test]
- //public void ParseCoilresponse2()
- //{
- // var data = new bool[5];
- // Array.Fill(data, false);
- // byte[] response = new byte[] { 0x01, 0x01, 0x01, 0x00, 0x51, 0x88 };
- // bool[] result = _modbusRtu.ParseCoilresponse(new byte[] { }, response, 5);
-
- // CollectionAssert.AreEqual(data, result);
- //}
-
- ///// <summary>
- ///// 解析寄存器的返回值
- ///// </summary>
- //[Test]
- //public void ParseRegistersresponse1()
- //{
- // ushort[] data = new ushort[5];
- // Array.Fill(data, (ushort)1);
- // byte[] response = new byte[] {0x01, 0x03, 0x0a, 0x00, 0x01, 0x00, 0x01, 0x00,
- // 0x01, 0x00, 0x01, 0x00, 0x01, 0x94, 0x26 };
-
- // ushort[] result = _modbusRtu.ParseRegistersresponse(new byte[] {},response, 5);
-
- // CollectionAssert.AreEqual(data, result);
- //}
-
- //[Test]
- //public void ParseRegistersresponse2()
- //{
- // ushort[] data = new ushort[2];
- // Array.Fill(data, (ushort)10);
- // byte[] response = new byte[] {0x01, 0x03, 0x04, 0x00,0x0a, 0x00, 0x0a, 0x5a, 0x36 };
-
- // ushort[] result = _modbusRtu.ParseRegistersresponse(new byte[] { }, response, 2);
-
- // CollectionAssert.AreEqual(data, result);
- //}
-
- }
|