private void GetChannelGroups()
        {
            foreach (var item in tabChannel.Items)
            {
                if (item is TabItem)
                {
                    TabItem ti = item as TabItem;
                    if (ti.Content is ucChannel)
                    {
                        ucChannel    uc = ti.Content as ucChannel;
                        ChannelGroup cg = channelGroups.First(x => x.Clock.ID == uc.ID);
                        uc.SetObj(cg);
                    }
                }
            }

            for (int i = 0; i < channelGroups.Count; i++)
            {
                for (int j = i + 1; j < channelGroups.Count; j++)
                {
                    var cgi = channelGroups[i];
                    var cgj = channelGroups[j];
                    if (cgi.VIO.ID == cgj.Clock.ID || cgi.VIO.ID == cgj.Data.ID || cgi.VIO.ID == cgj.VIO.ID)
                    {
                        throw new Exception("VIO has duplicated channel no. - " + cgi.VIO.ID + "!");
                    }
                }
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (var timingset in timingSets)
                {
                    ucTS uc = new ucTS();
                    uc.Set(timingset.ID);
                    stpTS.Children.Add(uc);
                }

                for (int i = 0; i < channelGroups.Count; i++)
                {
                    TabItem tabItem = new TabItem();
                    tabItem.Header = "Group" + (i + 1);
                    ucChannel uc = new ucChannel();
                    uc.Set(channelGroups[i]);
                    tabItem.Content = uc;
                    tabChannel.Items.Add(tabItem);
                }

#if REALHW
                Digital = new pe32h(true);
                LogMessage("Create pe32h instance successfully!");
                if (Digital.Initialize() != 0)
                {
                    LogMessage("Initialize pe32h failed!");
                }
                LogMessage("Initialize pe32h successfully!");
                pezMAX = Digital.lmload(1, 1, 0, filePEZ);
                if (pezMAX < 0)
                {
                    LogMessage("lmload failed!");
                }
                LogMessage("lmload successfully returns " + pezMAX + "!");
                Digital.rd_pesno(1);
                int data = 0;
                foreach (var channelgroup in channelGroups)
                {
                    int offset = channelgroup.Clock.ID - 1;
                    data = (data | (1 << offset));
                }
                Digital.set_rz(1, 1, data);
                LogMessage(string.Format("Digital.set_rz(1, 1, {0});", Convert.ToString(data, 2).PadLeft(32, '0')));
                Digital.set_ro(1, 1, 0);
                LogMessage(string.Format("Digital.set_ro(1, 1, {0});", Convert.ToString(0, 2).PadLeft(32, '0')));
#endif

                btnSet.IsEnabled   = true;
                btnDebug.IsEnabled = false;
            }
            catch (Exception ex)
            {
                btnSet.IsEnabled   = false;
                btnDebug.IsEnabled = false;
                MessageBox.Show(ex.Message);
                LogMessage(ex.Message);
            }
        }