選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

249 行
6.8 KiB

  1. using DryIoc;
  2. using Microsoft.EntityFrameworkCore;
  3. using ModbusDemo.Device;
  4. using ModbusDemo.Model;
  5. using Moq;
  6. using Prism.Ioc;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO.Ports;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace ModbusTest
  14. {
  15. public class ModbusRTUTest
  16. {
  17. private SerialPort _serialport;
  18. private Mock<ModbusDbContext> _mockDbContext;
  19. private ModbusRTU _modbusRtu;
  20. [SetUp]
  21. public void Setup()
  22. {
  23. _serialport = new SerialPort();
  24. _serialport.PortName = "COM3";
  25. _serialport.DataBits = 8;
  26. _serialport.BaudRate = 9600;
  27. _serialport.Parity = Parity.Even;
  28. _serialport.StopBits = StopBits.One;
  29. _serialport.ReadTimeout = 500;
  30. _serialport.WriteTimeout = 500;
  31. _serialport.Open();
  32. _mockDbContext = new Mock<ModbusDbContext>();
  33. _modbusRtu = new ModbusRTU(_serialport, _mockDbContext.Object);
  34. }
  35. [TearDown]
  36. public void TearDown()
  37. {
  38. // 确保在测试结束后释放资源
  39. _serialport.Dispose();
  40. _modbusRtu.Dispose();
  41. }
  42. /// <summary>
  43. /// 测试线圈读取
  44. /// </summary>
  45. [Test]
  46. public void ReadCoil1()
  47. {
  48. var result = _modbusRtu.ReadCoil(1, 300, 5);
  49. var expected = new bool[5];
  50. Array.Fill(expected, false);
  51. CollectionAssert.AreEqual(expected, result);
  52. }
  53. [Test]
  54. public void ReadCoil2()
  55. {
  56. var result = _modbusRtu.ReadCoil(1, 305, 5);
  57. var expected = new bool[5];
  58. Array.Fill(expected, true);
  59. CollectionAssert.AreEqual(expected, result);
  60. }
  61. /// <summary>
  62. /// 测试写入线圈
  63. /// </summary>
  64. [Test]
  65. public void WriteCoil1()
  66. {
  67. var data = new bool[5];
  68. Array.Fill(data, true);
  69. _modbusRtu.WriteCoil(1, 310, data);
  70. var expected = _modbusRtu.ReadCoil(1, 310, 5);
  71. CollectionAssert.AreEqual(data, expected);
  72. }
  73. [Test]
  74. public void WriteCoil2()
  75. {
  76. var data = new bool[5];
  77. Array.Fill(data, false);
  78. _modbusRtu.WriteCoil(1, 315, data);
  79. var expected = _modbusRtu.ReadCoil(1, 315, 5);
  80. CollectionAssert.AreEqual(data, expected);
  81. }
  82. /// <summary>
  83. /// 测试读取寄存器
  84. /// </summary>
  85. [Test]
  86. public void ReadRegister1()
  87. {
  88. ushort[] data = new ushort[5];
  89. Array.Fill(data, (ushort)0);
  90. var expected = _modbusRtu.ReadRegister(1, 300, 5);
  91. CollectionAssert.AreEqual(data, expected);
  92. }
  93. [Test]
  94. public void ReadRegister2()
  95. {
  96. ushort[] data = new ushort[5];
  97. Array.Fill(data, (ushort)1);
  98. var expected = _modbusRtu.ReadRegister(1, 305, 5);
  99. CollectionAssert.AreEqual(data, expected);
  100. }
  101. /// <summary>
  102. /// 测试写入寄存器
  103. /// </summary>
  104. [Test]
  105. public void WriteRegister1()
  106. {
  107. ushort[] data = new ushort[5];
  108. Array.Fill(data, (ushort)1);
  109. _modbusRtu.WriteRegister(1, 310, data);
  110. var expected = _modbusRtu.ReadRegister(1, 310, 5);
  111. CollectionAssert.AreEqual(data, expected);
  112. }
  113. [Test]
  114. public void WriteRegister2()
  115. {
  116. ushort[] data = new ushort[5];
  117. Array.Fill(data, (ushort)2);
  118. _modbusRtu.WriteRegister(1, 315, data);
  119. var expected = _modbusRtu.ReadRegister(1, 315, 5);
  120. CollectionAssert.AreEqual(data, expected);
  121. }
  122. [Test]
  123. public void CRC1()
  124. {
  125. byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };
  126. int length = data.Length;
  127. byte[] crc = ModbusRTU.CalculateCRC(data, length);
  128. // Assert
  129. Assert.That(crc[0], Is.EqualTo(0x77));
  130. Assert.That(crc[1], Is.EqualTo(0xF3));
  131. }
  132. [Test]
  133. public void CRC2()
  134. {
  135. byte[] data = { 0x49, 0x66, 0x51, 0x6C, 0x7F };
  136. int length = data.Length;
  137. byte[] crc = ModbusRTU.CalculateCRC(data, length);
  138. // Assert
  139. Assert.That(crc[0], Is.EqualTo(0xDA));
  140. Assert.That(crc[1], Is.EqualTo(0x77));
  141. }
  142. [Test]
  143. public void CRC3()
  144. {
  145. byte[] data = { 0x01, 0x90, 0x03, 0x0C, 0x01 };
  146. bool isTrue = ModbusRTU.ValidateCRC(data);
  147. // Assert
  148. Assert.IsTrue(isTrue);
  149. }
  150. [Test]
  151. public void CRC4()
  152. {
  153. byte[] data = { 0x01, 0x90, 0x03, 0x0C, 0x00 };
  154. bool isFalse = ModbusRTU.ValidateCRC(data);
  155. // Assert
  156. Assert.IsFalse(isFalse);
  157. }
  158. }
  159. ///// <summary>
  160. ///// 解析线圈的返回值
  161. ///// </summary>
  162. //[Test]
  163. //public void ParseCoilresponse1()
  164. //{
  165. // var data = new bool[5];
  166. // Array.Fill(data, true);
  167. // byte[] response = new byte[] { 0x01, 0x01, 0x01, 0x7f, 0x10, 0x68 };
  168. // bool[] bools = _modbusRtu.ParseCoilresponse(new byte[] { }, response, 5);
  169. // CollectionAssert.AreEqual(data, bools);
  170. //}
  171. //[Test]
  172. //public void ParseCoilresponse2()
  173. //{
  174. // var data = new bool[5];
  175. // Array.Fill(data, false);
  176. // byte[] response = new byte[] { 0x01, 0x01, 0x01, 0x00, 0x51, 0x88 };
  177. // bool[] result = _modbusRtu.ParseCoilresponse(new byte[] { }, response, 5);
  178. // CollectionAssert.AreEqual(data, result);
  179. //}
  180. ///// <summary>
  181. ///// 解析寄存器的返回值
  182. ///// </summary>
  183. //[Test]
  184. //public void ParseRegistersresponse1()
  185. //{
  186. // ushort[] data = new ushort[5];
  187. // Array.Fill(data, (ushort)1);
  188. // byte[] response = new byte[] {0x01, 0x03, 0x0a, 0x00, 0x01, 0x00, 0x01, 0x00,
  189. // 0x01, 0x00, 0x01, 0x00, 0x01, 0x94, 0x26 };
  190. // ushort[] result = _modbusRtu.ParseRegistersresponse(new byte[] {},response, 5);
  191. // CollectionAssert.AreEqual(data, result);
  192. //}
  193. //[Test]
  194. //public void ParseRegistersresponse2()
  195. //{
  196. // ushort[] data = new ushort[2];
  197. // Array.Fill(data, (ushort)10);
  198. // byte[] response = new byte[] {0x01, 0x03, 0x04, 0x00,0x0a, 0x00, 0x0a, 0x5a, 0x36 };
  199. // ushort[] result = _modbusRtu.ParseRegistersresponse(new byte[] { }, response, 2);
  200. // CollectionAssert.AreEqual(data, result);
  201. //}
  202. }