Beispiel #1
0
        public Block(byte[] Buffer, byte Company, ushort Position, ushort MaxSize)
        {
            byte Code = Buffer[Position];

            foreach (Protocol.StructBlock CurrentBlock in Protocol.BlockList)
            {
                if ((CurrentBlock.Company == Company) && (CurrentBlock.Code == Code) && (CurrentBlock.Size <= MaxSize))
                {
                    Crc        = System.BitConverter.ToUInt16(Buffer, Position + CurrentBlock.Size - 2);
                    CorrectCrc = (Crc == CRC16.Compute(Buffer, Position, CurrentBlock.Size - 2));

                    if (!CorrectCrc)
                    {
                        if (Properties.Settings.Default.IgnoreCRC)
                        {
                            Console.WriteLine("CRC error in block");
                        }
                        else
                        {
                            throw new Exception("CRC error in block").SetCode(Constant.ExceptionCRC);
                        }
                    }

                    ChosenBlock = CurrentBlock;

                    if (Properties.Settings.Default.ShowBlock)
                    {
                        Console.WriteLine(ChosenBlock.Name);
                    }

                    Array.Copy(Buffer, Position, Data, 0, ChosenBlock.Size);

                    return;
                }
            }

            throw new Exception("Unknown block");
        }
Beispiel #2
0
        public void EncodePrefix(byte[] Buffer)
        {
            // префикс

            if (Receipt == null)
            {
                Operation = 0;
                Size      = (ushort)(Constant.PrefixSize + 2);
            }
            else
            {
                Operation = 1;
                Size      = (ushort)(Constant.PrefixSize + 2 + Receipt.Size + 3);
            }
            Size = (ushort)(Constant.PrefixSize + 2 + (Receipt != null ? (Receipt.Size + 3) : 0));

            Buffer[0] = Id;
            Array.Copy(System.Text.Encoding.ASCII.GetBytes(TextId), 0, Buffer, 1, 3);
            Array.Copy(System.BitConverter.GetBytes(Size), 0, Buffer, 4, 2);
            Buffer[6] = SoftVersion;
            Buffer[7] = HardVersion;
            Buffer[8] = Channel;
            Array.Copy(System.BitConverter.GetBytes(Serial), 0, Buffer, 9, 4);
            Buffer[13] = Company;
            Buffer[14] = Device;
            Array.Copy(System.BitConverter.GetBytes(Imei), 0, Buffer, 15, 8);
            Array.Copy(System.BitConverter.GetBytes(Phone), 0, Buffer, 23, 4);
            Buffer[27] = 0;
            Buffer[28] = 0;
            Buffer[29] = 0;
            Buffer[30] = 0;
            Buffer[31] = Operation;

            Crc = CRC16.Compute(Buffer, 0, Size - 2);
            Array.Copy(System.BitConverter.GetBytes(Crc), 0, Buffer, Size - 2, 2);
        }