终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

KHKJMachine.cs 3.4 KiB

2 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using BPASmartClient.Helper;
  2. using BPASmartClient.Message;
  3. using BPASmartClient.SerialPort;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BPASmartClient.KHKJ
  10. {
  11. public class KHKJMachine
  12. {
  13. //通讯代理
  14. SerialPortClient commProxy = null;
  15. //数据仓库
  16. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  17. //是否下发指令,主线程等待
  18. private bool free = false;
  19. //状态询问指令
  20. private byte[] cmdAsk;
  21. public KHKJMachine()
  22. {
  23. MainLoop();
  24. }
  25. public void Communication(string serialPort)
  26. {
  27. commProxy = new SerialPortClient(serialPort, BaudRates.BR_9600);
  28. commProxy.SetDataStorage(dataStorage);
  29. commProxy.Start();
  30. free = true;
  31. }
  32. private void MainLoop()
  33. {
  34. ThreadManage.GetInstance().StartLong(new Action(() =>
  35. {
  36. if (!free)
  37. {
  38. commProxy.SendData(cmdAsk);
  39. }
  40. Thread.Sleep(500);
  41. }), "KHKJ询问线程");
  42. ThreadManage.GetInstance().StartLong(new Action(() =>
  43. {
  44. ResolveMsg();
  45. }), "KHKJ解析线程");
  46. }
  47. private void ResolveMsg()
  48. {
  49. List<byte> temp = new List<byte>();
  50. //一系列解包
  51. while (dataStorage.GetSize() > 0)
  52. {
  53. byte item = dataStorage.GetData();
  54. List<byte> data = new List<byte>() { item };
  55. if (Encoding.ASCII.GetString(data.ToArray()) == ":")
  56. {
  57. temp.Add(item);
  58. while (dataStorage.GetSize() < 32) { Thread.Sleep(5); }
  59. while (temp.Count < 32)
  60. {
  61. temp.Add(dataStorage.GetData());
  62. }
  63. List<byte> vs = new List<byte>() { temp[temp.Count - 4], temp[temp.Count - 3], temp[temp.Count - 2], temp[temp.Count - 1] };
  64. string t = Encoding.ASCII.GetString(vs.ToArray()).ToLower();
  65. var package = Encoding.ASCII.GetString(temp.ToArray());
  66. //if (package.Contains("\\r\\n"))
  67. //{
  68. // ProcessMsg(package);
  69. //}
  70. temp.Clear();
  71. }
  72. continue;
  73. }
  74. Thread.Sleep(500);
  75. }
  76. public void Stop()
  77. {
  78. try
  79. {
  80. commProxy.Stop();
  81. free = true;
  82. }
  83. catch (Exception ex)
  84. {
  85. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  86. }
  87. }
  88. /// <summary>
  89. /// 16进制转字节数组
  90. /// </summary>
  91. /// <param name="hexstring"></param>
  92. /// <returns></returns>
  93. public byte[] HexStringToByteArray( string hexstring)
  94. {
  95. var byteArray = new byte[hexstring.Length/2];
  96. for (int i = 0; i < byteArray.Length; i++)
  97. {
  98. var x = Convert.ToInt32(hexstring.Substring(i * 2, 2), 16);
  99. byteArray[i] = (byte)x;
  100. }
  101. return byteArray;
  102. }
  103. }
  104. }