예제 #1
0
        public Byte[] BuildPacket()
        {
            if (optData == null)
            {
                optData = new byte[0];
            }
            var ms = new MemoryStream();
            var bw = new BinaryWriter(ms);

            bw.Write((byte)0x55); // Sync byte
            bw.Write((byte)(data.Count >> 8));
            bw.Write((byte)(data.Count & 0xFF));
            bw.Write((byte)optData.Count); // optData length
            bw.Write((byte)type);          // Packet Type
            bw.Write(EnOceanChecksum.CalcCRC8(ms.GetBuffer(), 4, 1));
            foreach (var b in data)
            {
                bw.Write(b);
            }
            foreach (var b in optData)
            {
                bw.Write(b);
            }
            bw.Write(EnOceanChecksum.CalcCRC8(ms.GetBuffer(), (int)(ms.Length - 6), 6));
            this.rawPacket = ms.GetBuffer();
            Array.Resize <byte>(ref rawPacket, (int)ms.Length);
            return(this.rawPacket);
        }
예제 #2
0
        void processReceiveBuffer()
        {
AGAIN:
            while (receiveBuffer.Count > 0 && receiveBuffer[0] != 0x55)
            {
                receiveBuffer.RemoveAt(0);
            }
            if (receiveBuffer.Count < 6)
            {
                return;
            }
            receiveBuffer.RemoveAt(0); // Remove SYNC byte 0x55
            byte hdrCrc8 = EnOceanChecksum.CalcCRC8(receiveBuffer, 4);

            if (hdrCrc8 != receiveBuffer[4])
            {
                Console.WriteLine("CRC ERROR FOR PACKET HDR - or not a sync start\n");
                goto AGAIN;
            }
            UInt16 pktLen = receiveBuffer[0];

            pktLen *= 256;
            pktLen += receiveBuffer[1];
            Byte optLen  = receiveBuffer[2];
            Byte pktType = receiveBuffer[3];

            if ((pktLen + optLen + 6) > receiveBuffer.Count)
            {
                // Not enough data yet.. push back header..
                Console.WriteLine(" ABANDON FOR LATER - NOT ENOUGH DATA");
                receiveBuffer.Insert(0, 0x55);
                return;
            }
            List <byte> pktHdr = receiveBuffer.GetRange(0, 5);

            receiveBuffer.RemoveRange(0, 5); // Remove hdr
            Byte dtaCrc = EnOceanChecksum.CalcCRC8(receiveBuffer, pktLen + optLen);

            if (dtaCrc == receiveBuffer[optLen + pktLen])
            {
                // Console.WriteLine(" ----- MATCH DATA CRC OK");
                receiveBuffer.RemoveAt(receiveBuffer.Count - 1); // Remove checksum - we have checked it already
                List <byte> payload    = receiveBuffer.GetRange(0, pktLen);
                List <byte> optPayload = receiveBuffer.GetRange(pktLen, optLen);
                Console.WriteLine("Dispatching validated packet of {0} bytes and {1} bytes", payload.Count, optPayload.Count);
                EnOceanPacket parsedPkt = EnOceanPacket.Parse(pktHdr[3], payload, optPayload);
                rxPacketQueue.Enqueue(parsedPkt);
                rxCommThreadWaitHandle.Set(); // Notify rx thread
                receiveBuffer.RemoveRange(0, optLen + pktLen);
                goto AGAIN;
            }
        }