//private List<WIFISSID> ssids=new List<WIFISSID>(); //private wifiSo wifiso; public void Run(IProperties properties, GlobalDic <string, object> globalDic)//virtual { config = properties as DisConnectSsiProperties; ILog log = globalDic[typeof(ILog).ToString()] as ILog; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; try { Wifi objWifi = configGv.GetObject("ObjWifi") as Wifi; if (objWifi != null) { if (objWifi.ConnectionStatus == WifiStatus.Connected) { objWifi.Disconnect(); log.Info("断开WIFI连接成功"); } else { throw new BaseException(string.Format("未连接WIFI热点,无法断开连接1")); } } else { throw new BaseException(string.Format("未连接WIFI热点,objWifi为空,无法断开连接")); } //if (!hasSsid) //{ // throw new BaseException(string.Format("未扫描到SSID:{0}", ssid)); //} } catch (Exception ex) { throw new Exception("断开热点出错," + ex.Message); } }
//private List<WIFISSID> ssids=new List<WIFISSID>(); //private wifiSo wifiso; public void Run(IProperties properties, GlobalDic <string, object> globalDic)//virtual { config = properties as SocketDisConnectProperties; ILog log = globalDic[typeof(ILog).ToString()] as ILog; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; try { Socket objSocket = configGv.GetObject("ObjSocket") as Socket; if (objSocket != null) { if (objSocket.Connected) { //objSocket.Disconnect(true); objSocket.Close(); if (objSocket.Connected) { throw new BaseException(string.Format("断开连接失败,状态仍然为连接状态")); } log.Info("断开Soket连接成功"); } else { throw new BaseException(string.Format("未连接Soket服务器,无法断开连接1")); } } else { throw new BaseException(string.Format("未连接Soket服务器,objSocket为空,无法断开连接")); } } catch (Exception ex) { throw new Exception("断开Soket连接出错," + ex.Message); } }
private List <byte> buffer = new List <byte>(4096); //uart 接受数据buffer 未做任何处理 //public SKGExecuter() //{ // this.pattern = GlobalVaribles.PATTERN; //} public void Run(IProperties properties, GlobalDic <string, object> globalDic) { SkgAuthorizeCheckProperties config = properties as SkgAuthorizeCheckProperties; log = globalDic[typeof(ILog).ToString()] as ILog; //解析是否收到正确的包,包头,命令,校验位 //起始标志 数据长度 保留字 命令字 数据内容 异或校验码 //0XA5C3 9+N 0x0000 0x0037 Data 异或 //2 字节 2 字节 2 字节 2 字节 N 字节 1 字节 configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; byte[] dataArry = (byte[])configGv.GetObject(config.GlobalVariblesKey); //判断授权是否成功 //MCUID是否为00 #region //string fireWareVersion = config.FirewareVersion; //string softwareVersion = config.SoftwareVersion; //string firmwareName = config.FirmwareName; //#region ////是否完成过PCBA 0x01:已完成过 其他值:未完成过 //if (true) //{ // if (dataArry[0] == 0x01) // { // log.Info("完成过PCBA测试"); // } // else // { // log.Info("未完成过PCBA测试"); // } //} ////查询是否完成过整机测试 0x01:已完成过 其他值:未完成过 //if (true)//check Whole machine test //{ // if (dataArry[1] == 0x01) // { // log.Info("完成过整机测试"); // } // else // { // log.Info("未完成过整机测试"); // } //} ////查询是否完成过老化测试 0x01:已完成过 其他值:未完成过 //if (true)//checkAgeing //{ // if (dataArry[2] == 0x01) // { // log.Info("完成过老化测试"); // } // else // { // log.Info("未完成过老化测试"); // } //} ////硬件版本号(高位) 0x00 硬件版本号(高位) //if (true) //{ // byte[] dataFirmwareVersion = new byte[2]; // Array.Copy(dataArry, 3, dataFirmwareVersion, 0, 2); // string strFirmwareVersion = System.Text.Encoding.ASCII.GetString(dataFirmwareVersion); // //string strFirmwareVersion = byteToHexStr(dataFirmwareVersion); // if (strFirmwareVersion != fireWareVersion) // { // throw new BaseException(string.Format("硬件版本号:{0},与设置版本号:{1}不一致", strFirmwareVersion, fireWareVersion)); // } // log.Info(string.Format("硬件版本号:{0},与设置版本号:{1}一致", strFirmwareVersion, fireWareVersion)); //} //软件版本号(高位) 0x00 软件版本号 6 //软件版本号 0x01 软件版本号 7 //软件版本号 0x02 软件版本号 8 //软件版本号(低位) 0x03 软件版本号(低位) //if (true) //{ // byte[] dataSoftwareVersion = new byte[4]; // Array.Copy(dataArry, 5, dataSoftwareVersion, 0, 4); // string strSoftwareVersion = System.Text.Encoding.ASCII.GetString(dataSoftwareVersion); // //string strSoftwareVersion = byteToHexStr(dataSoftwareVersion); // if (strSoftwareVersion != softwareVersion) // { // throw new BaseException(string.Format("软件版本号:{0},与设置版本号:{1}不一致", strSoftwareVersion, softwareVersion)); // } // log.Info(string.Format("软件版本号:{0},与设置版本号:{1}一致", strSoftwareVersion, softwareVersion)); //} #endregion //授权事件确认检查 //dataArry[0] //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, 12); //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 ////电池电压 ////长度 2字节 //if (true) //{ // byte high = dataArry[27]; // byte low = dataArry[28]; // int vol = (high & 0xFF) << 8 | low; // CheckRange("电压值(mV):", vol, config.VolMaxValue, config.VolMinValue); //} ////NTC温度 ////长度 2字节 //if (true) //{ // byte high = dataArry[29]; // if (high == 0x80) // { // throw new BaseException(string.Format("NTC1 开路或短路,NTC温度输出 0x8000")); // } // byte low = dataArry[30]; // int ntc = (high & 0xFF) << 8 | low; // CheckRange("NTC1温度值(单位0.1摄氏度):", ntc, config.Ntc1MaxValue, config.Ntc1MinValue); //} ////PCBA-ID //if (true) //{ // if (dataArry[31] > 0x18) // { // throw new BaseException(string.Format("PCBID 有效长度:{0}不在0~24的合理范围内,未授权过", dataArry[31])); // } // byte[] dataPcbaId = new byte[24]; // Array.Copy(dataArry, 32, dataPcbaId, 0, 24); // //ASCII码的方式 // //string strDataMcuId = System.Text.Encoding.ASCII.GetString(dataMcuId); // //MCU ID:1F39A109高位在前1F,39的直接16进制拼接方式 // string strDataPcbaId = byteToHexStr(dataPcbaId); // log.Info(string.Format("PCBA-ID:{0}", strDataPcbaId)); //} ////SN //if (true) //{ // if (dataArry[31] > 0x18) // { // throw new BaseException(string.Format("SN 有效长度:{0}不在0~24的合理范围内,未授权过", dataArry[31])); // } // byte[] dataSn = new byte[24]; // Array.Copy(dataArry, 32, dataSn, 0, 24); // //ASCII码的方式 // //string strDataMcuId = System.Text.Encoding.ASCII.GetString(dataMcuId); // //MCU ID:1F39A109高位在前1F,39的直接16进制拼接方式 // //不足的,需要判断补0? // string strDataSn = byteToHexStr(dataSn); // log.Info(string.Format("SN:{0}", strDataSn)); //} ////固件名称 //if (true) //{ // byte[] dataFirmwareName = new byte[8]; // Array.Copy(dataArry, 81, dataFirmwareName, 0, 8); // string strDataFirmwareName = System.Text.Encoding.ASCII.GetString(dataFirmwareName); // //string strSoftwareVersion = byteToHexStr(dataSoftwareVersion); // if (strDataFirmwareName != firmwareName) // { // throw new BaseException(string.Format("固件名称:{0},与设置固件名称:{1}不一致", strDataFirmwareName, firmwareName)); // } // log.Info(string.Format("固件名称:{0},与设置固件名称:{1}一致", strDataFirmwareName, firmwareName)); //} ////电机转速 //if (true) //{ // byte high = dataArry[89]; // byte low = dataArry[90]; // int motorSpeed = (high & 0xFF) << 8 | low; // CheckRange("电机转速:", motorSpeed, config.MotorSpeedMaxValue, config.MotorSpeedMinValue); //} ////NTC2温度 //if (true) //{ // byte high = dataArry[91]; // if (high == 0x80) // { // throw new BaseException(string.Format("NTC2 开路或短路,NTC温度输出 0x8000")); // } // byte low = dataArry[92]; // int ntc = (high & 0xFF) << 8 | low; // CheckRange("NTC2温度值(单位0.1摄氏度):", ntc, config.Ntc2MaxValue, config.Ntc2MinValue); //} ////NTC3温度 //if (true) //{ // byte high = dataArry[93]; // if (high == 0x80) // { // throw new BaseException(string.Format("NTC3 开路或短路,NTC温度输出 0x8000")); // } // byte low = dataArry[94]; // int ntc = (high & 0xFF) << 8 | low; // CheckRange("NTC3温度值(单位0.1摄氏度):", ntc, config.Ntc3MaxValue, config.Ntc3MinValue); //} #endregion }
public void Run(IProperties properties, GlobalDic <string, object> globalDic) { SocketCommandProperties config = properties as SocketCommandProperties; ILog log = globalDic[typeof(ILog).ToString()] as ILog; configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; Socket objSocket = configGv.GetObject("ObjSocket") as Socket; if (objSocket == null) { throw new BaseException(string.Format("objSocket is null")); } if (config.CommandType == SocketCommandProperties.EnumCommandType.String) { string atCommand = PreTranslateAtCommand(config.SocketCommand); log.Info("SocketCommand:\r\n" + atCommand); byte[] bytesCommand = Encoding.ASCII.GetBytes(atCommand); objSocket.Send(bytesCommand, bytesCommand.Length, SocketFlags.None); Thread.Sleep(config.SocketCommandInterval); byte[] data = new byte[objSocket.ReceiveBufferSize]; int len = objSocket.Receive(data); string response = Encoding.ASCII.GetString(data, 0, len); log.Info("Socket Response:\r\n" + response); if (!string.IsNullOrEmpty(config.SocketCommandError)) { if (response.Contains(config.SocketCommandError)) { throw new BaseException(string.Format("contain error:[{0}]", config.SocketCommandError)); } } if (!string.IsNullOrEmpty(config.SocketCommandOk)) { if (!response.Contains(config.SocketCommandOk)) { throw new BaseException(string.Format("not contain ok:[{0}]", config.SocketCommandOk)); } log.Info(string.Format("contain ok:[{0}]", config.SocketCommandOk)); } 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("not contain check info:[{0}]", checkInfo)); } log.Info(string.Format("contain check info:[{0}]", 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 = Parse_r_n(config.GlobalVariblesKeyPattern[i]); 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 List <byte> buffer = new List <byte>(4096); //uart 接受数据buffer 未做任何处理 //public SKGExecuter() //{ // this.pattern = GlobalVaribles.PATTERN; //} public void Run(IProperties properties, GlobalDic <string, object> globalDic) { log = globalDic[typeof(ILog).ToString()] as ILog; SkgQueryCheckProperties config = properties as SkgQueryCheckProperties; //解析是否收到正确的包,包头,命令,校验位 //起始标志 数据长度 保留字 命令字 数据内容 异或校验码 //0XA5C3 9+N 0x0000 0x0037 Data 异或 //2 字节 2 字节 2 字节 2 字节 N 字节 1 字节 configGv = globalDic[typeof(GlobalVaribles).ToString()] as GlobalVaribles; byte[] dataArry = (byte[])configGv.GetObject(config.GlobalVariblesKey); //string fireWareVersion = config.FirewareVersion; //string softwareVersion = config.SoftwareVersion; //string firmwareName = config.FirmwareName; #region //是否完成过PCBA 0x01:已完成过 其他值:未完成过 if (config.IfCheckFinishTest == SkgQueryCheckProperties.EnumIfCheckFinishTest.检查是否完成过PCBA测试 || config.IfCheckFinishTest == SkgQueryCheckProperties.EnumIfCheckFinishTest.全检 ) { if (dataArry[0] == 0x00) { log.Info("完成过PCBA测试"); } else { //if (config.IfCheckFinishTest== SkgQueryCheckProperties.EnumIfCheckFinishTest.检查是否完成过PCBA测试) //{ throw new BaseException("未完成过PCBA测试"); //} //log.Info("未完成过PCBA测试"); } } //查询是否完成过整机测试 0x01:已完成过 其他值:未完成过 if (config.IfCheckFinishTest == SkgQueryCheckProperties.EnumIfCheckFinishTest.检查是否完成过整机测试 || config.IfCheckFinishTest == SkgQueryCheckProperties.EnumIfCheckFinishTest.全检 ) { if (dataArry[1] == 0x00) { log.Info("完成过整机测试"); } else { //if (config.IfCheckFinishTest == SkgQueryCheckProperties.EnumIfCheckFinishTest.检查是否完成过整机测试) //{ throw new BaseException("未完成过整机测试"); //} //log.Info("未完成过整机测试"); } } //查询是否完成过老化测试 0x01:已完成过 其他值:未完成过 if (config.IfCheckFinishTest == SkgQueryCheckProperties.EnumIfCheckFinishTest.检查是否完成过老化测试 || config.IfCheckFinishTest == SkgQueryCheckProperties.EnumIfCheckFinishTest.全检 ) { if (dataArry[2] == 0x00) { log.Info("完成过老化测试"); } else { //if (config.IfCheckFinishTest == SkgQueryCheckProperties.EnumIfCheckFinishTest.检查是否完成过老化测试) //{ throw new BaseException("未完成过老化测试"); //} //log.Info("未完成过老化测试"); } } //硬件版本号(高位) 0x00 硬件版本号(高位) if (!string.IsNullOrEmpty(config.FirewareVersion)) { byte[] dataFirmwareVersion = new byte[2]; Array.Copy(dataArry, 3, dataFirmwareVersion, 0, 2); string strFirmwareVersion = byteToHexStr(dataFirmwareVersion); //strFirmwareVersion = System.Text.Encoding.Unicode.GetString(dataFirmwareVersion);//ASCII //string strFirmwareVersion = byteToHexStr(dataFirmwareVersion); if (strFirmwareVersion != config.FirewareVersion) { throw new BaseException(string.Format("硬件版本号:{0},与设置版本号:{1}不一致", strFirmwareVersion, config.FirewareVersion)); } log.Info(string.Format("硬件版本号:{0},与设置版本号:{1}一致", strFirmwareVersion, config.FirewareVersion)); } //软件版本号(高位) 0x00 软件版本号 6 //软件版本号 0x01 软件版本号 7 //软件版本号 0x02 软件版本号 8 //软件版本号(低位) 0x03 软件版本号(低位) if (!string.IsNullOrEmpty(config.SoftwareVersion)) { byte[] dataSoftwareVersion = new byte[4]; Array.Copy(dataArry, 5, dataSoftwareVersion, 0, 4); string strSoftwareVersion = byteToHexStr(dataSoftwareVersion); //strSoftwareVersion = System.Text.Encoding.ASCII.GetString(dataSoftwareVersion); //string strSoftwareVersion = byteToHexStr(dataSoftwareVersion); if (strSoftwareVersion != config.SoftwareVersion) { throw new BaseException(string.Format("软件版本号:{0},与设置版本号:{1}不一致", strSoftwareVersion, config.SoftwareVersion)); } log.Info(string.Format("软件版本号:{0},与设置版本号:{1}一致", strSoftwareVersion, config.SoftwareVersion)); } //MCU ID byte[] dataMcuId = new byte[12]; Array.Copy(dataArry, 9, 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)); //BLE MAC byte[] dataBleMac = new byte[6]; Array.Copy(dataArry, 21, 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)); configGv.Add(GlobalVaribles.MAC, strDataBleMac); //电池电压 //长度 2字节 if (!(config.VolMaxValue == 0 && config.VolMinValue == 0)) { byte high = dataArry[27]; byte low = dataArry[28]; int vol = (high & 0xFF) << 8 | low; CheckRange("电压值(mV):", vol, config.VolMaxValue, config.VolMinValue); } //NTC温度 //长度 2字节 if (!(config.Ntc1MaxValue == 0 && config.Ntc1MinValue == 0)) { byte high = dataArry[29]; if (high == 0x80) { throw new BaseException(string.Format("NTC1 开路或短路,NTC温度输出 0x8000")); } byte low = dataArry[30]; int ntc = (high & 0xFF) << 8 | low; CheckRange("NTC1温度值(单位0.1摄氏度):", ntc, config.Ntc1MaxValue, config.Ntc1MinValue); } //PCBA-ID if (config.CheckNumberInFlash == SkgQueryCheckProperties.EnumCheckNumberInFlash.检查是否写入过PCBA_ID) { if (dataArry[30] > 0x18) { throw new BaseException(string.Format("PCBID 有效长度:{0}不在0~24的合理范围内,未授权过", dataArry[31])); } byte[] dataPcbaId = new byte[24]; Array.Copy(dataArry, 31, dataPcbaId, 0, 24); //ASCII码的方式 //string strDataMcuId = System.Text.Encoding.ASCII.GetString(dataMcuId); //MCU ID:1F39A109高位在前1F,39的直接16进制拼接方式 string strDataPcbaId = Encoding.ASCII.GetString(dataPcbaId);//byteToHexStr(dataPcbaId); strDataPcbaId = strDataPcbaId.Replace("\0", ""); log.Info(string.Format("读取的PCBA-ID:{0}", strDataPcbaId)); //int型处理 string preSnPcbaId = configGv.Get(GlobalVaribles.LABEL_SN); if (preSnPcbaId != strDataPcbaId) { throw new BaseException(string.Format("预写PCBIDA-ID :{0}与写入的PCBA-ID :{1},不一致", preSnPcbaId, strDataPcbaId)); } log.Info(string.Format("预写PCBIDA-ID :{0}与写入的PCBA-ID :{1},一致", preSnPcbaId, strDataPcbaId)); //configGv.Add("ReadPcbaId", strDataPcbaId); } else if (config.CheckNumberInFlash == SkgQueryCheckProperties.EnumCheckNumberInFlash.检查是否写入过SN) { if (dataArry[56] > 0x18)//31 { throw new BaseException(string.Format("SN 有效长度:{0}不在0~24的合理范围内,未授权过", dataArry[31])); } byte[] dataSn = new byte[24]; Array.Copy(dataArry, 57, dataSn, 0, 24);//32 //ASCII码的方式 //string strDataMcuId = System.Text.Encoding.ASCII.GetString(dataMcuId); //MCU ID:1F39A109高位在前1F,39的直接16进制拼接方式 //不足的,需要判断补0? //string strDataSn = byteToHexStr(dataSn); string strDataSn = Encoding.ASCII.GetString(dataSn);//byteToHexStr(dataPcbaId); strDataSn = strDataSn.Replace("\0", ""); log.Info(string.Format("读取的SN:{0}", strDataSn)); //int型处理 string preSn = configGv.Get(GlobalVaribles.LABEL_SN); if (preSn != strDataSn) { throw new BaseException(string.Format("预写SN :{0}与写入的SN :{1},不一致", preSn, strDataSn)); } //configGv.Add("ReadSn", strDataSn); } else if (config.CheckNumberInFlash == SkgQueryCheckProperties.EnumCheckNumberInFlash.检查是否写入过蓝牙广播信息) { //预留-查询返回信息中没有蓝牙广播信息 } else if (config.CheckNumberInFlash == SkgQueryCheckProperties.EnumCheckNumberInFlash.检查) { //跳过 } //固件名称 if (!string.IsNullOrEmpty(config.FirmwareName)) { //byte[] dataFirmwareName = new byte[8]; //Array.Copy(dataArry, 81, dataFirmwareName, 0, 8); byte[] dataFirmwareName = new byte[5]; Array.Copy(dataArry, 80, dataFirmwareName, 0, 5); string strDataFirmwareName = System.Text.Encoding.ASCII.GetString(dataFirmwareName); strDataFirmwareName = strDataFirmwareName.Replace("\0", " "); //string strSoftwareVersion = byteToHexStr(dataSoftwareVersion); //log.Info("固件名称:" + strDataFirmwareName); if (strDataFirmwareName != config.FirmwareName) { throw new BaseException(string.Format("固件名称:{0},与设置固件名称:{1}不一致", strDataFirmwareName, config.FirmwareName)); } log.Info(string.Format("固件名称:{0},与设置固件名称:{1}一致", strDataFirmwareName, config.FirmwareName)); } //电机转速 if (!(config.MotorSpeedMaxValue == 0 && config.MotorSpeedMinValue == 0)) { byte high = dataArry[89]; byte low = dataArry[90]; int motorSpeed = (high & 0xFF) << 8 | low; CheckRange("电机转速:", motorSpeed, config.MotorSpeedMaxValue, config.MotorSpeedMinValue); } //NTC2温度 if (!(config.Ntc2MaxValue == 0 && config.Ntc2MinValue == 0)) { byte high = dataArry[91]; if (high == 0x80) { throw new BaseException(string.Format("NTC2 开路或短路,NTC温度输出 0x8000")); } byte low = dataArry[92]; int ntc = (high & 0xFF) << 8 | low; CheckRange("NTC2温度值(单位0.1摄氏度):", ntc, config.Ntc2MaxValue, config.Ntc2MinValue); } //NTC3温度 if (!(config.Ntc3MaxValue == 0 && config.Ntc3MinValue == 0)) { byte high = dataArry[93]; if (high == 0x80) { throw new BaseException(string.Format("NTC3 开路或短路,NTC温度输出 0x8000")); } byte low = dataArry[94]; int ntc = (high & 0xFF) << 8 | low; CheckRange("NTC3温度值(单位0.1摄氏度):", ntc, config.Ntc3MaxValue, config.Ntc3MinValue); } #endregion }
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) { 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); // } //} }