Beispiel #1
0
        private bool AddPacket(byte[] buffer, int startIndex)
        {
            LCDPacket packet        = CreatePacket(buffer, startIndex);
            ushort    calculatedCRC = CRCGenerator.GenerateCRC(buffer, startIndex, packet.DataLength + 2, CRC_SEED);

            switch (packet.PacketType)
            {
            case LCDPacket.LCDPacketType.NORMAL_RESPONSE:
                AddResponsePacket(packet);
                break;

            case LCDPacket.LCDPacketType.NORMAL_REPORT:
                AddReportPacket(packet);
                break;

            case LCDPacket.LCDPacketType.ERROR_RESPONSE:
                AddResponsePacket(packet);
                break;
            }
            if (calculatedCRC != packet.CRC)
            {
                Console.WriteLine("CRC ERROR!!!: Calculated CRC={0} Actual CRC={1}", Convert.ToString(calculatedCRC, 16), packet.CRC);
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        private LCDPacket Send(byte type, byte dataLength, byte[] data)
        {
            ushort crc;

            if ((null == data && dataLength != 0) || dataLength > data.Length)
            {
                throw new ArgumentException("bad data sent to Send");
            }

            packetXMitBuffer[0] = type;
            packetXMitBuffer[1] = dataLength;
            if (0 != dataLength)
            {
                Array.Copy(data, 0, packetXMitBuffer, 2, dataLength);
            }

            crc = CRCGenerator.GenerateCRC(packetXMitBuffer, dataLength + 2, CRC_SEED);
            packetXMitBuffer[2 + dataLength + 1] = (byte)(crc >> 8);
            packetXMitBuffer[2 + dataLength]     = (byte)crc;
            lock (responseSignal)
            {
                responsePacket = null;
                // comPort
                com.Write(packetXMitBuffer, 0, dataLength + 4);
                if (Monitor.Wait(responseSignal, MAX_RESPONSE_TIME))
                {
                    return(responsePacket);
                }
            }

            return(null);
        }