private bool WaitForGreeting(int timeout) { if (m_port == null) { return(false); } byte[] buffer = new byte[7]; int offset = 0; int current_timeout; while (offset < 7) { if (offset == 0) { current_timeout = timeout; } else { current_timeout = T_FRAME_ABORT; } int rx = m_port.Read(buffer, offset, 7 - offset, current_timeout); if (rx <= 0) { return(false); } offset += rx; //remove garbage RemoveGreetingGarbage(buffer, ref offset); } return(true); }
private bool WaitForGreeting(int timeout) { if (_port == null) { return(false); } var buffer = new byte[7]; var offset = 0; while (offset < 7) { var currentTimeout = offset == 0 ? timeout : T_FRAME_ABORT; var rx = _port.Read(buffer, offset, 7 - offset, currentTimeout); if (rx <= 0) { return(false); } offset += rx; //remove garbage RemoveGreetingGarbage(buffer, ref offset); } return(true); }
private GetMessageStatus GetNextMessage(int timeoutMs, out BacnetMstpFrameTypes frameType, out byte destinationAddress, out byte sourceAddress, out int msgLength) { int timeout; frameType = BacnetMstpFrameTypes.FRAME_TYPE_TOKEN; destinationAddress = 0; sourceAddress = 0; msgLength = 0; //fetch header while (_localOffset < MSTP.MSTP_HEADER_LENGTH) { if (_port == null) { return(GetMessageStatus.ConnectionClose); } timeout = _localOffset > 0 ? T_FRAME_ABORT // set sub timeout : timeoutMs; // set big silence timeout //read var rx = _port.Read(_localBuffer, _localOffset, MSTP.MSTP_HEADER_LENGTH - _localOffset, timeout); if (rx == -ETIMEDOUT) { //drop message var status = _localOffset == 0 ? GetMessageStatus.Timeout : GetMessageStatus.SubTimeout; _localBuffer[0] = 0xFF; RemoveGarbage(); return(status); } if (rx < 0) { //drop message _localBuffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionError); } if (rx == 0) { //drop message _localBuffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionClose); } _localOffset += rx; //remove paddings & garbage RemoveGarbage(); } //decode if (MSTP.Decode(_localBuffer, 0, _localOffset, out frameType, out destinationAddress, out sourceAddress, out msgLength) < 0) { //drop message _localBuffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } //valid length? var fullMsgLength = msgLength + MSTP.MSTP_HEADER_LENGTH + (msgLength > 0 ? 2 : 0); if (msgLength > MaxBufferLength) { //drop message _localBuffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } //fetch data if (msgLength > 0) { timeout = T_FRAME_ABORT; //set sub timeout while (_localOffset < fullMsgLength) { //read var rx = _port.Read(_localBuffer, _localOffset, fullMsgLength - _localOffset, timeout); if (rx == -ETIMEDOUT) { //drop message var status = _localOffset == 0 ? GetMessageStatus.Timeout : GetMessageStatus.SubTimeout; _localBuffer[0] = 0xFF; RemoveGarbage(); return(status); } if (rx < 0) { //drop message _localBuffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionError); } if (rx == 0) { //drop message _localBuffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionClose); } _localOffset += rx; } //verify data crc if ( MSTP.Decode(_localBuffer, 0, _localOffset, out frameType, out destinationAddress, out sourceAddress, out msgLength) < 0) { //drop message _localBuffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } } //signal frame event if (FrameRecieved != null) { var frameTypeCopy = frameType; var destinationAddressCopy = destinationAddress; var sourceAddressCopy = sourceAddress; var msgLengthCopy = msgLength; ThreadPool.QueueUserWorkItem( o => { FrameRecieved(this, frameTypeCopy, destinationAddressCopy, sourceAddressCopy, msgLengthCopy); }, null); } Log.Debug($"{frameType} {destinationAddress:X2}"); //done return(GetMessageStatus.Good); }
private GetMessageStatus GetNextMessage(int timeout_ms, out BacnetMstpFrameTypes frame_type, out byte destination_address, out byte source_address, out int msg_length) { int timeout; frame_type = BacnetMstpFrameTypes.FRAME_TYPE_TOKEN; destination_address = 0; source_address = 0; msg_length = 0; //fetch header while (m_local_offset < MSTP.MSTP_HEADER_LENGTH) { if (m_port == null) { return(GetMessageStatus.ConnectionClose); } if (m_local_offset > 0) { timeout = T_FRAME_ABORT; //set sub timeout } else { timeout = timeout_ms; //set big silence timeout } //read int rx = m_port.Read(m_local_buffer, m_local_offset, MSTP.MSTP_HEADER_LENGTH - m_local_offset, timeout); if (rx == -ETIMEDOUT) { //drop message GetMessageStatus status = m_local_offset == 0 ? GetMessageStatus.Timeout : GetMessageStatus.SubTimeout; m_local_buffer[0] = 0xFF; RemoveGarbage(); return(status); } if (rx < 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionError); } if (rx == 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionClose); } m_local_offset += rx; //remove paddings & garbage RemoveGarbage(); } //decode if (MSTP.Decode(m_local_buffer, 0, m_local_offset, out frame_type, out destination_address, out source_address, out msg_length) < 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } //valid length? int full_msg_length = msg_length + MSTP.MSTP_HEADER_LENGTH + (msg_length > 0 ? 2 : 0); if (msg_length > MaxBufferLength) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } //fetch data if (msg_length > 0) { timeout = T_FRAME_ABORT; //set sub timeout while (m_local_offset < full_msg_length) { //read int rx = m_port.Read(m_local_buffer, m_local_offset, full_msg_length - m_local_offset, timeout); if (rx == -ETIMEDOUT) { //drop message GetMessageStatus status = m_local_offset == 0 ? GetMessageStatus.Timeout : GetMessageStatus.SubTimeout; m_local_buffer[0] = 0xFF; RemoveGarbage(); return(status); } if (rx < 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionError); } if (rx == 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionClose); } m_local_offset += rx; } //verify data crc if (MSTP.Decode(m_local_buffer, 0, m_local_offset, out frame_type, out destination_address, out source_address, out msg_length) < 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } } //signal frame event if (FrameRecieved != null) { BacnetMstpFrameTypes _frame_type = frame_type; byte _destination_address = destination_address; byte _source_address = source_address; int _msg_length = msg_length; ThreadPool.QueueUserWorkItem((o) => { FrameRecieved(this, _frame_type, _destination_address, _source_address, _msg_length); }, null); } if (StateLogging) { Trace.WriteLine("" + frame_type + " " + destination_address.ToString("X2") + " "); } //done return(GetMessageStatus.Good); }