Пример #1
0
        private void parseComRef(deviceEntry entry)
        {
            char tmp = (char)(mbReadBuf[0] >> 8);// MSB

            entry.comRef  = tmp.ToString();
            tmp           = (char)(mbReadBuf[0] & 0x00FF); //LSB
            entry.comRef += tmp.ToString();


            tmp           = (char)(mbReadBuf[1] >> 8);       // MSB
            entry.comRef += tmp.ToString();
            tmp           = (char)((mbReadBuf[1]) & 0x00FF); // LSB
            entry.comRef += tmp.ToString();


            tmp           = (char)(mbReadBuf[2] >> 8);       // MSB
            entry.comRef += tmp.ToString();
            tmp           = (char)((mbReadBuf[2]) & 0x00FF); // LSB
            entry.comRef += tmp.ToString();


            tmp           = (char)(mbReadBuf[3] >> 8);       // MSB
            entry.comRef += tmp.ToString();
            tmp           = (char)((mbReadBuf[3]) & 0x00FF); // LSB
            entry.comRef += tmp.ToString();


            tmp           = (char)(mbReadBuf[4] >> 8);       // MSB
            entry.comRef += tmp.ToString();
            tmp           = (char)((mbReadBuf[4]) & 0x00FF); // LSB
            entry.comRef += tmp.ToString();
        }
Пример #2
0
        public int getDeviceInformation(deviceEntry devEntry, string deviceIP)
        {
            int             retCode   = -1;
            modbusSlaveInfo slaveInfo = new modbusSlaveInfo();

            slaveInfo.slaveIPAdd = deviceIP;
            slaveInfo.slaveUid   = 1;
            slaveInfo.tcpPortId  = 502;

            mbAccess.setSlaveInfo(slaveInfo);

            if (mbAccess.readModbusRegister(devComRefAddress, devComRefNReg, mbReadBuf) == 0)
            {
                parseComRef(devEntry);
                if (mbAccess.readModbusRegister(controllerFWAddress, controllerFWNReg, mbReadBuf) == 0)
                {
                    parsekuVer(devEntry);
                }
                if (mbAccess.readModbusRegister(networkFWAddress, networkFWNReg, mbReadBuf) == 0)
                {
                    parsekcVer(devEntry);
                    retCode = 0;
                }
            }
            return(retCode);
        }
Пример #3
0
        private void parsekuVer(deviceEntry entry)
        {
            UInt16 regVal = mbReadBuf[0];
            int    major  = regVal / 10000;
            int    minor  = regVal % 10000;
            int    rev    = minor % 1000;
            string revStr;

            if (rev > 99)
            {
                revStr = rev.ToString();
            }
            else if (rev > 9)
            {
                revStr = "0" + rev.ToString();
            }
            else
            {
                revStr = "00" + rev.ToString();
            }

            minor /= 1000;

            entry.kuVer = major.ToString() + "." + minor.ToString() + "." + revStr;
        }
Пример #4
0
        public void startUpgradeTask(deviceEntry[] entry, int listSize, upgradeFileInfo uFileInfo)
        {
            deviceListSize = listSize;
            denty          = new deviceEntry[listSize];
            denty          = entry;
            semaUpdate     = new Semaphore(8, 8);
            deviceEntry dEntry = new deviceEntry();

            srcFileName = uFileInfo.srcFileName;
            srcFilePath = uFileInfo.srcFilePath;
            Thread myth;

            numberOfActiveUgradeTask = listSize;
            Thread uprgdTaskTrak = new Thread(uprgdFinishTaskTrack);

            uprgdTaskTrak.Start();
            int k = 0;

            for (k = 0; k < listSize; k++)
            {
                dEntry = denty[k];
                myth   = new Thread(() => updateThread(dEntry));
                myth.Start();
                Thread.Sleep(1000);
            }
        }
