终端一体化运控平台
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.
 
 
 

429 lines
12 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.KLMCoffee.Protocal
  8. {
  9. /// <summary>
  10. /// K95 咖啡机
  11. /// </summary>
  12. ///
  13. public class K95Command
  14. {
  15. //响应说明:00 代表发送成功
  16. // 01 代表机器正忙
  17. // 02 代表串口指令错误
  18. #region 私有变量
  19. private string address = "01";//地址,默认01
  20. private string command;//控制命令
  21. private string content;//控制内容
  22. private string lrc;//校验位
  23. #endregion
  24. #region 构造
  25. /// <summary>
  26. /// 构造函数
  27. /// </summary>
  28. /// <param name="_command">控制命令</param>
  29. /// <param name="_value">控制内容</param>
  30. /// <param name="_address">地址,默认“01”</param>
  31. public K95Command(string _command,string _content,string _address = "01")
  32. {
  33. command = _command;
  34. content = _content;
  35. address = _address;
  36. lrc = calLrcCommon("01" + _command + _content);
  37. }
  38. /// <summary>
  39. /// 提供无参数构造
  40. /// </summary>
  41. public K95Command()
  42. {
  43. }
  44. #endregion
  45. #region 外部调用场景与命令
  46. /// <summary>
  47. /// 发送通用常规命令
  48. /// </summary>
  49. public byte[] ReturnsCommandData(string _command,string _content)
  50. {
  51. try
  52. {
  53. command = _command;
  54. content = _content;
  55. lrc = calLrcCommon("01" + _command + _content);//生成校验位
  56. string commandStr = $":01{command}{content}{lrc}\r\n";//生成控制命令
  57. return ascii2Hex(commandStr);
  58. }
  59. catch (Exception ex)
  60. {
  61. return new byte[0];
  62. }
  63. }
  64. /// <summary>
  65. /// 返回取消制作
  66. /// </summary>
  67. public byte[] ReturnsCancelMake()
  68. {
  69. try
  70. {
  71. return ReturnsCommandData("03","0001000C");
  72. }
  73. catch (Exception ex)
  74. {
  75. return new byte[0];
  76. }
  77. }
  78. /// <summary>
  79. /// 返回放杯确认
  80. /// </summary>
  81. public byte[] ReturnsIsOK()
  82. {
  83. try
  84. {
  85. return ReturnsCommandData("06","0001");
  86. }
  87. catch (Exception ex)
  88. {
  89. return new byte[0];
  90. }
  91. }
  92. /// <summary>
  93. /// 清洗冲泡器
  94. /// </summary>
  95. public byte[] ReturnsWashCPJ()
  96. {
  97. try
  98. {
  99. return ReturnsCommandData("04","0001010A");
  100. }
  101. catch (Exception ex)
  102. {
  103. return new byte[0];
  104. }
  105. }
  106. /// <summary>
  107. /// 清洗奶沫器
  108. /// </summary>
  109. public byte[] ReturnsWashNMJ()
  110. {
  111. try
  112. {
  113. return ReturnsCommandData("04","0001040A");
  114. }
  115. catch (Exception ex)
  116. {
  117. return new byte[0];
  118. }
  119. }
  120. /// <summary>
  121. /// 清洗奶沫器
  122. /// </summary>
  123. public byte[] ReturnsWashNMJIsOK()
  124. {
  125. try
  126. {
  127. return ReturnsCommandData("06","0001000C");
  128. }
  129. catch (Exception ex)
  130. {
  131. return new byte[0];
  132. }
  133. }
  134. /// <summary>
  135. /// 状态查询
  136. /// </summary>
  137. public byte[] ReturnsStatusInquire()
  138. {
  139. try
  140. {
  141. return ReturnsCommandData("05","0000000D");
  142. }
  143. catch (Exception ex)
  144. {
  145. return new byte[0];
  146. }
  147. }
  148. /// <summary>
  149. /// 返回状态信息解析
  150. /// :0105 AD10(系统状态) 0000(制作状态)
  151. /// 0000(制作进度)0000(制作进度)
  152. /// 0000(故障信息) B(保养信息) 3A\r\n
  153. /// :010510000000000000000000F3A\r\n
  154. /// </summary>
  155. /// <param name="message"></param>
  156. public void StateResolution(string message)
  157. {
  158. try
  159. {
  160. SystemStatusModel systemStatus = new SystemStatusModel(message);
  161. }
  162. catch (Exception)
  163. {
  164. throw;
  165. }
  166. }
  167. #endregion
  168. #region 获取所有命令或类型
  169. /// <summary>
  170. /// 获取所有命令
  171. /// </summary>
  172. public List<string> GetCommand()
  173. {
  174. List<string> vs = new List<string>();
  175. try
  176. {
  177. //var obj = new K95CommandEnum();
  178. //var type = obj.GetType();
  179. //foreach (FieldInfo field in type.GetFields())
  180. //{
  181. // vs.Add(field.Name);
  182. //}
  183. vs = System.Enum.GetNames(typeof(K95CommandEnum))?.ToList();
  184. }
  185. catch (Exception ex)
  186. {
  187. return vs;
  188. }
  189. return vs;
  190. }
  191. /// <summary>
  192. /// 根据命令获取值
  193. /// </summary>
  194. public string GetCommandValue(string name)
  195. {
  196. try
  197. {
  198. //var obj = new K95CommandEnum();
  199. //var type = obj.GetType();
  200. //foreach (FieldInfo field in type.GetFields())
  201. //{
  202. // if (field.Name == name) return (string)field.GetValue(obj);
  203. //}
  204. return ToEnumValue<K95CommandEnum>(name).GetString();
  205. }
  206. catch (Exception ex)
  207. {
  208. return string.Empty;
  209. }
  210. return string.Empty;
  211. }
  212. /// <summary>
  213. /// 获取所有咖啡类型
  214. /// </summary>
  215. public List<string> GetCoffeeType()
  216. {
  217. List<string> vs = new List<string>();
  218. try
  219. {
  220. //var obj = new K95deFaultCoffeeEnum();
  221. //var type = obj.GetType();
  222. //foreach (FieldInfo field in type.GetFields())
  223. //{
  224. // vs.Add(field.Name);
  225. //}
  226. vs = System.Enum.GetNames(typeof(K95deFaultCoffeeEnum))?.ToList();
  227. }
  228. catch (Exception ex)
  229. {
  230. return vs;
  231. }
  232. return vs;
  233. }
  234. /// <summary>
  235. /// 根据咖啡类型获取值
  236. /// </summary>
  237. public string GetCoffeeTypeValue(string name)
  238. {
  239. try
  240. {
  241. //var obj = new K95deFaultCoffeeEnum();
  242. //var type = obj.GetType();
  243. //foreach (FieldInfo field in type.GetFields())
  244. //{
  245. // if (field.Name == name) return (string)field.GetValue(obj);
  246. //}
  247. return ToEnumValue<K95deFaultCoffeeEnum>(name).GetString();
  248. }
  249. catch (Exception ex)
  250. {
  251. return string.Empty;
  252. }
  253. return string.Empty;
  254. }
  255. #endregion
  256. #region 常规命令生成函数
  257. /// <summary>
  258. /// 附校验码生成代码
  259. /// </summary>
  260. /// <param name="data">地址+控制命令+控制内容</param>
  261. /// <returns></returns>
  262. public static string calLrcCommon(string data)
  263. {
  264. try
  265. {
  266. if (data.Length % 2 != 0)
  267. {
  268. data = data + "0";
  269. }
  270. int total = 0;
  271. int len = data.Length;
  272. int num = 0;
  273. while (num < len)
  274. {
  275. string s = data.Substring(num,2);
  276. total += Convert.ToInt32(s,16);
  277. num += 2;
  278. }
  279. total = ~total + 1;
  280. string checkSum = (total & 255).ToString("x").ToUpper();
  281. while (checkSum.Length < 2)
  282. {
  283. checkSum = "0" + checkSum;
  284. }
  285. return checkSum;
  286. }
  287. catch (Exception)
  288. {
  289. }
  290. return "";
  291. }
  292. /// <summary>
  293. /// 返回控制协议ASCII
  294. /// </summary>
  295. /// <returns></returns>
  296. public string toUartString()
  297. {
  298. string redata = string.Empty;
  299. try
  300. {
  301. redata = $":01{command}{content}{lrc}\r\n";
  302. return redata;
  303. }
  304. catch (Exception ex)
  305. {
  306. return "";
  307. }
  308. }
  309. /// <summary>
  310. /// ASCII转换成16进制
  311. /// </summary>
  312. /// <param name="str"></param>
  313. /// <returns></returns>
  314. public byte[] ascii2Hex(string str)
  315. {
  316. try
  317. {
  318. string str1 = str.Trim(); // 去掉字符串首尾处的空格
  319. char[] charBuf = str1.ToArray(); // 将字符串转换为字符数组
  320. ASCIIEncoding charToASCII = new ASCIIEncoding();
  321. byte[] TxdBuf = new byte[charBuf.Length + 4]; // 定义发送缓冲区;
  322. byte[] Buf = charToASCII.GetBytes(charBuf); // 转换为各字符对应的ASCII
  323. for (int i = 0; i < Buf.Length; i++)
  324. {
  325. TxdBuf[i] = Buf[i];
  326. }
  327. TxdBuf[charBuf.Length] = 0x5c;
  328. TxdBuf[charBuf.Length + 1] = 0x72;
  329. TxdBuf[charBuf.Length + 2] = 0x5c;
  330. TxdBuf[charBuf.Length + 3] = 0x6e;
  331. return TxdBuf;
  332. }
  333. catch (Exception ex)
  334. {
  335. return new byte[0];
  336. }
  337. }
  338. /// <summary>
  339. /// Int 转 16进制字符串 (保留几位)
  340. /// </summary>
  341. public string IntToHexString(int data,int length = 2)
  342. {
  343. try
  344. {
  345. return data.ToString("X").PadLeft(length,'0');
  346. }
  347. catch (Exception ex)
  348. {
  349. return string.Empty;
  350. }
  351. }
  352. /// <summary>
  353. /// 将一条十六进制字符串转换为ASCII
  354. /// </summary>
  355. /// <param name="hexstring">一条十六进制字符串</param>
  356. /// <returns>返回一条ASCII码</returns>
  357. public static string HexStringToASCII(string hexstring)
  358. {
  359. byte[] bt = HexStringToBinary(hexstring);
  360. string lin = "";
  361. for (int i = 0; i < bt.Length; i++)
  362. {
  363. lin = lin + bt[i] + " ";
  364. }
  365. string[] ss = lin.Trim().Split(new char[] { ' ' });
  366. char[] c = new char[ss.Length];
  367. int a;
  368. for (int i = 0; i < c.Length; i++)
  369. {
  370. a = Convert.ToInt32(ss[i]);
  371. c[i] = Convert.ToChar(a);
  372. }
  373. string b = new string(c);
  374. return b;
  375. }
  376. /// <summary>
  377. /// 16进制字符串转换为二进制数组
  378. /// </summary>
  379. /// <param name="hexstring">用空格切割字符串</param>
  380. /// <returns>返回一个二进制字符串</returns>
  381. public static byte[] HexStringToBinary(string hexstring)
  382. {
  383. string[] tmpary = hexstring.Trim().Split(' ');
  384. byte[] buff = new byte[tmpary.Length];
  385. for (int i = 0; i < buff.Length; i++)
  386. {
  387. buff[i] = Convert.ToByte(tmpary[i],16);
  388. }
  389. return buff;
  390. }
  391. /// <summary>
  392. /// 根据枚举名称获取枚举值
  393. /// </summary>
  394. public T ToEnumValue<T>(string name)
  395. {
  396. return (T)Enum.Parse(typeof(T),name);
  397. }
  398. #endregion
  399. }
  400. }