终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

241 рядки
8.8 KiB

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