终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KHKJMachine.cs 3.4 KiB

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