public void Run(IProperties properties, GlobalDic <string, object> globalDic) { SKGControlCommandProperties config = properties as SKGControlCommandProperties; List <ComDut> comDutList = globalDic[typeof(List <ComDut>).ToString()] as List <ComDut>; //ComDut comDut = globalDic[typeof(ComDut).ToString()] as ComDut; ComDut comDut = null; foreach (var item in comDutList) { if (item.PortName == config.PortName) { comDut = item; break; } } if (comDut == null) { throw new BaseException(string.Format("ComDut No PortName:{0}", config.PortName)); } //OpenPhoneProperties configOpenPhone = globalDic[typeof(OpenPhoneProperties).ToString()] as OpenPhoneProperties; List <OpenPhoneProperties> configList = globalDic[typeof(List <OpenPhoneProperties>).ToString()] as List <OpenPhoneProperties>; OpenPhoneProperties configOpenPhone = null; foreach (var item in configList) { if (item.PortName == config.PortName) { configOpenPhone = item; break; } } if (configOpenPhone == null) { throw new BaseException(string.Format("OpenPhoneProperties No PortName:{0}", config.PortName)); } log = globalDic[typeof(ILog).ToString()] as ILog; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; comDut.Parity = configOpenPhone.Parity; string endLine = configOpenPhone.EndLine; if (!string.IsNullOrEmpty(endLine)) { endLine = Parse_r_n(endLine); } string command = ""; command += config.Head; //int型的处理-转2字节byte,连接到16进制字符串中 int allLength = 9 + 17; //config.DataLength byte[] byteTemp = new byte[2]; byte[] oneByteTemp = new byte[1]; byteTemp = intToBytes(allLength); command += byteToHexStr(byteTemp); command += config.ReservedWord; command += config.CommandWord; //DataContent部分 //枚举类的处理 int intTemp = (int)config.PowerOnOffSetting;//开关机设置 if (config.PowerOnOffSetting != 0) { log.Info(string.Format("开关机设置:{0}", config.PowerOnOffSetting)); } command += intTemp.ToString().PadLeft(2, '0'); intTemp = (int)config.LedModeSetting; if (config.LedModeSetting != 0) { log.Info(string.Format("LED 模式设置:{0}", config.LedModeSetting)); } command += intTemp.ToString().PadLeft(2, '0'); intTemp = (int)config.EmsTestSwitch; if (config.EmsTestSwitch != 0) { log.Info(string.Format("EMS 测试开关设置:{0}", config.EmsTestSwitch)); } command += intTemp.ToString().PadLeft(2, '0'); //int型的处理 byteTemp = intToBytes(config.EmsPWSetting); if (config.EmsPWSetting != 0) { log.Info(string.Format("EMS 脉宽设置:{0}uS", config.EmsPWSetting)); } command += byteToHexStr(byteTemp); byteTemp = intToBytes(config.EmsFreqSetting); if (config.EmsFreqSetting != 0) { log.Info(string.Format("EMS 频率设置:{0}Hz", config.EmsFreqSetting)); } command += byteToHexStr(byteTemp); byteTemp = intToBytes(config.EmsAmplitudeSetting); if (config.EmsAmplitudeSetting != 0) { log.Info(string.Format("EMS 幅度设置:{0}mV", config.EmsAmplitudeSetting)); } command += byteToHexStr(byteTemp); //枚举类的处理 intTemp = (int)config.HeatingGearControl; if (config.HeatingGearControl != 0) { log.Info(string.Format("加热档位控制:{0}", config.HeatingGearControl)); } command += intTemp.ToString().PadLeft(2, '0'); intTemp = (int)config.VoiceControl; command += intTemp.ToString().PadLeft(2, '0'); intTemp = (int)config.WritePcbaFinishFlag; if (config.WritePcbaFinishFlag != 0) { log.Info(string.Format("PCBA 测试完成标志:{0}", config.WritePcbaFinishFlag)); } command += intTemp.ToString().PadLeft(2, '0'); intTemp = (int)config.WholeMachineFinishFlag; if (config.WholeMachineFinishFlag != 0) { log.Info(string.Format("PCBA 测试完成标志:{0}", config.WholeMachineFinishFlag)); } command += intTemp.ToString().PadLeft(2, '0'); intTemp = (int)config.BtTestOnOffSetting; if (config.BtTestOnOffSetting != 0) { log.Info(string.Format("蓝牙模块测试:{0}", config.BtTestOnOffSetting)); } command += intTemp.ToString().PadLeft(2, '0'); intTemp = (int)config.MotorControl; if (config.MotorControl != 0) { log.Info(string.Format("电机控制:{0}", config.MotorControl)); } command += intTemp.ToString().PadLeft(2, '0'); intTemp = (int)config.AginTestOnOffSetting; if (config.AginTestOnOffSetting != 0) { log.Info(string.Format("老化测试开关:{0}", config.AginTestOnOffSetting)); } command += intTemp.ToString().PadLeft(2, '0'); //int型的处理-转为1字节byte oneByteTemp = intToOneBytes(config.AginTestTime); if (config.AginTestTime != 0) { log.Info(string.Format("老化测试时间:{0}分钟", config.AginTestTime * 10)); } command += byteToHexStr(oneByteTemp); #region 多路振动控制 //高位在前,第16位,为第16个电机 if (true) { //int bit1 = (int)config.VibrationControl16; //bit1 = bit1 << 7; //int bit2 = (int)config.VibrationControl15; //bit2 = bit2 << 6; //int bit3 = (int)config.VibrationControl14; //bit3 = bit3 << 5; //int bit4 = (int)config.VibrationControl13; //bit4 = bit4 << 4; //int bit5 = (int)config.VibrationControl12; //bit5 = bit5 << 3; //int bit6 = (int)config.VibrationControl11; //bit6 = bit6 << 2; //int bit7 = (int)config.VibrationControl10; //bit7 = bit7 << 1; //int bit8 = (int)config.VibrationControl9; //int byte2 = bit1 | bit2 | bit3 | bit4 | bit5 | bit6 | bit7 | bit8; //command += byte2.ToString("X");//.PadLeft(2, '0') //bit1 = (int)config.VibrationControl8; //bit1 = bit1 << 7; //bit2 = (int)config.VibrationControl7; //bit2 = bit2 << 6; //bit3 = (int)config.VibrationControl6; //bit3 = bit3 << 5; //bit4 = (int)config.VibrationControl5; //bit4 = bit4 << 4; //bit5 = (int)config.VibrationControl4; //bit5 = bit5 << 3; //bit6 = (int)config.VibrationControl3; //bit6 = bit6 << 2; //bit7 = (int)config.VibrationControl2; //bit7 = bit7 << 1; //bit8 = (int)config.VibrationControl1; //int byte1 = bit1 | bit2 | bit3 | bit4 | bit5 | bit6 | bit7 | bit8; //command += byte1.ToString("X").PadLeft(2, '0'); } #endregion #region 640nm 红光控制,G7 新增功能 if (true) { //intTemp = (int)config.RedLightControl640nm; //command += intTemp.ToString("X").PadLeft(2, '0');// } #endregion //在加校验位 byte[] exceptXor = strToToHexByte(command); byte xor = ByteToXOR(exceptXor); command += xor.ToString("X").PadLeft(2, '0'); //发送完整控制命令 byte[] atCommand = strToToHexByte(command); //string atCommand = PreTranslateAtCommand(config.AtCommand); UartDisplay(atCommand, "T"); //log.Info("AT Commond=" + config.AtCommand); comDut.DtrEnable = configOpenPhone.Dtr; comDut.RtsEnable = configOpenPhone.Rts; comDut.Write(atCommand, 0, atCommand.Length); Thread.Sleep(config.AtCommandInterval); #region //DateTime start = DateTime.Now; //DateTime now = DateTime.Now; //TimeSpan timeSpan = now - start; //while (timeSpan.TotalMilliseconds <= config.Timeout) //{ // try // { // comDut.Write(atCommand, 0, atCommand.Length); // Thread.Sleep(config.AtCommandInterval); // int n = comDut.BytesToRead; // byte[] buf = new byte[n]; // comDut.Read(buf, 0, n); // //解析是否收到正确的包,包头,命令,校验位 // //如果收到的包正确 // if (true) // { // break; // } // } // catch (Exception e) // { // Console.WriteLine(e); // } // Thread.Sleep(100); // now = DateTime.Now; // timeSpan = now - start; //} //if (timeSpan.TotalMilliseconds > config.Timeout) //{ // throw new BaseException("超时:"); //} #endregion TimeUtils.Execute(() => { int n = comDut.BytesToRead; //if (n == 0 ) //{ // //继续循环执行 // return false; //} log.Info(string.Format("接收的长度:{0}", n)); if (n != 0) { byte[] buf = new byte[n]; comDut.Read(buf, 0, n); UartDisplay(buf, "R"); buffer.Clear(); buffer.AddRange(buf); } //byte[] buf = new byte[n]; //comDut.Read(buf, 0, n); //buffer.AddRange(buf); if (buffer.Count > 9) { if (buffer[0] == 0xA5) { //throw new BaseException(string.Format("Head Error")); //if (&& buf[1] == 0xC3)) //{ //} if (buffer[1] == 0xC3) { byte high = buffer[2]; byte low = buffer[3]; int iTotalLength = (high & 0xFF) << 8 | low; if (buffer.Count < iTotalLength) //数据区尚未接收完整 { //继续循环执行 log.Info(string.Format("数据尚未接收完整,已接收:{0},总长度:{1}", buffer.Count, iTotalLength)); return(false); } byte[] oneFrameBytes = new byte[iTotalLength]; buffer.CopyTo(0, oneFrameBytes, 0, iTotalLength); buffer.RemoveRange(0, iTotalLength); UartDisplay(oneFrameBytes, "R"); #region //开始校验-命令字 if (config.CommandType == SKGControlCommandProperties.EnumCommandType.查询指令) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x37)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else if (config.CommandType == SKGControlCommandProperties.EnumCommandType.控制指令) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x40)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else if (config.CommandType == SKGControlCommandProperties.EnumCommandType.发授权) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x23)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else { log.Info(string.Format("非法指令类型")); //继续循环执行 return(false); } //异或校验 1字节 byte[] bufOutOxr = new byte[oneFrameBytes.Length - 1]; Array.Copy(oneFrameBytes, 0, bufOutOxr, 0, oneFrameBytes.Length - 1); byte retXor = ByteToXOR(bufOutOxr); if (oneFrameBytes[iTotalLength - 1] != retXor) { log.Info(string.Format("核验RX的校验位:错误,返回校验位:{0:X2},计算校验位:{1:X2}", oneFrameBytes[iTotalLength - 1], retXor)); //继续循环执行 return(false); } log.Info(string.Format("核验RX的校验位:正确,返回校验位:{0:X2},计算校验位:{1:X2}", oneFrameBytes[iTotalLength - 1], retXor)); //数据内容 32个字节——长度是不固定的 byte[] dataArry = new byte[iTotalLength - 9]; //config.DataLength Array.Copy(oneFrameBytes, 8, dataArry, 0, iTotalLength - 9); //config.DataLength //添加到全局变量中 if (config.GlobalVariblesKey != null) { for (int i = 0; i < config.GlobalVariblesKey.Length; i++) { Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); if (!matchKey.Success) { throw new BaseException("read info key fail"); } string key = matchKey.Groups[1].ToString(); configGv.Add(key, dataArry); log.Info("应答报文格式检查PASS"); } } //跳出循环执行 return(true); #endregion } else { log.Info(string.Format("头部不是0xA5,0xC3,buffer.RemoveAt:{0:X2},{1:X2}", buffer[0], buffer[1])); buffer.RemoveRange(0, 2); //继续循环执行 return(false); } } else { log.Info(string.Format("头部不是0xA5,buffer.RemoveAt:{0:X2}", buffer[0])); buffer.RemoveAt(0); //继续循环执行 return(false); } } else { log.Info(string.Format("buffer.Count:{0}小于最小长度9", buffer.Count)); //继续循环执行 return(false); } }, config.Timeout); #region 原发送命令后等待一固定时间,再接收数据,对应不上则重试 ////解析是否收到正确的包,包头,命令,校验位 ////起始标志 数据长度 保留字 命令字 数据内容 异或校验码 ////0XA5C3 9+N 0x0000 0x0037 Data 异或 ////2 字节 2 字节 2 字节 2 字节 N 字节 1 字节 //UartDisplay(buf, "R"); //if (buf.Length < 9) //{ // throw new BaseException(string.Format("Response Length Not enough")); //} ////包头 //if (!(buf[0] == 0xA5 && buf[1] == 0xC3)) //{ // throw new BaseException(string.Format("Response Head Error")); //} ////长度 2字节 //byte high = buf[2]; //byte low = buf[3]; //int iTotalLength = (high & 0xFF) << 8 | low; //if (iTotalLength != n) //{ // throw new BaseException(string.Format("Response Length Error,长度位:{0},实际长度:{1}", iTotalLength, n)); //} ////命令字 //if (config.CommandType == SKGControlCommandProperties.EnumCommandType.查询指令) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x37)) // { // throw new BaseException(string.Format("Response Command Type Error")); // } //} //else if (config.CommandType == SKGControlCommandProperties.EnumCommandType.控制指令) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x40)) // { // throw new BaseException(string.Format("Response Command Type Error")); // } //} //else if (config.CommandType == SKGControlCommandProperties.EnumCommandType.下发授权) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x23)) // { // throw new BaseException(string.Format("Response Command Type Error")); // } //} //else //{ // throw new BaseException(string.Format("Response为非法指令类型")); //} ////异或校验 1字节 //byte[] bufOutOxr = new byte[buf.Length - 1]; //Array.Copy(buf, 0, bufOutOxr, 0, buf.Length - 1); //byte retXor = ByteToXOR(bufOutOxr); //if (buf[n - 1] != retXor) //{ // throw new BaseException(string.Format("核验RX的校验位:错误,返回校验位:{0:X2},计算校验位:{1:X2}", buf[n - 1], retXor)); //} //log.Info(string.Format("核验RX的校验位:正确,返回校验位:{0:X2},计算校验位:{1:X2}", buf[n - 1], retXor)); ////数据内容 32个字节 ////byte[] dataArry = new byte[config.DataLength]; ////Array.Copy(buf, 8, dataArry, 0, config.DataLength); //byte[] dataArry = new byte[iTotalLength - 9];//config.DataLength //Array.Copy(buf, 8, dataArry, 0, iTotalLength - 9);//config.DataLength ////添加到全局变量中 //if (config.GlobalVariblesKey != null) //{ // for (int i = 0; i < config.GlobalVariblesKey.Length; i++) // { // //string pattern = "-([0-9]{2}) "; // //Match matchValue = Regex.Match(response, pattern); // //if (!matchValue.Success) // //{ // // throw new BaseException("read info value fail"); // //} // //string value = matchValue.Groups[1].ToString(); // Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); // if (!matchKey.Success) // { // throw new BaseException("read info key fail"); // } // string key = matchKey.Groups[1].ToString(); // configGv.Add(key, dataArry); // log.Info("应答报文格式检查PASS"); // } //} #endregion //检查是否有效果!!! //通过另外一个AT口,去获取电流值 ////如果收到的包正确 //string str = byteToHexStr(buf); //string response = comDut.ReadExisting(); //comDut.DtrEnable = false; //comDut.RtsEnable = false; //log.Info("AT Response=" + response); //if (!string.IsNullOrEmpty(config.AtCommandError)) //{ // if (response.Contains(config.AtCommandError)) // { // throw new BaseException(string.Format("AT response=[{0}] contain error=[{1}]", response, config.AtCommandError)); // } //} //if (!string.IsNullOrEmpty(config.AtCommandOk)) //{ // if (!response.Contains(config.AtCommandOk)) // { // throw new BaseException(string.Format("AT response=[{0}] not contain error=[{1}]", response, config.AtCommandOk)); // } //} //if (config.CheckInfo != null) //{ // for (int i = 0; i < config.CheckInfo.Length; i++) // { // if (!string.IsNullOrEmpty(config.CheckInfo[i])) // { // string checkInfo = PreTranslateCheckInfo(config.CheckInfo[i]); // if (!response.Contains(checkInfo)) // { // throw new BaseException(string.Format("AT response=[{0}] not contain check info=[{1}]", response, checkInfo)); // } // } // } //} //if (config.GlobalVariblesKey != null && config.GlobalVariblesKeyPattern != null) //{ // if (config.GlobalVariblesKey.Length != config.GlobalVariblesKeyPattern.Length) // { // throw new BaseException("请保持GlobalVariblesKey与GlobalVariblesKeyPattern成对存在"); // } // for (int i = 0; i < config.GlobalVariblesKey.Length; i++) // { // string pattern = "-([0-9]{2}) "; // Match matchValue = Regex.Match(response, pattern); // if (!matchValue.Success) // { // throw new BaseException("read info value fail"); // } // string value = matchValue.Groups[1].ToString(); // Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); // if (!matchKey.Success) // { // throw new BaseException("read info key fail"); // } // string key = matchKey.Groups[1].ToString(); // configGv.Add(key, value); // } //} }
private void Timer_Tick(object sender, EventArgs e) { TimeSpan remainTime = countDown - (DateTime.Now - start); if (remainTime.Ticks <= 0) { //Timer timer = sender as Timer; timer.Stop(); log.Info(string.Format("未返回正确状态关键字:{0}", this.formTipAndUartCheckProperties.AtCommandOk)); log.Fail("Wait TimeOut, " + countDown.Seconds + "seconds"); //Thread.Sleep(100); this.Close(); } else { lblCountDown.Text = "倒计时:" + remainTime.Seconds + "秒"; if (formTipAndUartCheckProperties.UartDataType == FormTipAndUartCheckProperties.EnumUartDataType.String) { string response = comDut.ReadExisting(); log.Info("Response:" + response); if (!string.IsNullOrEmpty(this.formTipAndUartCheckProperties.AtCommandOk)) { if (response.Contains(this.formTipAndUartCheckProperties.AtCommandOk)) { result = true; timer.Stop(); //throw new BaseException(string.Format("not contain OK=[{0}]", this.formTipAndUartCheckProperties.AtCommandOk)); log.Pass(string.Format("包含关键字:{0}", this.formTipAndUartCheckProperties.AtCommandOk)); //Thread.Sleep(500); this.Close(); } } } else if (formTipAndUartCheckProperties.UartDataType == FormTipAndUartCheckProperties.EnumUartDataType.Hex) { int n = comDut.BytesToRead; //if (n>0) //{ // byte[] buf = new byte[n]; // comDut.Read(buf, 0, n); // UartDisplay(buf, "R"); //} log.Info(string.Format("接收的长度:{0}", n)); if (n != 0) { byte[] buf = new byte[n]; comDut.Read(buf, 0, n); UartDisplay(buf, "R"); buffer.Clear(); buffer.AddRange(buf); } if (buffer.Count > 9) { if (buffer[0] == 0xA5) { //throw new BaseException(string.Format("Head Error")); //if (&& buf[1] == 0xC3)) //{ //} if (buffer[1] == 0xC3) { byte high = buffer[2]; byte low = buffer[3]; int iTotalLength = (high & 0xFF) << 8 | low; if (buffer.Count < iTotalLength) //数据区尚未接收完整 { //继续循环执行 log.Info(string.Format("数据尚未接收完整,已接收:{0},总长度:{1}", buffer.Count, iTotalLength)); return; } byte[] oneFrameBytes = new byte[iTotalLength]; buffer.CopyTo(0, oneFrameBytes, 0, iTotalLength); buffer.RemoveRange(0, iTotalLength); UartDisplay(oneFrameBytes, "R"); #region //开始校验-命令字 if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x41)) { log.Info(string.Format("返回的命令字错误")); //继续循环执行 return; } log.Info("收到DUT状态事件上报"); //异或校验 1字节 byte[] bufOutOxr = new byte[oneFrameBytes.Length - 1]; Array.Copy(oneFrameBytes, 0, bufOutOxr, 0, oneFrameBytes.Length - 1); byte xor = ByteToXOR(bufOutOxr); if (oneFrameBytes[iTotalLength - 1] != xor) { log.Info(string.Format("核验RX的校验位:错误,返回校验位:{0:X2},计算校验位:{1:X2}", oneFrameBytes[iTotalLength - 1], xor)); //继续循环执行 return; } log.Info(string.Format("核验RX的校验位:正确,返回校验位:{0:X2},计算校验位:{1:X2}", oneFrameBytes[iTotalLength - 1], xor)); #region //再显示 按键事件,USB插入检查,电池状态,佩戴检测等状态 //数据内容 byte[] dataArry = new byte[iTotalLength - 9]; //config.DataLength Array.Copy(oneFrameBytes, 8, dataArry, 0, iTotalLength - 9); //config.DataLength //按键事件 switch (dataArry[0]) { case 0x00: log.Info("无按键"); break; case 0x01: log.Info("电源键按下"); break; case 0x02: log.Info("加热键被按下"); break; case 0x03: log.Info("+键被按下"); break; case 0x04: log.Info("-键被按下"); break; default: break; } //USB插入事件 switch (dataArry[1]) { case 0x00: log.Info("无状态改变"); break; case 0x01: log.Info("USB 未插入"); break; case 0x02: log.Info("USB 已插入"); break; default: break; } //电池状态 switch (dataArry[2]) { case 0x00: log.Info("无状态改变"); break; case 0x01: log.Info("未充电"); break; case 0x02: log.Info("充电中"); break; case 0x03: log.Info("充满状态"); break; case 0x04: log.Info("电量不足"); break; default: break; } //佩戴检测状态 switch (dataArry[3]) { case 0x00: log.Info("无状态改变"); break; case 0x01: log.Info("未检测到佩戴"); break; case 0x02: log.Info("检测到有佩戴"); break; default: break; } #endregion if (formTipAndUartCheckProperties.CheckKeyEvent != FormTipAndUartCheckProperties.EnumCheckKeyEvent.检查) { switch (formTipAndUartCheckProperties.CheckKeyEvent) { case FormTipAndUartCheckProperties.EnumCheckKeyEvent.无按键: if (dataArry[0] != 0x00) { return; } break; case FormTipAndUartCheckProperties.EnumCheckKeyEvent.电源键按下: if (dataArry[0] != 0x01) { return; } break; case FormTipAndUartCheckProperties.EnumCheckKeyEvent.加热键被按下: if (dataArry[0] != 0x02) { return; } break; case FormTipAndUartCheckProperties.EnumCheckKeyEvent.加键被按下: if (dataArry[0] != 0x03) { return; } break; case FormTipAndUartCheckProperties.EnumCheckKeyEvent.减键被按下: if (dataArry[0] != 0x04) { return; } break; default: break; } log.Pass(string.Format("PASS,检测到正确按键上报事件:{0}", formTipAndUartCheckProperties.CheckKeyEvent)); } if (formTipAndUartCheckProperties.CheckUsbInsert != FormTipAndUartCheckProperties.EnumCheckUsbInsertEvent.检查) { switch (formTipAndUartCheckProperties.CheckUsbInsert) { case FormTipAndUartCheckProperties.EnumCheckUsbInsertEvent.无状态改变: if (dataArry[1] != 0x00) { return; } break; case FormTipAndUartCheckProperties.EnumCheckUsbInsertEvent.USB未插入: if (dataArry[1] != 0x01) { return; } break; case FormTipAndUartCheckProperties.EnumCheckUsbInsertEvent.USB已插入: if (dataArry[1] != 0x02) { return; } break; default: break; } log.Pass(string.Format("PASS,检测到正确USB状态事件:{0}", formTipAndUartCheckProperties.CheckUsbInsert)); } if (formTipAndUartCheckProperties.CheckBatteryStatus != FormTipAndUartCheckProperties.EnumCheckBatteryStatus.检查) { switch (formTipAndUartCheckProperties.CheckBatteryStatus) { case FormTipAndUartCheckProperties.EnumCheckBatteryStatus.无状态改变: if (dataArry[2] != 0x00) { return; } break; case FormTipAndUartCheckProperties.EnumCheckBatteryStatus.未充电: if (dataArry[2] != 0x01) { return; } break; case FormTipAndUartCheckProperties.EnumCheckBatteryStatus.充电中: if (dataArry[2] != 0x02) { return; } break; case FormTipAndUartCheckProperties.EnumCheckBatteryStatus.充满状态: if (dataArry[2] != 0x03) { return; } break; case FormTipAndUartCheckProperties.EnumCheckBatteryStatus.电量不足: if (dataArry[2] != 0x04) { return; } break; default: break; } log.Pass(string.Format("PASS,检测到正确电池状态上报事件:{0}", formTipAndUartCheckProperties.CheckBatteryStatus)); } if (formTipAndUartCheckProperties.CheckWearingStatus != FormTipAndUartCheckProperties.EnumCheckWearingStatus.检查) { switch (formTipAndUartCheckProperties.CheckWearingStatus) { case FormTipAndUartCheckProperties.EnumCheckWearingStatus.无状态改变: if (dataArry[3] != 0x00) { return; } break; case FormTipAndUartCheckProperties.EnumCheckWearingStatus.未检测到佩戴: if (dataArry[3] != 0x01) { return; } break; case FormTipAndUartCheckProperties.EnumCheckWearingStatus.检测到有佩戴: if (dataArry[3] != 0x02) { return; } break; default: break; } log.Pass(string.Format("PASS,检测到正确的佩戴状态事件:{0}", formTipAndUartCheckProperties.CheckWearingStatus)); } //if (formTipAndUartCheckProperties.== 检查是否是充满状态事件) //{ // if (dataArry[2] != 0x03) // { // return; // } //} //跳出循环执行 result = true; timer.Stop(); //throw new BaseException(string.Format("not contain OK=[{0}]", this.formTipAndUartCheckProperties.AtCommandOk)); //log.Pass(string.Format("PASS,正确状态上报事件:{0}", this.formTipAndUartCheckProperties.AtCommandOk)); //Thread.Sleep(500); this.Close(); #endregion } else { log.Info(string.Format("头部不是0xA5,0xC3,buffer.RemoveAt:{0:X2},{1:X2}", buffer[0], buffer[1])); buffer.RemoveRange(0, 2); ////继续循环执行 //return false; } } else { log.Info(string.Format("头部不是0xA5,buffer.RemoveAt:{0:X2}", buffer[0])); buffer.RemoveAt(0); } } #region //字符串的检查关键字的方式 //string response = byteToHexStr(buf); #region //先检查格式 //if (buf.Length < 9) //{ // return; // //throw new BaseException(string.Format("Length Not enough")); //} ////包头 //if (!(buf[0] == 0xA5 && buf[1] == 0xC3)) //{ // return; // //throw new BaseException(string.Format("Head Error")); //} ////长度 2字节 //byte high = buf[2]; //byte low = buf[3]; //int iTotalLength = (high & 0xFF) << 8 | low; //if (iTotalLength != n) //{ // return; // //throw new BaseException(string.Format("Length Error,长度位:{0},实际长度:{1}", iTotalLength, n)); //} ////命令字 //if (!(buf[6] == 0xA0 && buf[7] == 0x41)) //{ // return; // //throw new BaseException(string.Format("CMD Type Error")); //} ////异或校验 1字节 //byte[] bufOutOxr = new byte[buf.Length - 1]; //Array.Copy(buf, 0, bufOutOxr, 0, buf.Length - 1); //byte xor = ByteToXOR(bufOutOxr); //if (buf[n - 1] != xor) //{ // return; // //throw new BaseException(string.Format("校验不正确,返回校验位:{0},计算校验位:{1}", buf[n - 1], xor)); //} //log.Info(string.Format("校验位核验正确,返回校验位:{0:X2},计算校验位:{1:X2}", buf[n - 1], xor)); #endregion #region //再显示 按键事件,USB插入检查,电池状态,佩戴检测等状态 ////数据内容 //byte[] dataArry = new byte[iTotalLength - 9];//config.DataLength //Array.Copy(buf, 8, dataArry, 0, iTotalLength - 9);//config.DataLength ////按键事件 //switch (dataArry[0]) //{ // case 0x00: // log.Info("无按键"); // break; // case 0x01: // log.Info("电源键按下"); // break; // case 0x02: // log.Info("加热键被按下"); // break; // case 0x03: // log.Info("+键被按下"); // break; // case 0x04: // log.Info("-键被按下"); // break; // default: // break; //} ////USB插入事件 //switch (dataArry[1]) //{ // case 0x00: // log.Info("无状态改变"); // break; // case 0x01: // log.Info("USB 未插入"); // break; // case 0x02: // log.Info("USB 已插入"); // break; // default: // break; //} ////电池状态 //switch (dataArry[2]) //{ // case 0x00: // log.Info("无状态改变"); // break; // case 0x01: // log.Info("未充电"); // break; // case 0x02: // log.Info("充电中"); // break; // case 0x03: // log.Info("充满状态"); // break; // case 0x04: // log.Info("电量不足"); // break; // default: // break; //} ////佩戴检测状态 //switch (dataArry[3]) //{ // case 0x00: // log.Info("无状态改变"); // break; // case 0x01: // log.Info("未检测到佩戴"); // break; // case 0x02: // log.Info("检测到有佩戴"); // break; // default: // break; //} #endregion //if (!string.IsNullOrEmpty(this.formTipAndUartCheckProperties.AtCommandOk)) //{ // if (response.Contains(this.formTipAndUartCheckProperties.AtCommandOk)) // { // result = true; // timer.Stop(); // //throw new BaseException(string.Format("not contain OK=[{0}]", this.formTipAndUartCheckProperties.AtCommandOk)); // log.Pass(string.Format("PASS,返回正确状态关键字:{0}", this.formTipAndUartCheckProperties.AtCommandOk)); // //Thread.Sleep(500); // this.Close(); // } //} #endregion } } }
public void Run(IProperties properties, GlobalDic <string, object> globalDic) { SKGProperties config = properties as SKGProperties; List <ComDut> comDutList = globalDic[typeof(List <ComDut>).ToString()] as List <ComDut>; //ComDut comDut = globalDic[typeof(ComDut).ToString()] as ComDut; ComDut comDut = null; foreach (var item in comDutList) { if (item.PortName == config.PortName) { comDut = item; break; } } if (comDut == null) { throw new BaseException(string.Format("ComDut No PortName:{0}", config.PortName)); } //OpenPhoneProperties configOpenPhone = globalDic[typeof(OpenPhoneProperties).ToString()] as OpenPhoneProperties; List <OpenPhoneProperties> configList = globalDic[typeof(List <OpenPhoneProperties>).ToString()] as List <OpenPhoneProperties>; OpenPhoneProperties configOpenPhone = null; foreach (var item in configList) { if (item.PortName == config.PortName) { configOpenPhone = item; break; } } if (configOpenPhone == null) { throw new BaseException(string.Format("OpenPhoneProperties No PortName:{0}", config.PortName)); } log = globalDic[typeof(ILog).ToString()] as ILog; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; comDut.Parity = configOpenPhone.Parity; string endLine = configOpenPhone.EndLine; if (!string.IsNullOrEmpty(endLine)) { endLine = Parse_r_n(endLine); } byte[] atCommand = strToToHexByte(config.AtCommand); //string atCommand = PreTranslateAtCommand(config.AtCommand); UartDisplay(atCommand, "T"); //log.Info("AT Commond=" + config.AtCommand); comDut.DtrEnable = configOpenPhone.Dtr; comDut.RtsEnable = configOpenPhone.Rts; comDut.Write(atCommand, 0, atCommand.Length); Thread.Sleep(config.AtCommandInterval); #region //DateTime start = DateTime.Now; //DateTime now = DateTime.Now; //TimeSpan timeSpan = now - start; //while (timeSpan.TotalMilliseconds <= config.Timeout) //{ // try // { // comDut.Write(atCommand, 0, atCommand.Length); // Thread.Sleep(config.AtCommandInterval); // int n = comDut.BytesToRead; // byte[] buf = new byte[n]; // comDut.Read(buf, 0, n); // //解析是否收到正确的包,包头,命令,校验位 // //如果收到的包正确 // if (true) // { // break; // } // } // catch (Exception e) // { // Console.WriteLine(e); // } // Thread.Sleep(100); // now = DateTime.Now; // timeSpan = now - start; //} //if (timeSpan.TotalMilliseconds > config.Timeout) //{ // throw new BaseException("超时:"); //} #endregion //发命令后一直循环接收->检验->拼接,超过500ms还没检验通过则超时FAIL TimeUtils.Execute(() => { int n = comDut.BytesToRead; //if (n==0) //{ // //继续循环执行 // return false; //} log.Info(string.Format("接收的长度:{0}", n)); if (n > 0) { byte[] buf = new byte[n]; comDut.Read(buf, 0, n); UartDisplay(buf, "R"); buffer.Clear(); buffer.AddRange(buf); } //byte[] buf = new byte[n]; //comDut.Read(buf, 0, n); //buffer.AddRange(buf); if (buffer.Count > 9) { if (buffer[0] == 0xA5) { //throw new BaseException(string.Format("Head Error")); //if (&& buf[1] == 0xC3)) //{ //} if (buffer[1] == 0xC3) { byte high = buffer[2]; byte low = buffer[3]; int iTotalLength = (high & 0xFF) << 8 | low; if (buffer.Count < iTotalLength) //数据区尚未接收完整 { //继续循环执行 log.Info(string.Format("数据尚未接收完整,已接收:{0},总长度:{1}", buffer.Count, iTotalLength)); return(false); } byte[] oneFrameBytes = new byte[iTotalLength]; buffer.CopyTo(0, oneFrameBytes, 0, iTotalLength); buffer.RemoveRange(0, iTotalLength); UartDisplay(oneFrameBytes, "R"); #region //开始校验-命令字 if (config.CommandType == SKGProperties.EnumCommandType.查询指令) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x37)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else if (config.CommandType == SKGProperties.EnumCommandType.控制指令) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x40)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else if (config.CommandType == SKGProperties.EnumCommandType.发授权) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x23)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else { log.Info(string.Format("非法指令类型")); //继续循环执行 return(false); } //异或校验 1字节 byte[] bufOutOxr = new byte[oneFrameBytes.Length - 1]; Array.Copy(oneFrameBytes, 0, bufOutOxr, 0, oneFrameBytes.Length - 1); byte xor = ByteToXOR(bufOutOxr); if (oneFrameBytes[iTotalLength - 1] != xor) { log.Info(string.Format("核验RX的校验位:错误,返回校验位:{0:X2},计算校验位:{1:X2}", oneFrameBytes[iTotalLength - 1], xor)); //继续循环执行 return(false); } log.Info(string.Format("核验RX的校验位:正确,返回校验位:{0:X2},计算校验位:{1:X2}", oneFrameBytes[iTotalLength - 1], xor)); //数据内容 32个字节——长度是不固定的 byte[] dataArry = new byte[iTotalLength - 9]; //config.DataLength Array.Copy(oneFrameBytes, 8, dataArry, 0, iTotalLength - 9); //config.DataLength //添加到全局变量中 if (config.GlobalVariblesKey != null) { for (int i = 0; i < config.GlobalVariblesKey.Length; i++) { Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); if (!matchKey.Success) { throw new BaseException("read info key fail"); } string key = matchKey.Groups[1].ToString(); configGv.Add(key, dataArry); log.Info("应答报文格式检查PASS"); } } //跳出循环执行 return(true); #endregion } else { log.Info(string.Format("头部不是0xA5,0xC3,buffer.RemoveAt:{0:X2},{1:X2}", buffer[0], buffer[1])); buffer.RemoveRange(0, 2); //继续循环执行 return(false); } } else { log.Info(string.Format("头部不是0xA5,buffer.RemoveAt:{0:X2}", buffer[0])); buffer.RemoveAt(0); //继续循环执行 return(false); } } else { //继续循环执行 log.Info(string.Format("buffer.Count:{0}小于最小长度9", buffer.Count)); return(false); } }, config.Timeout); //int n = comDut.BytesToRead; //byte[] buf = new byte[n]; //comDut.Read(buf, 0, n); #region ////1.缓存数据 //buffer.AddRange(buf); //if (!flagCmdHandle) //{ // //如果接受到的上一帧数据未处理,软件只接受数据进buffer,不提取指令 // return; //} //2.完整性判断 //while (buffer.Count >= 9) //至少包含帧头(2字节)、长度(1字节)、校验位(1字节);根据设计不同而不同 //{ // //2.1 查找数据头 // if (buffer[0] == 0xAA) //传输数据有帧头1,用于判断 // { // //if (buffer[1] == 0xFF) //传输数据有帧头2,用于判断 // //{ // int iDataLength = buffer[1]; // int iTotalLength = iDataLength + 1;//前两个与最后一个 // int cnt = 0; // //for (int i = 0; i < buffer.Count; i++) // //{ // // if (buffer[i] == 0x55 && buffer[i - 1] == 0xFF) // // { // // cnt++; // // } // //} // //if (buffer.Count>) // //{ // //} // if (buffer.Count < iTotalLength + cnt) //数据区尚未接收完整 // { // break; // } // //得到完整的数据,复制到rawBytes中进行校验 // iTotalLength = buffer.Count; // byte[] rawBytes = new byte[iTotalLength]; // buffer.CopyTo(0, rawBytes, 0, iTotalLength); // buffer.Clear(); // //if (buffer[10] == 0x01) // //{ // // byte[] rawBytes = { 0xFF, 0xFF, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA5, 0x01, 0x04, 0x09, 0xE0, 0xB6, 0xF5, 0x18, 0x8D, 0x40, 0x13, 0x78, 0x0D, 0xE4, 0xDE, 0xE1, 0x15, 0x96, 0x4A, 0x3E, 0xDC, 0x1C, 0xE1, 0xAA, 0xFF, 0x55, 0x85, 0xDB, 0xC6, 0xFD, 0x76, 0x32, 0x39, 0x6F, 0x8E, 0x67, 0xFA }; // //} // byte checksum = 0; //开始校验 // //if (rawBytes[rawBytes.Length - 1] == 0x55 && rawBytes[rawBytes.Length - 2] == 0xFF) // //{ // // for (int i = 2; i < rawBytes.Length - 2; i++) // // { // // checksum += rawBytes[i]; // // } // // if (checksum != rawBytes[rawBytes.Length - 2]) //校验失败,最后一个字节是校验位 // // { // // UartDisplay("Rx Error\r\n"); // // continue; // // } // //} // //else // //{ // for (int i = 1; i < rawBytes.Length - 1; i++) // { // checksum += rawBytes[i]; // } // checksum = (byte)~checksum; // checksum += 0x01; // if (checksum != rawBytes[rawBytes.Length - 1]) //校验失败,最后一个字节是校验位 // { // UartDisplay("Rx Error\r\n"); // continue; // } // //} // UartDisplay(rawBytes, "R"); // //ReceiveBytes = new byte[rawBytes.Length]; // //rawBytes.CopyTo(ReceiveBytes, 0); // flagCmdHandle = false; // RecCmd_AC(rawBytes); // //} // rawBytes = null; // flagCmdHandle = true; // } // else //帧头1不正确时,记得清除 // { // buffer.RemoveAt(0); // } //} #endregion #region ////解析是否收到正确的包,包头,命令,校验位 ////起始标志 数据长度 保留字 命令字 数据内容 异或校验码 ////0XA5C3 9+N 0x0000 0x0037 Data 异或 ////2 字节 2 字节 2 字节 2 字节 N 字节 1 字节 //UartDisplay(buf,"R"); //if (buf.Length < 9) //{ // throw new BaseException(string.Format("Length Not enough")); //} ////包头 //if (!(buf[0]==0xA5 && buf[1]==0xC3)) //{ // throw new BaseException(string.Format("Head Error")); //} ////长度 2字节 //byte high = buf[2]; //byte low = buf[3]; //int iTotalLength = (high & 0xFF) << 8 | low; //if (iTotalLength != n) //{ // throw new BaseException(string.Format("Length Error,长度位:{0},实际长度:{1}", iTotalLength,n)); //} ////命令字 //if (config.CommandType==SKGProperties.EnumCommandType.查询指令) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x37)) // { // throw new BaseException(string.Format("CMD Type Error")); // } //} //else if(config.CommandType == SKGProperties.EnumCommandType.控制指令) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x40)) // { // throw new BaseException(string.Format("CMD Type Error")); // } //} //else if(config.CommandType == SKGProperties.EnumCommandType.下发授权) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x23)) // { // throw new BaseException(string.Format("CMD Type Error")); // } //} //else //{ // throw new BaseException(string.Format("非法指令类型")); //} ////异或校验 1字节 //byte[] bufOutOxr = new byte[buf.Length - 1]; //Array.Copy(buf, 0, bufOutOxr, 0, buf.Length - 1); //byte xor = ByteToXOR(bufOutOxr); //if (buf[n-1] != xor) //{ // throw new BaseException(string.Format("核验RX的校验位:错误,返回校验位:{0:X2},计算校验位:{1:X2}", buf[n - 1], xor)); //} //log.Info(string.Format("核验RX的校验位:正确,返回校验位:{0:X2},计算校验位:{1:X2}", buf[n - 1], xor)); ////数据内容 32个字节——长度是不固定的 //byte[] dataArry = new byte[iTotalLength-9];//config.DataLength //Array.Copy(buf, 8, dataArry, 0, iTotalLength - 9);//config.DataLength ////添加到全局变量中 //if (config.GlobalVariblesKey != null) //{ // for (int i = 0; i < config.GlobalVariblesKey.Length; i++) // { // //string pattern = "-([0-9]{2}) "; // //Match matchValue = Regex.Match(response, pattern); // //if (!matchValue.Success) // //{ // // throw new BaseException("read info value fail"); // //} // //string value = matchValue.Groups[1].ToString(); // Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); // if (!matchKey.Success) // { // throw new BaseException("read info key fail"); // } // string key = matchKey.Groups[1].ToString(); // configGv.Add(key, dataArry); // log.Info("应答报文格式检查PASS"); // } //} #endregion ////如果收到的包正确 //string str = byteToHexStr(buf); //string response = comDut.ReadExisting(); //comDut.DtrEnable = false; //comDut.RtsEnable = false; //log.Info("AT Response=" + response); //if (!string.IsNullOrEmpty(config.AtCommandError)) //{ // if (response.Contains(config.AtCommandError)) // { // throw new BaseException(string.Format("AT response=[{0}] contain error=[{1}]", response, config.AtCommandError)); // } //} //if (!string.IsNullOrEmpty(config.AtCommandOk)) //{ // if (!response.Contains(config.AtCommandOk)) // { // throw new BaseException(string.Format("AT response=[{0}] not contain error=[{1}]", response, config.AtCommandOk)); // } //} //if (config.CheckInfo != null) //{ // for (int i = 0; i < config.CheckInfo.Length; i++) // { // if (!string.IsNullOrEmpty(config.CheckInfo[i])) // { // string checkInfo = PreTranslateCheckInfo(config.CheckInfo[i]); // if (!response.Contains(checkInfo)) // { // throw new BaseException(string.Format("AT response=[{0}] not contain check info=[{1}]", response, checkInfo)); // } // } // } //} //if (config.GlobalVariblesKey != null && config.GlobalVariblesKeyPattern != null) //{ // if (config.GlobalVariblesKey.Length != config.GlobalVariblesKeyPattern.Length) // { // throw new BaseException("请保持GlobalVariblesKey与GlobalVariblesKeyPattern成对存在"); // } // for (int i = 0; i < config.GlobalVariblesKey.Length; i++) // { // string pattern = "-([0-9]{2}) "; // Match matchValue = Regex.Match(response, pattern); // if (!matchValue.Success) // { // throw new BaseException("read info value fail"); // } // string value = matchValue.Groups[1].ToString(); // Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); // if (!matchKey.Success) // { // throw new BaseException("read info key fail"); // } // string key = matchKey.Groups[1].ToString(); // configGv.Add(key, value); // } //} }
public void Run(IProperties properties, GlobalDic <string, object> globalDic) { config = properties as CheckHexUartPowerOnProperties; ILog log = globalDic[typeof(ILog).ToString()] as ILog; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; List <ComDut> comDutList = globalDic[typeof(List <ComDut>).ToString()] as List <ComDut>; //ComDut comDut = globalDic[typeof(ComDut).ToString()] as ComDut; ComDut comDut = null; foreach (var item in comDutList) { if (item.PortName == config.PortName) { comDut = item; break; } } if (comDut == null) { throw new BaseException(string.Format("ComDut No PortName:{0}", config.PortName)); } //OpenPhoneProperties configOpenPhone = globalDic[typeof(OpenPhoneProperties).ToString()] as OpenPhoneProperties; List <OpenPhoneProperties> configList = globalDic[typeof(List <OpenPhoneProperties>).ToString()] as List <OpenPhoneProperties>; OpenPhoneProperties configOpenPhone = null; foreach (var item in configList) { if (item.PortName == config.PortName) { configOpenPhone = item; break; } } if (configOpenPhone == null) { throw new BaseException(string.Format("OpenPhoneProperties No PortName:{0}", config.PortName)); } string endLine = configOpenPhone.EndLine; if (!string.IsNullOrEmpty(endLine)) { endLine = Parse_r_n(endLine); } //byte[] atCommand = strToToHexByte(config.AtCommand); ////string atCommand = PreTranslateAtCommand(config.AtCommand); //log.Info("AT Commond=" + config.AtCommand); //comDut.DtrEnable = configOpenPhone.Dtr; //comDut.RtsEnable = configOpenPhone.Rts; //comDut.Parity = System.IO.Ports.Parity.Even; TimeUtils.Execute(() => { Thread.Sleep(config.AtCommandInterval); int length = comDut.ReadBufferSize; byte[] byteArray = new byte[length]; comDut.Read(byteArray, 0, length); configGv.Add("RetBytes", byteArray); byte[] retArray = configGv.GetObject("RetBytes") as byte[]; for (int i = 0; i < retArray.Length; i++) {//55 AA 03 if (retArray[i] == 0x55 && retArray[i + 1] == 0xAA && retArray[i + 2] == 0x03 ) { //flageRightRes = true; //startIndex = i; //break; log.Info(string.Format("检测到产品上电,55 AA 03")); //跳出循环执行 return(true); } } //继续循环执行 return(false); }, config.Timeout); }
public void Run(IProperties properties, GlobalDic <string, object> globalDic)//virtual { config = properties as FindDeviceProperties; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; List <ComDut> comDutList = globalDic[typeof(List <ComDut>).ToString()] as List <ComDut>; //ComDut comDut = globalDic[typeof(ComDut).ToString()] as ComDut; ComDut comDut = null; foreach (var item in comDutList) { if (item.PortName == config.PortName) { comDut = item; break; } } if (comDut == null) { throw new BaseException(string.Format("ComDut No PortName:{0}", config.PortName)); } log = globalDic[typeof(ILog).ToString()] as ILog; //int ret = -1; //string returnString = ""; string endLine = config.EndLine; string response = ""; if (config.CommandType == FindDeviceProperties.EnumCommandType.String) { TimeUtils.Execute(() => { comDut.Write(config.TestPowerOnAT + endLine); Thread.Sleep(config.AtCommandInterval); response += comDut.ReadExisting(); if (!string.IsNullOrEmpty(config.AtCommandOk)) { if (response.Contains(config.AtCommandOk)) { log.Info(string.Format("检测到产品上电,AT response=[{0}] contain =[{1}]", response, config.AtCommandOk)); //跳出循环执行 return(true); } } //继续循环执行 return(false); }, config.Timeout); if (!string.IsNullOrEmpty(config.AtCommandError)) { if (response.Contains(config.AtCommandError)) { throw new BaseException(string.Format("AT response contain error:\r\n[{0}]", config.AtCommandError)); } } if (config.CheckInfo != null) { for (int i = 0; i < config.CheckInfo.Length; i++) { if (!string.IsNullOrEmpty(config.CheckInfo[i])) { string checkInfo = PreTranslateCheckInfo(config.CheckInfo[i]); if (!response.Contains(checkInfo)) { throw new BaseException(string.Format("AT response not contain check info:\r\n{0}", checkInfo)); } log.Info(string.Format("AT response contain check info:\r\n{0}", checkInfo)); } } } if (config.GlobalVariblesKey != null && config.GlobalVariblesKeyPattern != null) { if (config.GlobalVariblesKey.Length != config.GlobalVariblesKey.Length) { throw new BaseException("GlobalVariblesKey's length != GlobalVariblesKeyPattern's length"); } for (int i = 0; i < config.GlobalVariblesKey.Length; i++) { if (!string.IsNullOrEmpty(config.GlobalVariblesKey[i])) { string pattern = Parse_r_n(config.GlobalVariblesKeyPattern[i]); //response = response.Replace("\r\n", "").Replace("\r", "").Replace("\n", ""); Match matchValue = Regex.Match(response, pattern); if (!matchValue.Success) { throw new BaseException(string.Format("read info {0} value fail", i)); } string value = matchValue.Groups[1].ToString(); Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); if (!matchKey.Success) { throw new BaseException(string.Format("read info {0} key fail", i)); } string key = matchKey.Groups[1].ToString(); configGv.Add(key, value); } } } } else if (config.CommandType == FindDeviceProperties.EnumCommandType.Hex) { TimeUtils.Execute(() => { byte[] atCommand = strToToHexByte(config.TestPowerOnAT); //string atCommand = PreTranslateAtCommand(config.AtCommand); //log.Info("AT Commond=" + config.TestPowerOnAT); comDut.Write(atCommand, 0, atCommand.Length); Thread.Sleep(config.AtCommandInterval); //string response = comDut.ReadExisting(); int n = comDut.BytesToRead; byte[] buf = new byte[n]; comDut.Read(buf, 0, n); response = byteToHexStr(buf); //comDut.Write(config.TestPowerOnAT + endLine); //Thread.Sleep(config.AtCommandInterval); //response += comDut.ReadExisting(); if (!string.IsNullOrEmpty(config.AtCommandOk)) { if (response.Contains(config.AtCommandOk)) { log.Info(string.Format("检测到产品上电,Response:\r\n{0}\r\n contain:\r\n{1}", response, config.AtCommandOk)); //跳出循环执行 return(true); } } //继续循环执行 return(false); }, config.Timeout); if (!string.IsNullOrEmpty(config.AtCommandError)) { if (response.Contains(config.AtCommandError)) { throw new BaseException(string.Format("AT response contain error:\r\n[{0}]", config.AtCommandError)); } } if (config.CheckInfo != null) { for (int i = 0; i < config.CheckInfo.Length; i++) { if (!string.IsNullOrEmpty(config.CheckInfo[i])) { string checkInfo = PreTranslateCheckInfo(config.CheckInfo[i]); if (!response.Contains(checkInfo)) { throw new BaseException(string.Format("AT response not contain check info:\r\n{0}", checkInfo)); } log.Info(string.Format("AT response contain check info:\r\n{0}", checkInfo)); } } } if (config.GlobalVariblesKey != null && config.GlobalVariblesKeyPattern != null) { if (config.GlobalVariblesKey.Length != config.GlobalVariblesKey.Length) { throw new BaseException("GlobalVariblesKey's length != GlobalVariblesKeyPattern's length"); } for (int i = 0; i < config.GlobalVariblesKey.Length; i++) { if (!string.IsNullOrEmpty(config.GlobalVariblesKey[i])) { string pattern = Parse_r_n(config.GlobalVariblesKeyPattern[i]); //response = response.Replace("\r\n", "").Replace("\r", "").Replace("\n", ""); Match matchValue = Regex.Match(response, pattern); if (!matchValue.Success) { throw new BaseException(string.Format("read info {0} value fail", i)); } string value = matchValue.Groups[1].ToString(); Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); if (!matchKey.Success) { throw new BaseException(string.Format("read info {0} key fail", i)); } string key = matchKey.Groups[1].ToString(); configGv.Add(key, value); } } } } //configGv.Add("PowerOnLog", response); }
public void Run(IProperties properties, GlobalDic <string, object> globalDic) { config = properties as CheckHexUartProperties; ILog log = globalDic[typeof(ILog).ToString()] as ILog; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; List <ComDut> comDutList = globalDic[typeof(List <ComDut>).ToString()] as List <ComDut>; //ComDut comDut = globalDic[typeof(ComDut).ToString()] as ComDut; ComDut comDut = null; foreach (var item in comDutList) { if (item.PortName == config.PortName) { comDut = item; break; } } if (comDut == null) { throw new BaseException(string.Format("ComDut No PortName:{0}", config.PortName)); } //OpenPhoneProperties configOpenPhone = globalDic[typeof(OpenPhoneProperties).ToString()] as OpenPhoneProperties; List <OpenPhoneProperties> configList = globalDic[typeof(List <OpenPhoneProperties>).ToString()] as List <OpenPhoneProperties>; OpenPhoneProperties configOpenPhone = null; foreach (var item in configList) { if (item.PortName == config.PortName) { configOpenPhone = item; break; } } if (configOpenPhone == null) { throw new BaseException(string.Format("OpenPhoneProperties No PortName:{0}", config.PortName)); } string endLine = configOpenPhone.EndLine; if (!string.IsNullOrEmpty(endLine)) { endLine = Parse_r_n(endLine); } byte[] atCommand = strToToHexByte(config.AtCommand); //string atCommand = PreTranslateAtCommand(config.AtCommand); log.Info("AT Commond=" + config.AtCommand); //comDut.DtrEnable = configOpenPhone.Dtr; //comDut.RtsEnable = configOpenPhone.Rts; //comDut.Parity = System.IO.Ports.Parity.Even; comDut.Write(atCommand, 0, atCommand.Length); log.Info("Tx:" + config.AtCommand); Thread.Sleep(config.AtCommandInterval); int length = comDut.ReadBufferSize; byte[] byteArray = new byte[length]; comDut.Read(byteArray, 0, length); #region //string hexStr = byteToHexStr(byteArray); ////log.Info("Rx:" + hexStr); //string response = System.Text.Encoding.ASCII.GetString(byteArray); ////response = response.Replace("\0", "~"); ////string response = comDut.ReadExisting(); ////byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(response); configGv.Add("RetBytes", byteArray); //comDut.DtrEnable = false; //comDut.RtsEnable = false; ////log.Info("Response:\r\n" + response); //if (!string.IsNullOrEmpty(config.AtCommandError)) //{ // if (response.Contains(config.AtCommandError)) // { // throw new BaseException(string.Format("AT response=[{0}] contain error=[{1}]", response, config.AtCommandError)); // } //} //if (!string.IsNullOrEmpty(config.AtCommandOk)) //{ // if (!response.Contains(config.AtCommandOk)) // { // throw new BaseException(string.Format("AT response=[{0}] not contain error=[{1}]", response, config.AtCommandOk)); // } //} //if (config.CheckInfo != null) //{ // for (int i = 0; i < config.CheckInfo.Length; i++) // { // if (!string.IsNullOrEmpty(config.CheckInfo[i])) // { // string checkInfo = PreTranslateCheckInfo(config.CheckInfo[i]); // if (!response.Contains(checkInfo)) // { // throw new BaseException(string.Format("AT response=[{0}] not contain check info=[{1}]", response, checkInfo)); // } // } // } //} //if (!string.IsNullOrEmpty(config.GlobalVariblesKey) && !string.IsNullOrEmpty(config.GlobalVariblesKeyPattern)) //{ // string pattern = Parse_r_n(config.GlobalVariblesKeyPattern); // Match matchValue = Regex.Match(response, pattern); // if (!matchValue.Success) // { // throw new BaseException("read info value fail"); // } // string value = matchValue.Groups[1].ToString(); // Match matchKey = Regex.Match(config.GlobalVariblesKey, this.pattern); // if (!matchKey.Success) // { // throw new BaseException("read info key fail"); // } // string key = matchKey.Groups[1].ToString(); // configGv.Add(key, value); //} #endregion byte[] retArray = configGv.GetObject("RetBytes") as byte[]; int startIndex = -1; //bool flageRightRes = false; for (int i = 0; i < retArray.Length; i++) { if (retArray[i] == 0xA6 && retArray[i + 1] == 0xA6) { //flageRightRes = true; startIndex = i; break; ////记录i的位置 //////核验校验位 ////sum += byteArray[i]; } } //byte[] dataArray = new byte[6]; //Array.Copy(retArray, startIndex, dataArray, 0, 6); //hexStr = byteToHexStr(dataArray); //log.Info(string.Format("收到7A 7A 21 D1 1B的回复:{0}\r\n", hexStr)); //log.Info(string.Format("收到7e 7e 03 bc 94 53的回复:{0}\r\n", hexStr)); if (startIndex == -1) { throw new BaseException("没有发现A6 A6 的回复"); } byte[] versionBtyes = new byte[3]; Array.Copy(retArray, startIndex + 3, versionBtyes, 0, 3); string strVersion = versionBtyes[2].ToString() + "." + versionBtyes[1].ToString() + "." + versionBtyes[0].ToString(); configGv.Add("VERSION", strVersion); log.Info(string.Format("版本号:{0}", strVersion)); byte[] wifiMacBtyes = new byte[12]; Array.Copy(retArray, startIndex + 6, wifiMacBtyes, 0, 12); string strImei = System.Text.Encoding.ASCII.GetString(wifiMacBtyes); configGv.Add("DevWifi_MAC", strImei); log.Info("WIFI MAC:" + strImei); byte[] softNumberBtyes = new byte[9]; Array.Copy(retArray, startIndex + 18, softNumberBtyes, 0, 9); string strSoftNumber = System.Text.Encoding.ASCII.GetString(softNumberBtyes); configGv.Add("SoftNumber", strSoftNumber); log.Info("软件编号:" + strSoftNumber); byte[] resultBtyes = new byte[4]; Array.Copy(retArray, startIndex + 27, resultBtyes, 0, 4); string strResult = System.Text.Encoding.ASCII.GetString(resultBtyes); configGv.Add("RESULT", strResult); log.Info("联网测试结果:" + strResult); byte[] btMacBtyes = new byte[12]; Array.Copy(retArray, startIndex + 31, btMacBtyes, 0, 12); string strBtMac = System.Text.Encoding.ASCII.GetString(btMacBtyes); log.Info("BT MAC:" + strBtMac); configGv.Add("DevBt_MAC", strBtMac); byte[] rssiBtyes = new byte[1]; Array.Copy(retArray, startIndex + 43, rssiBtyes, 0, 1); string strRssi = "-" + rssiBtyes[0].ToString(); log.Info("信号强度:" + strRssi); configGv.Add("RSSI", strRssi); //////byte[] iccidBtyes = new byte[20]; //////Array.Copy(retArray, startIndex + 88, iccidBtyes, 0, 20); //////string strIccid = System.Text.Encoding.ASCII.GetString(iccidBtyes); //////log.Info("ICCID:" + strIccid); //////if (strImsi == "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") //////{ ////// throw new BaseException("ICCID为空,获取失败"); //////} //////configGv.Add("ICCID", strIccid); }
public void Run(IProperties properties, GlobalDic <string, object> globalDic) { SKGAuthorizeCommandProperties config = properties as SKGAuthorizeCommandProperties; List <ComDut> comDutList = globalDic[typeof(List <ComDut>).ToString()] as List <ComDut>; //ComDut comDut = globalDic[typeof(ComDut).ToString()] as ComDut; ComDut comDut = null; foreach (var item in comDutList) { if (item.PortName == config.PortName) { comDut = item; break; } } if (comDut == null) { throw new BaseException(string.Format("ComDut No PortName:{0}", config.PortName)); } //OpenPhoneProperties configOpenPhone = globalDic[typeof(OpenPhoneProperties).ToString()] as OpenPhoneProperties; List <OpenPhoneProperties> configList = globalDic[typeof(List <OpenPhoneProperties>).ToString()] as List <OpenPhoneProperties>; OpenPhoneProperties configOpenPhone = null; foreach (var item in configList) { if (item.PortName == config.PortName) { configOpenPhone = item; break; } } if (configOpenPhone == null) { throw new BaseException(string.Format("OpenPhoneProperties No PortName:{0}", config.PortName)); } log = globalDic[typeof(ILog).ToString()] as ILog; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; comDut.Parity = configOpenPhone.Parity; string endLine = configOpenPhone.EndLine; if (!string.IsNullOrEmpty(endLine)) { endLine = Parse_r_n(endLine); } string command = ""; command += config.Head; //int型的处理-转2字节byte,连接到16进制字符串中 int allLength = 9 + 26;//config.DataLength byte[] byteTemp = new byte[2]; byte[] oneByteTemp = new byte[1]; byteTemp = intToBytes(allLength); command += byteToHexStr(byteTemp); command += config.ReservedWord; command += config.CommandWord; //DataContent部分 //枚举类的处理 int intTemp = (int)config.AuthorizeEvent; command += intTemp.ToString().PadLeft(2, '0'); //int型处理 string snPcbaId = configGv.Get(GlobalVaribles.LABEL_SN); if (config.AuthorizeEvent == SKGAuthorizeCommandProperties.EnumAuthorizeEvent.SN_授权 || config.AuthorizeEvent == SKGAuthorizeCommandProperties.EnumAuthorizeEvent.PCBA_ID授权 ) { #region //扫描的SN/PCBA码是直接16进制的连接 //int effectiveLength = snPcbaId.Length / 2; //command += effectiveLength.ToString("X2"); //command += snPcbaId.PadRight(48, '0'); #endregion #region //扫描的SN/PCBA码是字符型,ASCII码的方式 byte[] bytesSnPcba = System.Text.Encoding.ASCII.GetBytes(snPcbaId); command += bytesSnPcba.Length.ToString("X2"); command += byteToHexStr(bytesSnPcba).PadRight(48, '0'); #endregion } else if (config.AuthorizeEvent == SKGAuthorizeCommandProperties.EnumAuthorizeEvent.授权写入蓝牙广播报信息) { //V16版的协议不写蓝牙广播 byte[] bytesSnPcba = System.Text.Encoding.ASCII.GetBytes(config.BleBroadcastName); command += bytesSnPcba.Length.ToString("X2"); command += byteToHexStr(bytesSnPcba).PadRight(17, '0'); command += config.DeviceType; command += config.ReservedWord1; command += config.ReservedWord2; } else if (config.AuthorizeEvent == SKGAuthorizeCommandProperties.EnumAuthorizeEvent.解锁SWD) { byte[] seroArry = new byte[25]; command += byteToHexStr(seroArry); } #region //SN / PCBAID 需要填满 24 字节,不够 长的在后面补 0x00(例:SN 有效长度 为 16 字 节 则 第 17-24 字 节 填 充 0x00) //command += config.Sn_PCBA_ID.PadRight(48,'0'); //intTemp = (int)config.LedModeSetting; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.EmsTestSwitch; //command += intTemp.ToString().PadLeft(2, '0'); ////int型的处理 //byteTemp = intToBytes(config.EmsPWSetting); //command += byteToHexStr(byteTemp); //byteTemp = intToBytes(config.EmsFreqSetting); //command += byteToHexStr(byteTemp); //byteTemp = intToBytes(config.EmsAmplitudeSetting); //command += byteToHexStr(byteTemp); ////枚举类的处理 //intTemp = (int)config.HeatingGearControl; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VoiceControl; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.WritePcbaFinishFlag; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.WholeMachineFinishFlag; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.BtTestOnOffSetting; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.MotorControl; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.AginTestOnOffSetting; //command += intTemp.ToString().PadLeft(2, '0'); ////int型的处理-转为1字节byte //oneByteTemp = intToOneBytes(config.AginTestTime); //command += byteToHexStr(byteTemp); //#region 多路振动控制 //intTemp = (int)config.VibrationControl1; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl2; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl3; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl4; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl5; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl6; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl7; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl8; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl9; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl10; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl11; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl12; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl13; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl4; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl5; //command += intTemp.ToString().PadLeft(2, '0'); //intTemp = (int)config.VibrationControl6; //command += intTemp.ToString().PadLeft(2, '0'); //#endregion //intTemp = (int)config.RedLightControl640nm; //command += intTemp.ToString().PadLeft(2, '0'); #endregion //byteToHexStr(); //byte[] atCommand = strToToHexByte(config.AtCommand); //计算校验位 byte[] atCommandOutXor = strToToHexByte(command); byte[] atCommand = new byte[atCommandOutXor.Length + 1]; byte byteXor = ByteToXOR(atCommandOutXor); Array.Copy(atCommandOutXor, 0, atCommand, 0, atCommandOutXor.Length); //赋值最后一个校验位 atCommand[atCommandOutXor.Length] = byteXor; //string atCommand = PreTranslateAtCommand(config.AtCommand); UartDisplay(atCommand, "T"); //log.Info("AT Commond=" + config.AtCommand); comDut.DtrEnable = configOpenPhone.Dtr; comDut.RtsEnable = configOpenPhone.Rts; comDut.Write(atCommand, 0, atCommand.Length); Thread.Sleep(config.AtCommandInterval); //发命令后一直循环接收->检验->拼接,超过500ms还没检验通过则超时FAIL TimeUtils.Execute(() => { int n = comDut.BytesToRead; //if (n == 0) //{ // //继续循环执行 // return false; //} //byte[] buf = new byte[n]; //comDut.Read(buf, 0, n); //buffer.AddRange(buf); log.Info(string.Format("接收的长度:{0}", n)); if (n != 0) { byte[] buf = new byte[n]; comDut.Read(buf, 0, n); UartDisplay(buf, "R"); buffer.Clear(); buffer.AddRange(buf); } if (buffer.Count > 9) { if (buffer[0] == 0xA5) { //throw new BaseException(string.Format("Head Error")); //if (&& buf[1] == 0xC3)) //{ //} if (buffer[1] == 0xC3) { byte high = buffer[2]; byte low = buffer[3]; int iTotalLength = (high & 0xFF) << 8 | low; if (buffer.Count < iTotalLength) //数据区尚未接收完整 { log.Info(string.Format("数据尚未接收完整,已接收:{0},总长度:{1}", buffer.Count, iTotalLength)); //继续循环执行 return(false); } byte[] oneFrameBytes = new byte[iTotalLength]; buffer.CopyTo(0, oneFrameBytes, 0, iTotalLength); buffer.RemoveRange(0, iTotalLength); UartDisplay(oneFrameBytes, "R"); #region //开始校验-命令字 if (config.CommandType == SKGAuthorizeCommandProperties.EnumCommandType.查询指令) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x37)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else if (config.CommandType == SKGAuthorizeCommandProperties.EnumCommandType.控制指令) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x40)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else if (config.CommandType == SKGAuthorizeCommandProperties.EnumCommandType.发授权) { if (!(oneFrameBytes[6] == 0xA0 && oneFrameBytes[7] == 0x23)) { log.Info(string.Format("CMD Type Error")); //继续循环执行 return(false); } } else { log.Info(string.Format("非法指令类型")); //继续循环执行 return(false); } //异或校验 1字节 byte[] bufOutOxr = new byte[oneFrameBytes.Length - 1]; Array.Copy(oneFrameBytes, 0, bufOutOxr, 0, oneFrameBytes.Length - 1); byte xor = ByteToXOR(bufOutOxr); if (oneFrameBytes[iTotalLength - 1] != xor) { log.Fail(string.Format("核验RX的校验位:错误,返回校验位:{0:X2},计算校验位:{1:X2}", oneFrameBytes[iTotalLength - 1], xor)); //继续循环执行 return(false); } log.Info(string.Format("核验RX的校验位:正确,返回校验位:{0:X2},计算校验位:{1:X2}", oneFrameBytes[iTotalLength - 1], xor)); //数据内容 32个字节——长度是不固定的 byte[] dataArrys = new byte[iTotalLength - 9]; //config.DataLength Array.Copy(oneFrameBytes, 8, dataArrys, 0, iTotalLength - 9); //config.DataLength //添加到全局变量中 if (config.GlobalVariblesKey != null) { for (int i = 0; i < config.GlobalVariblesKey.Length; i++) { Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); if (!matchKey.Success) { //throw new BaseException("read info key fail"); log.Info("read info key fail"); //继续循环执行 return(false); } string key = matchKey.Groups[1].ToString(); configGv.Add(key, dataArrys); log.Info("应答报文格式检查PASS"); } } //跳出循环执行 return(true); #endregion } else { log.Info(string.Format("头部不是0xA5,0xC3,buffer.RemoveAt:{0:X2},{1:X2}", buffer[0], buffer[1])); buffer.RemoveRange(0, 2); //继续循环执行 return(false); } } else { log.Info(string.Format("头部不是0xA5,buffer.RemoveAt:{0:X2}", buffer[0])); buffer.RemoveAt(0); //继续循环执行 return(false); } } else { log.Info(string.Format("buffer.Count:{0}小于最小长度9", buffer.Count)); //继续循环执行 return(false); } }, config.Timeout); //返回的数据部分处理 byte[] dataArry = null; if (config.GlobalVariblesKey != null) { for (int i = 0; i < config.GlobalVariblesKey.Length; i++) { Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); if (!matchKey.Success) { throw new BaseException("read info key fail"); //log.Info("read info key fail"); ////继续循环执行 //return false; } string key = matchKey.Groups[1].ToString(); dataArry = (byte[])configGv.GetObject(key); } } if (dataArry == null) { throw new BaseException("未获取到回复的数据部分"); } string revAuthorizeContent = byteToHexStr(dataArry).Substring(4, 48); string sendAuthorizeContent = command.Substring(20, 48); if (revAuthorizeContent != sendAuthorizeContent) { throw new BaseException(string.Format("授权失败\r\n回复的授权内容:{0}\r\n发送的授权内容:{1}\r\n不一致", revAuthorizeContent, sendAuthorizeContent)); //log.Fail(string.Format("授权失败\r\n回复的授权内容:{0}\r\n发送的授权内容:{1}\r\n不一致", revAuthorizeContent, sendAuthorizeContent)); ////继续循环执行 //return false; } log.Info(string.Format("回复的授权内容:{0}\r\n发送的授权内容:{1}\r\n一致", revAuthorizeContent, sendAuthorizeContent)); //MCU ID byte[] dataMcuId = new byte[12]; Array.Copy(dataArry, 26, dataMcuId, 0, 12); string strDataMcuId = byteToHexStr(dataMcuId); log.Info(string.Format("MCU ID:{0}", strDataMcuId)); if (strDataMcuId.Contains("00000000000000000000")) { throw new BaseException(string.Format("授权失败,数据为00的MCUID")); //log.Fail(string.Format("授权失败,数据为00的MCUID")); ////继续循环执行 //return false; } //BLE MAC byte[] dataBleMac = new byte[6]; Array.Copy(dataArry, 38, dataBleMac, 0, 6); //ASCII码的方式 //string strDataMcuId = System.Text.Encoding.ASCII.GetString(dataMcuId); //BLE MAC:1F39A109,低位在前0x09,0xA1的直接16进制拼接方式 string strDataBleMac = byteToHexStrReverse(dataBleMac); log.Info(string.Format("BLE MAC:{0}", strDataBleMac)); log.Info(string.Format("授权成功")); #region //DateTime start = DateTime.Now; //DateTime now = DateTime.Now; //TimeSpan timeSpan = now - start; //while (timeSpan.TotalMilliseconds <= config.Timeout) //{ // try // { // comDut.Write(atCommand, 0, atCommand.Length); // Thread.Sleep(config.AtCommandInterval); // int n = comDut.BytesToRead; // byte[] buf = new byte[n]; // comDut.Read(buf, 0, n); // //解析是否收到正确的包,包头,命令,校验位 // //如果收到的包正确 // if (true) // { // break; // } // } // catch (Exception e) // { // Console.WriteLine(e); // } // Thread.Sleep(100); // now = DateTime.Now; // timeSpan = now - start; //} //if (timeSpan.TotalMilliseconds > config.Timeout) //{ // throw new BaseException("超时:"); //} #endregion // #region 原发送命令后等待一固定时间,再接收数据,对应不上则重试 //int n = comDut.BytesToRead; //byte[] buf = new byte[n]; //comDut.Read(buf, 0, n); ////解析是否收到正确的包,包头,命令,校验位 ////起始标志 数据长度 保留字 命令字 数据内容 异或校验码 ////0XA5C3 9+N 0x0000 0x0037 Data 异或 ////2 字节 2 字节 2 字节 2 字节 N 字节 1 字节 //UartDisplay(buf, "R"); //if (buf.Length < 9) //{ // throw new BaseException(string.Format("Response Length Not enough")); //} ////包头 //if (!(buf[0] == 0xA5 && buf[1] == 0xC3)) //{ // throw new BaseException(string.Format("Response Head Error")); //} ////长度 2字节 //byte high = buf[2]; //byte low = buf[3]; //int iTotalLength = (high & 0xFF) << 8 | low; //if (iTotalLength != n) //{ // throw new BaseException(string.Format("Response Length Error,长度位:{0},实际长度:{1}", iTotalLength, n)); //} ////命令字 //if (config.CommandType == SKGAuthorizeCommandProperties.EnumCommandType.查询指令) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x37)) // { // throw new BaseException(string.Format("Response Command Type Error")); // } //} //else if (config.CommandType == SKGAuthorizeCommandProperties.EnumCommandType.控制指令) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x40)) // { // throw new BaseException(string.Format("Response Command Type Error")); // } //} //else if (config.CommandType == SKGAuthorizeCommandProperties.EnumCommandType.下发授权) //{ // if (!(buf[6] == 0xA0 && buf[7] == 0x23)) // { // throw new BaseException(string.Format("Response Command Type Error")); // } //} //else //{ // throw new BaseException(string.Format("Response为非法指令类型")); //} ////异或校验 1字节 //byte[] bufOutOxr = new byte[buf.Length-1]; //Array.Copy(buf, 0, bufOutOxr, 0, buf.Length - 1); //byte xor = ByteToXOR(bufOutOxr); //if (buf[n - 1] != xor) //{ // throw new BaseException(string.Format("核验RX的校验位:错误,返回校验位:{0:X2},计算校验位:{1:X2}", buf[n - 1], xor)); //} //log.Info(string.Format("核验RX的校验位:正确,返回校验位:{0:X2},计算校验位:{1:X2}", buf[n - 1], xor)); ////数据内容 32个字节 ////byte[] dataArry = new byte[config.DataLength]; ////Array.Copy(buf, 8, dataArry, 0, config.DataLength); //byte[] dataArry = new byte[iTotalLength - 9];//config.DataLength //Array.Copy(buf, 8, dataArry, 0, iTotalLength - 9);//config.DataLength ////添加到全局变量中 //if (config.GlobalVariblesKey != null) //{ // for (int i = 0; i < config.GlobalVariblesKey.Length; i++) // { // //string pattern = "-([0-9]{2}) "; // //Match matchValue = Regex.Match(response, pattern); // //if (!matchValue.Success) // //{ // // throw new BaseException("read info value fail"); // //} // //string value = matchValue.Groups[1].ToString(); // Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); // if (!matchKey.Success) // { // throw new BaseException("read info key fail"); // } // string key = matchKey.Groups[1].ToString(); // configGv.Add(key, dataArry); // log.Info("应答报文格式检查PASS"); // } //} #endregion //检查是否授权正确: //授权事件确认检查 //string revAuthorizeContent = byteToHexStr(dataArry).Substring(4, 48); //string sendAuthorizeContent = command.Substring(20, 48); //if (revAuthorizeContent!= sendAuthorizeContent) //{ // throw new BaseException(string.Format("授权失败\r\n回复的授权内容:{0}\r\n发送的授权内容:{1}\r\n不一致", revAuthorizeContent, sendAuthorizeContent)); //} //log.Info(string.Format("回复的授权内容:{0}\r\n发送的授权内容:{1}\r\n一致", revAuthorizeContent, sendAuthorizeContent)); ////MCU ID //byte[] dataMcuId = new byte[12]; //Array.Copy(dataArry, 26, dataMcuId, 0, 12); ////ASCII码的方式 ////string strDataMcuId = System.Text.Encoding.ASCII.GetString(dataMcuId); ////MCU ID:1F39A109高位在前1F,39的直接16进制拼接方式 //string strDataMcuId = byteToHexStr(dataMcuId); //log.Info(string.Format("MCU ID:{0}", strDataMcuId)); //if (strDataMcuId.Contains("00000000000000000000")) //{ // throw new BaseException(string.Format("授权失败,数据为00的MCUID")); //} ////BLE MAC //byte[] dataBleMac = new byte[6]; //Array.Copy(dataArry, 38, dataBleMac, 0, 6); ////ASCII码的方式 ////string strDataMcuId = System.Text.Encoding.ASCII.GetString(dataMcuId); ////BLE MAC:1F39A109,低位在前0x09,0xA1的直接16进制拼接方式 //string strDataBleMac = byteToHexStrReverse(dataBleMac); //log.Info(string.Format("BLE MAC:{0}", strDataBleMac)); //log.Info(string.Format("授权成功")); ////如果收到的包正确 //string str = byteToHexStr(buf); //string response = comDut.ReadExisting(); //comDut.DtrEnable = false; //comDut.RtsEnable = false; //log.Info("AT Response=" + response); //if (!string.IsNullOrEmpty(config.AtCommandError)) //{ // if (response.Contains(config.AtCommandError)) // { // throw new BaseException(string.Format("AT response=[{0}] contain error=[{1}]", response, config.AtCommandError)); // } //} //if (!string.IsNullOrEmpty(config.AtCommandOk)) //{ // if (!response.Contains(config.AtCommandOk)) // { // throw new BaseException(string.Format("AT response=[{0}] not contain error=[{1}]", response, config.AtCommandOk)); // } //} //if (config.CheckInfo != null) //{ // for (int i = 0; i < config.CheckInfo.Length; i++) // { // if (!string.IsNullOrEmpty(config.CheckInfo[i])) // { // string checkInfo = PreTranslateCheckInfo(config.CheckInfo[i]); // if (!response.Contains(checkInfo)) // { // throw new BaseException(string.Format("AT response=[{0}] not contain check info=[{1}]", response, checkInfo)); // } // } // } //} //if (config.GlobalVariblesKey != null && config.GlobalVariblesKeyPattern != null) //{ // if (config.GlobalVariblesKey.Length != config.GlobalVariblesKeyPattern.Length) // { // throw new BaseException("请保持GlobalVariblesKey与GlobalVariblesKeyPattern成对存在"); // } // for (int i = 0; i < config.GlobalVariblesKey.Length; i++) // { // string pattern = "-([0-9]{2}) "; // Match matchValue = Regex.Match(response, pattern); // if (!matchValue.Success) // { // throw new BaseException("read info value fail"); // } // string value = matchValue.Groups[1].ToString(); // Match matchKey = Regex.Match(config.GlobalVariblesKey[i], this.pattern); // if (!matchKey.Success) // { // throw new BaseException("read info key fail"); // } // string key = matchKey.Groups[1].ToString(); // configGv.Add(key, value); // } //} }