예제 #1
0
        /// <summary>
        /// Sends command to the LCD. CRC is automatically appended!
        /// </summary>
        /// <param name="Command">The Command to Send</param>
        /// <param name="Data">Any data that should also be sent.</param>
        public void SendCommand(Commands Command, byte[] Data)
        {
            List <byte> _sendList = new List <byte>();

            //Add the Command Byte
            _sendList.Add(Convert.ToByte(Command));

            //Add the Data Length
            _sendList.Add(Convert.ToByte(Data.Length));

            //Add the byte array
            _sendList.AddRange(Data);

            //Append the CRC
            CRC.CalculateCRC(ref _sendList);

            //Register Packet Event if we sent one!


            //Write the command out
            outputQueue.Add(_sendList.ToArray());
        }
예제 #2
0
        private void processIncomingQueue()
        {
            bool checkAgain = false;

            lock (_incomingQueueLock)
            {
                if (_incomingBytes.Count < 4)
                {
                    return;
                }

                byte command = _incomingBytes.Peek();

                int maskedCommand = command & 0x3F;

                if (maskedCommand < 0 || maskedCommand > 35)
                {
                    // Invalid packet
                    _incomingBytes.Dequeue();
                    checkAgain = true;
                }
                else
                {
                    byte packetLength = _incomingBytes.ElementAt(1);
                    int  totalLength  = packetLength + 4;

                    if (packetLength > 22)
                    {
                        // Invalid packet
                        _incomingBytes.Dequeue();
                        checkAgain = true;
                    }
                    else if (_incomingBytes.Count >= totalLength)
                    {
                        // We have enough data for a full packet
                        // Let's process it now.

                        //Create a new packet
                        CFAPacket _Packet = new CFAPacket()
                        {
                            Command    = command,
                            DataLength = packetLength,
                            Data       = new byte[packetLength]
                        };

                        //Read in all data packets
                        for (int i = 0; i < _Packet.DataLength; i++)
                        {
                            _Packet.Data[i] = _incomingBytes.ElementAt(i + 2);
                        }

                        //Read the CRC
                        byte[] _crcByte = new byte[2];

                        _crcByte[0] = _incomingBytes.ElementAt(totalLength - 2);
                        _crcByte[1] = _incomingBytes.ElementAt(totalLength - 1);

                        _Packet.CRC = BitConverter.ToUInt16(_crcByte, 0);


                        List <byte> _tempBytes = new byte[] { command, packetLength }.Concat(_Packet.Data).ToList();
                        CRC.CalculateCRC(ref _tempBytes);

                        if (_crcByte[0] == _tempBytes.ElementAt(_tempBytes.Count - 2) &&
                            _crcByte[1] == _tempBytes.Last())
                        {
                            for (int i = 0; i < totalLength; i++)
                            {
                                _incomingBytes.Dequeue();
                            }
                            this.parseData(_Packet);
                        }
                        else
                        {
                            _incomingBytes.Dequeue();
                        }


                        // Check for additional packets
                        checkAgain = true;
                    }
                }
            }
            if (checkAgain)
            {
                processIncomingQueue();
            }
        }