示例#1
0
 public byte[] ToByteArray()
 {
     using (var MemoryStream = new MemoryStream())
     {
         Writer = new BinaryWriter(MemoryStream);
         Writer.Write(HEADER);
         Writer.Write((byte)Cmd);
         Writer.Write((byte)Channel);
         var data = Uint16ToString(Value);
         Writer.Write(data[0]);
         Writer.Write(data[1]);
         Writer.Write(data[2]);
         var Xor = CheckXor.GetStringXOR(MemoryStream.ToArray());
         Writer.Write((byte)Xor[0]);
         Writer.Write((byte)Xor[1]);
         return(MemoryStream.ToArray());
     }
 }
示例#2
0
        public void AddByte(byte bt)
        {
            if (!IsHeaderFind && bt == PACKAGE_HEADER)
            {
                IsHeaderFind = true;
                DataRecieveQueue.Enqueue(bt);
                if (PackageSize == 1)
                {
                    IsPackageFind = true;
                    return;
                }
            }
            else if (IsHeaderFind)
            {
                DataRecieveQueue.Enqueue(bt);
                if (DataRecieveQueue.Count == PackageSize)
                {
                    RawData = DataRecieveQueue.ToArray();
                    var CalcXOR = CheckXor.GetStringXOR(RawData, 0, RawData.Length - 2);

                    //Clear queue
                    int dataLen = DataRecieveQueue.Count;
                    for (int i = 0; i < dataLen; i++)
                    {
                        DataRecieveQueue.Dequeue();
                    }

                    if (RawData[PackageSize - 1] == CalcXOR[1] && RawData[PackageSize - 2] == CalcXOR[0])
                    {
                        OnPackageRecieved?.Invoke(this, new PackageRecieveArgs()
                        {
                            RawData = this.RawData,
                            Cmd     = (EnumCommand)RawData[1]
                        });
                        IsPackageFind = true;
                    }
                    else
                    {
                        IsHeaderFind  = false;
                        IsPackageFind = false;
                    }
                }
            }
        }