/// <summary> /// HartIPResponse /// </summary> /// <param name="inBuffer">BinaryReader</param> /// <param name="TimeStamp">String response timestamp</param> internal HartIPResponse(BinaryReader inBuffer, String TimeStamp) : base("Rx") { byte Version, MsgType, MsgId, Status; ushort usMsgByteCount; Version = inBuffer.ReadByte(); if (Version != HARTIPMessage.HART_UDP_TCP_MSG_VERSION) { throw new ArgumentException(String.Format("HartIPResponse Error: Unsupported Response Message Version: {0}.", Version)); } // If the input timestamp is null or empty, get the current time as timestamp if ((TimeStamp != null) && (TimeStamp.Length > 0)) { m_TimeStamp = TimeStamp; } else { m_TimeStamp = HartUtil.GetTimeStamp(); } MsgType = inBuffer.ReadByte(); MsgId = inBuffer.ReadByte(); Status = inBuffer.ReadByte(); // Create message header MsgHeader = new HartIPMessageHeader(MsgType, (MessageId)MsgId, Status); // transaction id m_TransactionId = (ushort)IPAddress.NetworkToHostOrder((short)inBuffer.ReadUInt16()); // byte count usMsgByteCount = (ushort)IPAddress.NetworkToHostOrder((short)inBuffer.ReadUInt16()); // message body byte count m_HartCmdSize = (ushort)(usMsgByteCount - HARTIPMessage.HART_MSG_HEADER_SIZE); if (m_HartCmdSize > HARTIPMessage.MAX_HARTMSG_LENGTH) { m_HartCmdSize = HARTIPMessage.MAX_HARTMSG_LENGTH; } // get data bytes if message has data if (m_HartCmdSize > 0) { ushort usExtra; int nIndex; int j, i; // get the message bytes for (i = MsgHeader.ByteCount, j = 0; i < usMsgByteCount; i++, j++) { m_HartCmd[j] = inBuffer.ReadByte(); } // if the message is the ACK or BACK (notify) type if (MsgType == HARTIPMessage.HART_ACK || MsgType == HARTIPMessage.HART_PUBLISH_NOTIFY) { // Check if response is initiate session if (MsgHeader.MsgId == MessageId.SESSION_INITIATE) { nIndex = 0; m_cDataCount = (byte)m_HartCmdSize; m_cRspcode = MsgHeader.Status; } // Check if it is a long frame else if ((m_HartCmd[0] & 0x80) == 0x80) { // get the extra usExtra = (ushort)((m_HartCmd[0] & 0x60) >> 5); nIndex = 6 + usExtra; m_cCmd = m_HartCmd[nIndex++]; m_cDataCount = m_HartCmd[nIndex++]; // subtract response code and device status bytes m_cDataCount -= 2; // get respsone code and device status bytes m_cRspcode = m_HartCmd[nIndex++]; m_cDeviceStatus = m_HartCmd[nIndex++]; } else { // it is a short frame nIndex = 2; m_cCmd = m_HartCmd[nIndex++]; m_cDataCount = m_HartCmd[nIndex++]; m_cRspcode = m_HartCmd[nIndex++]; m_cDeviceStatus = m_HartCmd[nIndex++]; // subtract response code and device status bytes m_cDataCount -= 2; } // Get the rest of response data bytes and store them in the data array m_Data = new byte[m_cDataCount]; for (i = nIndex, j = 0; j < m_cDataCount; i++, j++) { m_Data[j] = m_HartCmd[i]; } } } else { m_cRspcode = MsgHeader.Status; } }
/// <summary> /// HART Request /// </summary> /// <param name="MsgId">MessageId message id</param> /// <param name="TransactionId">ushort transaction id</param> internal HartIPRequest(MessageId MsgId, ushort TransactionId) : base(TransactionId, "Tx") { MsgHeader = new HartIPMessageHeader(HARTIPMessage.HART_STX, MsgId, 0); }