/// <summary> /// 设备打开函数,无法时打开将引发异常 /// </summary> public void Open() { //如果串口已打开 则先关闭 if (_com.IsOpen) { _com.Close(); } _com.Open(); //初始化设备 if (_com.IsOpen) { _com.Write("ATE0\r"); Thread.Sleep(50); _com.Write("AT+CMGF=0\r"); Thread.Sleep(50); _com.Write("AT+CNMI=2,1\r"); } }
/// <summary> /// 返回连接信息的Open方法 /// </summary> public bool Open(out string sResult) { //如果串口已打开 则先关闭 sResult = ""; if (_com.IsOpen) { _com.Close(); } _com.Open(); //初始化设备 if (_com.IsOpen) { try { _com.DataReceived -= sp_DataReceived; _com.Write("ATE0\r"); Thread.Sleep(200); sResult += " ATE0:" + _com.ReadExisting(); _com.Write("AT+CMGF=0\r"); Thread.Sleep(200); sResult += " AT+CMGF=0:" + _com.ReadExisting(); _com.Write("AT+CNMI=2," + recvMsgLoc + "\r"); Thread.Sleep(200); sResult += " AT+CNMI=2," + recvMsgLoc + ":" + _com.ReadExisting(); //绑定事件 _com.DataReceived += sp_DataReceived; return(true); } catch (Exception ex) { //打印日志 string errTxt = string.Format(" Connect Send AT Exception Result:{0}\r\n{1}\r\n{2}", sResult, ex.Message, ex.StackTrace); LogHelpers.Error(errTxt); throw new Exception(" Connect Send AT Exception:" + ex.ToString() + " Result:" + sResult); } } return(false); }
/// <summary> /// 返回连接信息的Open方法 /// </summary> public bool Open(out string sResult, int SleepTime) { //如果串口已打开 则先关闭 sResult = ""; if (_com.IsOpen) { _com.Close(); } _com.Open(); //初始化设备 if (_com.IsOpen) { try { _com.DataReceived -= sp_DataReceived; Thread.Sleep(SleepTime); //原200毫秒, _com.Write("ATE0\r"); Thread.Sleep(200); sResult += " ATE0:" + _com.ReadExisting(); _com.Write("AT+CMGF=0\r"); Thread.Sleep(200); sResult += " AT+CMGF=0:" + _com.ReadExisting(); _com.Write("AT+CNMI=2," + recvMsgLoc + "\r"); Thread.Sleep(200); sResult += " AT+CNMI=2," + recvMsgLoc + ":" + _com.ReadExisting(); //绑定事件 _com.DataReceived += sp_DataReceived; return(true); } catch (Exception ex) { throw new Exception(" Connect Send AT Exception:" + ex.ToString() + " Result:" + sResult); } } return(false); }
public bool Open() { _createCom(); return(Com.Open()); }