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

K95Command.cs 12 KiB

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