public void SendCommand(SerialDataChannel serial_)
 {
     try
     {
         serial_.Write(new byte[] { 0xAA }, 0, 1);
         TrueCheckSum = ActualCheckSum;
         serial_.Write(comData_, 0, 2);
         if (PayLoadLength > 0)
         {
             serial_.Write(data_, 0, PayLoadLength);
         }
     }
     catch
     {
     }
 }
        public static ProtocolError FromStream(ref PacketCommandMini command, SerialDataChannel serial, int timeOut, System.Windows.Forms.TextBox flush = null)
        {
            long start = DateTime.Now.Millisecond;

            bool hasAA = false;

            while (!hasAA && (DateTime.Now.Millisecond - start) <= timeOut)
            {
                if (serial.BytesToRead < 3)
                {
                    continue;
                }
                if (serial.ReadByte() == 0xAA)
                {
                    hasAA = true;
                    break;
                }
            }
            if (!hasAA)
            {
                return(ProtocolError.ReadTimeout);
            }
            serial.Read(command.comData_, 0, 2);
            command.PayLoad = new byte[command.PayLoadLength];
            int dlen = command.PayLoadLength;
            int i    = 0;

            while (i < dlen && (DateTime.Now.Millisecond - start) <= timeOut)
            {
                if (serial.BytesToRead > 0)
                {
                    command.PayLoad[i++] = (byte)serial.ReadByte();
                }
            }
            if (i < dlen) // not all of the bytes were received
            {
                return(ProtocolError.ReadTimeout);
            }
            // calculate checksum
            if (command.TrueCheckSum != command.ActualCheckSum)
            {
                return(ProtocolError.CheckSumMismatch);
            }

            // we got the command. letes return
            return(ProtocolError.None);
        }