终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

240 řádky
8.8 KiB

  1. using BPA.Helper;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO.Ports;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace BPASmartClient.SerialPort
  11. {
  12. public class IceMakerHelper
  13. {
  14. private System.IO.Ports.SerialPort comPort = new System.IO.Ports.SerialPort();
  15. ConcurrentQueue<byte[]> SerialMessages = new ConcurrentQueue<byte[]>();
  16. public bool IsOpen => comPort.IsOpen;
  17. public bool Open(string portName, int baudRate)
  18. {
  19. while (!System.IO.Ports.SerialPort.GetPortNames().Contains(portName))
  20. {
  21. Thread.Sleep(1000);
  22. }
  23. while (!comPort.IsOpen)
  24. {
  25. comPort.PortName = portName;
  26. comPort.BaudRate = baudRate;
  27. comPort.DataBits = 8;
  28. comPort.Parity = Parity.None;
  29. comPort.StopBits = StopBits.One;
  30. comPort.ReadTimeout = 1000;
  31. comPort.WriteTimeout = 1000;
  32. //comPort.DataReceived += ComPort_DataReceived;
  33. comPort.RtsEnable = true;
  34. try
  35. {
  36. comPort.Open();
  37. }
  38. catch (Exception ex)
  39. {
  40. MessageLog.GetInstance.ShowEx(ex.ToString());
  41. Thread.Sleep(5000);
  42. }
  43. }
  44. MessageLog.GetInstance.Show($"{portName} 串口打开成功");
  45. return comPort.IsOpen;
  46. }
  47. /*
  48. 帧头FF 帧头FE 功能码 数据 1 数据 2 和校验 结束符
  49. 和校验:只校验功能码和数据。=功能码+数据
  50. 接口方式:RS485 波特率:9600
  51. 01 查询状态指令 需要主控板定时发送查询指令,解析制冰机自检后回复的状态信息,以判断是否可以出冰。
  52. 02 出冰指令 若制冰机状态为等待出冰,此时主控板可以发送出冰指令,以实现定量冰块输出。
  53. 03 结束出冰指令 制冰机出冰过程中,由主控板按一定的时间发送结束指令折算时间所对应的冰量
  54. 04 待机指令 当制冰机处于工作时,发送该指令使制冰机处于待机状态
  55. 05 开机指令 当制冰机处于待机时,发送该指令使制冰机,制满一桶冰。
  56. 06 抽水请求指令 预留抽水请求指令
  57. 07 版本查询指令 品牌+硬件版本+软件版本
  58. 09 冰量控制 通过定时开关方式控制制冰机出冰量
  59. */
  60. /// <summary>
  61. /// 获取制冰机状态
  62. /// </summary>
  63. /// <param name="index"></param>
  64. /// <returns></returns>
  65. public byte GetDeviceStatus()
  66. {
  67. List<int> res = new List<int>();
  68. byte[] buffers = new byte[7] { 0xFF, 0xFE, 0x01, 0x00, 0x00, 0x01, 0x5A };
  69. //发送 0xFF, 0xFE, 0x01, 0x00, 0x00, XX, 0x5A
  70. //返回 0xFF, 0xFD, XX , 0x01, 0x00, XX, 0x5A
  71. if (comPort.IsOpen)
  72. {
  73. comPort.Write(buffers, 0, buffers.Length);
  74. DateTime newDate = DateTime.Now;
  75. while (comPort.BytesToRead < 7)
  76. {
  77. Thread.Sleep(1);
  78. if (DateTime.Now.Subtract(newDate).TotalSeconds >= 2) break;
  79. }
  80. byte[] receive = new byte[comPort.BytesToRead];
  81. comPort.Read(receive, 0, receive.Length);
  82. if (receive.Length == 7)
  83. {
  84. return receive[2];
  85. }
  86. }
  87. return default;
  88. }
  89. /// <summary>
  90. /// 开始制做
  91. /// </summary>
  92. /// <returns>true:开始制作</returns>
  93. public bool StartCook()
  94. {
  95. byte[] buffers = new byte[7] { 0xFF, 0xFE, 0x02, 0x00, 0x00, 0x02, 0x5A };
  96. //发送 0xFF, 0xFE, 0x02, 0x00, 0x00, XX, 0x5A
  97. //返回 0xFF, 0xFD, 0x02, 0x00, 0x00, XX, 0x5A
  98. if (comPort.IsOpen)
  99. {
  100. comPort.Write(buffers, 0, buffers.Length);
  101. while (comPort.BytesToRead < 7)
  102. {
  103. Thread.Sleep(1);
  104. }
  105. byte[] receive = new byte[comPort.BytesToRead];
  106. comPort.Read(receive, 0, receive.Length);
  107. if (receive.Length == 7)
  108. return receive[1] == 0xFD;
  109. }
  110. return false;
  111. }
  112. /// <summary>
  113. /// 结束制作
  114. /// </summary>
  115. /// <returns></returns>
  116. public bool EndCook()
  117. {
  118. byte[] buffers = new byte[7] { 0xFF, 0xFE, 0x03, 0x00, 0x00, 0x03, 0x5A };
  119. //发送 0xFF, 0xFE, 0x03, 0x00, 0x00, XX, 0x5A
  120. //返回 0xFF, 0xFD, 0x03, 0x00, 0x00, XX, 0x5A
  121. if (comPort.IsOpen)
  122. {
  123. comPort.Write(buffers, 0, buffers.Length);
  124. while (comPort.BytesToRead < 7)
  125. {
  126. Thread.Sleep(1);
  127. }
  128. byte[] receive = new byte[comPort.BytesToRead];
  129. comPort.Read(receive, 0, receive.Length);
  130. if (receive.Length == 7)
  131. return receive[1] == 0xFD;
  132. }
  133. return false;
  134. }
  135. /// <summary>
  136. /// 待机
  137. /// </summary>
  138. /// <returns></returns>
  139. public bool Standby()
  140. {
  141. byte[] buffers = new byte[7] { 0xFF, 0xFE, 0x04, 0x00, 0x00, 0x04, 0x5A };
  142. //发送 0xFF, 0xFE, 0x04, 0x00, 0x00, XX, 0x5A
  143. //返回 0xFF, 0xFD, 0x04, 0x00, 0x00, XX, 0x5A
  144. if (comPort.IsOpen)
  145. {
  146. comPort.Write(buffers, 0, buffers.Length);
  147. while (comPort.BytesToRead < 7)
  148. {
  149. Thread.Sleep(1);
  150. }
  151. byte[] receive = new byte[comPort.BytesToRead];
  152. comPort.Read(receive, 0, receive.Length);
  153. if (receive.Length == 7)
  154. return receive[1] == 0xFD;
  155. }
  156. return default;
  157. }
  158. /// <summary>
  159. /// 开机
  160. /// </summary>
  161. /// <returns></returns>
  162. public bool PowerOn()
  163. {
  164. byte[] buffers = new byte[7] { 0xFF, 0xFE, 0x05, 0x00, 0x00, 0x05, 0x5A };
  165. //发送 0xFF, 0xFE, 0x05, 0x00, 0x00, XX, 0x5A
  166. //返回 0xFF, 0xFD, 0x05, 0x00, 0x00, XX, 0x5A
  167. if (comPort.IsOpen)
  168. {
  169. comPort.Write(buffers, 0, buffers.Length);
  170. while (comPort.BytesToRead < 7)
  171. {
  172. Thread.Sleep(1);
  173. }
  174. byte[] receive = new byte[comPort.BytesToRead];
  175. comPort.Read(receive, 0, receive.Length);
  176. if (receive.Length == 6)
  177. return receive[1] == 0xFD;
  178. }
  179. return default;
  180. }
  181. /// <summary>
  182. /// 抽水
  183. /// </summary>
  184. /// <returns>true:允许抽水,false:不允许抽水</returns>
  185. public bool pump()
  186. {
  187. byte[] buffers = new byte[7] { 0xFF, 0xFE, 0x06, 0x00, 0x00, 0x06, 0x5A };
  188. //发送 0xFF, 0xFE, 0x04, 0x00, 0x00, XX, 0x5A
  189. //返回 0xFF, 0xFD, 0x04, 0x00, 0x00, XX, 0x5A
  190. if (comPort.IsOpen)
  191. {
  192. comPort.Write(buffers, 0, buffers.Length);
  193. while (comPort.BytesToRead < 7)
  194. {
  195. Thread.Sleep(1);
  196. }
  197. byte[] receive = new byte[comPort.BytesToRead];
  198. comPort.Read(receive, 0, receive.Length);
  199. if (receive.Length == 7)
  200. return receive[3] == 0x00;
  201. }
  202. return default;
  203. }
  204. /// <summary>
  205. /// 冰量控制
  206. /// </summary>
  207. /// <param name="value">出冰时间 单位0.1s</param>
  208. /// <returns></returns>
  209. public bool IceValue(byte value)
  210. {
  211. byte checkNum = (byte)(0x09 + value);
  212. byte[] buffers = new byte[7] { 0xFF, 0xFE, 0x09, value, 0x00, checkNum, 0x5A };
  213. //发送 0xFF, 0xFE, 0x09, value, 0x00, XX, 0x5A
  214. //返回 0xFF, 0xFD, 0x09, 0x00, 0x00, XX, 0x5A
  215. if (comPort.IsOpen)
  216. {
  217. comPort.Write(buffers, 0, buffers.Length);
  218. while (comPort.BytesToRead < 7)
  219. {
  220. Thread.Sleep(1);
  221. }
  222. byte[] receive = new byte[comPort.BytesToRead];
  223. comPort.Read(receive, 0, receive.Length);
  224. if (receive.Length == 7)
  225. return receive[3] == 0x00;
  226. }
  227. return default;
  228. }
  229. }
  230. }