private CCTCommunicationA()
        {
            Log.Debug("Starting CCTCommunicationA ...");
            tapArray      = new byte[] { 7, 4, 5, 3, 1, 2, 3, 2, 6, 1 };
            secArray      = new byte[6];
            emptyBuffer   = new byte[] { };
            buffer        = new byte[256];
            crc8          = new CCTCrc8();
            crc16         = new CCTCrc16();
            _baudrate     = 9600;
            readWriteLock = new Object();

            // ReadComPort();
            InitSerialPort();
        }
        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);
        }