public tclsUDSComms(string szAdapterDeviceName, IntPtr MDIFormHandle)
        {
            string szTransferBlockSize;

            mclsUDS            = new tclsUDS();
            mclsCommsInterface = new tclsCommsInterface(MDIFormHandle);
            int iTransferBlockSizeLegal;

            tclsIniParser mclsIniParser = new tclsIniParser(AppDomain.CurrentDomain.BaseDirectory + "Config\\MDAC ECUHost Calibration.INI");

            tclsErrlog.LogAppend("INI file opened...");

            try
            {
                szTransferBlockSize = mclsIniParser.GetSetting("Program", "TransferBlockSize");
                miTransferBlockSize = ConstantData.ISO15765.rs32SEG_TRANSFER_BLOCK_SIZE;

                for (iTransferBlockSizeLegal = 1; iTransferBlockSizeLegal <= 512; iTransferBlockSizeLegal *= 2)
                {
                    if (iTransferBlockSizeLegal == (int)Convert.ToInt16(szTransferBlockSize))
                    {
                        miTransferBlockSize = iTransferBlockSizeLegal;
                        break;
                    }
                }

                tclsErrlog.LogAppend("Transfer block size: " + miTransferBlockSize.ToString());
            }
            catch
            {
                miTransferBlockSize = ConstantData.ISO15765.rs32SEG_TRANSFER_BLOCK_SIZE;
            }


            maqiMeasureSubIDXPendingArray = new Queue <int> [ConstantData.BUFFERSIZES.u16UDS_MEASURE_RATE_COUNT];

            mau8InPacketBuffer   = new byte[ConstantData.BUFFERSIZES.u16UDSOU_BUFF_RXPAYLOAD_SIZE];
            mau8ChannelTXPayload = new byte[ConstantData.BUFFERSIZES.u16UDSOU_BUFF_TXPAYLOAD_SIZE];

            mstTransferPageCB.enChannelMode      = tenChannelMode.enChannelModeNone;
            mstTransferPageCB.u32StartAddress    = 0;
            mstTransferPageCB.u32EndAddress      = 0;
            mstTransferPageCB.iBlockSize         = miTransferBlockSize;
            mstTransferPageCB.u32BytesToTransfer = 0;
            miDDDIIDX       = 0;
            miResponseCount = 0;
            mboReplayActive = false;
            mstRPCResponse  = new tstRPCResponse();
            miCommsWaits    = 0;

            mboChannelActive = mclsCommsInterface.CommsTryConnect(this,
                                                                  AppDomain.CurrentDomain.BaseDirectory + "Config\\MDAC ECUHost Calibration.INI",
                                                                  ConstantData.BUFFERSIZES.u16UDSOU_BUFF_RXPAYLOAD_SIZE);

            tclsErrlog.LogAppend("Channel active: " + mboChannelActive.ToString());
        }
        public void enProcessRPCResponse(byte[] au8Data, int iRXBufferStart, int iRXBufferEnd, ref tstRPCResponse stRPCResponse)
        {
            List <Byte[]> lstResponse;
            List <UInt32> lstData;
            UInt32        u32Data;
            int           iMeasureDataIDX;
            int           iResponseIDX;
            bool          boAbortLoop = false;

            lstResponse = mclsISO15765.lstUnsegmentData(au8Data, iRXBufferStart, iRXBufferEnd);
            lstData     = new List <UInt32>();

            if (0 < lstResponse.Count)
            {
                //try
                //{
                for (iResponseIDX = 0; (iResponseIDX < lstResponse.Count) && (false == boAbortLoop); iResponseIDX++)
                {
                    byte[] au8Response = lstResponse[iResponseIDX];

                    switch (au8Response[2])
                    {
                    case 0x40 + ConstantData.UDS.ru8SID_RDBI:
                    {
                        UInt16 iReceivedMeasSID    = BitConverter.ToUInt16(au8Response, 3);
                        int    iReceivedMeasureIDX = (int)(iReceivedMeasSID & 0xff);

                        iMeasureDataIDX = 5;
                        lstData.Clear();

                        foreach (tstMeasurement stMeasurement in tclsASAM.mailstActiveMeasLists[iReceivedMeasureIDX])
                        {
                            switch (stMeasurement.iByteCount)
                            {
                            case 1:
                            {
                                u32Data = (byte)au8Response[iMeasureDataIDX];
                                break;
                            }

                            case 2:
                            {
                                u32Data = (UInt32)BitConverter.ToUInt16(au8Response, iMeasureDataIDX);
                                break;
                            }

                            case 4:
                            {
                                u32Data = BitConverter.ToUInt32(au8Response, iMeasureDataIDX);
                                break;
                            }

                            default:
                            {
                                u32Data = 0;
                                break;
                            }
                            }

                            lstData.Add(u32Data);
                            iMeasureDataIDX += stMeasurement.iByteCount;
                        }

                        Program.vMeasuresUpdate(lstData, iReceivedMeasureIDX);
                        stRPCResponse.enRPCResponse = tenProgramEvent.enRPCOK;
                        break;
                    }

                    case 0x40 + ConstantData.UDS.ru8SID_RMBA:
                    {
                        UInt32 u32TargetAddress       = (UInt32)(au8Response[3] + 0x100 * au8Response[4] + 0x10000 * au8Response[5] + 0x1000000 * au8Response[6]);
                        int    iResponseDataByteCount = (int)(0x100 * au8Response[0] + au8Response[1] - 6);
                        byte[] au8PageData            = new byte[iResponseDataByteCount]; //matthew check
                        bool   boTransferComplete;
                        bool   boUpdateOK;

                        Array.Copy(au8Response, 7, au8PageData, 0, au8PageData.Length);
                        boTransferComplete = Program.mAPP_clsUDPComms.boTransferCallBack(ref u32TargetAddress);

                        if (true == boTransferComplete)
                        {
                            tclsDataPage.vSetWorkingData(u32TargetAddress, au8PageData, false, true);
                            stRPCResponse.enRPCResponse = tenProgramEvent.enRPCUploadComplete;
                            boAbortLoop = true;
                        }
                        else
                        {
                            boUpdateOK = tclsDataPage.vSetWorkingData(u32TargetAddress, au8PageData, false, false);

                            if (true == boUpdateOK)
                            {
                                stRPCResponse.enRPCResponse = tenProgramEvent.enRPCUploadIncrement;
                                Program.vNotifyProgramEvent(tenProgramEvent.enProgramMessage, 0, "Uploading address: " + u32TargetAddress.ToString());
                            }
                        }
                        break;
                    }

                    case 0x40 + ConstantData.UDS.ru8SID_WMBA:
                    {
                        UInt32 u32TargetAddress = (UInt32)(au8Response[3] + 0x100 * au8Response[4] + 0x10000 * au8Response[5] + 0x1000000 * au8Response[6]);
                        bool   boTransferComplete;

                        boTransferComplete = Program.mAPP_clsUDPComms.boTransferCallBack(ref u32TargetAddress);

                        if (true == boTransferComplete)
                        {
                            stRPCResponse.enRPCResponse = tenProgramEvent.enRPCDownloadComplete;
                            boAbortLoop = true;
                        }
                        else
                        {
                            stRPCResponse.enRPCResponse = tenProgramEvent.enRPCDownloadIncrement;
                            Program.vNotifyProgramEvent(tenProgramEvent.enProgramMessage, 0, "Downloading address: " + u32TargetAddress.ToString());
                        }
                        break;
                    }

                    case 0x40 + ConstantData.UDS.ru8SID_RC:
                    {
                        UInt32 u32TargetAddress = 0;

                        if (ConstantData.UDS.ru8RCID_HighByte == au8Response[4])
                        {
                            switch (au8Response[5])
                            {
                            case ConstantData.UDS.ru8RCID_WorkNVMFreeze:
                            {
                                Program.mAPP_clsUDPComms.boTransferCallBack(ref u32TargetAddress);
                                break;
                            }

                            case ConstantData.UDS.ru8RCID_WorkNVMClear:
                            {
                                Program.mAPP_clsUDPComms.boTransferCallBack(ref u32TargetAddress);
                                break;
                            }

                            default:
                            {
                                break;
                            }
                            }
                        }
                        break;
                    }

                    case 0x40 + ConstantData.UDS.ru8SID_DDDI:
                    {
                        stRPCResponse.enRPCResponse = tenProgramEvent.enRPCDDDIOK;
                        stRPCResponse.u32RPCData1   = (UInt32)(0x100 * au8Response[5]);
                        stRPCResponse.u32RPCData1  += (UInt32)au8Response[4];
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
                //}
                //catch
                //{
                //    Program.vNotifyProgramEvent(tenProgramEvent.enProgramError, "RPC Response Exception");
                //}
            }
        }