Exemplo n.º 1
0
        /// <summary>
        /// HDLC注册给底层数据收发外设(UART)的字节接受回调函数
        /// </summary>
        /// <param name="rxByte">接受的新字节数据</param>
        private void Rx1Byte(byte rxByte)
        {
            // lock the module
            // start of frame
            if (m_bBusyRevHdlcFrame == false && m_u8LastRxByte == HDLC_FLAG && rxByte != HDLC_FLAG)
            {
                // I'm now receiving
                m_bBusyRevHdlcFrame = true;
                // create the HDLC frame
                InputOpen();
                // add the byte just received
                InputWrite(rxByte);
            }
            // middle of frame
            else if (m_bBusyRevHdlcFrame == true && rxByte != HDLC_FLAG)
            {
                // add the byte just received
                InputWrite(rxByte);
                if (m_u8InputBufFill + 1 > HDLC_MAX_BUFFER_SIZE)
                {
                    m_u8InputBufFill    = 0;
                    m_bBusyRevHdlcFrame = false;
                    // input buffer overflow, drop the incoming fram
                    CommStackLog.RecordErr(enLogLayer.eHdlc, "Rx overflow frame: " + CommStackLog.ToHexString(m_u8aInputBuf, m_u8InputBufFill));
                }
            }
            // end of frame
            else if (m_bBusyRevHdlcFrame == true && rxByte == HDLC_FLAG)
            {
                // finalize the HDLC frame
                InputClose();
                if (m_u8InputBufFill >= 4)
                {
                    // hand over frame to upper layer
                    if (m_evHDLCFrameArrived != null)
                    {
                        m_evHDLCFrameArrived(m_u8aInputBuf, m_u8InputBufFill);
                        m_u8InputBufFill = 0;
                    }
                }

                m_bBusyRevHdlcFrame = false;
            }

            m_u8LastRxByte = rxByte;
        }
Exemplo n.º 2
0
        /// <summary>
        /// HDLC层发送数据结束
        /// </summary>
        public void OutputClose()
        {
            UInt16 u16finalCrc = 0;

            u16finalCrc = (UInt16)(~m_u16OutputCrc);
            // write the CRC value
            OutputWrite((byte)((u16finalCrc >> 0) & 0xFF));
            OutputWrite((byte)((u16finalCrc >> 8) & 0xFF));
            // write closing HDLC flag
            lock (CommStackLog.LockLog)
            {
                //m_Com.Send(HDLC_FLAG);
                m_u8aOutputBuf[m_u8OutputBufFill++] = HDLC_FLAG;
                m_Com.SendArr(m_u8aOutputBuf, m_u8OutputBufFill);
                CommStackLog.RecordInf(enLogLayer.eHdlc, "Tx: " + CommStackLog.ToHexString(m_u8aOutputPacketBuf, m_u8OutputPacketFill));
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// HDLC层接收数据结束
 /// </summary>
 private void InputClose()
 {
     lock (CommStackLog.LockLog)
     {
         if (m_u16InputCrc == HDLC_CRCGOOD)
         {
             // remove the CRC from the input buffer
             m_u8InputBufFill -= 2;
             CommStackLog.RecordInf(enLogLayer.eHdlc, "Rx: " + CommStackLog.ToHexString(m_u8aInputBuf, m_u8InputBufFill));
         }
         else
         {
             CommStackLog.RecordErr(enLogLayer.eHdlc, "Rx crc error: " + m_u16InputCrc.ToString("X4"));
             m_u8InputBufFill = 0;
         }
         // reset escaping
         m_bInputEscaping = false;
     }
 }