Пример #5
0
        /* upgrade button click*/
        private void btUpgrade_Click(object sender, RoutedEventArgs e)
        {
            int nDevice = grdDeviceList.SelectedItems.Count;

            deviceEntry[] entry = new deviceEntry[nDevice];
            try
            {
                if (nDevice < 1)
                {
                    MessageBox.Show("Please select at least one device to be upgraded ");
                    return;
                }
                for (int i = 0; i < nDevice; i++)
                {
                    entry[i] = (deviceEntry)grdDeviceList.SelectedItems[i];
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            Upgrade updateWin = new Upgrade(ref entry, nDevice);

            updateWin.Show();
        }
Пример #6
0
 public backupNRestoreProgressInfo()
 {
     pbMax  = 0;
     pbMin  = 0;
     pbVal  = 0xFFFF;
     actOn  = actionOnControl.NO_ACTION;
     dEntry = new deviceEntry();
 }
Пример #7
0
 public scanInfo()
 {
     pvMax      = 0;
     pvMin      = 0;
     pvVal      = 0;
     IPAddress  = " ";
     deviceInfo = new deviceEntry();
     actOn      = actionOnControl.PROGRESS_BAR;
 }
Пример #8
0
 public updateInfo()
 {
     fileName        = " ";
     filePath        = " ";
     serverIpAddress = " ";
     winControlIndex = 0xFF;
     pbMax           = 0;
     pbMin           = 0;
     pbVal           = 0xFFFF;
     actOn           = actionOnControl.NO_ACTION;
     dEntry          = new deviceEntry();
 }
Пример #9
0
        public int scanNetwork(deviceEntry[] deviceList, tcpNIpInfoStruct portInfo)
        {
            scanInfo sInfo = new scanInfo();

            int    retCode  = -1;
            string serverIP = portInfo.serverIP;

            sInfo.pvMax = portInfo.nDevice;
            sInfo.pvMin = 0;
            sInfo.pvVal = 0;
            sInfo.actOn = actionOnControl.PROGRESS_BAR;
            try
            {
                callBack(sInfo);
                for (int i = 0; i < portInfo.nDevice; i++)
                {
                    deviceEntry entry = new deviceEntry();
                    sInfo.IPAddress = serverIP;
                    sInfo.actOn     = actionOnControl.LEBEL;
                    callBack(sInfo);

                    if (devIdObj.getDeviceInformation(entry, serverIP) == 0)
                    {
                        // update data grid only if device available in the network else look for next device
                        entry.deviceIP   = serverIP;
                        deviceList[i]    = entry;
                        sInfo.deviceInfo = entry;
                        sInfo.actOn      = actionOnControl.DATA_GRID;
                        callBack(sInfo);
                    }
                    long IPLong = utility.IP2LongEndianLess(serverIP);
                    IPLong++; // go for next IP address
                    serverIP = utility.LongToIP(IPLong);

                    // update only progress bar
                    sInfo.pvVal++;
                    sInfo.actOn = actionOnControl.PROGRESS_BAR;
                    callBack(sInfo);
                }//For Loop
                sInfo.actOn = actionOnControl.PROCESS_COMPLETED;
                callBack(sInfo);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return(retCode);
        }
Пример #10
0
 public void addDeviceToList(deviceEntry[] dList, int nDevice)
 {
     if (dList == null || nDevice == 0)
     {
         return;
     }
     if (deviceList == null)
     {
         deviceList = new deviceEntry[nDevice];
     }
     for (int i = 0; i < nDevice; i++)
     {
         deviceList[i] = dList[i];
     }
 }
Пример #11
0
        private void btTestProgrssBar_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                deviceEntry entry = new deviceEntry();
                entry.comRef   = "LTMR08EBD";
                entry.deviceIP = "122.122.122." + index.ToString();
                entry.kcVer    = "4.5.0";
                entry.kuVer    = "3.7.0";

                entry.kcVer = "4.5.0" + index.ToString();
                entry.kuVer = "3.7.0" + index.ToString();
                grdDeviceList.Items.Add(entry);
                Console.WriteLine(index);
                index++;
            }
        }
Пример #12
0
 public StateMachine(scanProgressBar_Callback cb, dgrUpdateListRead_CallBack ucb, callback_ProgressBar cpb)
 {
     updateThread       = new Thread(new ThreadStart(updateTask));
     t6tAccess          = new TeSysTMbAccess(cb);
     ftpupdgr           = new upgradeTask(cpb);
     toolState          = TOOLSTATE.UNINIT;
     upgrade_Callback   = ucb;
     utility            = new ftpMassUpgrade.toolUitility();
     clientPort         = 0;
     serverPort         = 0;
     noOfDevice         = 0;
     deviceList         = new deviceEntry[250];
     upgradedDeviceList = new deviceEntry[250];
     tmpEntry           = new deviceEntry();
     btScanDisabled     = false;
     portInfo           = new tcpNIpInfoStruct();
     fileInfo           = new upgradeFileInfo();
 }
Пример #13
0
        private LTRM_ComercialRef getDeviceComRef(deviceEntry device)
        {
            LTRM_ComercialRef cRef = LTRM_ComercialRef.NONE;

            string cRatings = device.comRef.Substring(4, 2);

            if (cRatings == "08")
            {
                cRef = LTRM_ComercialRef.LTMR_08;
            }
            else if (cRatings == "27")
            {
                cRef = LTRM_ComercialRef.LTMR_27;
            }
            else if (cRatings == "10")//100
            {
                cRef = LTRM_ComercialRef.LTMR_100;
            }

            return(cRef);
        }
Пример #14
0
        // public void updateThread(int deviceId)
        public void updateThread(deviceEntry dEntry)
        {
            updateInfo uInfo = new updateInfo();

            Console.WriteLine("upgrade task invoked and waiting from sema");
            semaUpdate.WaitOne();
            int i = 0;

            lock (thislock)
            {
                for (i = 0; i < 8; i++)
                {
                    if (info[i].winControlIndex == 0xFF)
                    {
                        uInfo = info[i];
                        break;
                    }
                }
                if (i < 8)
                {
                    uInfo.winControlIndex = i;
                }
            }
            if (i > 8) // somthing is worng in memory allocation process did't able get windows resources
            {
                return;
            }
            uInfo.serverIpAddress = dEntry.deviceIP;
            uInfo.fileName        = srcFileName;
            uInfo.filePath        = srcFilePath;
            int rTry = 0;

            do
            {   // send file to device and retry for 3 time in case fail
                if (PostDatatoDevice(uInfo) == -1)
                // if (dumy(uInfo) == -1)
                {
                    uInfo.actOn = actionOnControl.PROGRESS_BAR;
                    uInfo.pbVal = 0;
                    cbPb(uInfo);
                    rTry++;
                    Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }while(rTry < 3);
            // Successful only within 3 time retry
            if (rTry < 3)
            {
                dEntry.upgradeStatus = "Successful";
            }
            else
            {
                //denty[deviceId].upgradeStatus = "Failed";
                dEntry.upgradeStatus = "Failed";
            }
            uInfo.actOn  = actionOnControl.DATA_GRID;
            uInfo.dEntry = dEntry;
            cbPb(uInfo);
            lock (thislock)
            {
                info[i].winControlIndex = 0xFF;
            }

            numberOfActiveUgradeTask--;
            // release the semaphore for other task
            semaUpdate.Release();
        }
Пример #15
0
        public int scanNetwork(deviceEntry[] deviceList, tcpNIpInfoStruct portInfo)
        {
            scanInfo sInfo = new scanInfo();

            int    retCode  = -1;
            string serverIP = portInfo.serverIP;

            sInfo.pvMax = portInfo.nDevice;
            sInfo.pvMin = 0;
            sInfo.pvVal = 0;
            sInfo.actOn = actionOnControl.PROGRESS_BAR;
            try
            {
                callBack(sInfo);
                for (int i = 0; i < portInfo.nDevice; i++)
                {
                    deviceEntry entry = new deviceEntry();
                    sInfo.IPAddress = serverIP;
                    sInfo.actOn     = actionOnControl.LEBEL;
                    callBack(sInfo);
                    if (mbClient.modbusInit(portInfo.clientIP, (ushort)portInfo.clientPort) == 0)
                    {
                        if (mbClient.mbOpen(serverIP, (ushort)portInfo.serverPort) == 0)
                        {
                            if (mbClient.mbRead(64, 5, mbReadBuf) == 0)
                            {
                                parseComRef(entry);
                                retCode = 0;
                            }
                            if (mbClient.mbRead(76, 1, mbReadBuf) == 0)
                            {
                                parsekuVer(entry);
                                retCode = 0;
                            }
                            if (mbClient.mbRead(62, 1, mbReadBuf) == 0)
                            {
                                parsekcVer(entry);
                                retCode = 0;
                            }
                            mbClient.mbClose();
                            entry.deviceIP = serverIP;
                        }
                    }
                    long IPLong = utility.IP2LongEndianLess(serverIP);
                    IPLong++; // go for next IP address
                    serverIP = utility.LongToIP(IPLong);

                    sInfo.pvVal++;
                    sInfo.actOn = actionOnControl.PROGRESS_BAR;
                    callBack(sInfo);
                    //update status only in device available
                    if (retCode == 0)
                    {
                        deviceList[i]    = entry;
                        sInfo.deviceInfo = entry;
                        sInfo.actOn      = actionOnControl.DATA_GRID;
                        callBack(sInfo);
                    }
                    retCode = -1;
                }//For Loop
                deviceEntry lastEntry = new deviceEntry();
                sInfo.deviceInfo = lastEntry;
                sInfo.actOn      = actionOnControl.PROCESS_COMPLETED;
                callBack(sInfo);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                mbClient.mbClose();
            }

            return(retCode);
        }
Пример #16
0
 public NetworkScan(scanProgressBar_Callback cb)
 {
     t6tAccess  = new TeSysTMbAccess(cb);
     deviceList = new deviceEntry[MAX_DEVICE_SUPPORTED];
     portInfo   = new tcpNIpInfoStruct();
 }
Пример #17
0
 public deviceIdentification()
 {
     mbReadBuf  = new ushort[120];
     deviceInfo = new deviceEntry();
     mbAccess   = new ModbusRegisterAccess();
 }
Пример #18
0
        /* Read LTMR configuration including host configuration register and
         * custom logic file over Modbus serial line(HMI Port) */
        private void readLTMRConfiguration(deviceEntry dEntry)
        {
            ushort modbusStartAddress;
            ushort numberofRegister;

            byte[]          data     = new byte[512];
            UInt16[]        regVal   = new UInt16[120];
            int             index    = 0;
            confFileFormate confData = new confFileFormate();

            //Register Address 540-599, 600-699,800-898,1250-1269,3000-3087,3088-3119)
            confFileHeader bHeader = new confFileHeader();

            bHeader.comRef  = dEntry.comRef;
            bHeader.kcFwVer = dEntry.kcVer;
            bHeader.kuFwVer = dEntry.kuVer;

            modbusSlaveInfo sInfo = new modbusSlaveInfo();

            sInfo.slaveIPAdd = dEntry.deviceIP;
            sInfo.slaveUid   = 1;
            sInfo.tcpPortId  = 502;
            mbAccess.setSlaveInfo(sInfo);

            mbAccess.readModbusRegister(70, 5, regVal);

            bHeader.serialNumber = regVal[0].ToString() + regVal[1].ToString() + regVal[2].ToString() + regVal[3].ToString() + regVal[4].ToString();
            // confData.fileHeader = bHeader;
            confData.fileData = new confData[20480];

            modbusStartAddress = 540;
            numberofRegister   = 60;

            if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) == 0)
            {
                for (int i = 0; i < numberofRegister; i++)
                {
                    confData.fileData[i].regAdd = (ushort)(modbusStartAddress + i);
                    confData.fileData[i].regVal = (ushort)(regVal[i]);
                }

                index = index + numberofRegister;
                modbusStartAddress = 600;
                numberofRegister   = 100;
                //if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) == 0)
                {
                    //confFile.Write("  Main Setting     " + " \n");
                    for (int i = 0; i < numberofRegister; i++)
                    {
                        confData.fileData[index + i].regAdd = (ushort)(modbusStartAddress + i);
                        confData.fileData[index + i].regVal = (ushort)(regVal[i]);
                    }

                    index = index + numberofRegister;
                    modbusStartAddress = 800;
                    numberofRegister   = 99;
                    //if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                    if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) == 0)
                    {
                        for (int i = 0; i < numberofRegister; i++)
                        {
                            confData.fileData[index + i].regAdd = (ushort)(modbusStartAddress + i);
                            confData.fileData[index + i].regVal = (ushort)(regVal[i]);
                        }
                        index = index + numberofRegister;
                        modbusStartAddress = 1250;
                        numberofRegister   = 20;
                        //if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                        if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) == 0)
                        {
                            for (int i = 0; i < numberofRegister; i++)
                            {
                                confData.fileData[index + i].regAdd = (ushort)(modbusStartAddress + i);
                                confData.fileData[index + i].regVal = (ushort)(regVal[i]);
                            }
                            index = index + numberofRegister;
                            modbusStartAddress = 3000;
                            numberofRegister   = 88;
                            // if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                            if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) == 0)
                            {
                                for (int i = 0; i < numberofRegister; i++)
                                {
                                    confData.fileData[index + i].regAdd = (ushort)(modbusStartAddress + i);
                                    confData.fileData[index + i].regVal = (ushort)(regVal[i]);
                                }
                                index = index + numberofRegister;
                                modbusStartAddress = 3088;
                                numberofRegister   = 32;
                                // if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                                if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) == 0)
                                {
                                    for (int i = 0; i < numberofRegister; i++)
                                    {
                                        confData.fileData[index + i].regAdd = (ushort)(modbusStartAddress + i);
                                        confData.fileData[index + i].regVal = (ushort)(regVal[i]);
                                    }
                                    index = index + numberofRegister;
                                }
                            }
                        }
                    }
                }
            }
            bHeader.confRegLen       = index;
            bHeader.customStartIndex = index;
            confData.fileHeader      = bHeader;

            /* check Operating mode before starting uploading custom logic file*/
            //((modbusStartAddress, numberofRegister, regVal) == 0)
            if (mbAccess.readModbusRegister(540, 1, regVal) == 0)
            {
                /* Custom logic file will present if Reg(540)>255 else no custom logic file hance ignore reading it*/
                if (regVal[0] > 255)
                {
                    bHeader.customRegLen = readLTMRLogicFile(ref confData);
                }
            }

            confData.fileHeader = bHeader;
            string confFileName = dEntry.deviceIP.Replace('.', '_');

            confFileName = confFileName + "_conf.csv";
            writeConfFile(confData, confFileName);
            //MessageBox.Show("backup Completed");
        }
