Exemplo n.º 1
0
        private void SendFrame(MessageFrame frame)
        {
            if (SourceAddress == -1 || _port == null)
            {
                return;
            }
            int tx;

            if (frame.Data == null || frame.Data.Length == 0)
            {
                var tmpTransmitBuffer = new byte[MSTP.MSTP_HEADER_LENGTH];
                tx = MSTP.Encode(tmpTransmitBuffer, 0, frame.FrameType, frame.DestinationAddress,
                                 (byte)SourceAddress, 0);
                _port.Write(tmpTransmitBuffer, 0, tx);
            }
            else
            {
                tx = MSTP.Encode(frame.Data, 0, frame.FrameType, frame.DestinationAddress, (byte)SourceAddress,
                                 frame.DataLength);
                _port.Write(frame.Data, 0, tx);
            }
            frame.SendMutex.Set();

            //debug
            if (StateLogging)
            {
                Trace.WriteLine($"         {frame.FrameType} {frame.DestinationAddress.ToString("X2")} ");
            }
        }
Exemplo n.º 2
0
        private void SendFrame(MessageFrame frame)
        {
            if (m_TS == -1 || m_port == null)
            {
                return;
            }
            int tx;

            if (frame.data == null || frame.data.Length == 0)
            {
                byte[] tmp_transmit_buffer = new byte[MSTP.MSTP_HEADER_LENGTH];
                tx = MSTP.Encode(tmp_transmit_buffer, 0, frame.frame_type, frame.destination_address, (byte)m_TS, 0);
                m_port.Write(tmp_transmit_buffer, 0, tx);
            }
            else
            {
                tx = MSTP.Encode(frame.data, 0, frame.frame_type, frame.destination_address, (byte)m_TS, frame.data_length);
                m_port.Write(frame.data, 0, tx);
            }
            frame.send_mutex.Set();

            //debug
            if (StateLogging)
            {
                Trace.WriteLine("         " + frame.frame_type + " " + frame.destination_address.ToString("X2") + " ");
            }
        }
Exemplo n.º 3
0
        public static int Decode(byte[] buffer, int offset, int maxLength, out BacnetPtpFrameTypes frameType, out int msgLength)
        {
            if (maxLength < PTP_HEADER_LENGTH)                              // not enough data
            {
                frameType = BacnetPtpFrameTypes.FRAME_TYPE_CONNECT_REQUEST; // don't care
                msgLength = 0;
                return(-1);                                                 //not enough data
            }

            frameType = (BacnetPtpFrameTypes)buffer[offset + 2];
            msgLength = (buffer[offset + 3] << 8) | (buffer[offset + 4] << 0);
            var    crcHeader = buffer[offset + 5];
            ushort crcData   = 0;

            if (msgLength > 0)
            {
                if (offset + 6 + msgLength + 1 >= buffer.Length)
                {
                    return(-1);
                }

                crcData = (ushort)((buffer[offset + 6 + msgLength + 1] << 8) | (buffer[offset + 6 + msgLength + 0] << 0));
            }

            if (buffer[offset + 0] != PTP_PREAMBLE1)
            {
                return(-1);
            }

            if (buffer[offset + 1] != PTP_PREAMBLE2)
            {
                return(-1);
            }

            if (MSTP.CRC_Calc_Header(buffer, offset + 2, 3) != crcHeader)
            {
                return(-1);
            }

            if (msgLength > 0 && maxLength >= PTP_HEADER_LENGTH + msgLength + 2 && MSTP.CRC_Calc_Data(buffer, offset + 6, msgLength) != crcData)
            {
                return(-1);
            }

            return(8 + msgLength + (msgLength > 0 ? 2 : 0));
        }
Exemplo n.º 4
0
 public static int Encode(byte[] buffer, int offset, BacnetPtpFrameTypes frameType, int msgLength)
 {
     buffer[offset + 0] = PTP_PREAMBLE1;
     buffer[offset + 1] = PTP_PREAMBLE2;
     buffer[offset + 2] = (byte)frameType;
     buffer[offset + 3] = (byte)((msgLength & 0xFF00) >> 8);
     buffer[offset + 4] = (byte)((msgLength & 0x00FF) >> 0);
     buffer[offset + 5] = MSTP.CRC_Calc_Header(buffer, offset + 2, 3);
     if (msgLength > 0)
     {
         //calculate data crc
         var dataCrc = MSTP.CRC_Calc_Data(buffer, offset + 6, msgLength);
         buffer[offset + 6 + msgLength + 0] = (byte)(dataCrc & 0xFF);  //LSB first!
         buffer[offset + 6 + msgLength + 1] = (byte)(dataCrc >> 8);
     }
     return(PTP_HEADER_LENGTH + msgLength + (msgLength > 0 ? 2 : 0));
 }
Exemplo n.º 5
0
        private void SendFrame(MessageFrame frame)
        {
            if (SourceAddress == -1 || _port == null)
            {
                return;
            }
            int tx;

            if (frame.Data == null || frame.Data.Length == 0)
            {
                var tmpTransmitBuffer = new byte[MSTP.MSTP_HEADER_LENGTH];
                tx = MSTP.Encode(tmpTransmitBuffer, 0, frame.FrameType, frame.DestinationAddress,
                                 (byte)SourceAddress, 0);
                _port.Write(tmpTransmitBuffer, 0, tx);
            }
            else
            {
                tx = MSTP.Encode(frame.Data, 0, frame.FrameType, frame.DestinationAddress, (byte)SourceAddress,
                                 frame.DataLength);
                _port.Write(frame.Data, 0, tx);
            }
            frame.SendMutex.Set();
            Log.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Message, "BacnetMstpProtocol", $"{frame.FrameType} {frame.DestinationAddress:X2}");
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }