/// <summary> /// 静态构造函数 /// </summary> static ReadWriteIdHandle() { atSnRead = new CmdRead(CmdRead.ReadIdType.SnRead, "AT+CBSN\r\n", "+CBSN:"); atIccidRead = new CmdRead(CmdRead.ReadIdType.IccidRead, "AT+CCID\r\n", "+CCID:"); atImeiRead = new CmdRead(CmdRead.ReadIdType.ImeiRead, "AT+CGSN\r\n", "+CGSN:"); atEidRead = new CmdRead(CmdRead.ReadIdType.EidRead, "AT+CEID\r\n", "+CEID:"); atVersonRead = new CmdRead(CmdRead.ReadIdType.VersonRead, "AT+CGMR\r\n", VersonStart); atImeiWrite = new CmdWrite(CmdWrite.WriteIdType.ImeiWrite, "AT+EGMR=1,7,\"", "\"\r\n"); atSnWrite = new CmdWrite(CmdWrite.WriteIdType.SnWrite, "AT+EGMR=1,5,\"", "\"\r\n"); }
/// <summary> /// 读取模块ID /// </summary> /// <param name="CmdRead"></param> /// <returns></returns> public string ReadId(CmdRead.ReadIdType readIdType) { string id = null; CmdRead CmdRead = null; switch (readIdType) { case CmdRead.ReadIdType.SnRead: CmdRead = atSnRead; break; case CmdRead.ReadIdType.ImeiRead: CmdRead = atImeiRead; break; case CmdRead.ReadIdType.EidRead: CmdRead = atEidRead; break; case CmdRead.ReadIdType.IccidRead: CmdRead = atIccidRead; break; case CmdRead.ReadIdType.VersonRead: CmdRead = atVersonRead; break; default: throw new NullReferenceException("CmdRead.ReadIdType引用异常"); } //发命令获取数据 string readCmd = CmdRead.Cmd; //读取指令 if (ATCmdCommunication(readCmd) != 0) { return(null); } //借助关键字符串提取出相应的ID string idKeySubstr = CmdRead.IdKeySubstr.ToUpper(); //提取的关键字符串 if (readIdType == CmdRead.ReadIdType.VersonRead) { #region AT+CGMR\r\n后面的 //int positon; //if ((positon = recive.ToUpper().LastIndexOf(idKeySubstr)) >= 0) //{ // id = recive.Substring(positon + idKeySubstr.Length); // //id = recive.Substring(positon); // string[] sp = { "\r\n" }; // id = id.Split(sp, StringSplitOptions.RemoveEmptyEntries)[0]; //} #endregion #region 以CMIOT为关键字 string[] spit = { "\r\n" }; string[] line = recive.Split(spit, StringSplitOptions.RemoveEmptyEntries); int positon; for (int i = 0; i < line.Length; i++) { if ((positon = line[i].IndexOf(idKeySubstr)) >= 0) { id = line[i].Substring(positon); //id = System.Text.RegularExpressions.Regex.Replace(temp, @"[^0-9A-Z]", ""); } } #endregion } else { string[] spit = { "\r\n" }; string[] line = recive.Split(spit, StringSplitOptions.RemoveEmptyEntries); int pos; for (int i = 0; i < line.Length; i++) { if ((pos = line[i].IndexOf(idKeySubstr)) >= 0) { string temp = line[i].Substring(pos + idKeySubstr.Length); id = System.Text.RegularExpressions.Regex.Replace(temp, @"[^0-9A-Z]", ""); } } } return(id); }