private void SetComm(object sender, Command command) { byte[] data = ByteStrUtil.HexToByte(command.Data); Dispatcher.Invoke(new Action(() => { //解析光源信息字 string[] lightSource = ByteToBinary(data[0]); meamtObjType.SelectedIndex = int.Parse(lightSource[7]); lPType.SelectedIndex = int.Parse(lightSource[6]); pixel.SelectedIndex = Convert.ToInt32(lightSource[4] + lightSource[5], 2); sensorType.SelectedIndex = int.Parse(lightSource[3]); lSType.SelectedIndex = int.Parse(lightSource[2]); //设置控制电压参数 byte[] vol = new byte[4]; Array.Copy(data, 1, vol, 0, 4); Array.Reverse(vol, 0, vol.Length); lSControlVol.Text = BitConverter.ToSingle(vol, 0).ToString("f1"); //解析双光路应用模式信息字 string[] appModeInfo = ByteToBinary(Convert.ToByte(data[5])); dualLPAppModeInfo.SelectedIndex = Convert.ToInt32(appModeInfo[6] + appModeInfo[7], 2); //调零对应浓度计算次数 byte[] calTimes = new byte[2]; Array.Copy(data, 6, calTimes, 0, 2); Array.Reverse(calTimes, 0, calTimes.Length); zeroConcCalTimes.Text = BitConverter.ToUInt16(calTimes, 0).ToString(); })); ExceptionUtil.Instance.LogMethod("读取光谱仪公共参数成功"); }
/// <summary> /// CRC16校验 /// </summary> /// <param name="hexByte">待校验数据</param> public static string CRC16(string hexByte) { byte[] data = ByteStrUtil.HexToByte(hexByte); int len = data.Length; byte[] result = new byte[] { 0, 0 }; if (len > 0) { ushort crc = 0xFFFF; for (int i = 0; i < len; i++) { crc = (ushort)(crc ^ (data[i])); for (int j = 0; j < 8; j++) { crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1); } } byte hi = (byte)((crc & 0xFF00) >> 8); //高位置 byte lo = (byte)(crc & 0x00FF); //低位置 result = new byte[] { lo, hi }; } return(ByteStrUtil.ByteToHex(result)); }
private void SetRange(object sender, Command command) { byte[] data = ByteStrUtil.HexToByte(command.Data); Dispatcher.Invoke(new Action(() => { //设置气体1,2,3,4判据数据 byte[] gas_1_crit = new byte[4]; Array.Copy(data, 2, gas_1_crit, 0, 4); Array.Reverse(gas_1_crit, 0, gas_1_crit.Length); gas_1_critData.Text = BitConverter.ToSingle(gas_1_crit, 0).ToString(); byte[] gas_2_crit = new byte[4]; Array.Copy(data, 6, gas_2_crit, 0, 4); Array.Reverse(gas_2_crit, 0, gas_2_crit.Length); gas_2_critData.Text = BitConverter.ToSingle(gas_2_crit, 0).ToString(); byte[] gas_3_crit = new byte[4]; Array.Copy(data, 10, gas_3_crit, 0, 4); Array.Reverse(gas_3_crit, 0, gas_3_crit.Length); gas_3_critData.Text = BitConverter.ToSingle(gas_3_crit, 0).ToString(); byte[] gas_4_crit = new byte[4]; Array.Copy(data, 14, gas_4_crit, 0, 4); Array.Reverse(gas_4_crit, 0, gas_4_crit.Length); gas_4_critData.Text = BitConverter.ToSingle(gas_4_crit, 0).ToString(); })); ExceptionUtil.Instance.LogMethod("读取光谱仪量程切换判据成功"); }
/// <summary> /// 得到一段光谱采集数据 /// </summary> /// <param name="sender"></param> /// <param name="command"></param> private void GetSpecData(object sender, Command command) { resetFlag = false; errorCount = 0; byte[] data = ByteStrUtil.HexToByte(command.Data); if (data.Length == 1) { string msg = null; switch (data[0]) { case 01: msg = "另一通信口正在读取,请等待"; break; case 02: msg = "数据没有准备好,请等待"; break; case 03: msg = "没有处于读光谱数据模式,请等待"; break; case 04: msg = "下传的当前拆分包号超限,请等待"; break; case 05: msg = "读取本次光谱超时(时间间隔超时),请重新读取新的光谱数据"; break; } dataCache.ClearAllData(); currentPackage = 1; ExceptionUtil.Instance.ExceptionMethod(msg, true); return; } currentPackage = data[3]; if (currentPackage < data[4]) { //缓存 byte[] bytes = new byte[data.Length - 5]; Array.Copy(data, 5, bytes, 0, bytes.Length); dataCache.AddDataArray(bytes); //再发送 currentPackage++; SendSpecCmn(lightPath, dataType); } else if (currentPackage == data[4]) { //添加最后一段数据 byte[] bytes = new byte[data.Length - 5]; Array.Copy(data, 5, bytes, 0, bytes.Length); dataCache.AddDataArray(bytes); //存储本次数据 currentPackage = 1; //从缓存中取出所有数据并解析 ParseSpecData(dataCache.GetAllData(true)); } }
/// <summary> /// 接收向量数据 /// </summary> /// <param name="sender"></param> /// <param name="command"></param> private void GetVectorInfo(object sender, Command command) { resetFlag = false; errorCount = 0; byte[] data = ByteStrUtil.HexToByte(command.Data); if (data.Length == 1) { string msg = null; switch (data[0]) { case 01: msg = "另一通信口正在读取,请等待"; break; case 02: msg = "下传的当前拆分包号超限"; break; case 03: msg = "读取本次向量超时(时间间隔超时),请重新读取"; break; } vectorInfoQueue.Clear(); currentPackage = 1; ExceptionUtil.Instance.ExceptionMethod(msg, true); return; } currentPackage = data[4]; if (currentPackage < data[5]) { //缓存 byte[] bytes = new byte[data.Length - 6]; Array.Copy(data, 6, bytes, 0, bytes.Length); foreach (byte b in bytes) { vectorInfoQueue.Enqueue(b); } //再发送 currentPackage++; SendVectorCmn(null, null, null, null); } else if (currentPackage == data[4]) { //添加最后一段数据 byte[] bytes = new byte[data.Length - 6]; Array.Copy(data, 6, bytes, 0, bytes.Length); foreach (byte b in bytes) { vectorInfoQueue.Enqueue(b); } currentPackage = 1; //从缓存中取出所有数据并解析 byte[] datas = vectorInfoQueue.ToArray(); vectorInfoQueue.Clear(); ParseVectorData(datas); } }
/// <summary> /// 读取间隔订阅 /// </summary> /// <param name="sender"></param> /// <param name="command"></param> public void SetReadInterval(object sender, Command command) { byte[] data = ByteStrUtil.HexToByte(command.Data); byte[] timeInterval = new byte[2]; if (data[1] == 09) { Array.Copy(data, 2, timeInterval, 0, 2); Array.Reverse(timeInterval, 0, 2); Dispatcher.Invoke(new Action(() => { ReadInterval.Text = BitConverter.ToUInt16(timeInterval, 0).ToString(); })); } }
private void SetCali(object sender, Command command) { byte[] data = ByteStrUtil.HexToByte(command.Data); Dispatcher.Invoke(new Action(() => { //设置气体1,2,3,4标定系数 byte[] gas_1_lowCC = new byte[4]; Array.Copy(data, 2, gas_1_lowCC, 0, 4); Array.Reverse(gas_1_lowCC, 0, gas_1_lowCC.Length); gas_1_lowRangCC.Text = BitConverter.ToSingle(gas_1_lowCC, 0).ToString("f3"); byte[] gas_1_highCC = new byte[4]; Array.Copy(data, 6, gas_1_highCC, 0, 4); Array.Reverse(gas_1_highCC, 0, gas_1_highCC.Length); gas_1_highRangCC.Text = BitConverter.ToSingle(gas_1_highCC, 0).ToString("f3"); byte[] gas_2_lowCC = new byte[4]; Array.Copy(data, 10, gas_2_lowCC, 0, 4); Array.Reverse(gas_2_lowCC, 0, gas_2_lowCC.Length); gas_2_lowRangCC.Text = BitConverter.ToSingle(gas_2_lowCC, 0).ToString("f3"); byte[] gas_2_highCC = new byte[4]; Array.Copy(data, 14, gas_2_highCC, 0, 4); Array.Reverse(gas_2_highCC, 0, gas_2_highCC.Length); gas_2_highRangCC.Text = BitConverter.ToSingle(gas_2_highCC, 0).ToString("f3"); byte[] gas_3_lowCC = new byte[4]; Array.Copy(data, 18, gas_3_lowCC, 0, 4); Array.Reverse(gas_3_lowCC, 0, gas_3_lowCC.Length); gas_3_lowRangCC.Text = BitConverter.ToSingle(gas_3_lowCC, 0).ToString("f3"); byte[] gas_3_highCC = new byte[4]; Array.Copy(data, 22, gas_1_highCC, 0, 4); Array.Reverse(gas_3_highCC, 0, gas_3_highCC.Length); gas_3_highRangCC.Text = BitConverter.ToSingle(gas_3_highCC, 0).ToString("f3"); byte[] gas_4_lowCC = new byte[4]; Array.Copy(data, 26, gas_4_lowCC, 0, 4); Array.Reverse(gas_4_lowCC, 0, gas_4_lowCC.Length); gas_4_lowRangCC.Text = BitConverter.ToSingle(gas_4_lowCC, 0).ToString("f3"); byte[] gas_4_highCC = new byte[4]; Array.Copy(data, 30, gas_4_highCC, 0, 4); Array.Reverse(gas_4_highCC, 0, gas_4_highCC.Length); gas_1_highRangCC.Text = BitConverter.ToSingle(gas_4_highCC, 0).ToString("f3"); })); ExceptionUtil.Instance.LogMethod("读取光谱仪标定系数成功"); }
private void SetLightPath(object sender, Command command) { byte[] data = ByteStrUtil.HexToByte(command.Data); if ("0F".Equals(data[1].ToString("x2").ToUpper())) { Dispatcher.Invoke(new Action(() => { //光路信息,组分信息 alpp_lPInfo.SelectedIndex = data[0]; compntInfo.SelectedIndex = data[2]; //量程信息 string[] rangeInfo_1 = ByteToBinary(data[3]); gas_liquid_1_range.SelectedIndex = int.Parse(rangeInfo_1[7]); gas_liquid_2_range.SelectedIndex = int.Parse(rangeInfo_1[4]); string[] rangeInfo_2 = ByteToBinary(data[4]); gas_liquid_3_range.SelectedIndex = int.Parse(rangeInfo_2[7]); gas_liquid_4_range.SelectedIndex = int.Parse(rangeInfo_1[4]); //光谱平均次数,浓度滑动平均次数,零点平均次数,标定平均次数,打灯次数 specATs.Text = data[5].ToString(); concSlidATs.Text = data[6].ToString(); zeroATs.Text = data[7].ToString(); caliATs.Text = data[8].ToString(); lightTimes.Text = data[9].ToString(); //积分时间,采样时间间隔,光路切换时间间隔 byte[] integrTime = new byte[2]; Array.Copy(data, 10, integrTime, 0, 2); Array.Reverse(integrTime, 0, integrTime.Length); integTime.Text = BitConverter.ToUInt16(integrTime, 0).ToString(); byte[] samplInter = new byte[2]; Array.Copy(data, 12, samplInter, 0, 2); Array.Reverse(samplInter, 0, samplInter.Length); samplInterval.Text = BitConverter.ToUInt16(samplInter, 0).ToString(); byte[] lPSwitchInter = new byte[2]; Array.Copy(data, 14, lPSwitchInter, 0, 2); Array.Reverse(lPSwitchInter, 0, lPSwitchInter.Length); lPSwitchInterval.Text = BitConverter.ToUInt16(lPSwitchInter, 0).ToString(); })); ExceptionUtil.Instance.LogMethod("读取光谱仪所有光路参数成功"); } }
/// <summary> /// 设置光谱仪设备号 /// </summary> /// <param name="sender"></param> /// <param name="command"></param> private void SetDeviceNo(object sender, Command command) { byte[] deviceNo = ByteStrUtil.HexToByte(command.Data); this.deviceNo = Encoding.Default.GetString(deviceNo).ToUpper(); MessageBox.Show("Copyright © 天津七一二通信广播股份有限公司\n光谱仪软件版本:" + this.deviceNo, "关于系统", MessageBoxButton.OK, MessageBoxImage.Information); }
/// <summary> /// 转发分配实现 /// </summary> /// <param name="command"></param> public void DataForwardMethod(Command command) { //读回应 if (command.ExpandCmn == "AA") { switch (command.Cmn) { case "20": ReadCommParam(this, command); break; case "21": ReadLPParam(this, command); break; case "24": ReadSpecData(this, command); break; case "25": ReadDeviceNo(this, command); break; case "26": ReadRangeSwitch(this, command); break; case "27": ReadZeroParam(this, command); break; case "28": ReadCaliParam(this, command); break; case "29": ReadConcMeasure(this, command); break; case "2C": ReadVectorInfo(this, command); break; default: break; } } //写回应 if (command.ExpandCmn == "99") { switch (command.Data) { case "88": ExceptionUtil.Instance.LogMethod("设置成功"); //Console.WriteLine("设置成功"); break; case "99": ExceptionUtil.Instance.ExceptionMethod("设置失败", true); //Console.WriteLine("设置失败"); break; default: byte[] data = ByteStrUtil.HexToByte(command.Data); if (data.Length == 6 && data[3] == data[4]) { switch (data[5]) { case 0x88: ExceptionUtil.Instance.LogMethod("设置成功"); //Console.WriteLine("设置成功"); break; case 0x99: ExceptionUtil.Instance.ExceptionMethod("设置失败", true); //Console.WriteLine("设置失败"); break; case 0xAA: ExceptionUtil.Instance.ExceptionMethod("向量更新失败,全部重传", true); //Console.WriteLine("向量更新失败,全部重传"); break; } } break; } } }
private void GetConcMeasureData(object sender, Command command) { try { byte[] data = ByteStrUtil.HexToByte(command.Data); byte statusCode = data[1]; string msg = string.Empty; bool error = true; switch (statusCode) { case 01: msg = "设备正在调零,请等待"; break; case 02: msg = "设备正在标定,请等待"; break; case 03: msg = "光谱数据读取中,请等待"; break; case 04: msg = "设备维护中..."; break; case 05: msg = "设备故障"; break; case 0xAA: msg = "设备待机中"; break; default: error = false; break; } if (error) { ExceptionUtil.Instance.ExceptionMethod(msg, true); return; } List <float> concList = new List <float>(); byte[] pressData = new byte[4]; byte[] tempData = new byte[4]; Array.Copy(data, 7, tempData, 0, 4); Array.Copy(data, 11, pressData, 0, 4); byte[] concData = new byte[data.Length - 15]; Array.Copy(data, 15, concData, 0, data.Length - 15); for (int i = 0; i < concData.Length / 10; i++) { byte[] conc = new byte[4]; Array.Copy(concData, 10 * i + 6, conc, 0, 4); Array.Reverse(conc); concList.Add(BitConverter.ToSingle(conc, 0)); } Array.Reverse(pressData); Array.Reverse(tempData); Dispatcher.BeginInvoke(new Action(() => { UpadateUI(concList, BitConverter.ToSingle(pressData, 0), BitConverter.ToSingle(tempData, 0)); })); concentrationPage.UpdateChart(concList); } catch (Exception e) { ExceptionUtil.Instance.ExceptionMethod(e.Message, true); } }