/// <summary> /// 发送AT指令 逐条发送AT指令 调用一次发送一条指令 /// 能返回一个OK或ERROR算一条指令 /// </summary> /// <param name="ATCom">AT指令</param> /// <returns>发送指令后返回的字符串</returns> public string SendAT(string ATCom) { string result = string.Empty; //忽略接收缓冲区内容,准备发送 _com.DiscardInBuffer(); //注销事件关联,为发送做准备 _com.DataReceived -= sp_DataReceived; //发送AT指令 try { _com.Write(ATCom + "\r"); } catch (Exception ex) { //打印日志 string errTxt = string.Format(" SendAT:{0}\r\n{1}\r\n{2}", "发送AT指令", ex.Message, ex.StackTrace); LogHelpers.Error(errTxt); _com.DataReceived += sp_DataReceived; throw ex; } //接收数据 循环读取数据 直至收到“OK”或“ERROR” string temp = string.Empty; DateTime startTime = DateTime.Now; try { while (!(temp.Contains("OK") || temp.Contains("ERROR"))) //&& (DateTime.Now.CompareTo(StartTime) < 60000) { temp = _com.ReadExisting(); result += temp; Thread.Sleep(100); if ((DateTime.Now - startTime).Seconds > 15)//运行时间大于20s,结束循环 { //打印日志 LogHelpers.Error(string.Format(" 获取短信返回状态码超时,result:{0}", result)); throw new Exception(" Read:" + temp); //break; } } return(result); } catch (Exception ex) { //打印日志 string errTxt = string.Format(" Read:{0}\r\n{1}\r\n{2}", temp, ex.Message, ex.StackTrace); LogHelpers.Error(errTxt); throw new Exception(ex.ToString() + " Read:" + temp); } finally { //事件重新绑定 正常监视串口数据 _com.DataReceived += sp_DataReceived; } }
/// <summary> /// 发送AT指令 逐条发送AT指令 调用一次发送一条指令 /// 能返回一个OK或ERROR算一条指令 /// </summary> /// <param name="ATCom">AT指令</param> /// <returns>发送指令后返回的字符串</returns> public string SendAT(string ATCom) { string result = string.Empty; Console.WriteLine("忽略接收缓冲区内容,准备发送"); //忽略接收缓冲区内容,准备发送 _com.DiscardInBuffer(); Console.WriteLine("注销事件关联,为发送做准备"); //注销事件关联,为发送做准备 lock (lockObject) { _com.DataReceived -= sp_DataReceived; } //发送AT指令 try { Console.WriteLine("发送AT指令"); _com.Write(ATCom + "\r"); } catch (Exception ex) { Console.WriteLine("发送AT指令失败:" + ex.Message); lock (lockObject) { _com.DataReceived += sp_DataReceived; } throw ex; } Console.WriteLine("接收数据 循环读取数据 直至收到“OK”或“ERROR”"); //接收数据 循环读取数据 直至收到“OK”或“ERROR” try { string temp = string.Empty; while (temp.Trim() != "OK" && temp.Trim() != "ERROR") { temp = _com.ReadLine(); Console.WriteLine("调试:\"" + ATCom + "\"---返回:" + temp); result += temp; } return(result); } catch (Exception ex) { throw ex; } finally { //事件重新绑定 正常监视串口数据 lock (lockObject) { _com.DataReceived += sp_DataReceived; } } }
/// <summary> /// 发送AT指令 逐条发送AT指令 调用一次发送一条指令 /// 能返回一个OK或ERROR算一条指令 /// </summary> /// <param name="ATCom">AT指令</param> /// <returns>发送指令后返回的字符串,返回null说明端口问题,建议重启</returns> public string SendAT(string ATCom) { string result = string.Empty; //忽略接收缓冲区内容,准备发送 _com.DiscardInBuffer(); //注销事件关联,为发送做准备 _com.DataReceived -= sp_DataReceived; //发送AT指令 try { _com.Write(ATCom + "\r"); } catch (Exception ex) { _com.DataReceived += sp_DataReceived; throw ex; } //接收数据 循环读取数据 直至收到“OK”或“ERROR” string temp = string.Empty; DateTime StartTime = DateTime.Now; try { while (!(temp.Contains("OK") || temp.Contains("ERROR"))) //&& (DateTime.Now.CompareTo(StartTime) < 60000) { temp = _com.ReadExisting(); result += temp; Thread.Sleep(100); } return(result); } catch (Exception ex) { //throw new Exception(ex.ToString() + " Read:" + temp); return(null); } finally { //事件重新绑定 正常监视串口数据 _com.DataReceived += sp_DataReceived; } }