Пример #19
0
        private void btStart_Click(object sender, RoutedEventArgs e)
        {
            int selectedLst = grdBnRDeviceList.SelectedItems.Count;

            deviceEntry[] dEntry         = new deviceEntry[selectedLst];
            bool          isFileSelected = false;

            try
            {
                if (selectedLst < 1)
                {
                    MessageBox.Show("Please select at leat one device to be Backed-up or Restored");
                    return;
                }


                for (int i = 0; i < selectedLst; i++)
                {
                    dEntry[i] = (deviceEntry)(grdBnRDeviceList.SelectedItems[i]);
                }
            }
            catch (excepetion ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (rdBackup.IsChecked == true)
            {
                ltmrBnR.backupDeviceConfiguration(dEntry, selectedLst);
            }
            else if (rdRestore.IsChecked == true)
            {
                TeSysTConfiFile confFiles = new TeSysTConfiFile();
                if (txtRef100Amp.Text != "")
                {
                    confFiles.fileName100Amp = txtRef100Amp.Text.ToString();
                    isFileSelected           = true;
                }
                else
                {
                    confFiles.fileName100Amp = "";
                }

                if (txtRef27Amp.Text != "")
                {
                    confFiles.fileName27Amp = txtRef27Amp.Text.ToString();
                    isFileSelected          = true;
                }
                else
                {
                    confFiles.fileName27Amp = "";
                }
                if (txtRef8Amp.Text != "")
                {
                    confFiles.fileName8Amp = txtRef8Amp.Text.ToString();
                    isFileSelected         = true;
                }
                else
                {
                    confFiles.fileName8Amp = "";
                }


                if (isFileSelected == true)
                {
                    ltmrBnR.restoreDeviceConfiguration(dEntry, selectedLst, confFiles);
                }
                else
                {
                    MessageBox.Show("please select at least one configuration file");
                }
            }
        }
Пример #20
0
        ///* read custom logic file from LTMR*/
        //private void readLTMRLogicFile()
        //{
        //    byte tokenId = 0;
        //    int pageId = 0;
        //    int wordRemain = 0;
        //    int logicFileSize = 0;
        //    UInt16[] regVal = new UInt16[32];
        //    byte[] data = new byte[512];
        //    int failCode = 0;
        //    int isInConfigMode = 0;
        //    StreamWriter fWriter = null;
        //    /* enter in config mode before starting custom logic file access it is mandatory requirment */
        //    //if (comm.Read(ViewModelConstants.NodeNumber, 601, 1, ref regVal) == true)
        //    if (mbAccess.readModbusRegister( 601, 1, regVal) > 0)
        //    {
        //        isInConfigMode = regVal[0] & 0x0001;
        //        if (isInConfigMode == 0)
        //        {
        //            regVal[0] |= 0x0001;
        //            //if (comm.Write(ViewModelConstants.NodeNumber, 601, 1, regVal) == false)
        //            if (mbAccess.mbWrite(601, 1, regVal) == -1)
        //            {
        //                MessageBox.Show("logic File Back-up failed");
        //                failCode = -1;
        //            }
        //        }
        //    }
        //    else
        //    {
        //        failCode = -1;
        //    }
        //    /* get  the custom logic file size from device. which to be uploaded*/
        //    //if (comm.Read(ViewModelConstants.NodeNumber, 1203, 1, ref regVal) == true)
        //    if (mbAccess.mbRead(1203, 1, regVal) > 0)
        //    {
        //        logicFileSize = regVal[0];
        //        wordRemain = logicFileSize + 1;// 1 byte added for len of file
        //        if (logicFileSize > 0)
        //        {
        //            /* get  the token to access the logic file available in device. */
        //            //if (comm.Read(ViewModelConstants.NodeNumber, 1209, 1, ref regVal) == true)
        //            if (myClient.mbRead(1209, 1, regVal) > 0)
        //            {
        //                tokenId = (byte)(regVal[0] / 256); // taken is only MSB
        //            }
        //            else
        //            {
        //                failCode = -1;
        //            }
        //        }
        //        else
        //        {
        //            failCode = -1;
        //            MessageBox.Show("Logic File Size is zero");
        //        }
        //    }
        //    if (tokenId > 0 && failCode == 0)
        //    {

        //        try
        //        {
        //            fWriter = new StreamWriter("logicFile.csv");
        //        }
        //        catch (FileLoadException e)
        //        {
        //            MessageBox.Show(" Fail to open File -- may be used by other application ");
        //        }
        //        while (wordRemain > 0)
        //        {


        //            /* Tell device to want access logical memory section (Custom logic) */
        //            regVal[0] = 1;
        //            //if (comm.Write(ViewModelConstants.NodeNumber, 1207, 1, regVal) == true)
        //            if (myClient.mbWrite(1207, 1, regVal) == 0)
        //            {
        //                /* Tell device the page number wants to read */
        //                regVal[0] = (ushort)pageId;
        //                //if (comm.Write(ViewModelConstants.NodeNumber, 1208, 1, regVal) == true)
        //                if (myClient.mbWrite(1208, 1, regVal) == 0)
        //                {
        //                    int cmd = tokenId * 256; // token in MSB
        //                    cmd += 1; //read command
        //                    regVal[0] = (ushort)cmd;

        //                    /*Send command to device telling READ with provided token*/
        //                    //if (comm.Write(ViewModelConstants.NodeNumber, 1209, 1, regVal) == true)
        //                    if (myClient.mbWrite(1209, 1, regVal) == 0)

        //                    {
        //                        int nReg = (wordRemain > 16) ? 16 : wordRemain;

        //                        /* read custom logic file of Max size 16 register/Word of asked pageId */
        //                        // if (comm.Read(ViewModelConstants.NodeNumber, 1210, (ushort)nReg, ref regVal) == true)
        //                        if (myClient.mbRead(1210, (ushort)nReg, regVal) > 0)
        //                        {
        //                            /* incereament tokenId by one every time for new request*/
        //                            tokenId++;
        //                            /* incereament pageId by Max Page size every time for new page request*/
        //                            pageId += 16;
        //                            wordRemain -= nReg;
        //                            /* dump this page in file and go for next pageId*/
        //                            for (int i = 0; i < nReg; i++)
        //                            {
        //                                fWriter.WriteLine(regVal[i].ToString());
        //                            }
        //                        }
        //                        if (wordRemain <= 0)
        //                        {
        //                            /* Release the token after uploading complete logic file */
        //                            cmd = tokenId * 256; // token in MSB
        //                            cmd += 5; //release token command
        //                            regVal[0] = (ushort)cmd;
        //                            //if (comm.Write(ViewModelConstants.NodeNumber, 1209, 1, regVal) == true)
        //                            if (myClient.mbWrite(1209, 1, regVal) == 0)
        //                            {
        //                                MessageBox.Show("Logical file backup successful ");
        //                            }

        //                        }

        //                    }
        //                    else
        //                    {
        //                        failCode = -1;
        //                    }
        //                }
        //                else
        //                {
        //                    failCode = -1;
        //                }
        //            }
        //            else
        //            {
        //                failCode = -1;
        //            }
        //        }//while
        //        fWriter.Close();
        //        if (failCode == -1)
        //        {
        //            MessageBox.Show("logical file Backup failed");
        //        }
        //    }

        //    /* Exit from config mode if enter in this fuction else nothing to do */
        //    if (isInConfigMode == 0)
        //    {
        //        //if (comm.Read(ViewModelConstants.NodeNumber, 601, 1, ref regVal) == true)
        //        if (myClient.mbRead(601, 1, regVal) > 0)
        //        {
        //            regVal[0] &= 0xFFFE;
        //            //if (comm.Write(ViewModelConstants.NodeNumber, 601, 1, regVal) == false)
        //            if (myClient.mbWrite(601, 1, regVal) == -1)
        //            {
        //                MessageBox.Show("Failed to exit from config mode");
        //            }

        //        }
        //    }
        //}

        /* Read LTMR configuration including host configuration register and
         * custom logic file over Modbus serial line(HMI Port) */
        private void readLTMRConfiguration(deviceEntry dEntry)
        {
            ushort modbusStartAddress;
            ushort numberofRegister;

            byte[]   data   = new byte[512];
            UInt16[] regVal = new UInt16[120];
            int      index  = 0;


            //Register Address 540-599, 600-699,800-898,1250-1269,3000-3087,3088-3119)
            backupFileHeader bHeader = new backupFileHeader();

            bHeader.comRef  = dEntry.comRef;
            bHeader.kcFwVer = dEntry.kcVer;
            bHeader.kuFwVer = dEntry.kuVer;

            modbusSlaveInfo sInfo = new modbusSlaveInfo();

            sInfo.slaveIPAdd = dEntry.deviceIP;
            sInfo.slaveUid   = 1;
            sInfo.tcpPortId  = 502;
            mbAccess.setSlaveInfo(sInfo);

            mbAccess.readModbusRegister(70, 5, regVal);

            bHeader.serialNumber = regVal[0].ToString() + regVal[1].ToString() + regVal[2].ToString() + regVal[3].ToString() + regVal[4].ToString();

            modbusStartAddress = 540;
            numberofRegister   = 60;
            //if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
            if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) > 0)
            {
                for (int i = 0; i < numberofRegister; i++)
                {
                    Config8Amp[i].regAdd = (ushort)(modbusStartAddress + i);
                    Config8Amp[i].regVal = (ushort)(regVal[i]);
                }

                index = index + numberofRegister;
                modbusStartAddress = 600;
                numberofRegister   = 100;
                //if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) > 0)
                {
                    //confFile.Write("  Main Setting     " + " \n");
                    for (int i = 0; i < numberofRegister; i++)
                    {
                        Config8Amp[index + i].regAdd = (ushort)(modbusStartAddress + i);
                        Config8Amp[index + i].regVal = (ushort)(regVal[i]);
                    }

                    index = index + numberofRegister;
                    modbusStartAddress = 800;
                    numberofRegister   = 99;
                    //if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                    if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) > 0)
                    {
                        for (int i = 0; i < numberofRegister; i++)
                        {
                            Config8Amp[index + i].regAdd = (ushort)(modbusStartAddress + i);
                            Config8Amp[index + i].regVal = (ushort)(regVal[i]);
                        }
                        index = index + numberofRegister;
                        modbusStartAddress = 1250;
                        numberofRegister   = 20;
                        //if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                        if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) > 0)
                        {
                            for (int i = 0; i < numberofRegister; i++)
                            {
                                Config8Amp[index + i].regAdd = (ushort)(modbusStartAddress + i);
                                Config8Amp[index + i].regVal = (ushort)(regVal[i]);
                            }
                            index = index + numberofRegister;
                            modbusStartAddress = 3000;
                            numberofRegister   = 88;
                            // if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                            if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) > 0)
                            {
                                for (int i = 0; i < numberofRegister; i++)
                                {
                                    Config8Amp[index + i].regAdd = (ushort)(modbusStartAddress + i);
                                    Config8Amp[index + i].regVal = (ushort)(regVal[i]);
                                }
                                index = index + numberofRegister;
                                modbusStartAddress = 3088;
                                numberofRegister   = 32;
                                // if (comm.Read(ViewModelConstants.NodeNumber, modbusStartAddress, numberofRegister, ref data) == true)
                                if (mbAccess.readModbusRegister(modbusStartAddress, numberofRegister, regVal) > 0)
                                {
                                    for (int i = 0; i < numberofRegister; i++)
                                    {
                                        Config8Amp[index + i].regAdd = (ushort)(modbusStartAddress + i);
                                        Config8Amp[index + i].regVal = (ushort)(regVal[i]);
                                    }
                                    index = index + numberofRegister;
                                }
                            }
                        }
                    }
                }
            }

            bHeader.confRegLen       = index;
            bHeader.customStartIndex = index;

            string confFileName = dEntry.deviceIP.Replace('.', '_');

            confFileName = confFileName + "conf.csv";
            StreamWriter confFile = new StreamWriter(confFileName);

            // First write Backup file header to file then data
            confFile.Write(" Comercial ref" + ";" + bHeader.comRef + " \n");
            confFile.Write(" Serial No" + ";" + bHeader.serialNumber + " \n");
            confFile.Write(" KU FW Version" + ";" + bHeader.kuFwVer + " \n");
            confFile.Write(" KC FW Version" + ";" + bHeader.kcFwVer + " \n");
            confFile.Write(" lenght of Conf" + ";" + bHeader.confRegLen.ToString() + " \n");
            confFile.Write(" StartAdd CL" + ";" + bHeader.customStartIndex.ToString() + " \n");
            confFile.Write(" lenght of CL" + ";" + bHeader.customRegLen.ToString() + " \n");

            //write configuration register to file
            for (int i = 0; i < bHeader.confRegLen; i++)
            {
                confFile.Write(Config8Amp[i].regAdd.ToString() + ";" + Config8Amp[i].regVal.ToString() + " \n");
            }
            confFile.Close();

            /* check Operating mode before starting uploading custom logic file*/
            //if (myClient.mbRead(540, 1, regVal) > 0)
            //{
            //    /* Custom logic file will present if Reg(540)>255 else no custom logic file hance egnor reading it*/
            //    if (regVal[0] > 255)
            //    {
            //        readLTMRLogicFile();
            //    }

            //}
            MessageBox.Show("backup Completed");
        }