|
- using BPASmartClient.Model.咖啡机.Enum;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.KLMCoffee.Protocal
- {
- /// <summary>
- /// K95 咖啡机
- /// </summary>
- ///
- public class K95Command
- {
- //响应说明:00 代表发送成功
- // 01 代表机器正忙
- // 02 代表串口指令错误
- #region 私有变量
- private string address = "01";//地址,默认01
- private string command;//控制命令
- private string content;//控制内容
- private string lrc;//校验位
- #endregion
-
- #region 构造
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="_command">控制命令</param>
- /// <param name="_value">控制内容</param>
- /// <param name="_address">地址,默认“01”</param>
- public K95Command(string _command,string _content,string _address = "01")
- {
- command = _command;
- content = _content;
- address = _address;
- lrc = calLrcCommon("01" + _command + _content);
- }
- /// <summary>
- /// 提供无参数构造
- /// </summary>
- public K95Command()
- {
-
- }
- #endregion
-
- #region 外部调用场景与命令
- /// <summary>
- /// 发送通用常规命令
- /// </summary>
- public byte[] ReturnsCommandData(string _command,string _content)
- {
- try
- {
- command = _command;
- content = _content;
- lrc = calLrcCommon("01" + _command + _content);//生成校验位
- string commandStr = $":01{command}{content}{lrc}\r\n";//生成控制命令
- return ascii2Hex(commandStr);
- }
- catch (Exception ex)
- {
- return new byte[0];
- }
- }
-
- /// <summary>
- /// 返回取消制作
- /// </summary>
- public byte[] ReturnsCancelMake()
- {
- try
- {
- return ReturnsCommandData("03","0001000C");
- }
- catch (Exception ex)
- {
- return new byte[0];
- }
- }
-
- /// <summary>
- /// 返回放杯确认
- /// </summary>
- public byte[] ReturnsCupIsOK()
- {
- try
- {
- return ReturnsCommandData("06","0001");
- }
- catch (Exception ex)
- {
- return new byte[0];
- }
- }
-
- /// <summary>
- /// 清洗冲泡器
- /// </summary>
- public byte[] ReturnsWashCPJ()
- {
- try
- {
- return ReturnsCommandData("04","0001010A");
- }
- catch (Exception ex)
- {
- return new byte[0];
- }
- }
-
- /// <summary>
- /// 清洗奶沫器
- /// </summary>
- public byte[] ReturnsWashNMJ()
- {
- try
- {
- return ReturnsCommandData("04","0001040A");
- }
- catch (Exception ex)
- {
- return new byte[0];
- }
- }
-
- /// <summary>
- /// 清洗奶沫器确认
- /// </summary>
- public byte[] ReturnsWashNMJIsOK()
- {
- try
- {
- return ReturnsCommandData("06","0001000C");
- }
- catch (Exception ex)
- {
- return new byte[0];
- }
- }
-
- /// <summary>
- /// 状态查询
- /// </summary>
- public byte[] ReturnsStatusInquire()
- {
- try
- {
- return ReturnsCommandData("05","0000000D");
- }
- catch (Exception ex)
- {
- return new byte[0];
- }
- }
-
- /// <summary>
- /// 返回状态信息解析
- /// :0105 AD10(系统状态) 0000(制作状态)
- /// 0000(制作进度)0000(制作进度)
- /// 0000(故障信息) B(保养信息) 3A\r\n
- /// :010510000000000000000000F3A\r\n
- /// </summary>
- /// <param name="message"></param>
- public SystemStatusModel StateResolution(string message)
- {
- try
- {
- return new SystemStatusModel(message);
- }
- catch (Exception)
- {
- return null;
- }
- }
- #endregion
-
- #region 获取所有命令或类型
- /// <summary>
- /// 获取所有命令
- /// </summary>
- public List<string> GetCommand()
- {
- List<string> vs = new List<string>();
- try
- {
- //var obj = new K95CommandEnum();
- //var type = obj.GetType();
- //foreach (FieldInfo field in type.GetFields())
- //{
- // vs.Add(field.Name);
- //}
- vs = System.Enum.GetNames(typeof(K95CommandEnum))?.ToList();
- }
- catch (Exception ex)
- {
- return vs;
- }
- return vs;
- }
-
- /// <summary>
- /// 根据命令获取值
- /// </summary>
- public string GetCommandValue(string name)
- {
- try
- {
- //var obj = new K95CommandEnum();
- //var type = obj.GetType();
- //foreach (FieldInfo field in type.GetFields())
- //{
- // if (field.Name == name) return (string)field.GetValue(obj);
- //}
- return ToEnumValue<K95CommandEnum>(name).GetString();
- }
- catch (Exception ex)
- {
- return string.Empty;
- }
- return string.Empty;
- }
-
- /// <summary>
- /// 获取所有咖啡类型
- /// </summary>
- public List<string> GetCoffeeType()
- {
- List<string> vs = new List<string>();
- try
- {
- //var obj = new K95deFaultCoffeeEnum();
- //var type = obj.GetType();
- //foreach (FieldInfo field in type.GetFields())
- //{
- // vs.Add(field.Name);
- //}
- vs = System.Enum.GetNames(typeof(K95deFaultCoffeeEnum))?.ToList();
- }
- catch (Exception ex)
- {
- return vs;
- }
- return vs;
- }
-
- /// <summary>
- /// 根据咖啡类型获取值
- /// </summary>
- public string GetCoffeeTypeValue(string name)
- {
- try
- {
- //var obj = new K95deFaultCoffeeEnum();
- //var type = obj.GetType();
- //foreach (FieldInfo field in type.GetFields())
- //{
- // if (field.Name == name) return (string)field.GetValue(obj);
- //}
- return ToEnumValue<K95deFaultCoffeeEnum>(name).GetString();
- }
- catch (Exception ex)
- {
- return string.Empty;
- }
- return string.Empty;
- }
- #endregion
-
- #region 常规命令生成函数
- /// <summary>
- /// 附校验码生成代码
- /// </summary>
- /// <param name="data">地址+控制命令+控制内容</param>
- /// <returns></returns>
- public static string calLrcCommon(string data)
- {
- try
- {
- if (data.Length % 2 != 0)
- {
- data = data + "0";
- }
- int total = 0;
- int len = data.Length;
- int num = 0;
- while (num < len)
- {
- string s = data.Substring(num,2);
- total += Convert.ToInt32(s,16);
- num += 2;
- }
- total = ~total + 1;
- string checkSum = (total & 255).ToString("x").ToUpper();
- while (checkSum.Length < 2)
- {
- checkSum = "0" + checkSum;
- }
- return checkSum;
- }
- catch (Exception)
- {
- }
- return "";
- }
-
- /// <summary>
- /// 返回控制协议ASCII
- /// </summary>
- /// <returns></returns>
- public string toUartString()
- {
- string redata = string.Empty;
- try
- {
- redata = $":01{command}{content}{lrc}\r\n";
- return redata;
- }
- catch (Exception ex)
- {
- return "";
- }
- }
-
- /// <summary>
- /// ASCII转换成16进制
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public byte[] ascii2Hex(string str)
- {
- try
- {
- string str1 = str.Trim(); // 去掉字符串首尾处的空格
- char[] charBuf = str1.ToArray(); // 将字符串转换为字符数组
- ASCIIEncoding charToASCII = new ASCIIEncoding();
- byte[] TxdBuf = new byte[charBuf.Length + 4]; // 定义发送缓冲区;
- byte[] Buf = charToASCII.GetBytes(charBuf); // 转换为各字符对应的ASCII
- for (int i = 0; i < Buf.Length; i++)
- {
- TxdBuf[i] = Buf[i];
- }
- TxdBuf[charBuf.Length] = 0x5c;
- TxdBuf[charBuf.Length + 1] = 0x72;
- TxdBuf[charBuf.Length + 2] = 0x5c;
- TxdBuf[charBuf.Length + 3] = 0x6e;
- return TxdBuf;
- }
- catch (Exception ex)
- {
- return new byte[0];
- }
- }
-
- /// <summary>
- /// Int 转 16进制字符串 (保留几位)
- /// </summary>
- public string IntToHexString(int data,int length = 2)
- {
- try
- {
- return data.ToString("X").PadLeft(length,'0');
- }
- catch (Exception ex)
- {
- return string.Empty;
- }
- }
-
- /// <summary>
- /// 将一条十六进制字符串转换为ASCII
- /// </summary>
- /// <param name="hexstring">一条十六进制字符串</param>
- /// <returns>返回一条ASCII码</returns>
- public static string HexStringToASCII(string hexstring)
- {
- byte[] bt = HexStringToBinary(hexstring);
- string lin = "";
- for (int i = 0; i < bt.Length; i++)
- {
- lin = lin + bt[i] + " ";
- }
-
-
- string[] ss = lin.Trim().Split(new char[] { ' ' });
- char[] c = new char[ss.Length];
- int a;
- for (int i = 0; i < c.Length; i++)
- {
- a = Convert.ToInt32(ss[i]);
- c[i] = Convert.ToChar(a);
- }
-
- string b = new string(c);
- return b;
- }
-
- /// <summary>
- /// 16进制字符串转换为二进制数组
- /// </summary>
- /// <param name="hexstring">用空格切割字符串</param>
- /// <returns>返回一个二进制字符串</returns>
- public static byte[] HexStringToBinary(string hexstring)
- {
-
- string[] tmpary = hexstring.Trim().Split(' ');
- byte[] buff = new byte[tmpary.Length];
- for (int i = 0; i < buff.Length; i++)
- {
- buff[i] = Convert.ToByte(tmpary[i],16);
- }
- return buff;
- }
-
- /// <summary>
- /// 根据枚举名称获取枚举值
- /// </summary>
- public T ToEnumValue<T>(string name)
- {
- return (T)Enum.Parse(typeof(T),name);
- }
- #endregion
-
- }
- }
|