/// <summary> /// Send HART-IP request /// </summary> /// <param name="Request"><see cref="HartIPRequest"/></param> /// <returns><see cref="HartIPResponse"/></returns> private HartIPResponse SendRequest(HartIPRequest Request) { HartIPResponse Rsp = null; do { try { if (m_HartIPConn != null) { MsgResponse MsgRsp = new MsgResponse(Request.m_Timeout); if (!m_HartIPConn.SendHartRequest(Request, MsgRsp)) { m_Error = m_HartIPConn.LastError; break; } // wait for the object return or timeout if (MsgRsp.GetResponse()) { Rsp = MsgRsp.ResponseMsg; } } if (Rsp == null) { if ((m_HartIPConn != null) && (m_HartIPConn.LastError.Length > 0)) { m_Error = m_HartIPConn.LastError; } else { m_Error = "Waiting Hart-IP Request waiting response timeout."; } Logger.Log(m_Error, false); break; } } catch (ThreadInterruptedException Ex) { m_Error = "Send Hart Request Exception: " + Ex.Message; Logger.Log(m_Error, true); } catch (ThreadStateException TSEx) { m_Error = "Send Hart Request Exception: " + TSEx.Message; Logger.Log(m_Error, true); } } while (false); /* ONCE */ return(Rsp); }
/// <summary> /// Send a Hart-IP Request /// </summary> /// <param name="Request"><see cref="HartIPRequest"></see></param> /// <param name="MsgRsp"><see cref="MsgResponse"></see></param> /// <para>see the HartIPRequest.Command for HART specification references.</para> /// <returns>bool if it is success</returns> public bool SendHartRequest(HartIPRequest Request, MsgResponse MsgRsp) { bool bSuccess = false; if (Request == null) { throw new ArgumentException("Invalid argument in SendHartRequest."); } lock (SyncRoot) { m_Error = String.Empty; try { if (m_RspMsgReader == null) { // Create a thread that is constantly reading on the input stream m_bStopped = false; m_RspMsgReader = new Thread(new ThreadStart(this.ReceiveMsg)); m_RspMsgReader.Name = "HartIPConnection"; m_RspMsgReader.Start(); } // add the request object into the m_Requests list MsgRequest MsgReq = new MsgRequest(Request.TransactionId, MsgRsp); m_Requests.Add(MsgReq); // send the request bSuccess = SendRequest(Request); LogMsg.Instance.Log(Request.ToString()); } catch (SocketException se) { m_Error = String.Format("SendHartRequest SocketException: ErrorCode:{0}. {1}", se.ErrorCode, se.Message); LogMsg.Instance.Log("Error, " + m_Error, true); } catch (Exception e) { m_Error = String.Format("SendHartRequest Exception: {0}", e.Message); LogMsg.Instance.Log("Error, " + m_Error, true); } if (!bSuccess) { LogMsg.Instance.Log("Error, Failed sending Request: " + Request.ToString(), true); DequeueRequest(Request.TransactionId); } } return(bSuccess); }
/// <summary> /// Create a HART Close Session Request, send it to the Gateway, /// and close the socket. /// </summary> /// <param name="Result"><see cref="HARTMsgResult">HARTMsgResult</see></param> public void CloseSession(HARTMsgResult Result) { HartIPRequest Req = null; do { Req = HartIPRequest.CloseSession(TransactionId); MsgResponse MsgRsp = new MsgResponse(HARTIPConnect.USE_SOCKET_TIMEOUT_DEFAULT); if (SendHartRequest(Req, MsgRsp)) { try { // wait for the object return or timeout if (MsgRsp.GetResponse() == false) { Result.AddMessage("Close Session failed: No response was received.", false, true); break; } if (!MsgRsp.ResponseMsg.IsValidResponse) { Result.AddMessage("Close Session failed: Received an invalid response msg type.", false, true); break; } LogMsg.Instance.Log(MsgRsp.ResponseMsg.ToString()); } catch { Result.AddMessage("Close Session failed.", false, true); break; } } else { Result.AddMessage("Close Session failed.", false, true); break; } } while (false); /* ONCE */ Close(); Result.AddMessage("Closed the HPort socket.", false, true); }
/// <summary> /// Contructor /// </summary> /// <param name="ReqMsgId">ushort request's Transaction Id</param> /// <param name="Response"><see cref="MsgResponse"/></param> public MsgRequest(ushort ReqMsgId, MsgResponse Response) { this.ReqMsgId = ReqMsgId; this.Response = Response; }