コード例 #1
0
ファイル: HDLC.cs プロジェクト: joshbenwaa/Final_422
        /// <summary>
        /// Creates the HDLC frame and stores it into buffer
        /// </summary>
        public void CreateHDLC()
        {
            byte        temp;
            List <byte> tmpBuffer  = new List <byte>();
            List <byte> tmpBuffer2 = new List <byte>();

            // Clear buffer
            tmpBuffer.Clear();

            // Add cmd
            tmpBuffer.Add(cmd);

            // Add Length
            Length = (byte)Data.Count;
            tmpBuffer.Add((byte)Data.Count);

            // Add Data
            for (byte i = 0; i < Length; i++)
            {
                tmpBuffer.Add(Data[i]);
            }

            // Add CRC
            crc = DPA_UTILS.CalcCRC(tmpBuffer);
            tmpBuffer.Add(crc);

            // Make a HDLC frame
            tmpBuffer2.Clear();
            tmpBuffer2.Add((byte)0x7E);    // Flag sequence
            foreach (byte tmp in tmpBuffer)
            {
                temp = tmp;
                // If there is Flag sequence or Control escape
                if ((temp == (byte)0x7D) || (temp == (byte)0x7E))
                {
                    tmpBuffer2.Add((byte)0x7D);
                    tmpBuffer2.Add((byte)(temp ^ 0x20));
                }
                else
                {
                    tmpBuffer2.Add((byte)temp);
                }
            }
            tmpBuffer2.Add((byte)0x7E);    // Flag sequence

            // Copy result to Buffer
            Buffer = new byte[tmpBuffer2.Count];
            tmpBuffer2.CopyTo(Buffer);
        }
コード例 #2
0
ファイル: HDLC.cs プロジェクト: joshbenwaa/Final_422
        /// <summary>
        /// Method to parse through incoming bytes and create a HDLC frame
        /// </summary>
        /// <param name="character">the byte to check</param>
        /// <returns>Returns the state of the HDLC frame after checking one character</returns>
        public DPA_RX_STATE DPA_RX_Parse(byte character)
        {
            int          i       = 0;
            DPA_RX_STATE ret_val = DPA_RX_STATE.DPA_RX_NOERR;

            if (character == HDLC_FLAG_SEQUENCE)        // flag sequence
            {
                // first Flag sequnce
                if (Buffer.Count == 0)
                {
                    Buffer.Add((byte)character);
                    HDLC_CE = false;
                }
                else
                {
                    // It is error state - too short message
                    if (Buffer.Count < (HDLC_MIN_LEN - 1))
                    {
                        // Maybe it is start of new frame...
                        Buffer.Clear(); Buffer.Add(character);
                        return(DPA_RX_STATE.DPA_RX_FE);
                    }
                    // Correct length
                    else
                    {
                        // Check CRC
                        Buffer.RemoveAt(0); // remove first Flag sequnce
                        byte crc = DPA_UTILS.CalcCRC(Buffer);
                        // CRC is OK
                        if (crc == 0)
                        {
                            byte[] tmpBuffer = new byte[Buffer.Count];
                            Buffer.CopyTo(tmpBuffer);
                            cmd    = tmpBuffer[0];
                            Length = tmpBuffer[1];
                            for (i = 0; i < Length; i++)
                            {
                                Data.Add(tmpBuffer[i + 2]); //exception
                            }
                            CRC = tmpBuffer[Length + 2];
                            Buffer.Clear();
                            return(DPA_RX_STATE.DPA_RX_OK);
                        }
                        // CRC is no OK
                        else
                        {
                            // Maybe it is start of new frame...
                            Buffer.Clear(); Buffer.Add((byte)0x7E);
                            return(DPA_RX_STATE.DPA_RX_CRCERR);
                        }
                    }
                }
            }
            else // if another character received
            {
                // if it is not the first character and length is within borders
                if ((Buffer.Count > 0) && (Buffer.Count < HDLC_MAX_LEN))
                {
                    // if Control Esape received
                    if (character == HDLC_CONTROL_ESCAPE)
                    {
                        HDLC_CE = true;
                    }
                    else
                    {   // else insert character
                        if (HDLC_CE == false)
                        {
                            Buffer.Add((byte)character);
                        }
                        else
                        {
                            HDLC_CE = false;
                            Buffer.Add((byte)((byte)character ^ (byte)HDLC_ESCAPE_BIT));
                        }
                    }
                }
            }

            return(ret_val);
        }