MessageTest.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. using Edge.Core.Parser.BinaryParser.Util;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using ZhongSheng_NonIC_Pump;
  6. namespace ZhongSheng_NonIC_Pump_Test
  7. {
  8. [TestClass]
  9. public class MessageTest
  10. {
  11. public static bool ValueEquals(IEnumerable<byte> array1, IEnumerable<byte> array2)
  12. {
  13. if (array1 == null && array2 == null)
  14. {
  15. return true;
  16. }
  17. if ((array1 == null) || (array2 == null))
  18. {
  19. return false;
  20. }
  21. if (array1.Count() != array2.Count())
  22. {
  23. return false;
  24. }
  25. if (array1.Equals(array2))
  26. {
  27. return true;
  28. }
  29. else
  30. {
  31. for (int Index = 0; Index < array1.Count(); Index++)
  32. {
  33. if (!Equals(array1.ElementAt(Index), array2.ElementAt(Index)))
  34. {
  35. return false;
  36. }
  37. }
  38. }
  39. return true;
  40. }
  41. [TestMethod]
  42. public void Checksum_TestMethod1()
  43. {
  44. var data = new byte[] { 0x01, 0xFF, 0x01, 0x08, 0x30, 0x20, 0x19, 0x12, 0x01, 0x23, 0x59, 0x59 };
  45. var expect = 0x18;
  46. var actual = Parser.CaculateCheckSum(data);
  47. Assert.AreEqual(expect, actual);
  48. }
  49. [TestMethod]
  50. public void MessageCutter_TestMethod1()
  51. {
  52. var data = new byte[] { 0xFA, 0x01, 0xFF, 0x01, 0x08, 0x30, 0x20, 0x19, 0x12, 0x01, 0x23, 0x59, 0x59, 0x00 };
  53. var msgCutter = new MessageCutter();
  54. byte[] validMsg = null;
  55. msgCutter.OnMessageCut += (_, __) => { validMsg = msgCutter.Message; };
  56. msgCutter.Feed(data);
  57. Assert.IsNotNull(validMsg);
  58. Assert.AreEqual(true, ValueEquals(validMsg, data), "the actual is: 0x" + validMsg.ToHexLogString());
  59. }
  60. [TestMethod]
  61. public void MessageCutter_TestMethod2()
  62. {
  63. var data = new byte[] { 0x00, 0x01, 0x02, 0xFA, 0x01, 0xFF, 0x01, 0x01, 0x30, 0x00 };
  64. var msgCutter = new MessageCutter();
  65. byte[] validMsg = null;
  66. msgCutter.OnMessageCut += (_, __) => { validMsg = msgCutter.Message; };
  67. msgCutter.Feed(data);
  68. Assert.IsNotNull(validMsg);
  69. Assert.AreEqual(true, ValueEquals(validMsg, data.Skip(3)), "the actual is: 0x" + validMsg.ToHexLogString());
  70. }
  71. [TestMethod]
  72. public void MessageCutter_TestMethod3()
  73. {
  74. var data = new byte[] { 0x00,
  75. 0xFA, 0x01, 0xFF, 0x01, 0x01, 0x30, 0x00,
  76. 0x01, 0x02,
  77. 0xFA, 0x01, 0xFF, 0x01, 0x02, 0x30, 0x31, 0x00 };
  78. var msgCutter = new MessageCutter();
  79. List<byte[]> validMsgs = new List<byte[]>();
  80. msgCutter.OnMessageCut += (_, __) => { validMsgs.Add(msgCutter.Message); };
  81. msgCutter.Feed(data);
  82. Assert.AreEqual(2, validMsgs.Count);
  83. Assert.AreEqual(true, ValueEquals(validMsgs[0], new byte[] { 0xFA, 0x01, 0xFF, 0x01, 0x01, 0x30, 0x00 }), "the actual[0] is: 0x" + validMsgs[0].ToHexLogString());
  84. Assert.AreEqual(true, ValueEquals(validMsgs[1], new byte[] { 0xFA, 0x01, 0xFF, 0x01, 0x02, 0x30, 0x31, 0x00 }), "the actual[1] is: 0x" + validMsgs[1].ToHexLogString());
  85. }
  86. [TestMethod]
  87. public void GenericInquiryRequest_TestMethod1()
  88. {
  89. var msg = new GenericInquiryRequest(new System.DateTime(2019, 12, 1, 23, 59, 59));
  90. msg.TargetAddress = 1;
  91. msg.SourceAddress = 0xFF;
  92. msg.FrameSeqNo = 1;
  93. var parser = new Parser();
  94. var actual = parser.Serialize(msg);
  95. var expect = new byte[] { 0xFA, 0x01, 0xFF, 0x01, 0x08, 0x30, 0x20, 0x19, 0x12, 0x01, 0x23, 0x59, 0x59, 0x18 };
  96. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  97. }
  98. [TestMethod]
  99. public void PumpInIdleResponse_TestMethod1()
  100. {
  101. var raw = new byte[] {
  102. 0xFA, 0x01, 0xFF,
  103. // frameNo.
  104. 0x01,
  105. //length code
  106. 0x08,
  107. //command code
  108. 0x31,
  109. 0x00,
  110. 0x18 };
  111. var parser = new Parser();
  112. var actual = parser.Deserialize(raw);
  113. Assert.IsNotNull(actual);
  114. Assert.IsTrue(actual is PumpInIdleResponse);
  115. }
  116. [TestMethod]
  117. public void PumpInOperationResponse_FuellingState_TestMethod1()
  118. {
  119. var raw = new byte[] {
  120. 0xFA, 0x01, 0xFF,
  121. // frameNo.
  122. 0x01,
  123. //length code
  124. 0x0E,
  125. //command code
  126. 0x31,
  127. //信息计数n(1字节)
  128. 0x01,
  129. //状态字 枪号 金额(带2位小数) 升数(带2位小数) 价格(带2位小数)
  130. 0x01, 0x01, 0x00,0x00,0x00,0x01, 0x00,0x00,0x02, 0x00,0x00,0x03,
  131. //checksum
  132. 0x00
  133. };
  134. var parser = new Parser();
  135. var actual = parser.Deserialize(raw) as PumpInOperationResponse;
  136. Assert.IsNotNull(actual);
  137. Assert.IsTrue(actual.PumpStateBlocks == null);
  138. Assert.IsTrue(actual.FuellingDataBlocks.Count() == 1);
  139. Assert.IsTrue(actual.FuellingDataBlocks.First().NozzleNumber == 1);
  140. Assert.IsTrue(actual.FuellingDataBlocks.First().Amount == 1);
  141. Assert.IsTrue(actual.FuellingDataBlocks.First().Volume == 2);
  142. Assert.IsTrue(actual.FuellingDataBlocks.First().Price == 3);
  143. }
  144. [TestMethod]
  145. public void PumpInOperationResponse_FuellingState_TestMethod2()
  146. {
  147. var raw = new byte[] {
  148. 0xFA, 0x01, 0xFF,
  149. // frameNo.
  150. 0x01,
  151. //length code
  152. 0x0E,
  153. //command code
  154. 0x31,
  155. //信息计数n(1字节)
  156. 0x01,
  157. //状态字 枪号 金额(带2位小数) 升数(带2位小数) 价格(带2位小数)
  158. 0x01, 0x09, 0x00,0x00,0x00,0x05, 0x00,0x00,0x06, 0x00,0x00,0x07,
  159. //checksum
  160. 0x00
  161. };
  162. var parser = new Parser();
  163. var actual = parser.Deserialize(raw) as PumpInOperationResponse;
  164. Assert.IsNotNull(actual);
  165. Assert.IsTrue(actual.PumpStateBlocks == null);
  166. Assert.IsTrue(actual.FuellingDataBlocks.Count() == 1);
  167. Assert.IsTrue(actual.FuellingDataBlocks.First().NozzleNumber == 9);
  168. Assert.IsTrue(actual.FuellingDataBlocks.First().Amount == 5);
  169. Assert.IsTrue(actual.FuellingDataBlocks.First().Volume == 6);
  170. Assert.IsTrue(actual.FuellingDataBlocks.First().Price == 7);
  171. }
  172. [TestMethod]
  173. public void PumpInOperationResponse_FuellingState_TestMethod3()
  174. {
  175. var raw = new byte[] {
  176. 0xFA, 0x01, 0xFF,
  177. // frameNo.
  178. 0x01,
  179. //length code
  180. 0x0E,
  181. //command code
  182. 0x31,
  183. //信息计数n(1字节)
  184. 0x02,
  185. //状态字 枪号 金额(带2位小数) 升数(带2位小数) 价格(带2位小数)
  186. 0x01, 0x08, 0x00,0x00,0x00,0x05, 0x00,0x00,0x06, 0x00,0x00,0x07,
  187. //状态字 枪号 金额(带2位小数) 升数(带2位小数) 价格(带2位小数)
  188. 0x01, 0x09, 0x00,0x00,0x00,0x07, 0x00,0x00,0x08, 0x00,0x00,0x09,
  189. //checksum
  190. 0x00
  191. };
  192. var parser = new Parser();
  193. var actual = parser.Deserialize(raw) as PumpInOperationResponse;
  194. Assert.IsNotNull(actual);
  195. Assert.IsTrue(actual.PumpStateBlocks == null);
  196. Assert.IsTrue(actual.FuellingDataBlocks.Count() == 2);
  197. Assert.IsTrue(actual.FuellingDataBlocks.First().NozzleNumber == 8);
  198. Assert.IsTrue(actual.FuellingDataBlocks.First().Amount == 5);
  199. Assert.IsTrue(actual.FuellingDataBlocks.First().Volume == 6);
  200. Assert.IsTrue(actual.FuellingDataBlocks.First().Price == 7);
  201. Assert.IsTrue(actual.FuellingDataBlocks.Skip(1).First().NozzleNumber == 9);
  202. Assert.IsTrue(actual.FuellingDataBlocks.Skip(1).First().Amount == 7);
  203. Assert.IsTrue(actual.FuellingDataBlocks.Skip(1).First().Volume == 8);
  204. Assert.IsTrue(actual.FuellingDataBlocks.Skip(1).First().Price == 9);
  205. }
  206. [TestMethod]
  207. public void PumpInOperationResponse_PumpState_TestMethod1()
  208. {
  209. var raw = new byte[] {
  210. 0xFA, 0x01, 0xFF,
  211. // frameNo.
  212. 0x01,
  213. //length code
  214. 0x0E,
  215. //command code
  216. 0x31,
  217. //信息计数n(1字节)
  218. 0x01,
  219. //状态字 枪号 支付模式 枪状态 授权状态
  220. 0x02, 0x01, 0x04, 0x00, 0x00,
  221. //checksum
  222. 0x00
  223. };
  224. var parser = new Parser();
  225. var actual = parser.Deserialize(raw) as PumpInOperationResponse;
  226. Assert.IsNotNull(actual);
  227. Assert.IsTrue(actual.FuellingDataBlocks == null);
  228. Assert.IsTrue(actual.PumpStateBlocks != null);
  229. Assert.IsTrue(actual.PumpStateBlocks.Count() == 1);
  230. Assert.IsTrue(actual.PumpStateBlocks.First().NozzleNumber == 1);
  231. Assert.IsTrue(actual.PumpStateBlocks.First().AuthMode == PumpAuthModeEnum.需中控授权模式);
  232. Assert.IsTrue(actual.PumpStateBlocks.First().NozzleLiftState == NozzleLiftStateEnum.未提);
  233. Assert.IsTrue(actual.PumpStateBlocks.First().PumpAuthorizeState == PumpAuthorizeStateEnum.未授权);
  234. }
  235. [TestMethod]
  236. public void PumpInOperationResponse_PumpState_TestMethod2()
  237. {
  238. var raw = new byte[] {
  239. 0xFA, 0x01, 0xFF,
  240. // frameNo.
  241. 0x01,
  242. //length code
  243. 0x0E,
  244. //command code
  245. 0x31,
  246. //信息计数n(1字节)
  247. 0x02,
  248. //状态字 枪号 支付模式 枪状态 授权状态
  249. 0x02, 0x01, 0x00, 0x01, 0x01,
  250. //状态字 枪号 支付模式 枪状态 授权状态
  251. 0x02, 0x03, 0x04, 0x02, 0x00,
  252. //checksum
  253. 0x00
  254. };
  255. var parser = new Parser();
  256. var actual = parser.Deserialize(raw) as PumpInOperationResponse;
  257. Assert.IsNotNull(actual);
  258. Assert.IsTrue(actual.FuellingDataBlocks == null);
  259. Assert.IsTrue(actual.PumpStateBlocks != null);
  260. Assert.IsTrue(actual.PumpStateBlocks.Count() == 2);
  261. Assert.IsTrue(actual.PumpStateBlocks.First().NozzleNumber == 1);
  262. Assert.IsTrue(actual.PumpStateBlocks.First().AuthMode == PumpAuthModeEnum.自授权模式);
  263. Assert.IsTrue(actual.PumpStateBlocks.First().NozzleLiftState == NozzleLiftStateEnum.已提);
  264. Assert.IsTrue(actual.PumpStateBlocks.First().PumpAuthorizeState == PumpAuthorizeStateEnum.授权成功);
  265. Assert.IsTrue(actual.PumpStateBlocks.Skip(1).First().NozzleNumber == 3);
  266. Assert.IsTrue(actual.PumpStateBlocks.Skip(1).First().AuthMode == PumpAuthModeEnum.自授权模式);
  267. Assert.IsTrue(actual.PumpStateBlocks.Skip(1).First().NozzleLiftState == NozzleLiftStateEnum.第二条枪提起);
  268. Assert.IsTrue(actual.PumpStateBlocks.Skip(1).First().PumpAuthorizeState == PumpAuthorizeStateEnum.未授权);
  269. }
  270. [TestMethod]
  271. public void PumpInOperationResponse_PumpState_TestMethod3()
  272. {
  273. //0xFA E0 FF 0B 15 31
  274. //02
  275. //02 01 00 00 00 00 00
  276. //01 02 00 00 00 00 00 00 00 00 03 20
  277. //08
  278. var raw = new byte[] {
  279. 0xFA, 0xE0, 0xFF,
  280. // frameNo.
  281. 0x0B,
  282. //length code
  283. 0x15,
  284. //command code
  285. 0x31,
  286. //信息计数n(1字节)
  287. 0x02,
  288. //状态字 枪号 支付模式 状态 枪状态 授权状态
  289. 0x02, 0x01, 0x00, 0x00,0x00, 0x00, 0x00,
  290. //状态字 枪号 金额 升数 价格
  291. 0x01, 0x02, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00, 0x00, 0x03, 0x20,
  292. //checksum
  293. 0x20
  294. };
  295. var parser = new Parser();
  296. var actual = parser.Deserialize(raw) as PumpInOperationResponse;
  297. Assert.IsNotNull(actual);
  298. Assert.IsTrue(actual.PumpStateBlocks != null);
  299. Assert.IsTrue(actual.PumpStateBlocks.Count() == 1);
  300. Assert.IsTrue(actual.PumpStateBlocks.First().NozzleNumber == 1);
  301. Assert.IsTrue(actual.PumpStateBlocks.First().AuthMode == PumpAuthModeEnum.自授权模式);
  302. Assert.IsTrue(actual.PumpStateBlocks.First().NozzleLiftState == NozzleLiftStateEnum.未提);
  303. Assert.IsTrue(actual.PumpStateBlocks.First().PumpAuthorizeState == PumpAuthorizeStateEnum.未授权);
  304. Assert.IsTrue(actual.FuellingDataBlocks != null);
  305. Assert.IsTrue(actual.FuellingDataBlocks.Count() == 1);
  306. Assert.IsTrue(actual.FuellingDataBlocks.First().Amount == 0);
  307. Assert.IsTrue(actual.FuellingDataBlocks.First().Volume == 0);
  308. Assert.IsTrue(actual.FuellingDataBlocks.First().Price == (3 * 16 * 16 + 2 * 16));
  309. }
  310. [TestMethod]
  311. public void PumpNotifyTransactionDoneEvent_TestMethod1()
  312. {
  313. var raw = new byte[] {
  314. 0xFA, 0x01, 0xFF,
  315. // frameNo.
  316. 0x01,
  317. //length code
  318. 0x30,
  319. //command code
  320. 0x32,
  321. //交易金额 交易油量 交易单价 交易时间
  322. 0x00,0x00,0x00,0x01, 0x00,0x00,0x02, 0x00,0x00,0x03, 0x20,0x20,0x01,0x08,0x16,0x17,0x59,
  323. //交易枪号 油量总量 金额总量 支付模式
  324. 0x09, 0x00,0x00,0x00,0x04, 0x00,0x00,0x00,0x00,0x05, 0x06,
  325. //checksum
  326. 0x00 };
  327. var parser = new Parser();
  328. var actual = parser.Deserialize(raw) as PumpNotifyTransactionDoneEvent;
  329. Assert.IsNotNull(actual);
  330. Assert.IsTrue(actual.交易金额 == 1);
  331. Assert.IsTrue(actual.交易油量 == 2);
  332. Assert.IsTrue(actual.交易单价 == 3);
  333. Assert.IsTrue(actual.交易时间_Raw == "20200108161759");
  334. Assert.IsTrue(actual.交易时间 == new System.DateTime(2020, 01, 08, 16, 17, 59));
  335. Assert.IsTrue(actual.交易枪号 == 9);
  336. Assert.IsTrue(actual.油量总量 == 4);
  337. Assert.IsTrue(actual.金额总量 == 5);
  338. Assert.IsTrue(actual.支付模式 == 6);
  339. }
  340. [TestMethod]
  341. public void AckPumpTransactionRequest_TestMethod1()
  342. {
  343. var msg = new AckPumpTransactionRequest(9);
  344. msg.TargetAddress = 1;
  345. msg.SourceAddress = 0xFF;
  346. var parser = new Parser();
  347. var actual = parser.Serialize(msg);
  348. var expect = new byte[] { 0xFA, 0x01, 0xFF, 0x09,
  349. 0x02,
  350. 0x32, 0x00, 0x0F };
  351. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  352. }
  353. [TestMethod]
  354. public void ReadPumpAccumulatorResponse_TestMethod1()
  355. {
  356. var raw = new byte[] {
  357. 0xFA, 0x01, 0xFF,
  358. // frameNo.
  359. 0x01,
  360. //length code
  361. 0x30,
  362. //command code
  363. 0x33,
  364. //信息计数n
  365. 0x01,
  366. //枪号
  367. 0x03,
  368. //金额累计(带2位小数)
  369. 0x00,0x00,0x00,0x00,0x05,
  370. //油量累计(带2位小数)
  371. 0x00,0x00,0x00,0x09,
  372. //checksum
  373. 0x00 };
  374. var parser = new Parser();
  375. var actual = parser.Deserialize(raw) as ReadPumpAccumulatorResponse;
  376. Assert.IsNotNull(actual);
  377. Assert.IsTrue(actual.信息计数 == 1);
  378. Assert.IsTrue(actual.NozzleAndAccumulators != null);
  379. Assert.IsTrue(actual.NozzleAndAccumulators.Count == 1);
  380. Assert.IsTrue(actual.NozzleAndAccumulators.First().枪号 == 3);
  381. Assert.IsTrue(actual.NozzleAndAccumulators.First().金额累计 == 5);
  382. Assert.IsTrue(actual.NozzleAndAccumulators.First().油量累计 == 9);
  383. }
  384. [TestMethod]
  385. public void ReadPumpAccumulatorResponse_TestMethod2()
  386. {
  387. var raw = new byte[] {
  388. 0xFA, 0x01, 0xFF,
  389. // frameNo.
  390. 0x01,
  391. //length code
  392. 0x30,
  393. //command code
  394. 0x33,
  395. //信息计数n
  396. 0x02,
  397. //枪号
  398. 0x03,
  399. //金额累计(带2位小数)
  400. 0x00,0x00,0x00,0x00,0x05,
  401. //油量累计(带2位小数)
  402. 0x00,0x00,0x00,0x09,
  403. //枪号
  404. 0x04,
  405. //金额累计(带2位小数)
  406. 0x00,0x00,0x00,0x00,0x06,
  407. //油量累计(带2位小数)
  408. 0x00,0x00,0x00,0x07,
  409. //checksum
  410. 0x00 };
  411. var parser = new Parser();
  412. var actual = parser.Deserialize(raw) as ReadPumpAccumulatorResponse;
  413. Assert.IsNotNull(actual);
  414. Assert.IsTrue(actual.信息计数 == 2);
  415. Assert.IsTrue(actual.NozzleAndAccumulators != null);
  416. Assert.IsTrue(actual.NozzleAndAccumulators.Count == 2);
  417. Assert.IsTrue(actual.NozzleAndAccumulators.First().枪号 == 3);
  418. Assert.IsTrue(actual.NozzleAndAccumulators.First().金额累计 == 5);
  419. Assert.IsTrue(actual.NozzleAndAccumulators.First().油量累计 == 9);
  420. Assert.IsTrue(actual.NozzleAndAccumulators.Skip(1).First().枪号 == 4);
  421. Assert.IsTrue(actual.NozzleAndAccumulators.Skip(1).First().金额累计 == 6);
  422. Assert.IsTrue(actual.NozzleAndAccumulators.Skip(1).First().油量累计 == 7);
  423. }
  424. [TestMethod]
  425. public void ChangePumpPriceRequest_TestMethod1()
  426. {
  427. var msg = new ChangePumpPriceRequest(2, 9);
  428. msg.TargetAddress = 1;
  429. msg.SourceAddress = 0xFF;
  430. msg.FrameSeqNo = 3;
  431. var parser = new Parser();
  432. var actual = parser.Serialize(msg);
  433. var expect = new byte[] {
  434. //数据包头, 目标地址, 源地址, 帧号/控制
  435. 0xFA, 0x01, 0xFF, 0x03,
  436. //有效数据长度
  437. 0x06,
  438. //有效数据
  439. 0x34, 0x01, 0x02,0x00,0x00,0x09,
  440. //数据校验
  441. 0x3B };
  442. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  443. }
  444. [TestMethod]
  445. public void ChangePumpPriceRequest_TestMethod2()
  446. {
  447. var msg = new ChangePumpPriceRequest(
  448. new[] {
  449. new NozzleAndPrice() {
  450. 枪号 = 4,
  451. 油价 = 6},
  452. new NozzleAndPrice() {
  453. 枪号 = 5,
  454. 油价 = 7},
  455. new NozzleAndPrice() {
  456. 枪号 = 6,
  457. 油价 = 8},
  458. });
  459. msg.TargetAddress = 1;
  460. msg.SourceAddress = 0xFF;
  461. msg.FrameSeqNo = 3;
  462. var parser = new Parser();
  463. var actual = parser.Serialize(msg);
  464. var expect = new byte[] {
  465. //数据包头, 目标地址, 源地址, 帧号/控制
  466. 0xFA, 0x01, 0xFF, 0x03,
  467. //有效数据长度
  468. 0x0E,
  469. //有效数据
  470. 0x34, 0x03, 0x04,0x00,0x00,0x06, 0x05,0x00,0x00,0x07, 0x06,0x00,0x00,0x08,
  471. //数据校验
  472. 0x5E };
  473. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  474. }
  475. [TestMethod]
  476. public void StartPumpRequest_TestMethod1()
  477. {
  478. var msg = new StartPumpRequest(2, StartPumpRequest.PresetType.定金额加油, 9);
  479. msg.TargetAddress = 1;
  480. msg.SourceAddress = 0xFF;
  481. msg.FrameSeqNo = 3;
  482. var parser = new Parser();
  483. var actual = parser.Serialize(msg);
  484. var expect = new byte[] {
  485. //数据包头, 目标地址, 源地址, 帧号/控制
  486. 0xFA, 0x01, 0xFF, 0x03,
  487. //有效数据长度
  488. 0x07,
  489. //有效数据
  490. 0x35, 0x02, 0x02, 0x00,0x00,0x00,0x09,
  491. //数据校验
  492. 0x3E };
  493. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  494. }
  495. [TestMethod]
  496. public void StartPumpRequest_TestMethod2()
  497. {
  498. var msg = new StartPumpRequest(2, StartPumpRequest.PresetType.定油量体积加油, 9);
  499. msg.TargetAddress = 1;
  500. msg.SourceAddress = 0xFF;
  501. msg.FrameSeqNo = 3;
  502. var parser = new Parser();
  503. var actual = parser.Serialize(msg);
  504. var expect = new byte[] {
  505. //数据包头, 目标地址, 源地址, 帧号/控制
  506. 0xFA, 0x01, 0xFF, 0x03,
  507. //有效数据长度
  508. 0x07,
  509. //有效数据
  510. 0x35, 0x02, 0x01, 0x00,0x00,0x00,0x09,
  511. //数据校验
  512. 0x39 };
  513. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  514. }
  515. [TestMethod]
  516. public void StartPumpRequest_TestMethod3()
  517. {
  518. var msg = new StartPumpRequest(2, StartPumpRequest.PresetType.随意加油, 9);
  519. msg.TargetAddress = 1;
  520. msg.SourceAddress = 0xFF;
  521. msg.FrameSeqNo = 3;
  522. var parser = new Parser();
  523. var actual = parser.Serialize(msg);
  524. var expect = new byte[] {
  525. //数据包头, 目标地址, 源地址, 帧号/控制
  526. 0xFA, 0x01, 0xFF, 0x03,
  527. //有效数据长度
  528. 0x07,
  529. //有效数据
  530. 0x35, 0x02, 0x00, 0x00,0x00,0x00,0x09,
  531. //数据校验
  532. 0x38 };
  533. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  534. }
  535. [TestMethod]
  536. public void DisplayInfoRequest_TestMethod1()
  537. {
  538. var msg = new DisplayInfoRequest(2, "000", "123");
  539. msg.TargetAddress = 1;
  540. msg.SourceAddress = 0xFF;
  541. msg.FrameSeqNo = 3;
  542. var parser = new Parser();
  543. var actual = parser.Serialize(msg);
  544. var expect = new byte[] {
  545. //数据包头, 目标地址, 源地址, 帧号/控制
  546. 0xFA, 0x01, 0xFF, 0x03,
  547. //有效数据长度
  548. 0x09,
  549. //有效数据
  550. 0x37, 0x02, 0x30,0x30,0x30, 0x03, 0x31,0x32,0x33,
  551. //数据校验
  552. 0x0A };
  553. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  554. }
  555. [TestMethod]
  556. public void DisplayInfoRequest_TestMethod2()
  557. {
  558. var msg = new DisplayInfoRequest(9, "000", "12345");
  559. msg.TargetAddress = 1;
  560. msg.SourceAddress = 0xFF;
  561. msg.FrameSeqNo = 3;
  562. var parser = new Parser();
  563. var actual = parser.Serialize(msg);
  564. var expect = new byte[] {
  565. //数据包头, 目标地址, 源地址, 帧号/控制
  566. 0xFA, 0x01, 0xFF, 0x03,
  567. //有效数据长度
  568. 0x0B,
  569. //有效数据
  570. 0x37, 0x09, 0x30,0x30,0x30, 0x05, 0x31,0x32,0x33,0x34,0x35,
  571. //数据校验
  572. 0x68 };
  573. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  574. }
  575. [TestMethod]
  576. public void SetPumpConfigRequest_TestMethod1()
  577. {
  578. var msg = new SetPumpConfigRequest(SetPumpConfigRequest.ParameterTableTypeEnum.TableA,
  579. new[] {
  580. new NozzleConfig() {
  581. 枪号 = 9,
  582. 参数列表 = "12345"
  583. }
  584. });
  585. msg.TargetAddress = 1;
  586. msg.SourceAddress = 0xFF;
  587. msg.FrameSeqNo = 3;
  588. var parser = new Parser();
  589. var actual = parser.Serialize(msg);
  590. var expect = new byte[] {
  591. //数据包头, 目标地址, 源地址, 帧号/控制
  592. 0xFA, 0x01, 0xFF, 0x03,
  593. //有效数据长度
  594. 0x09,
  595. //有效数据 参数列表A: 0x01 枪号1
  596. 0x52, 0x01, 0x01, 0x09, 0x31,0x32,0x33,0x34,0x35,
  597. //数据校验
  598. 0x1B };
  599. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  600. }
  601. [TestMethod]
  602. public void SetPumpConfigRequest_TestMethod2()
  603. {
  604. var msg = new SetPumpConfigRequest(SetPumpConfigRequest.ParameterTableTypeEnum.TableB,
  605. new[] {
  606. new NozzleConfig() {
  607. 枪号 = 9,
  608. 参数列表 = "23456"
  609. }
  610. });
  611. msg.TargetAddress = 1;
  612. msg.SourceAddress = 0xFF;
  613. msg.FrameSeqNo = 3;
  614. var parser = new Parser();
  615. var actual = parser.Serialize(msg);
  616. var expect = new byte[] {
  617. //数据包头, 目标地址, 源地址, 帧号/控制
  618. 0xFA, 0x01, 0xFF, 0x03,
  619. //有效数据长度
  620. 0x09,
  621. //有效数据 参数列表A: 0x01 枪号1
  622. 0x52, 0x01, 0x02, 0x09, 0x32,0x33,0x34,0x35,0x36,
  623. //数据校验
  624. 0x1D };
  625. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  626. }
  627. [TestMethod]
  628. public void SetPumpConfigRequest_TestMethod3()
  629. {
  630. var msg = new SetPumpConfigRequest(SetPumpConfigRequest.ParameterTableTypeEnum.TableA,
  631. new[] {
  632. new NozzleConfig() {
  633. 枪号 = 7,
  634. 参数列表 = "12345"
  635. },
  636. new NozzleConfig() {
  637. 枪号 = 8,
  638. 参数列表 = "23456"
  639. }
  640. });
  641. msg.TargetAddress = 1;
  642. msg.SourceAddress = 0xFF;
  643. msg.FrameSeqNo = 3;
  644. var parser = new Parser();
  645. var actual = parser.Serialize(msg);
  646. var expect = new byte[] {
  647. //数据包头, 目标地址, 源地址, 帧号/控制
  648. 0xFA, 0x01, 0xFF, 0x03,
  649. //有效数据长度
  650. 0x0F,
  651. //有效数据 参数列表A: 0x01 枪号1 枪号2
  652. 0x52, 0x01, 0x01, 0x07, 0x31,0x32,0x33,0x34,0x35, 0x08, 0x32,0x33,0x34,0x35,0x36,
  653. //数据校验
  654. 0x6A };
  655. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  656. }
  657. [TestMethod]
  658. public void AckSetPumpConfigResponse_TestMethod1()
  659. {
  660. var raw = new byte[] {
  661. 0xFA, 0x01, 0xFF,
  662. // frameNo.
  663. 0x01,
  664. //length code
  665. 0x02,
  666. //command code
  667. 0x52,
  668. //参数[0x01 +状态(1BIN)] 参数状态为0x00表示设置成功,其他表示失败
  669. 0x01, 0x00,
  670. //checksum
  671. 0x00 };
  672. var parser = new Parser();
  673. var actual = parser.Deserialize(raw) as AckSetPumpConfigResponse;
  674. Assert.IsNotNull(actual);
  675. Assert.IsTrue(actual.FrameSeqNo == 0x01);
  676. Assert.IsTrue(actual.状态 == 0x00);
  677. }
  678. [TestMethod]
  679. public void AckSetPumpConfigResponse_TestMethod2()
  680. {
  681. var raw = new byte[] {
  682. 0xFA, 0x01, 0xFF,
  683. // frameNo.
  684. 0x29,
  685. //length code
  686. 0x02,
  687. //command code
  688. 0x52,
  689. //参数[0x01 +状态(1BIN)] 参数状态为0x00表示设置成功,其他表示失败
  690. 0x01, 0x99,
  691. //checksum
  692. 0x00 };
  693. var parser = new Parser();
  694. var actual = parser.Deserialize(raw) as AckSetPumpConfigResponse;
  695. Assert.IsNotNull(actual);
  696. Assert.IsTrue(actual.FrameSeqNo == 0x29);
  697. Assert.IsTrue(actual.状态 == 0x99);
  698. }
  699. [TestMethod]
  700. public void ReadPumpConfigRequest_TestMethod1()
  701. {
  702. var msg = new ReadPumpConfigRequest(SetPumpConfigRequest.ParameterTableTypeEnum.TableA, 9);
  703. msg.TargetAddress = 1;
  704. msg.SourceAddress = 0xFF;
  705. msg.FrameSeqNo = 3;
  706. var parser = new Parser();
  707. var actual = parser.Serialize(msg);
  708. var expect = new byte[] {
  709. //数据包头, 目标地址, 源地址, 帧号/控制
  710. 0xFA, 0x01, 0xFF, 0x03,
  711. //有效数据长度
  712. 0x04,
  713. //有效数据 参数[0x02+ 参数列表类型X(1BIN) + 枪数(1BCD)
  714. 0x52, 0x02, 0x01, 0x09,
  715. //数据校验
  716. 0x31 };
  717. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  718. }
  719. [TestMethod]
  720. public void ReadPumpConfigRequest_TestMethod2()
  721. {
  722. var msg = new ReadPumpConfigRequest(SetPumpConfigRequest.ParameterTableTypeEnum.TableB, 4);
  723. msg.TargetAddress = 1;
  724. msg.SourceAddress = 0xFF;
  725. msg.FrameSeqNo = 3;
  726. var parser = new Parser();
  727. var actual = parser.Serialize(msg);
  728. var expect = new byte[] {
  729. //数据包头, 目标地址, 源地址, 帧号/控制
  730. 0xFA, 0x01, 0xFF, 0x03,
  731. //有效数据长度
  732. 0x04,
  733. //有效数据 参数[0x02+ 参数列表类型X(1BIN) + 枪数(1BCD)
  734. 0x52, 0x02, 0x02, 0x04,
  735. //数据校验
  736. 0x35 };
  737. Assert.AreEqual(true, ValueEquals(expect, actual), "the actual is: 0x" + actual.ToHexLogString());
  738. }
  739. [TestMethod]
  740. public void ReadPumpConfigResponse_TestMethod1()
  741. {
  742. var raw = new byte[] {
  743. 0xFA, 0x01, 0xFF,
  744. // frameNo.
  745. 0x01,
  746. //length code
  747. 0x0A,
  748. //command code
  749. 0x52,
  750. //参数[0x02+ 参数列表类型X(1BIN) +枪数(1BCD)+ 枪号1(1BCD)+ 参数列表(5BCD)
  751. 0x02, 0x01, 0x01, 0x07, 0x31,0x32,0x33,0x34,0x35,
  752. //checksum
  753. 0x00 };
  754. var parser = new Parser();
  755. var actual = parser.Deserialize(raw) as ReadPumpConfigResponse;
  756. Assert.IsNotNull(actual);
  757. Assert.IsTrue(actual.FrameSeqNo == 0x01);
  758. Assert.IsTrue(actual.参数列表类型 == SetPumpConfigRequest.ParameterTableTypeEnum.TableA);
  759. Assert.IsNotNull(actual.NozzleConfigs);
  760. Assert.IsTrue(actual.NozzleConfigs.Count == 1);
  761. Assert.IsTrue(actual.NozzleConfigs.First().枪号 == 7);
  762. Assert.IsTrue(actual.NozzleConfigs.First().参数列表 == "12345");
  763. }
  764. [TestMethod]
  765. public void ReadPumpConfigResponse_TestMethod2()
  766. {
  767. var raw = new byte[] {
  768. 0xFA, 0x01, 0xFF,
  769. // frameNo.
  770. 0x01,
  771. //length code
  772. 0x10,
  773. //command code
  774. 0x52,
  775. //参数[0x02+ 参数列表类型X(1BIN) +枪数(1BCD)+ 枪号1(1BCD)+ 参数列表(5BCD)
  776. 0x02, 0x01, 0x01, 0x07, 0x31,0x32,0x33,0x34,0x35,
  777. //枪号2(1BCD)+ 参数列表(5BCD)
  778. 0x08, 0x32,0x33,0x34,0x35,0x36,
  779. //checksum
  780. 0x00 };
  781. var parser = new Parser();
  782. var actual = parser.Deserialize(raw) as ReadPumpConfigResponse;
  783. Assert.IsNotNull(actual);
  784. Assert.IsTrue(actual.FrameSeqNo == 0x01);
  785. Assert.IsTrue(actual.参数列表类型 == SetPumpConfigRequest.ParameterTableTypeEnum.TableA);
  786. Assert.IsNotNull(actual.NozzleConfigs);
  787. Assert.IsTrue(actual.NozzleConfigs.Count == 2);
  788. Assert.IsTrue(actual.NozzleConfigs.First().枪号 == 7);
  789. Assert.IsTrue(actual.NozzleConfigs.First().参数列表 == "12345");
  790. Assert.IsTrue(actual.NozzleConfigs.Skip(1).First().枪号 == 8);
  791. Assert.IsTrue(actual.NozzleConfigs.Skip(1).First().参数列表 == "23456");
  792. }
  793. [TestMethod]
  794. public void ReadPumpConfigResponse_TestMethod3()
  795. {
  796. var raw = new byte[] {
  797. 0xFA, 0x01, 0xFF,
  798. // frameNo.
  799. 0x04,
  800. //length code
  801. 0x10,
  802. //command code
  803. 0x52,
  804. //参数[0x02+ 参数列表类型X(1BIN) +枪数(1BCD)+ 枪号1(1BCD)+ 参数列表(5BCD)
  805. 0x02, 0x02, 0x01, 0x10, 0x31,0x32,0x33,0x34,0x35,
  806. //枪号2(1BCD)+ 参数列表(5BCD)
  807. 0x18, 0x32,0x33,0x34,0x35,0x36,
  808. //checksum
  809. 0x00 };
  810. var parser = new Parser();
  811. var actual = parser.Deserialize(raw) as ReadPumpConfigResponse;
  812. Assert.IsNotNull(actual);
  813. Assert.IsTrue(actual.FrameSeqNo == 0x04);
  814. Assert.IsTrue(actual.参数列表类型 == SetPumpConfigRequest.ParameterTableTypeEnum.TableB);
  815. Assert.IsNotNull(actual.NozzleConfigs);
  816. Assert.IsTrue(actual.NozzleConfigs.Count == 2);
  817. Assert.IsTrue(actual.NozzleConfigs.First().枪号 == 10);
  818. Assert.IsTrue(actual.NozzleConfigs.First().参数列表 == "12345");
  819. Assert.IsTrue(actual.NozzleConfigs.Skip(1).First().枪号 == 18);
  820. Assert.IsTrue(actual.NozzleConfigs.Skip(1).First().参数列表 == "23456");
  821. }
  822. //from real site log
  823. [TestMethod]
  824. public void WildResponse_TestMethod1()
  825. {
  826. var raw = "FA E0 FF 10 0C 31 02 02 01 03 00 00 02 02 03 00 00 0A".ToBytes();
  827. var parser = new Parser();
  828. var actual = parser.Deserialize(raw);
  829. }
  830. }
  831. }