public static string GetSum(string hexString, out List <string> list) { list = new List <string>(); var carray = hexString.Replace(" ", "").ToArray(); if (carray.Length % 2 == 0) { for (var i = 0; i < carray.Length / 2; i++) { var each = carray[i] + "" + carray[i + 1]; list.Add(each); } var sum = 0; foreach (var d in list) { var data = JinZhiConvert.JinZhiResult("0123456789abcdef", "0123456789", d); sum += Convert.ToInt32(data); } var result = JinZhiConvert.JinZhiResult("0123456789", "0123456789abcdef", sum + ""); if (result.Length > 1) { return(result.Substring(result.Length - 2)); } return(result); } return(string.Empty); }
public static string GetCRC(string str) { // str = "55ff010100e6004921003422003c002100260055002500a202240000002000ffffffffffffffffffffffff01221212122301224f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010f067600"; var sum = 0; if (str.Length % 2 != 0) { return(string.Empty); } else { var list_two = new List <string>(); for (var i = 0; i < str.Length / 2; i++) { var j = i * 2; var c1 = str[j]; var c2 = str[j + 1]; var sr = c1 + "" + c2; list_two.Add(sr); } foreach (var d in list_two) { sum += Convert.ToInt32(JinZhiConvert.JinZhiResult("0123456789abcdef", "0123456789", d.ToLower())); } } var crc = JinZhiConvert.JinZhiResult("0123456789", "0123456789abcdef", sum + ""); if (crc.Length > 2) { crc = str + crc.Substring(crc.Length - 2); return(crc); } return(string.Empty); }
/// <summary> /// 解析压缩码 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="value"></param> /// <returns></returns> public static string GetOrgMsg(int a, int b, string value) { var str = "55ff01" + (a + "").PadLeft(2, '0') + (b + "").PadLeft(2, '0'); if (value.IndexOf("_") != -1) { var values = value.Split(new string[] { "_", "." }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var v in values) { if (v.Length < 2) { sb.Append(v.PadLeft(2, '0')); } else if (v.Length < 4) { sb.Append(v); } else if (v.Length >= 4) { if (v.StartsWith("ffff", StringComparison.CurrentCultureIgnoreCase) || v.StartsWith("0000", StringComparison.CurrentCultureIgnoreCase)) { //需要补充的字符 var toPaddingChar = v[3]; var toPaddingCount = v.Substring(4).ToLower(); var intPaddingCount = Convert.ToInt32(JinZhiConvert.JinZhiResult("0123456789abcdef", "0123456789", toPaddingCount, 0)) * 2; var data = /*v.Substring(0, 4) +*/ (toPaddingChar + "").PadRight(intPaddingCount, toPaddingChar); sb.Append(data); } else { sb.Append(v); } } } var lenx = sb.ToString().Length / 2; var len = JinZhiConvert.JinZhiResult("0123456789", "0123456789abcdef", lenx + "").PadLeft(2, '0'); var fstr = GetCRC(str + len + sb.ToString()); return(fstr); } else { var lenx = value.Length / 2; var len = JinZhiConvert.JinZhiResult("0123456789", "0123456789abcdef", lenx + "").PadLeft(2, '0'); var fstr = GetCRC(str + len + value.ToString()); return(fstr); } }
private static int CenterAirConditionCRC(string[] arr_buff) { var crc = 0xffff; for (var j = 0; j < 4; j++) { crc = crc ^ Convert.ToInt32(JinZhiConvert.JinZhiResult("0123456789abcdef", "0123456789", arr_buff[j])); for (var i = 0; i < 8; i++) { var data = crc & 0x0001; if (data > 0) { crc = crc >> 1; crc = crc ^ 0xa001; } else { crc = crc >> 1; } } } return(crc); }
/* * 2.3 空调红外码 * * 空调的发码规则不同于其它红外设备的发码,说明如下:共七个键, 搜索对码时, 发的是开机键的数据 * 所发数据内容:0x30+0x01+(2B)+(7B)+(1B)+(?B:arc_table)+0xFF+CHECKSUM(1B) B:表示字节, * 含义: * 1. 0x30 0x01 为固定值 * 2. (2B)是该遥控器对的红外码是码库中的第几组码,如:第120组 则为 0x00 0x78 * 3. (7B)共七个字节 * 第0个字节: 数据为对应空调的温度:19-30度(0x13-0x1E),默认:25度;十六进制,与显示对应,通过温度加减键调节 * 第1个字节: 风量数据:自动:01,低:02,中:03,高:04,与显示对应:默认:01, * 第2个字节: 手动风向:向下:03,中:02,向上:01,默认02,与显示对应 * 第3个字节: 自动风向:01,打开,00关,默认开:01,与显示对应 * 第4个字节: 开关数据:开机时:0x01,关机时:0x00,通过按开关机(电源)键实现,开机后,其它键才有效,相关符号才显示) * 第5个字节: 点击的按键对应数据,电源:0x01,模式:0x02,风量:0x03,手动风向: * 0x04, 自动风向:0x05,温度加:0x06, 温度减:0x07, /* 表示当前按下的是哪个键 * 第6个字节: 模式对应数据和显示:自动(默认):0x01,制冷:0X02,抽湿:0X03,送风:0x04;制热:0x05,这些值按模式键实现 * 4. (1B)对应码库中每组数据的第一个字节值+1, 即括号内的第一个字节加1:如第0组时为0x04+1=0x05,即0x05,第12组时为0x06+1=0x07,即0x07 * 5. (?B:arc_table) 对应码库中每组数据除第一个字节值之外的所有数据 * 6. 0xFF为固定值 * 7. CHECKSUM(1B) 校验和,最后一个字节为前面所有数据之和的低8位,(第0x30到0xFF数据之和取低8位) * 示例: 0x30 0x01 0x00 0x00 0x1B 0x01 0x02 0x01 0x01 0x01 0x02 0x05 0x00 0x0f 0x04 0x3D 0xFF 0xA9 * * */ public static string AirCode(string code, CommandMode mode, string value) { /* * 30 01 0215 19020100010102 030000 FF */ var spdata = SplitData(code); if (spdata.Count == 6) { ///7b var _7b = spdata[2]; //温度 var wendu = _7b.Substring(0, 2); //风量 var fengliang = _7b.Substring(2, 2); //4 ///手工风向 var manuFengXiang = _7b.Substring(4, 2); //6 //自动风向 var autoFengXiang = _7b.Substring(6, 2); //8 //开关数据 var data_kaiguan = _7b.Substring(8, 2); //10 //点击的按键对应数据 var key_rel = _7b.Substring(10, 2); //12 //模式对应数据和显示 var mode_data = _7b.Substring(12, 2); //模式对应 ///调低温度 if (mode == CommandMode.AdjustDownTemperature) { //0x13-0x1E //19-30 //Convert.ToString() var ivalue = 0; wendu = JinZhiConvert.JinZhiResult("0123456789abcdef", "0123456789", wendu); var bint = int.TryParse(wendu, out ivalue); if (bint) { if (ivalue >= minValue && ivalue <= maxValue) { int intValue = 0; var beiint = int.TryParse(value, out intValue); if (beiint) { var finalvalue = minValue; if (ivalue - intValue > minValue) { finalvalue = ivalue - intValue; } var tmp = (finalvalue).ToString("x2"); wendu = tmp; /* * 点击的按键对应数据,电源:0x01,模式:0x02,风量:0x03,手动风向: * 0x04, 自动风向:0x05,温度加:0x06, 温度减:0x07, */ key_rel = "07"; } else { var tmp = (minValue).ToString("x2"); wendu = tmp; /* * 点击的按键对应数据,电源:0x01,模式:0x02,风量:0x03,手动风向: * 0x04, 自动风向:0x05,温度加:0x06, 温度减:0x07, */ key_rel = "07"; } } } } ///调高温度 else if (mode == CommandMode.AdjustUpTemperature) { var ivalue = 0; wendu = JinZhiConvert.JinZhiResult("0123456789abcdef", "0123456789", wendu); var bint = int.TryParse(wendu, out ivalue); if (bint) { if (ivalue >= minValue && ivalue <= maxValue) { int intValue = 0; var beiint = int.TryParse(value, out intValue); if (beiint) { var finalvalue = maxValue; if (ivalue + intValue < maxValue) { finalvalue = ivalue + intValue; } var tmp = (finalvalue).ToString("x2"); wendu = tmp; key_rel = "06"; } } else { var tmp = (maxValue).ToString("x2"); wendu = tmp; key_rel = "06"; } } } ///设置模式 else if (mode == CommandMode.SetMode) { //模式对应数据和显示:自动(默认):0x01,制冷:0X02,抽湿:0X03,送风:0x04; 制热:0x05,这些值按模式键实现 ///制热 if (value.Equals("heat")) { mode_data = "05"; } ///制冷 else if (value.Equals("cold") || value.Equals("cool")) { mode_data = "02"; } ///送风 else if (value.Equals("airsupply") || value.Equals("fan")) { mode_data = "04"; } ///抽湿 else if (value.Equals("dehumidification")) { mode_data = "03"; } else if (value.Equals("auto")) { mode_data = "01"; } key_rel = "02"; } ///设置温度 else if (mode == CommandMode.SetTemperature) { var ivalue = 0; wendu = JinZhiConvert.JinZhiResult("0123456789abcdef", "0123456789", wendu); var bint = int.TryParse(wendu, out ivalue); if (bint) { int intValue = 0; var beiint = int.TryParse(value, out intValue); if (beiint) { if (intValue >= minValue && intValue <= maxValue) { var tmp = intValue.ToString("x2"); if (intValue >= ivalue) { key_rel = "06"; } else { key_rel = "07"; } wendu = tmp; /* * 点击的按键对应数据,电源:0x01,模式:0x02,风量:0x03,手动风向: * 0x04, 自动风向:0x05,温度加:0x06, 温度减:0x07, */ } } } } ///设置风速 else if (mode == CommandMode.SetWindSpeed) { /* * 风量数据:自动:01,低:02,中:03,高:04,与显示对应:默认:01, */ int ivalue = 1; /* * auto 自动风 * low 低风 * medium 中风 * high 高风 */ if (value.Equals("auto", StringComparison.CurrentCultureIgnoreCase)) { ivalue = 1; fengliang = (ivalue + "").PadLeft(2, '0'); key_rel = "03"; } else if (value.Equals("low", StringComparison.CurrentCultureIgnoreCase)) { ivalue = 2; fengliang = (ivalue + "").PadLeft(2, '0'); key_rel = "03"; } else if (value.Equals("medium", StringComparison.CurrentCultureIgnoreCase)) { ivalue = 3; fengliang = (ivalue + "").PadLeft(2, '0'); key_rel = "03"; } else if (value.Equals("high", StringComparison.CurrentCultureIgnoreCase)) { ivalue = 4; fengliang = (ivalue + "").PadLeft(2, '0'); key_rel = "03"; } else { var bint = int.TryParse(value, out ivalue); if (bint) { if (ivalue >= 1 && ivalue <= 4) { fengliang = (ivalue + "").PadLeft(2, '0'); key_rel = "03"; } } } } else if (mode == CommandMode.TurnOn) { data_kaiguan = "01"; key_rel = "01"; } else if (mode == CommandMode.TurnOff) { data_kaiguan = "00"; key_rel = "01"; } if (mode != CommandMode.TurnOff) { data_kaiguan = "01"; key_rel = "01"; } var pdata = wendu + fengliang + manuFengXiang + autoFengXiang + data_kaiguan + key_rel + mode_data; spdata[2] = pdata; var data = string.Join("", spdata); //var all = data + GetSum(data); var all = data; return(all); } return(string.Empty); }
/// <summary> /// 中央空调发送码(485) /// </summary> /// <param name="modbus">modus地址</param> /// <param name="mode">模式</param> /// <param name="value">值</param> /// <param name="preValue">前值</param> /// <returns></returns> public static string CenterAirCode(string modbus, CommandMode mode, string value, string preValue) { //03 06 0003 00c8 var listStr = new List <string>(); preValue = preValue.Replace(" ", ""); if (string.IsNullOrEmpty(preValue)) { preValue = "0306000300c8"; } if (preValue.Length == 12 || preValue.Length == 16) { var address = preValue.Substring(4, 4); var avalue = preValue.Substring(8, 4); var x_tmp = new string[] { modbus, "06", address, avalue, "" }; x_tmp[0] = modbus.PadLeft(2, '0'); var workMode = ""; var workModeValue = ""; //调低温度 if (mode == CommandMode.AdjustDownTemperature) { workMode = "0003"; var d = 0.0; //avalue=0000 var result = JinZhiConvert.JinZhiResult("0123456789abcdef", avalue) * 1.0 / 10; if (result == 0 || result < 10 || result > 30) { d = 18; } else { d = Convert.ToDouble(result); } d = d - 1; if (d >= 10 && d <= 30) { var dvalue = (int)Math.Floor(d * 10); var hexValue = Convert.ToString(dvalue, 16); workModeValue = hexValue.PadLeft(4, '0'); } // wendu = JinZhiConvert.JinZhiResult("0123456789abcdef", "0123456789", wendu); } else if (mode == CommandMode.AdjustUpTemperature) { workMode = "0003"; var d = 0.0; //avalue=0000 var result = JinZhiConvert.JinZhiResult("0123456789abcdef", avalue) * 1.0 / 10; if (result == 0 || result < 10 || result > 30) { d = 18; } else { d = Convert.ToDouble(result); } d = d + 1; if (d >= 10 && d <= 30) { var dvalue = (int)Math.Floor(d * 10); var hexValue = Convert.ToString(dvalue, 16); workModeValue = hexValue.PadLeft(4, '0'); } } else if (mode == CommandMode.TurnOn) { workMode = "0000"; workModeValue = "0001"; } else if (mode == CommandMode.TurnOff) { workMode = "0000"; workModeValue = "0000"; } ///设置工作模式 else if (mode == CommandMode.SetMode) { workMode = "0001"; if (value.Equals("heat", StringComparison.CurrentCultureIgnoreCase)) { workModeValue = "0003"; } ///制冷 else if (value.Equals("cold", StringComparison.CurrentCultureIgnoreCase) || value.Equals("cool", StringComparison.CurrentCultureIgnoreCase)) { workModeValue = "0000"; } ///送风 else if (value.Equals("airsupply", StringComparison.CurrentCultureIgnoreCase) || value.Equals("fan", StringComparison.CurrentCultureIgnoreCase)) { workModeValue = "0002"; } ///抽湿 else if (value.Equals("dehumidification", StringComparison.CurrentCultureIgnoreCase)) { workModeValue = "0001"; } else if (value.Equals("auto", StringComparison.CurrentCultureIgnoreCase)) { workModeValue = "0001"; } } else if (mode == CommandMode.SetWindSpeed) { ///设置风速 if (value.Equals("auto", StringComparison.CurrentCultureIgnoreCase)) { workMode = "0002"; workModeValue = "0000"; } else if (value.Equals("low", StringComparison.CurrentCultureIgnoreCase)) { workMode = "0002"; workModeValue = "0003"; } else if (value.Equals("medium", StringComparison.CurrentCultureIgnoreCase)) { workMode = "0002"; workModeValue = "0002"; } else if (value.Equals("high", StringComparison.CurrentCultureIgnoreCase)) { workMode = "0002"; workModeValue = "0001"; } else { workMode = "0002"; workModeValue = "0000"; } } else if (mode == CommandMode.SetTemperature) { var d = 0.0; workMode = "0003"; var isD = double.TryParse(value, out d); workModeValue = "0100"; if (isD) { if (d >= 10 && d <= 30) { var dvalue = (int)Math.Floor(d * 10); var hexValue = Convert.ToString(dvalue, 16); workModeValue = hexValue.PadLeft(4, '0'); } } } if (!string.IsNullOrEmpty(workMode) && !string.IsNullOrEmpty(workModeValue)) { x_tmp[2] = workMode; x_tmp[3] = workModeValue; } var t1 = convertByteHexStringToByte(string.Join("", x_tmp)); var t2 = Compute_CRC_16_Modbus(t1); var c1 = Convert.ToString(t2, 16).PadLeft(4, '0'); var change = c1.Substring(2, 2) + c1.Substring(0, 2); // var crc = Convert.ToString(centerAirConditionCRC(x_tmp), 16).PadLeft(4, '0'); x_tmp[4] = change; return(string.Join("", x_tmp)); } return(string.Empty); }