コード例 #1
0
        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);
        }
コード例 #2
0
        public bool SendCCTalkCommand(byte srcAddress, byte destAddress, byte commandHeader, bool noParamBytes, byte[] paramBuffer, out bool noRxDataBytes, out byte[] rxDataBuffer, out string commsStatus, bool encrypted, bool useCRCChecksum, byte[] secArray)
        {
            bool flag1 = false, flag2 = false;
            int  i1 = 0, i2 = 0;

            byte[] bArr1 = new byte[256];
            noRxDataBytes = true;
            rxDataBuffer  = null;
            commsStatus   = "";
            byte[] bArr2 = null;
            byte[] bArr3 = new byte[256];
            byte[] bArr4 = null;
            try
            {
                if (_PortIsDisapiered)
                {
                    if (CheckIfPortExists())
                    {
                        _PortIsDisapiered = false;
                    }
                    else
                    {
                        return(false);
                    }
                }
                // _logger.Debug("commandHeader:" + commandHeader.ToString());
                lock (readWriteLock)
                {
                    if (noParamBytes)
                    {
                        bArr2 = new byte[5];
                    }
                    else
                    {
                        bArr2 = new byte[(5 + paramBuffer.Length)];
                    }
                    bArr2[0] = destAddress;
                    if (noParamBytes)
                    {
                        bArr2[1] = 0;
                    }
                    else
                    {
                        bArr2[1] = (byte)paramBuffer.Length;
                    }
                    bArr2[2] = srcAddress;
                    bArr2[3] = commandHeader;
                    if (!noParamBytes)
                    {
                        for (int i3 = 0; i3 < paramBuffer.Length; i3++)
                        {
                            bArr2[i3 + 4] = paramBuffer[i3];
                        }
                        bArr2[3 + paramBuffer.Length + 1] = 0;
                    }
                    else
                    {
                        bArr2[4] = 0;
                    }
                    if (encrypted)
                    {
                        // byte[] bArr5 = new byte[(bArr2.Length - 2)];

                        //new added
                        int leng1           = 5;
                        int parambufferleng = 0;
                        if (paramBuffer != null)
                        {
                            parambufferleng = paramBuffer.Length;
                        }
                        leng1 = 5 + parambufferleng;
                        CCTCrc16 crc16 = new CCTCrc16();
                        crc16.Reset();
                        crc16.PushByte(bArr2[0]);
                        crc16.PushByte(bArr2[1]);
                        crc16.PushData(bArr2, 3, leng1 - 4);
                        bArr2[2] = (byte)(crc16.CRC & 255);

                        if (noParamBytes)
                        {
                            bArr2[4] = (byte)(crc16.CRC >> 8);
                        }
                        else if (4 + parambufferleng + 1 <= bArr2.Length)
                        {
                            bArr2[4 + parambufferleng] = (byte)(crc16.CRC >> 8);
                        }
                        else
                        {
                            Log.Error("CRC16-Encryption-Error. Buffer:" + bArr2.ToString());
                            flag1 = false; flag2 = false;
                            return(false);
                        }

                        // CalculateCRCChecksum(ref bArr2);
                        // CalculateSimpleChecksumForSend(ref bArr2);
                        //for (int i4 = 0; i4 < bArr5.Length; i4++)
                        //{
                        //    bArr5[i4] = bArr2[i4 + 2];
                        //}

                        //int leng = 5;
                        //if (paramBuffer != null)
                        //{
                        //    leng = 5 + paramBuffer.Length;
                        //}
                        CCTCrypt.Encrypt(bArr2, 2, bArr2.Length - 2, secArray);
                        // CCTCrypt.Encrypt(bArr2, 2, leng - 2, secArray);
                        //EncryptCcTalk(ref bArr5);
                        //for (int i5 = 0; i5 < bArr5.Length; i5++)
                        //{
                        //    bArr2[i5 + 2] = bArr5[i5];
                        //}
                        // bArr5 = null;
                    }
                    else if (useCRCChecksum)
                    {
                        CalculateCRCChecksum(ref bArr2);
                    }
                    else
                    {
                        CalculateSimpleChecksumForSend(ref bArr2);
                    }

                    try
                    {
                        if (SerPort != null)
                        {
                            SerPort.DiscardInBuffer();
                            SerPort.Write(bArr2, 0, bArr2.Length);
                            if ((commandHeader == 253) || (commandHeader == 252))
                            {
                                Thread.Sleep(1500);
                            }
                            else
                            {
                                Thread.Sleep(100);
                            }

                            i1 = i2 = 0;
                            while (SerPort.BytesToRead != 0)
                            {
                                byte readbyte = (byte)SerPort.ReadByte();
                                if (i1 < bArr1.Length)
                                {
                                    bArr1[i1] = readbyte;
                                    if (i1 >= bArr2.Length)
                                    {
                                        bArr3[i2] = bArr1[i1];
                                        flag2     = true;
                                        i2++;
                                    }
                                }
                                else
                                {
                                    Log.Error("Resultbuffer <  bytes read from comPort.");
                                }
                                i1++;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error("", e);
                        _PortIsDisapiered = !CheckIfPortExists();
                        return(false);
                    }


                    if (flag2)
                    {
                        flag1 = true;
                        if ((commandHeader == 253) || (commandHeader == 252))
                        {
                            noRxDataBytes = false;
                            rxDataBuffer  = new byte[i2];
                            for (int i6 = 0; i6 < i2; i6++)
                            {
                                rxDataBuffer[i6] = bArr3[i6];
                            }
                        }
                        else
                        {
                            bArr4 = new byte[i2];
                            for (int i7 = 0; i7 < i2; i7++)
                            {
                                bArr4[i7] = bArr3[i7];
                            }
                            bool flag3 = false;
                            if (encrypted)
                            {
                                byte[] bArr6 = new byte[(bArr4.Length - 2)];
                                for (int i8 = 0; i8 < bArr6.Length; i8++)
                                {
                                    bArr6[i8] = bArr4[i8 + 2];
                                }
                                //DecryptCcTalk(ref bArr6);
                                //for (int i9 = 0; i9 < bArr6.Length; i9++)
                                //{
                                //    bArr4[i9 + 2] = bArr6[i9];
                                //}
                                bArr6 = null;
                                CCTCrypt.Decrypt(bArr4, 2, bArr4.Length - 2, secArray);
                                // CCTCrypt.Decrypt(bArr4, 2, i2 - 2, secArray);
                                crc16.Reset();
                                crc16.PushByte(bArr4[0]);
                                crc16.PushByte(bArr4[1]);
                                crc16.PushData(bArr4, 3, i2 - 4);
                                flag3 = (bArr4[2] + (bArr4[i2 - 1] << 8)) == crc16.CRC;
                                // flag3 = CheckCRCChecksum(bArr4);
                                // flag3 = CheckSimpleChecksumForReceive(bArr4);
                            }
                            else if (useCRCChecksum)
                            {
                                flag3 = CheckCRCChecksum(bArr4);
                            }
                            else
                            {
                                flag3 = CheckSimpleChecksumForReceive(bArr4);
                            }

                            if (!flag3)
                            {
                                commsStatus = "Checksum Error at Receive";
                                flag1       = false;
                            }
                            else if (bArr4[1] != 0)
                            {
                                noRxDataBytes = false;
                                rxDataBuffer  = new byte[bArr4[1]];
                                for (int i10 = 0; i10 < bArr4[1]; i10++)
                                {
                                    rxDataBuffer[i10] = bArr4[i10 + 4];
                                }
                            }
                        }
                    }
                    else
                    {
                        commsStatus = "No Data received";
                        flag1       = false;
                    }
                }
            }
            catch (Exception Ex)
            {
                Log.Error("In SendCCTalkCommand.", Ex);
                _PortIsDisapiered = !CheckIfPortExists();
                return(false);
            }
            return(flag1);
        }