Exemple #1
0
 public void Start()
 {
     offset   = (long)0;
     qpcStart = (long)0;
     paused   = false;
     CCTPerformanceTimer.QueryPerformanceCounter(ref qpcStart);
 }
Exemple #2
0
        static CCTPerformanceTimer()
        {
            long l = (long)0;

            CCTPerformanceTimer.QueryPerformanceFrequency(ref l);
            CCTPerformanceTimer.ticksFactor = 10000000.0 / (double)l;
        }
Exemple #3
0
 public void Resume()
 {
     if (paused)
     {
         paused = false;
         CCTPerformanceTimer.QueryPerformanceCounter(ref qpcStart);
     }
 }
Exemple #4
0
 public void Pause()
 {
     if (!paused)
     {
         paused = true;
         long l = (long)0;
         CCTPerformanceTimer.QueryPerformanceCounter(ref l);
         offset += l - qpcStart;
     }
 }
        public bool SendCCTalkCommand(byte address, byte command, byte[] data, int timeOut, out byte[] response, CCTMessageType msgType, byte[] encryptionCode)
        {
            if (data.Length > 252)
            {
                throw new Exception("to many bytes in ccTalk data");
            }
            bool flag1 = false;

            response = emptyBuffer;


            lock (this)
            {
                int i1 = 4 + data.Length + 1;
                buffer[0] = address;
                buffer[1] = (byte)data.Length;
                buffer[2] = 1;
                buffer[3] = command;
                data.CopyTo(buffer, 4);
                switch (msgType)
                {
                case CCTMessageType.NoneCRC:
                    break;

                case CCTMessageType.EncryptedNoneCRC:
                    CCTCrypt.Encrypt(buffer, 2, i1 - 2, encryptionCode);
                    break;

                case CCTMessageType.StandardCRC8:
                    crc8.Reset();
                    crc8.PushData(buffer, 0, i1 - 1);
                    buffer[4 + data.Length] = crc8.CRC;
                    break;

                case CCTMessageType.EncryptedCRC8:
                    crc8.Reset();
                    crc8.PushData(buffer, 0, i1 - 1);
                    buffer[4 + data.Length] = crc8.CRC;
                    CCTCrypt.Encrypt(buffer, 2, i1 - 2, encryptionCode);
                    break;

                case CCTMessageType.StandardCRC16:
                    crc16.Reset();
                    crc16.PushByte(buffer[0]);
                    crc16.PushByte(buffer[1]);
                    crc16.PushData(buffer, 3, i1 - 4);
                    buffer[2] = (byte)(crc16.CRC & 255);
                    buffer[4 + data.Length] = (byte)(crc16.CRC >> 8);
                    break;

                case CCTMessageType.EncryptedCRC16:
                    crc16.Reset();
                    crc16.PushByte(buffer[0]);
                    crc16.PushByte(buffer[1]);
                    crc16.PushData(buffer, 3, i1 - 4);
                    buffer[2] = (byte)(crc16.CRC & 255);
                    buffer[4 + data.Length] = (byte)(crc16.CRC >> 8);
                    CCTCrypt.Encrypt(buffer, 2, i1 - 2, encryptionCode);
                    break;
                }
                Thread.Sleep(20);

                if (SerPort == null)
                {
                    return(false);
                }
                SerPort.DiscardInBuffer();
                SerPort.Write(buffer, 0, i1);

                if ((command == 253) || (command == 252))
                {
                    Thread.Sleep(1500);
                }
                else
                {
                    Thread.Sleep(100);
                }

                bool flag2 = false, flag3 = false;
                int  i2 = 0, i3 = 0;
                CCTPerformanceTimer performanceTimer = new CCTPerformanceTimer();
                performanceTimer.Start();
                while (!flag2)
                {
                    try
                    {
                        Thread.Sleep(20);
                        while (SerPort.BytesToRead > 0)
                        {
                            if (flag3)
                            {
                                TimeSpan timeSpan1 = performanceTimer.TimeSpan;
                                timeOut = (int)timeSpan1.TotalMilliseconds + 50;
                            }
                            buffer[i2++] = (byte)SerPort.ReadByte();
                            i3           = i2 >= 3 ? 5 + buffer[1] : -1;
                            if (i2 == i3)
                            {
                                if (buffer[0] == address)
                                {
                                    i2    = 0;
                                    flag3 = true;
                                }
                                if (buffer[0] == 1)
                                {
                                    flag2 = true;
                                    switch (msgType)
                                    {
                                    case CCTMessageType.NoneCRC:
                                        break;

                                    case CCTMessageType.EncryptedNoneCRC:
                                        CCTCrypt.Decrypt(buffer, 2, i2 - 2, encryptionCode);
                                        break;

                                    case CCTMessageType.StandardCRC8:
                                        crc8.Reset();
                                        crc8.PushData(buffer, 0, i2 - 1);
                                        flag1 = buffer[i2 - 1] == crc8.CRC;
                                        break;

                                    case CCTMessageType.EncryptedCRC8:
                                        CCTCrypt.Decrypt(buffer, 2, i2 - 2, encryptionCode);
                                        crc8.Reset();
                                        crc8.PushData(buffer, 0, i2 - 1);
                                        flag1 = buffer[i2 - 1] == crc8.CRC;
                                        break;

                                    case CCTMessageType.StandardCRC16:
                                        crc16.Reset();
                                        crc16.PushByte(buffer[0]);
                                        crc16.PushByte(buffer[1]);
                                        crc16.PushData(buffer, 3, i2 - 4);
                                        flag1 = (buffer[2] + (buffer[i2 - 1] << 8)) == crc16.CRC;
                                        break;

                                    case CCTMessageType.EncryptedCRC16:
                                        CCTCrypt.Decrypt(buffer, 2, i2 - 2, encryptionCode);
                                        crc16.Reset();
                                        crc16.PushByte(buffer[0]);
                                        crc16.PushByte(buffer[1]);
                                        crc16.PushData(buffer, 3, i2 - 4);
                                        flag1 = (buffer[2] + (buffer[i2 - 1] << 8)) == crc16.CRC;
                                        break;
                                    }
                                    if (flag1)
                                    {
                                        response = new byte[buffer[1]];
                                        Array.Copy(buffer, 4, response, 0, response.Length);
                                    }
                                }
                            }
                        }
                        TimeSpan timeSpan2 = performanceTimer.TimeSpan;
                        if (timeSpan2.TotalMilliseconds > (double)timeOut)
                        {
                            flag2 = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("", ex);
                    }
                }
            }
            response = new byte[buffer[1]];
            Array.Copy(buffer, 4, response, 0, response.Length);
            return(flag1);
        }