from tkinter import *
from tkinter import ttk
import os
import sys
import datetime as dt
import matplotlib
import matplotlib.pyplot as plt
import random
import threading, time
from time import sleep
import inspect
from inspect import currentframe
from cview_model import *
from cview_log import *


if sys.platform.startswith("darwin"):
    style_dfl="aqua"
elif sys.platform.startswith("linux"):
    style_dfl="default"
elif sys.platform.startswith("cygwin"):
    style_dfl="classic"
elif sys.platform.startswith("win"):
    style_dfl="winnative"
else :
    cwg_log.debug("unsupported OS: "+sys.platform)

class PlotControl:
    def __init__(self, parent, fig, channels, units, lbl_txt, ax, depth):
        self.fig = fig
        self.ax = ax
        self.channels = channels
        self.cbox = ttk.Combobox(parent,
                                 style = "GrnGry.TCombobox",
                                 width = 10,
                                 values = channels)

        self.lbl = ttk.Label(parent,
                             text = lbl_txt,
                             style = "BlkGains.TLabel",
                             width = 8,
                             padding = "2 2 10 10",
                             anchor = "center",
                             relief = "sunken")

        self.units = units
        self.selected = int(0)
        self.ys = []
        if depth > 0:
            depth *= -1
        self.depth = depth
        self.changed = False
        return

    def Update(self,xs,yval):
        try:
            self.selected = self.GetField()
            self.ys.append(float(yval))

            while len(self.ys) < len(xs):
                cwg_log.debug("extending y")
                self.ys.append(float(yval))

            self.Trim(len(xs))
            self.ax.clear()
            self.ax.plot(xs, self.ys)
            plt.xticks(rotation=45, ha='right')
            plt.subplots_adjust(bottom=0.15, top=0.95, wspace=0.2, hspace=0.2, left=0.123,right=0.9)
            self.ax.set_title(self.channels[self.selected])
            self.ax.set_ylabel(self.units[self.selected])
            plt.draw()
        except Exception as e:
            cwg_log.exception("Update error")
        return

    def SetYAxis(self,channel):
        if channel in self.channels:
            self.selected = self.channels.index(channel)
            self.cbox.current(self.selected)
        return

    def GetField(self):
        retval = -1
        chan = ""
        try:
            chan = self.cbox.get()
            if chan in self.cbox['values']:
                retval = self.cbox['values'].index(chan)
        except Exception as e:
            cwg_log.exception("GetField: ch["+str(chan)+"/"+str(len(chan))+"]")
        return retval

    def SetDepth(self,depth):
        if(depth>0):
            depth *= -1
        retval = self.depth = depth
        return retval

    def Reset(self):
        self.Trim(0)
        self.ax.clear()
        plt.xticks(rotation=45, ha='right')
        plt.subplots_adjust(bottom=0.15, top=0.95, wspace=0.2, hspace=0.2, left=0.123,right=0.9)
        self.ax.set_title(self.channels[self.selected])
        self.ax.set_ylabel(self.units[self.selected])
        plt.draw()
        return

    def Clear(self):
        self.ax.clear()
        self.ax.set_title("")
        self.ax.set_ylabel("")
        self.ys = []
        return

    def Trim(self, depth):
        if int(depth)==0:
            self.ys = []
        elif len(self.ys) > int(depth):
            if depth>0:
                depth *= -1
            self.ys = self.ys[int(depth):]
        cwg_log.debug(self.cbox.get()+" len(ys):"+str(len(self.ys)))
        return

class View:
    def __init__(self, parent):

        self.window = Toplevel(parent)
        self.window.title("CView")
        self.window.protocol('WM_DELETE_WINDOW', parent.destroy)

        #Set root background color
        self.window.config(background = "gainsboro")
        self.window.columnconfigure(0, weight = 1)
        self.window.rowconfigure(0, weight = 1)
        self.style = ttk.Style()
        # for some reason, creating style here creates a root/Toplevel window
        style = ttk.Style()
        self.style.theme_use(style_dfl)
        self.style.configure("BlkGains.TLabel", foreground = "black", background = "gainsboro")
        self.style.configure("BlkGains.TFrame", foreground = "black", background = "gainsboro")
        self.style.configure("RedGains.TLabel", foreground = "red", background = "gainsboro")
        self.style.configure("GrnWht.TLabel", foreground = "green", background = "white")
        self.style.configure("GrnGry.TCombobox", foreground = "green", background = "gray")
        self.style.configure("BluGry.TCombobox", foreground = "blue", background = "gray")
        self.style.configure("BlkGry.TCombobox", foreground = "black", background = "gray")
        self.style.configure("RedGry.TCombobox", foreground = "red", background = "gray")
        self.style.configure("RedWht.TLabel", foreground = "red", background = "white")
        self.style.configure("GrnWht.TLabel", foreground = "green", background = "white")
        self.style.configure("BluWht.TButton", foreground = "blue", background = "white")
        self.style.configure("GrnWht.TButton", foreground = "green", background = "white")
        self.style.configure("RedWht.TButton", foreground = "red", background = "white")
        self.style.configure("BluWht.TEntry", foreground = "blue", background = "white")
        self.style.configure("BlkWht.TEntry", foreground = "black", background = "white")
        self.style.configure("GrnWht.TEntry", foreground = "green", background = "white")
        self.style.configure("RedWht.TEntry", foreground = "red", background = "white")
        self.style.configure("BlkWht.TSpinbox", foreground = "black", background = "white")
        self.style.configure("BluWht.TSpinbox", foreground = "blue", background = "white")
        self.style.configure("GrnWht.TSpinbox", foreground = "green", background = "white")
        self.style.configure("BlkWht.TCheckbutton", foreground = "black", background = "white")

        ####################
        # Notebook and frames

        self.notebook=ttk.Notebook(self.window)

        self.frm_nav = ttk.Frame(self.window, padding = "2 2 10 10", style="BlkGains.TFrame")

        self.frm_upload = ttk.Frame( self.notebook, padding = "2 2 10 10")
        self.frm_rb_up = ttk.Frame(self.frm_upload, padding = "2 2 10 10")
        self.frm_upaction = ttk.Frame(self.frm_upload, width = 100, padding = "2 2 15 15")

        self.frm_download = ttk.Frame(self.notebook, padding = "2 2 10 10")
        self.frm_rb_down = ttk.Frame(self.frm_download, padding = "2 2 10 10")
        self.frm_down_action = ttk.Frame(self.frm_download, width = 100, padding = "2 2 15 15")

        self.frm_settings = ttk.Frame(self.notebook, padding = "2 2 10 10")
        self.frm_plot = ttk.Frame(self.notebook, padding = "2 2 10 10")
        self.frm_rb_stream_log = ttk.Frame(self.frm_plot, padding = "2 2 10 10")

        self.frm_control = ttk.Frame(self.notebook, padding = "2 2 10 10")

        self.frm_mdio = ttk.Frame(self.notebook, padding = "2 2 10 10")
        self.frm_mdio_io_panel = ttk.Frame(self.frm_mdio, padding = "2 2 10 10")
        self.frm_mdio_io_sel = ttk.Frame(self.frm_mdio_io_panel, padding = "2 2 10 10")
        self.frm_mdio_macro_panel = ttk.Frame(self.frm_mdio, padding = "2 2 10 10")
        self.frm_mdio_preset_dir = ttk.Frame(self.frm_mdio_macro_panel, padding = "2 2 10 10")

        self.frm_mmeas = ttk.Frame(self.notebook, padding = "2 2 10 10")
        self.frm_mmeas_tm_panel = ttk.Frame(self.frm_mmeas, padding = "2 2 10 10")
        self.frm_mmeas_tm_out = ttk.Frame(self.frm_mmeas_tm_panel, padding = "2 2 10 10")
        self.frm_mmeas_tm_dest = ttk.Frame(self.frm_mmeas_tm_panel, padding = "2 2 10 10")
        self.frm_mmeas_tm_fmt = ttk.Frame(self.frm_mmeas_tm_panel, padding = "2 2 10 10")

        self.notebook.add(self.frm_settings, text = "Settings")
        self.notebook.add(self.frm_upload, text = "Host > CWG")
        self.notebook.add(self.frm_download, text = "CWG > Host")
        self.notebook.add(self.frm_plot, text = "Plot")
        self.notebook.add(self.frm_control, text = "Control")
        self.notebook.add(self.frm_mdio, text = "MDIO")
        self.notebook.add(self.frm_mmeas, text = "MMEAS")

        self.notebook_mmeas=ttk.Notebook(self.frm_mmeas)

        self.frm_mmeas_fo = ttk.Frame(self.notebook_mmeas, padding = "2 2 10 10", style="BlkGains.TFrame")
        self.frm_mmeas_fo_cfmt = ttk.Frame(self.frm_mmeas_fo, padding = "2 2 10 10")
        self.frm_mmeas_fo_hfmt = ttk.Frame(self.frm_mmeas_fo, padding = "2 2 10 10")

        self.frm_mmeas_fz = ttk.Frame(self.notebook_mmeas, padding = "2 2 10 10", style="BlkGains.TFrame")
        self.frm_mmeas_fz_outm = ttk.Frame(self.frm_mmeas_fz, padding = "2 2 10 10")
        self.frm_mmeas_fz_mfmt = ttk.Frame(self.frm_mmeas_fz, padding = "2 2 10 10")
        self.frm_mmeas_fz_outc = ttk.Frame(self.frm_mmeas_fz, padding = "2 2 10 10")
        self.frm_mmeas_fz_cfmt = ttk.Frame(self.frm_mmeas_fz, padding = "2 2 10 10")

        self.frm_mmeas_cyc = ttk.Frame(self.frm_mmeas, padding = "2 2 10 10")
        self.frm_mmeas_cyc_op = ttk.Frame(self.frm_mmeas_cyc, padding = "2 2 10 10")

        self.notebook_mmeas.add(self.frm_mmeas_fo, text = "FO")
        self.notebook_mmeas.add(self.frm_mmeas_fz, text = "FZ")
        self.notebook_mmeas.add(self.frm_mmeas_cyc, text = "CYC")


        ####################
        # Settings controls

        self.btn_ser_device = ttk.Button(self.frm_settings,
                                         text = "Device",
                                         width = 10,
                                         padding = "2 2 10 10"
                                         )

        self.cbox_ser_device = ttk.Combobox(self.frm_settings,
                               style = "BlkGry.TCombobox",
                               width = 35)

        self.cbox_ser_speed = ttk.Combobox(self.frm_settings,
                               style = "BlkGry.TCombobox",
                               width = 35)
        self.cbox_ser_speed['values']=(SER_B9600,SER_B19200)
        try:
            self.cbox_ser_speed.current(1)
        except Exception as e:
            cwg_log.exception("cbox_ser_speed error")

        self.btn_ser_refresh = ttk.Button(self.frm_settings,
                                          text = "Refresh",
                                          width = 10,
                                          padding = "2 2 10 10")

        self.btn_ser_connect = ttk.Button(self.frm_settings,
                                  text = "Connect",
                                  width = 10,
                                  padding = "2 2 10 10")

        self.btn_ser_disconnect = ttk.Button(self.frm_settings,
                                  text = "Disconnect",
                                  width = 10,
                                  padding = "2 2 10 10")

        ####################
        # Upload controls

        self.str_up_src = StringVar()
        self.ent_up_src = ttk.Entry(self.frm_upload,
                                    width = 35,
                                    textvariable = self.str_up_src,
                                    style = "GrnWht.TEntry")
        self.ent_up_src.insert(0,"")

        self.btn_up_src = ttk.Button(self.frm_upload,
                                text = "host",
                                width = 10,
                                padding = "2 2 10 10",
                                command = lambda: browseFiles(cwd_dir, ent_up_src ) )
        self.ent_up_src.insert(0,"")

        self.str_up_dest = StringVar()
        self.cbox_up_dest = ttk.Combobox(self.frm_upload,
                                         style = "RedGry.TCombobox",
                                         textvariable = self.str_up_dest,
                                         width = 35)

        self.btn_up_dest = ttk.Button(self.frm_upload,
                                      text = "cwg",
                                      width = 10,
                                      padding = "2 2 10 10"
                                      )

        self.mode_sel_up = BooleanVar()
        self.mode_sel_up.set(MODE_TEXT)

        self.rb_up_mode_txt = ttk.Radiobutton(self.frm_rb_up,
                                         text = MODE_TEXT_STR,
                                         variable = self.mode_sel_up,
                                         value = MODE_TEXT
                                         )
        self.rb_up_mode_bin = ttk.Radiobutton(self.frm_rb_up,
                                         text = MODE_BIN_STR,
                                         variable = self.mode_sel_up,
                                         value = MODE_BIN,
                                         )

        self.btn_up_swap = ttk.Button(self.frm_upload,
                                      text = "Swap Src/Des"
                                      )

        self.btn_up_upload = ttk.Button(self.frm_upaction,
                                        text = "Upload"
                                        # commaand = lambda: validateUpload()
                                        )

        ####################
        # Download controls

        self.str_down_dest = StringVar()
        self.ent_down_dest = ttk.Entry(self.frm_download,
                                      width = 35,
                                      textvariable = self.str_down_dest,
                                      style = "RedWht.TEntry"
                                      )

        self.ent_down_dest.insert(0,"")


        self.btn_down_dest = ttk.Button(self.frm_download,
                                        text = "host",
                                        width = 10,
                                        padding = "2 2 10 10"
                                        )

        self.str_down_src = StringVar()
        self.cbox_down_src = ttk.Combobox(self.frm_download,
                                          style = "GrnGry.TCombobox",
                                          textvariable = self.str_down_src,
                                          width = 35)

        self.btn_down_src = ttk.Button(self.frm_download,
                                        text = "cwg",
                                        width = 10,
                                        padding = "2 2 10 10"
                                       )

        self.mode_sel_down = BooleanVar()
        self.mode_sel_down.set(MODE_TEXT)
        self.rb_down_mode_txt = ttk.Radiobutton(self.frm_rb_down,
                                           text = MODE_TEXT_STR,
                                           variable = self.mode_sel_down,
                                           value = MODE_TEXT
                                           )
        self.rb_down_mode_bin = ttk.Radiobutton(self.frm_rb_down,
                                           text = MODE_BIN_STR,
                                           variable = self.mode_sel_down,
                                           value = MODE_BIN,
                                           )

        self.btn_down_swap = ttk.Button(self.frm_download,
                                        text = "Swap Src/Des"
                                        )

        self.btn_down_download = ttk.Button(self.frm_down_action,
                                       text = "Download",
                                       command = lambda: validateDownload() )

        ####################
        # Navigation controls
        self.btn_quit = ttk.Button(self.frm_nav,
                              text = "Quit")

        ####################
        # Plot controls
        # Create figure for plotting
        # This defines the Python GUI backend to use for matplotlib

        # Initialize random number generator
        random.seed()

        # Set up plot
        plot_buffer_depth=-20
        self.plot_period_sec = 1.0
        self.plot_period_sec_str = str(self.plot_period_sec)
        self.nplots = 4

        self.btxt_plot_toggle=StringVar()
        self.btxt_plot_toggle.set("Start")
        self.btn_plot_toggle = ttk.Button(self.frm_plot,
                                          textvariable = self.btxt_plot_toggle
                                          )

        self.btxt_plot_clear=StringVar()
        self.btxt_plot_clear.set("Clear Plots")
        self.btn_plot_clear = ttk.Button(self.frm_plot,
                                          textvariable = self.btxt_plot_clear
                                          )

        self.ent_plot_depth = ttk.Entry(self.frm_plot,
                                   width = 5,
                                   justify = "right",
                                   style = "BlkWht.TEntry")
        self.ent_plot_depth.insert(0,"20")
        self.lbl_plot_depth = ttk.Label(self.frm_plot,
                                   text = "points",
                                   style = "BlkGains.TLabel",
                                   width = 8,
                                   padding = "2 2 10 10",
                                   anchor = "center",
                                   relief = "sunken")

        self.ent_plot_period_sec = ttk.Entry(self.frm_plot,
                                    width = 5,
                                    justify = "right",
                                    textvariable=self.plot_period_sec_str,
                                    style = "BlkWht.TEntry")
        self.ent_plot_period_sec.insert(0,self.plot_period_sec_str)
        self.lbl_plot_period_sec = ttk.Label(self.frm_plot,
                                    text = "period",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.plot_channel_names = [
                                   "time","seq_n","cycle_id","meas_n","ens_n",
                                   "ens_len","id","per_s",
                                   "co2","co2abs","h2o",
                                   "h2odew","h2oabs","cell_temp",
                                   "cell_pres","ivolt","flow",
                                   "raw_co2","raw_co2ref",
                                   "raw_h2o","raw_h2oref","len",
                                   "none"
                                ]
        self.plot_yaxis_units = [
                                   "UTC","n","n","n","n",
                                   "n","RRMM","sec",
                                   "co2","co2abs","h2o",
                                   "h2odew","h2oabs","deg C",
                                   "cell_pres","V","flow",
                                   "counts","counts",
                                   "counts","counts","bytes",
                                   "none"
                                   ]
        self.stream_sel_log = BooleanVar()
        self.stream_sel_log.set(STREAM_LOG_DI)
        self.rb_stream_log_en = ttk.Radiobutton(self.frm_rb_stream_log,
                                           text = STREAM_LOG_EN_STR,
                                           variable = self.stream_sel_log,
                                           value = STREAM_LOG_EN
                                           )
        self.rb_stream_log_di = ttk.Radiobutton(self.frm_rb_stream_log,
                                           text = STREAM_LOG_DI_STR,
                                           variable = self.stream_sel_log,
                                           value = STREAM_LOG_DI,
                                           )

        self.lbl_stream_log = ttk.Label(self.frm_rb_stream_log,
                                    text = "Log Stream",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.stream_sel_passive = BooleanVar()
        self.stream_sel_passive.set(STREAM_PASSIVE_DI)
        self.rb_stream_passive_en = ttk.Radiobutton(self.frm_rb_stream_log,
                                           text = STREAM_PASSIVE_EN_STR,
                                           variable = self.stream_sel_passive,
                                           value = STREAM_PASSIVE_EN
                                           )
        self.rb_stream_passive_di = ttk.Radiobutton(self.frm_rb_stream_log,
                                           text = STREAM_PASSIVE_DI_STR,
                                           variable = self.stream_sel_passive,
                                           value = STREAM_PASSIVE_DI,
                                           )

        self.lbl_stream_passive = ttk.Label(self.frm_rb_stream_log,
                                    text = "Stream Passive",
                                    style = "BlkGains.TLabel",
                                    width = 15,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        # create plot figure
        self.fig = plt.figure(num="LI-COR")

        # plot X axis values
        self.xs = []
        # list of PlotControl objects (1 per plot)
        # encapsulates sub-plot and configuration
        self.plot_controls = list()
        i = 0
        while i < self.nplots:
            # add sub-plot
            ax=self.fig.add_subplot(self.nplots,1,(i+1))

            if i == (self.nplots-1):
                # make only last plot x-axis visible
                ax.xaxis.set_visible(True)
            else:
                ax.xaxis.set_visible(False)
            plot = PlotControl(self.frm_plot,
                               self.fig,
                               self.plot_channel_names,
                               self.plot_yaxis_units,
                               "plot "+str(i),
                               ax,
                               plot_buffer_depth
                            )
            # set defaults
            if i == 0:
                plot.SetYAxis("co2")
            elif i==1:
                plot.SetYAxis("co2abs")
            elif i==2:
                plot.SetYAxis("raw_co2")
            elif i==3:
                plot.SetYAxis("raw_co2ref")

            self.plot_controls.append(plot)
            i += 1
        # end while

        # set default plot channels
        # disable plot interactive mode and make visible visible
        plt.ioff()
        plt.show(block=False)

        ####################
        # LI-COR controls

        self.btn_licor_power_on = ttk.Button(self.frm_control,
                                          text = LI_POWER_ON_STR
                                          )
        self.btn_licor_power_off = ttk.Button(self.frm_control,
                                          text = LI_POWER_OFF_STR
                                          )

        self.lbl_licor_power = ttk.Label(self.frm_control,
                                    text = "licor",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.btn_cron_en = ttk.Button(self.frm_control,
                                          text = CRON_EN_STR
                                          )
        self.btn_cron_di = ttk.Button(self.frm_control,
                                          text = CRON_DI_STR
                                          )

        self.lbl_cron = ttk.Label(self.frm_control,
                                    text = "cron",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.btn_send_escape = ttk.Button(self.frm_control,
                                          text = SEND_ESC_STR
                                          )

        self.lbl_send_escape = ttk.Label(self.frm_control,
                                    text = "send esc",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        ####################
        # MDIO controls

        # MDIO IO controls
        self.lbl_mdio_port = ttk.Label(self.frm_mdio_io_panel,
                                    text = "IO",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.lbl_mdio_action = ttk.Label(self.frm_mdio_io_panel,
                                    text = "Action",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.lbl_mdio_th = ttk.Label(self.frm_mdio_io_panel,
                                    text = "Th (ms)",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mdio_th = StringVar()
        self.ent_mdio_th = ttk.Entry(self.frm_mdio_io_panel,
                                    width = 10,
                                    textvariable = self.str_mdio_th,
                                    style = "BlkWht.TEntry")

        self.lbl_mdio_tl = ttk.Label(self.frm_mdio_io_panel,
                                    text = "Tl (ms)",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mdio_tl = StringVar()
        self.ent_mdio_tl = ttk.Entry(self.frm_mdio_io_panel,
                                    width = 10,
                                    textvariable = self.str_mdio_tl,
                                    style = "BlkWht.TEntry")

        self.lbl_mdio_cyc = ttk.Label(self.frm_mdio_io_panel,
                                    text = "Cycles",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mdio_cyc = StringVar()
        self.ent_mdio_cyc = ttk.Entry(self.frm_mdio_io_panel,
                                    width = 10,
                                    textvariable = self.str_mdio_cyc,
                                    style = "BlkWht.TEntry")

        self.lbl_mdio_pdel = ttk.Label(self.frm_mdio_io_panel,
                                    text = "Post-Del (ms)",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mdio_pdel = StringVar()
        self.ent_mdio_pdel = ttk.Entry(self.frm_mdio_io_panel,
                                    width = 10,
                                    textvariable = self.str_mdio_pdel,
                                    style = "BlkWht.TEntry")

        self.btn_mdio_io = ttk.Button(self.frm_mdio_io_panel,
                                         text = "Run IO",
                                         width = 10,
                                         padding = "2 2 10 10"
                                         )

        self.sbox_mdio_port_type = ttk.Spinbox(self.frm_mdio_io_sel,
                                            style = "BluWht.TSpinbox",
                                            text="type",
                                            width = 2,
                                            wrap=True)
        self.sbox_mdio_port_type['values'] = ('V', 'P', 'A')
        self.sbox_mdio_port_type.set('V')

        self.sbox_mdio_port_n = ttk.Spinbox(self.frm_mdio_io_sel,
                                            style = "BluWht.TSpinbox",
                                            text="number",
                                            width = 2,
                                            wrap=True
                                            )
        self.sbox_mdio_port_n['values'] = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*')
        self.sbox_mdio_port_n.set('0')

        self.sbox_mdio_port_sel = ttk.Spinbox(self.frm_mdio_io_sel,
                                            style = "BluWht.TSpinbox",
                                            text="sel",
                                            width = 2,
                                            wrap=True
                                            )
        self.sbox_mdio_port_sel['values'] = ('A', 'B', '*')
        self.sbox_mdio_port_sel.set('A')

        self.sbox_mdio_op = ttk.Spinbox(self.frm_mdio_io_panel,
                                            style = "BlkWht.TSpinbox",
                                            text="op",
                                            width = 4,
                                            wrap=True
                                            )
        self.sbox_mdio_op['values'] = ('IOH', 'IOL', 'PHL', 'PLH', 'SHL', 'SLH')
        self.sbox_mdio_op.set('IOH')

        # MDIO Preset controls
        self.mdio_preset_dir = BooleanVar()
        self.mdio_preset_dir.set(PRESET_DIR_POS)
        self.rb_preset_dir_pos = ttk.Radiobutton(self.frm_mdio_preset_dir,
                                           text = PRESET_DIR_POS_STR,
                                           variable = self.mdio_preset_dir,
                                           value = PRESET_DIR_POS
                                           )
        self.rb_preset_dir_neg = ttk.Radiobutton(self.frm_mdio_preset_dir,
                                           text = PRESET_DIR_NEG_STR,
                                           variable = self.mdio_preset_dir,
                                           value = PRESET_DIR_NEG,
                                           )

        self.cbox_mdio_preset = ttk.Combobox(self.frm_mdio_macro_panel,
                               style = "BlkGry.TCombobox",
                               width = 10)

        self.lbl_mdio_preset = ttk.Label(self.frm_mdio_macro_panel,
                                    text = "Preset",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.cbox_mdio_preset['values']=( \
        'LI', 'UL', 'AIPUMP', 'SWPUMP', 'SAPUMP', 'ACPUMP', \
        'IOPURGE', 'IOFILL', 'IOZERO', 'IOZVENT', 'IOZCAL', \
        'IOSZERO', 'IOSZVENT', 'IOSAMPLE', 'IOAIR', \
        'IOSTD', 'IOSTDFILL', 'IOREST', 'IOSCAL', 'IOSCALGAS', \
        'IOVEX1', 'IOVEX2', 'IOVEX3', 'IOVEX4', \
        'IOVEX5', 'IOVEX6', 'IOVEX7', 'IOVEX8')
        self.cbox_mdio_preset.set('')

        self.btn_mdio_preset = ttk.Button(self.frm_mdio_macro_panel,
                                         text = "Preset",
                                         width = 10,
                                         padding = "2 2 10 10"
                                         )

        # MDIO Macro controls
        self.cbox_mdio_macro = ttk.Combobox(self.frm_mdio_macro_panel,
                               style = "BlkGry.TCombobox",
                               width = 10)

        self.lbl_mdio_macro = ttk.Label(self.frm_mdio_macro_panel,
                                    text = "Macro",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.cbox_mdio_macro['values']=( \
        'FILL', 'PURGE', 'ACID', 'ZERO', 'ZCAL', 'SZERO', \
        'SAMPLE', 'AIR', 'SCAL', 'STD', 'MTERM', \
        'REST', 'VEX')
        self.cbox_mdio_macro.set('')

        self.btn_mdio_macro = ttk.Button(self.frm_mdio_macro_panel,
                                         text = "Macro",
                                         width = 10,
                                         padding = "2 2 10 10"
                                         )


        self.lbl_mdio_ovr_vh = ttk.Label(self.frm_mdio_macro_panel,
                                    text = "Valve Ton (ms)",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mdio_ovr_vh = StringVar()
        self.ent_mdio_ovr_vh = ttk.Entry(self.frm_mdio_macro_panel,
                                    width = 10,
                                    textvariable = self.str_mdio_ovr_vh,
                                    style = "BlkWht.TEntry")

        self.lbl_mdio_ovr_ph = ttk.Label(self.frm_mdio_macro_panel,
                                    text = "Pump Ton (ms)",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mdio_ovr_ph = StringVar()
        self.ent_mdio_ovr_ph = ttk.Entry(self.frm_mdio_macro_panel,
                                    width = 10,
                                    textvariable = self.str_mdio_ovr_ph,
                                    style = "BlkWht.TEntry")
        self.lbl_mdio_ovr_pl = ttk.Label(self.frm_mdio_macro_panel,
                                    text = "Pump Toff (ms)",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mdio_ovr_pl = StringVar()
        self.ent_mdio_ovr_pl = ttk.Entry(self.frm_mdio_macro_panel,
                                    width = 10,
                                    textvariable = self.str_mdio_ovr_pl,
                                    style = "BlkWht.TEntry")
        self.lbl_mdio_ovr_pn = ttk.Label(self.frm_mdio_macro_panel,
                                    text = "Pump Cycles",
                                    style = "BlkGains.TLabel",
                                    width = 12,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mdio_ovr_pn = StringVar()
        self.ent_mdio_ovr_pn = ttk.Entry(self.frm_mdio_macro_panel,
                                    width = 10,
                                    textvariable = self.str_mdio_ovr_pn,
                                    style = "BlkWht.TEntry")

        ####################
        # MMEAS controls

        # MMEAS TM controls
        self.lbl_mmeas_tm = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "TM",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.sbox_mmeas_tm = ttk.Spinbox(self.frm_mmeas_tm_panel,
                                            style = "BlkWht.TSpinbox",
                                            text="op",
                                            width = 10,
                                            wrap=True)

        self.sbox_mmeas_tm['values'] = ( \
        'FILL', 'PURGE', 'ACID', 'ZERO', 'ZCAL', 'SZERO', \
        'SAMPLE', 'AIR', 'SCAL', 'STD')
        self.sbox_mmeas_tm.set('')

        self.lbl_mmeas_handler = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "Handler",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.sbox_mmeas_handler = ttk.Spinbox(self.frm_mmeas_tm_panel,
                                            style = "BlkWht.TSpinbox",
                                            text="handler",
                                            width = 10,
                                            wrap=True)

        self.sbox_mmeas_handler['values'] = ( \
        'FO', 'FZ')
        self.sbox_mmeas_handler.set('')

        self.lbl_mmeas_ovr_p = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "P (period)",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mmeas_ovr_p = StringVar()
        self.ent_mmeas_ovr_p = ttk.Entry(self.frm_mmeas_tm_panel,
                                    width = 10,
                                    textvariable = self.str_mmeas_ovr_p,
                                    style = "BlkWht.TEntry")

        self.lbl_mmeas_ovr_t = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "T",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mmeas_ovr_t = StringVar()
        self.ent_mmeas_ovr_t = ttk.Entry(self.frm_mmeas_tm_panel,
                                    width = 10,
                                    textvariable = self.str_mmeas_ovr_t,
                                    style = "BlkWht.TEntry")

        self.lbl_mmeas_ovr_n = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "N",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mmeas_ovr_n = StringVar()
        self.ent_mmeas_ovr_n = ttk.Entry(self.frm_mmeas_tm_panel,
                                    width = 10,
                                    textvariable = self.str_mmeas_ovr_n,
                                    style = "BlkWht.TEntry")

        self.lbl_mmeas_ovr_b = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "B",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mmeas_ovr_b = StringVar()
        self.ent_mmeas_ovr_b = ttk.Entry(self.frm_mmeas_tm_panel,
                                    width = 10,
                                    textvariable = self.str_mmeas_ovr_b,
                                    style = "BlkWht.TEntry")

        self.lbl_mmeas_ovr_w = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "W",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")
        self.str_mmeas_ovr_w = StringVar()
        self.ent_mmeas_ovr_w = ttk.Entry(self.frm_mmeas_tm_panel,
                                    width = 10,
                                    textvariable = self.str_mmeas_ovr_w,
                                    style = "BlkWht.TEntry")

        self.lbl_mmeas_rmid = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "RMID",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.sbox_mmeas_rmid = ttk.Spinbox(self.frm_mmeas_tm_panel,
                                            style = "BlkWht.TSpinbox",
                                            text="rmid",
                                            width = 10,
                                            wrap=True)

        self.sbox_mmeas_rmid['values'] = ( \
        'RMSAMPLE', 'RMSCAL', 'RMZCAL','')
        self.sbox_mmeas_rmid.set('')

        self.lbl_mmeas_mmid = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "MMID",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.sbox_mmeas_mmid = ttk.Spinbox(self.frm_mmeas_tm_panel,
                                            style = "BlkWht.TSpinbox",
                                            text="mmid",
                                            width = 10,
                                            wrap=True)

        self.sbox_mmeas_mmid['values'] = ( \
        'MMAIR', 'MMSTD', 'ZERO', 'MMSAMPLE', 'MMPRES', 'MMPRES', \
        'MMPOSTS', 'MMPREZ', 'MMPOSTZ', 'MMPREB', 'MMBLINE', 'MMPOSTB', \
        'MMSCAL', 'MMZCAL', '')
        self.sbox_mmeas_mmid.set('')

        self.btn_mmeas_tm = ttk.Button(self.frm_mmeas_tm_panel,
                                         text = "TM",
                                         width = 10,
                                         padding = "2 2 10 10"
                                         )
        self.var_mmeas_status = StringVar()
        self.lbl_mmeas_status = ttk.Label(self.frm_mmeas,
                                    text="Status",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    justify = LEFT,
                                    relief = "flat")

        self.ent_mmeas_status = ttk.Entry(self.frm_mmeas,
                                    width = 40,
                                    justify = LEFT,
                                    textvariable = self.var_mmeas_status,
                                    style = "BlkWht.TEntry")

        ####################
        # MMEAS CYC controls
        self.lbl_mmeas_cyc = ttk.Label(self.frm_mmeas_tm_panel,
                                    text = "Cycle",
                                    style = "BlkGains.TLabel",
                                    width = 10,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.lbl_mmeas_cyc_op = ttk.Label(self.frm_mmeas_cyc,
                                    text = "Action",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.var_mmeas_cyc_rst = BooleanVar()
        self.var_mmeas_cyc_rst.set(False)
        self.var_mmeas_cyc_start = BooleanVar()
        self.var_mmeas_cyc_start.set(False)
        self.var_mmeas_cyc_end = BooleanVar()
        self.var_mmeas_cyc_end.set(False)

        self.cbtn_mmeas_cyc_rst = ttk.Checkbutton(self.frm_mmeas_cyc_op,
                                            style = "BlkWht.TCheckbutton",
                                            text="reset",
                                            variable=self.var_mmeas_cyc_rst,
                                            width = 6)
        self.cbtn_mmeas_cyc_start = ttk.Checkbutton(self.frm_mmeas_cyc_op,
                                            style = "BlkWht.TCheckbutton",
                                            text="start",
                                            variable=self.var_mmeas_cyc_start,
                                            width = 6)
        self.cbtn_mmeas_cyc_end = ttk.Checkbutton(self.frm_mmeas_cyc_op,
                                            style = "BlkWht.TCheckbutton",
                                            text="end",
                                            variable=self.var_mmeas_cyc_end,
                                            width = 6)

        self.lbl_mmeas_cyc_rtid = ttk.Label(self.frm_mmeas_cyc,
                                    text = "RTID",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.sbox_mmeas_cyc_rtid = ttk.Spinbox(self.frm_mmeas_cyc,
                                            style = "BlkWht.TSpinbox",
                                            text="cyc_rtid",
                                            width = 10,
                                            wrap=True)

        self.sbox_mmeas_cyc_rtid['values'] = ( \
        'RTZ0','')
        self.sbox_mmeas_cyc_rtid.set('')

        self.lbl_mmeas_cyc_rmid = ttk.Label(self.frm_mmeas_cyc,
                                    text = "RMID",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.sbox_mmeas_cyc_rmid = ttk.Spinbox(self.frm_mmeas_cyc,
                                            style = "BlkWht.TSpinbox",
                                            text="cyc_rmid",
                                            width = 10,
                                            wrap=True)

        self.sbox_mmeas_cyc_rmid['values'] = ( \
        'RMSAMPLE', 'RMSCAL', 'RMZCAL', '')
        self.sbox_mmeas_cyc_rmid.set('')

        self.btn_mmeas_cyc = ttk.Button(self.frm_mmeas_tm_panel,
                                         text = "CYC",
                                         width = 10,
                                         padding = "2 2 10 10"
                                         )

        ####################
        # MMEAS FO Tab
        ####################
        self.lbl_mmeas_fo_log = ttk.Label(self.frm_mmeas_fo,
                                    text = "Log",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.sbox_mmeas_fo_log = ttk.Spinbox(self.frm_mmeas_fo,
                                            style = "BlkWht.TSpinbox",
                                            text="fo_log",
                                            width = 10,
                                            wrap=True)

        self.sbox_mmeas_fo_log['values'] = ( \
        'orec', 'raw', '*','')
        self.sbox_mmeas_fo_log.set('')


        self.var_mmeas_fo_cons = BooleanVar()
        self.var_mmeas_fo_cons.set(False)
        self.var_mmeas_fo_cfmt_c = BooleanVar()
        self.var_mmeas_fo_cfmt_c.set(False)
        self.var_mmeas_fo_cfmt_p = BooleanVar()
        self.var_mmeas_fo_cfmt_p.set(False)
        self.var_mmeas_fo_cfmt_b = BooleanVar()
        self.var_mmeas_fo_cfmt_b.set(False)
        self.var_mmeas_fo_cfmt_x = BooleanVar()
        self.var_mmeas_fo_cfmt_x.set(False)
        self.var_mmeas_fo_cfmt_xx = BooleanVar()
        self.var_mmeas_fo_cfmt_xx.set(False)
        self.var_mmeas_fo_cfmt_xxx = BooleanVar()
        self.var_mmeas_fo_cfmt_xxx.set(False)
        self.var_mmeas_fo_cfmt_r = BooleanVar()
        self.var_mmeas_fo_cfmt_r.set(False)
        self.var_mmeas_fo_cfmt_h = BooleanVar()
        self.var_mmeas_fo_cfmt_h.set(False)
        self.var_mmeas_fo_cfmt_d = BooleanVar()
        self.var_mmeas_fo_cfmt_d.set(False)
        self.var_mmeas_fo_cfmt_v = BooleanVar()
        self.var_mmeas_fo_cfmt_v.set(False)

        self.lbl_mmeas_fo_cons = ttk.Label(self.frm_mmeas_fo,
                                    text = "Console",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.cbtn_mmeas_fo_cons = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="Console",
                                            variable=self.var_mmeas_fo_cons,
                                            width = 6)
        self.cbtn_mmeas_fo_cfmt_c = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="csv",
                                            variable=self.var_mmeas_fo_cfmt_c,
                                            width = 5)
        self.cbtn_mmeas_fo_cfmt_p = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="pretty",
                                            variable=self.var_mmeas_fo_cfmt_p,
                                            width = 5)
        self.cbtn_mmeas_fo_cfmt_b = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="B85",
                                            variable=self.var_mmeas_fo_cfmt_b,
                                            width = 5)
        self.cbtn_mmeas_fo_cfmt_x = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="x",
                                             variable=self.var_mmeas_fo_cfmt_x,
                                           width = 5)
        self.cbtn_mmeas_fo_cfmt_xx = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="xx",
                                            variable=self.var_mmeas_fo_cfmt_xx,
                                            width = 5)
        self.cbtn_mmeas_fo_cfmt_xxx = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="x*",
                                            variable=self.var_mmeas_fo_cfmt_xxx,
                                            width = 5)
        self.cbtn_mmeas_fo_cfmt_r = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="raw",
                                            variable=self.var_mmeas_fo_cfmt_r,
                                            width = 5)
        self.cbtn_mmeas_fo_cfmt_h = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="header",
                                            variable=self.var_mmeas_fo_cfmt_h,
                                            width = 5)
        self.cbtn_mmeas_fo_cfmt_d = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="data",
                                            variable=self.var_mmeas_fo_cfmt_d,
                                            width = 5)
        self.cbtn_mmeas_fo_cfmt_v = ttk.Checkbutton(self.frm_mmeas_fo_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="verbose",
                                            variable=self.var_mmeas_fo_cfmt_v,
                                            width = 6)

        self.var_mmeas_fo_host = BooleanVar()
        self.var_mmeas_fo_host.set(False)
        self.var_mmeas_fo_hfmt_c = BooleanVar()
        self.var_mmeas_fo_hfmt_c.set(False)
        self.var_mmeas_fo_hfmt_p = BooleanVar()
        self.var_mmeas_fo_hfmt_p.set(False)
        self.var_mmeas_fo_hfmt_b = BooleanVar()
        self.var_mmeas_fo_hfmt_b.set(False)
        self.var_mmeas_fo_hfmt_x = BooleanVar()
        self.var_mmeas_fo_hfmt_x.set(False)
        self.var_mmeas_fo_hfmt_xx = BooleanVar()
        self.var_mmeas_fo_hfmt_xx.set(False)
        self.var_mmeas_fo_hfmt_xxx = BooleanVar()
        self.var_mmeas_fo_hfmt_xxx.set(False)
        self.var_mmeas_fo_hfmt_r = BooleanVar()
        self.var_mmeas_fo_hfmt_r.set(False)
        self.var_mmeas_fo_hfmt_h = BooleanVar()
        self.var_mmeas_fo_hfmt_h.set(False)
        self.var_mmeas_fo_hfmt_d = BooleanVar()
        self.var_mmeas_fo_hfmt_d.set(False)
        self.var_mmeas_fo_hfmt_v = BooleanVar()
        self.var_mmeas_fo_hfmt_v.set(False)

        self.lbl_mmeas_fo_host = ttk.Label(self.frm_mmeas_fo,
                                    text = "Host",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.cbtn_mmeas_fo_host = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="Host",
                                            variable=self.var_mmeas_fo_host,
                                            width = 6)
        self.cbtn_mmeas_fo_hfmt_c = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="csv",
                                            variable=self.var_mmeas_fo_hfmt_c,
                                            width = 5)
        self.cbtn_mmeas_fo_hfmt_p = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="pretty",
                                            variable=self.var_mmeas_fo_hfmt_p,
                                            width = 5)
        self.cbtn_mmeas_fo_hfmt_b = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="B85",
                                            variable=self.var_mmeas_fo_hfmt_b,
                                            width = 5)
        self.cbtn_mmeas_fo_hfmt_x = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="x",
                                             variable=self.var_mmeas_fo_hfmt_x,
                                           width = 5)
        self.cbtn_mmeas_fo_hfmt_xx = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="xx",
                                            variable=self.var_mmeas_fo_hfmt_xx,
                                            width = 5)
        self.cbtn_mmeas_fo_hfmt_xxx = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="x*",
                                            variable=self.var_mmeas_fo_hfmt_xxx,
                                            width = 5)
        self.cbtn_mmeas_fo_hfmt_r = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="raw",
                                            variable=self.var_mmeas_fo_hfmt_r,
                                            width = 5)
        self.cbtn_mmeas_fo_hfmt_h = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="header",
                                            variable=self.var_mmeas_fo_hfmt_h,
                                            width = 5)
        self.cbtn_mmeas_fo_hfmt_d = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="data",
                                            variable=self.var_mmeas_fo_hfmt_d,
                                            width = 5)
        self.cbtn_mmeas_fo_hfmt_v = ttk.Checkbutton(self.frm_mmeas_fo_hfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="verbose",
                                            variable=self.var_mmeas_fo_hfmt_v,
                                            width = 6)

        ####################
        # MMEAS FZ Tab
        ####################
        self.lbl_mmeas_fz_log = ttk.Label(self.frm_mmeas_fz,
                                    text = "Log",
                                    style = "BlkGains.TLabel",
                                    width = 8,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.sbox_mmeas_fz_log = ttk.Spinbox(self.frm_mmeas_fz,
                                            style = "BlkWht.TSpinbox",
                                            text="fz_log",
                                            width = 10,
                                            wrap=True)

        self.sbox_mmeas_fz_log['values'] = ( \
        'orec', 'raw', '*','')
        self.sbox_mmeas_fz_log.set('')


        self.var_mmeas_fz_cbuf_l = BooleanVar()
        self.var_mmeas_fz_cbuf_l.set(False)
        self.var_mmeas_fz_cbuf_c = BooleanVar()
        self.var_mmeas_fz_cbuf_c.set(False)
        self.var_mmeas_fz_cbuf_h = BooleanVar()
        self.var_mmeas_fz_cbuf_h.set(False)
        self.var_mmeas_fz_cbuf_q = BooleanVar()
        self.var_mmeas_fz_cbuf_q.set(False)
        self.var_mmeas_fz_cfmt_c = BooleanVar()
        self.var_mmeas_fz_cfmt_c.set(False)
        self.var_mmeas_fz_cfmt_p = BooleanVar()
        self.var_mmeas_fz_cfmt_p.set(False)
        self.var_mmeas_fz_cfmt_b = BooleanVar()
        self.var_mmeas_fz_cfmt_b.set(False)
        self.var_mmeas_fz_cfmt_x = BooleanVar()
        self.var_mmeas_fz_cfmt_x.set(False)
        self.var_mmeas_fz_cfmt_xx = BooleanVar()
        self.var_mmeas_fz_cfmt_xx.set(False)
        self.var_mmeas_fz_cfmt_xxx = BooleanVar()
        self.var_mmeas_fz_cfmt_xxx.set(False)
        self.var_mmeas_fz_cfmt_r = BooleanVar()
        self.var_mmeas_fz_cfmt_r.set(False)
        self.var_mmeas_fz_cfmt_h = BooleanVar()
        self.var_mmeas_fz_cfmt_h.set(False)
        self.var_mmeas_fz_cfmt_d = BooleanVar()
        self.var_mmeas_fz_cfmt_d.set(False)
        self.var_mmeas_fz_cfmt_v = BooleanVar()
        self.var_mmeas_fz_cfmt_v.set(False)

        self.lbl_mmeas_fz_cbuf = ttk.Label(self.frm_mmeas_fz,
                                    text = "Cycle Buffer",
                                    style = "BlkGains.TLabel",
                                    width = 10,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")


        self.cbtn_mmeas_fz_cbuf_l = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="log",
                                            variable=self.var_mmeas_fz_cbuf_l,
                                            width = 6)
        self.cbtn_mmeas_fz_cbuf_c = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="cons",
                                            variable=self.var_mmeas_fz_cbuf_c,
                                            width = 6)
        self.cbtn_mmeas_fz_cbuf_h = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="host",
                                            variable=self.var_mmeas_fz_cbuf_h,
                                            width = 6)
        self.cbtn_mmeas_fz_cbuf_q = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="queue",
                                            variable=self.var_mmeas_fz_cbuf_q,
                                            width = 6)

        self.cbtn_mmeas_fz_cfmt_c = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="csv",
                                            variable=self.var_mmeas_fz_cfmt_c,
                                            width = 5)
        self.cbtn_mmeas_fz_cfmt_p = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="pretty",
                                            variable=self.var_mmeas_fz_cfmt_p,
                                            width = 5)
        self.cbtn_mmeas_fz_cfmt_b = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="B85",
                                            variable=self.var_mmeas_fz_cfmt_b,
                                            width = 5)
        self.cbtn_mmeas_fz_cfmt_x = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="x",
                                             variable=self.var_mmeas_fz_cfmt_x,
                                           width = 5)
        self.cbtn_mmeas_fz_cfmt_xx = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="xx",
                                            variable=self.var_mmeas_fz_cfmt_xx,
                                            width = 5)
        self.cbtn_mmeas_fz_cfmt_xxx = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="x*",
                                            variable=self.var_mmeas_fz_cfmt_xxx,
                                            width = 5)
        self.cbtn_mmeas_fz_cfmt_r = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="raw",
                                            variable=self.var_mmeas_fz_cfmt_r,
                                            width = 5)
        self.cbtn_mmeas_fz_cfmt_h = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="header",
                                            variable=self.var_mmeas_fz_cfmt_h,
                                            width = 5)
        self.cbtn_mmeas_fz_cfmt_d = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="data",
                                            variable=self.var_mmeas_fz_cfmt_d,
                                            width = 5)
        self.cbtn_mmeas_fz_cfmt_v = ttk.Checkbutton(self.frm_mmeas_fz_cfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="verbose",
                                            variable=self.var_mmeas_fz_cfmt_v,
                                            width = 6)

        self.var_mmeas_fz_mbuf_l = BooleanVar()
        self.var_mmeas_fz_mbuf_l.set(False)
        self.var_mmeas_fz_mbuf_c = BooleanVar()
        self.var_mmeas_fz_mbuf_c.set(False)
        self.var_mmeas_fz_mbuf_h = BooleanVar()
        self.var_mmeas_fz_mbuf_h.set(False)
        self.var_mmeas_fz_mbuf_q = BooleanVar()
        self.var_mmeas_fz_mbuf_q.set(False)
        self.var_mmeas_fz_mfmt_c = BooleanVar()
        self.var_mmeas_fz_mfmt_c.set(False)
        self.var_mmeas_fz_mfmt_p = BooleanVar()
        self.var_mmeas_fz_mfmt_p.set(False)
        self.var_mmeas_fz_mfmt_b = BooleanVar()
        self.var_mmeas_fz_mfmt_b.set(False)
        self.var_mmeas_fz_mfmt_x = BooleanVar()
        self.var_mmeas_fz_mfmt_x.set(False)
        self.var_mmeas_fz_mfmt_xx = BooleanVar()
        self.var_mmeas_fz_mfmt_xx.set(False)
        self.var_mmeas_fz_mfmt_xxx = BooleanVar()
        self.var_mmeas_fz_mfmt_xxx.set(False)
        self.var_mmeas_fz_mfmt_r = BooleanVar()
        self.var_mmeas_fz_mfmt_r.set(False)
        self.var_mmeas_fz_mfmt_h = BooleanVar()
        self.var_mmeas_fz_mfmt_h.set(False)
        self.var_mmeas_fz_mfmt_d = BooleanVar()
        self.var_mmeas_fz_mfmt_d.set(False)
        self.var_mmeas_fz_mfmt_v = BooleanVar()
        self.var_mmeas_fz_mfmt_v.set(False)

        self.lbl_mmeas_fz_mbuf = ttk.Label(self.frm_mmeas_fz,
                                    text = "Meas Buffer",
                                    style = "BlkGains.TLabel",
                                    width = 10,
                                    padding = "2 2 10 10",
                                    anchor = "center",
                                    relief = "sunken")

        self.cbtn_mmeas_fz_mbuf_l = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="log",
                                            variable=self.var_mmeas_fz_mbuf_l,
                                            width = 6)
        self.cbtn_mmeas_fz_mbuf_c = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="cons",
                                            variable=self.var_mmeas_fz_mbuf_c,
                                            width = 6)
        self.cbtn_mmeas_fz_mbuf_h = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="host",
                                            variable=self.var_mmeas_fz_mbuf_h,
                                            width = 6)
        self.cbtn_mmeas_fz_mbuf_q = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="queue",
                                            variable=self.var_mmeas_fz_mbuf_q,
                                            width = 6)
        self.cbtn_mmeas_fz_mfmt_c = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="csv",
                                            variable=self.var_mmeas_fz_mfmt_c,
                                            width = 5)
        self.cbtn_mmeas_fz_mfmt_p = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="pretty",
                                            variable=self.var_mmeas_fz_mfmt_p,
                                            width = 5)
        self.cbtn_mmeas_fz_mfmt_b = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="B85",
                                            variable=self.var_mmeas_fz_mfmt_b,
                                            width = 5)
        self.cbtn_mmeas_fz_mfmt_x = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="x",
                                             variable=self.var_mmeas_fz_mfmt_x,
                                           width = 5)
        self.cbtn_mmeas_fz_mfmt_xx = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="xx",
                                            variable=self.var_mmeas_fz_mfmt_xx,
                                            width = 5)
        self.cbtn_mmeas_fz_mfmt_xxx = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="x*",
                                            variable=self.var_mmeas_fz_mfmt_xxx,
                                            width = 5)
        self.cbtn_mmeas_fz_mfmt_r = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="raw",
                                            variable=self.var_mmeas_fz_mfmt_r,
                                            width = 5)
        self.cbtn_mmeas_fz_mfmt_h = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="header",
                                            variable=self.var_mmeas_fz_mfmt_h,
                                            width = 5)
        self.cbtn_mmeas_fz_mfmt_d = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="data",
                                            variable=self.var_mmeas_fz_mfmt_d,
                                            width = 5)
        self.cbtn_mmeas_fz_mfmt_v = ttk.Checkbutton(self.frm_mmeas_fz_mfmt,
                                            style = "BlkWht.TCheckbutton",
                                            text="verbose",
                                            variable=self.var_mmeas_fz_mfmt_v,
                                            width = 6)


        ####################
        # Layout

        # root frame contents
        self.notebook.grid(column = 0, row = 0, sticky = (N, W, E, S))
        self.frm_nav.grid(column = 0, row = 1)

        # notebook contents
        #frm_upload.grid(column = 0, row = 0)

        # frm_nav contents
        self.btn_quit.grid(column = 2, row = 0)

        # frm_settings contents
        self.btn_ser_refresh.grid(column = 1, row = 1, sticky = (W,E))
        self.btn_ser_connect.grid(column = 1, row = 2, sticky = (W,E))
        self.btn_ser_disconnect.grid(column = 1, row = 3, sticky = (W,E))
        self.cbox_ser_device.grid(column = 2, row = 1)
        self.cbox_ser_speed.grid(column = 2, row = 2)

        # frm_upload contents
        self.frm_upaction.grid(column = 2, row = 7)
        self.frm_rb_up.grid(column = 1, row = 4, sticky = (W, E))
        self.btn_up_src.grid(column = 1, row = 2, sticky = (W,E))
        self.ent_up_src.grid(column = 2, row = 2)
        self.btn_up_dest.grid(column = 1, row = 3, sticky = (W,E))
        self.cbox_up_dest.grid(column = 2, row = 3)
        self.btn_up_swap.grid(column = 1, row = 5, sticky = (W,E))
        self.btn_up_upload.grid(column = 0,row = 0 )

        # frm_rb_up contents
        self.rb_up_mode_txt.grid(column = 1, row = 0, sticky = (W,E))
        self.rb_up_mode_bin.grid(column = 1, row = 1, sticky = (W,E))

        # frm_download contents
        self.frm_down_action.grid(column = 2, row = 7)
        self.frm_rb_down.grid(column = 1, row = 4, sticky = (W, E))
        self.btn_down_dest.grid(column = 1, row = 3, sticky = (W,E))
        self.ent_down_dest.grid(column = 2, row = 3)
        self.btn_down_swap.grid(column = 1, row = 5, sticky = (W,E))
        self.btn_down_download.grid(column = 0,row = 0 )
        self.btn_down_src.grid(column = 1,row = 2, sticky = (W,E) )
        self.cbox_down_src.grid(column = 2,row = 2 )
        self.btn_down_swap.grid(column = 1, row = 5, sticky = (W,E))

        # frm_rb_down contents
        self.rb_down_mode_txt.grid(column = 1, row = 0, sticky = (W,E))
        self.rb_down_mode_bin.grid(column = 1, row = 1, sticky = (W,E))

        # frm_plot contents
        self.lbl_plot_depth.grid(column = 1, row = 1, sticky = (W,E))
        self.ent_plot_depth.grid(column = 2, row = 1)
        self.lbl_plot_period_sec.grid(column = 1, row = 2, sticky = (W,E))
        self.ent_plot_period_sec.grid(column = 2, row = 2)
        self.btn_plot_toggle.grid(column = 1, row = 3)
        self.frm_rb_stream_log.grid(column = 1, row = 5, sticky = (W, E))
        self.lbl_stream_log.grid(column = 1, row = 0, sticky = (W,E))
        self.rb_stream_log_en.grid(column = 1, row = 1, sticky = (W,E))
        self.rb_stream_log_di.grid(column = 1, row = 2, sticky = (W,E))
        self.lbl_stream_passive.grid(column = 1, row = 3, sticky = (W,E))
        self.rb_stream_passive_en.grid(column = 1, row = 4, sticky = (W,E))
        self.rb_stream_passive_di.grid(column = 1, row = 5, sticky = (W,E))

        # position plot control elements
        i=0
        while i<self.nplots:
            self.plot_controls[int(i)].lbl.grid(column = 4, row = i+1)
            self.plot_controls[int(i)].cbox.grid(column = 5, row = i+1)
            i+=1
        self.btn_plot_clear.grid(column = 4, row = i+1, sticky = (W, E))

        # frm_control contents
        self.lbl_licor_power.grid(column = 1, row = 1)
        self.btn_licor_power_on.grid(column = 2, row = 1)
        self.btn_licor_power_off.grid(column = 3, row = 1)
        self.lbl_cron.grid(column = 1, row = 2)
        self.btn_cron_en.grid(column = 2, row = 2)
        self.btn_cron_di.grid(column = 3, row = 2)
        self.lbl_send_escape.grid(column = 1, row = 3)
        self.btn_send_escape.grid(column = 2, row = 3)

        # frm_mdio contents
        self.lbl_mdio_port.grid(column = 1, row = 1)
        self.lbl_mdio_action.grid(column = 1, row = 2)

        self.sbox_mdio_port_type.grid(column=1, row=1)
        self.sbox_mdio_port_n.grid(column=2, row=1)
        self.sbox_mdio_port_sel.grid(column=3, row=1)
        self.sbox_mdio_op.grid(column=2, row=2)

        self.lbl_mdio_th.grid(column = 1, row = 3)
        self.ent_mdio_th.grid(column = 2, row = 3)

        self.lbl_mdio_tl.grid(column = 1, row = 4)
        self.ent_mdio_tl.grid(column = 2, row = 4)

        self.lbl_mdio_cyc.grid(column = 1, row = 5)
        self.ent_mdio_cyc.grid(column = 2, row = 5)

        self.lbl_mdio_pdel.grid(column = 1, row = 6)
        self.ent_mdio_pdel.grid(column = 2, row = 6)
        self.btn_mdio_io.grid(column = 2, row = 7)

        self.lbl_mdio_macro.grid(column = 1, row = 1)
        self.cbox_mdio_macro.grid(column = 2, row = 1)
        self.btn_mdio_macro.grid(column = 2, row = 2)

        self.rb_preset_dir_pos.grid(column = 1, row = 1)
        self.rb_preset_dir_neg.grid(column = 2, row = 1)
        self.lbl_mdio_preset.grid(column = 1, row = 3)
        self.cbox_mdio_preset.grid(column = 2, row = 4)
        self.btn_mdio_preset.grid(column = 2, row = 5)

        self.lbl_mdio_ovr_vh.grid(column = 1, row = 6)
        self.ent_mdio_ovr_vh.grid(column = 2, row = 6)

        self.lbl_mdio_ovr_ph.grid(column = 1, row = 7)
        self.ent_mdio_ovr_ph.grid(column = 2, row = 7)

        self.lbl_mdio_ovr_pl.grid(column = 1, row = 8)
        self.ent_mdio_ovr_pl.grid(column = 2, row = 8)

        self.lbl_mdio_ovr_pn.grid(column = 1, row = 9)
        self.ent_mdio_ovr_pn.grid(column = 2, row = 9)

        # mdio sub-frames
        self.frm_mdio_io_sel.grid(column = 2, row = 1)
        self.frm_mdio_preset_dir.grid(column = 2, row = 3)

        # mdio major frames
        self.frm_mdio_io_panel.grid(column = 1, row = 1, sticky = (N, S))
        self.frm_mdio_macro_panel.grid(column = 2, row = 1, sticky = (N, S))

        # frm_mmeas contents
        self.lbl_mmeas_tm.grid(column=1, row=1, sticky = (E, W))
        self.sbox_mmeas_tm.grid(column=2, row=1, sticky = (E, W))

        self.lbl_mmeas_handler.grid(column=1, row=2, sticky = (E, W))
        self.sbox_mmeas_handler.grid(column=2, row=2, sticky = (E, W))

        self.lbl_mmeas_ovr_p.grid(column=1, row=3, sticky = (E, W))
        self.ent_mmeas_ovr_p.grid(column=2, row=3, sticky = (E, W))
        self.lbl_mmeas_ovr_t.grid(column=1, row=4, sticky = (E, W))
        self.ent_mmeas_ovr_t.grid(column=2, row=4, sticky = (E, W))
        self.lbl_mmeas_ovr_n.grid(column=1, row=5, sticky = (E, W))
        self.ent_mmeas_ovr_n.grid(column=2, row=5, sticky = (E, W))
        self.lbl_mmeas_ovr_b.grid(column=1, row=6, sticky = (E, W))
        self.ent_mmeas_ovr_b.grid(column=2, row=6, sticky = (E, W))
        self.lbl_mmeas_ovr_w.grid(column=1, row=7, sticky = (E, W))
        self.ent_mmeas_ovr_w.grid(column=2, row=7, sticky = (E, W))

        self.lbl_mmeas_rmid.grid(column=1, row=8, sticky = (E, W))
        self.sbox_mmeas_rmid.grid(column=2, row=8, sticky = (E, W))
        self.lbl_mmeas_mmid.grid(column=1, row=9, sticky = (E, W))
        self.sbox_mmeas_mmid.grid(column=2, row=9, sticky = (E, W))

        self.btn_mmeas_tm.grid(column = 2, row = 13)

        self.lbl_mmeas_cyc.grid(column=1, row=14, sticky = (E, W))
        self.btn_mmeas_cyc.grid(column=2, row=14)
        self.lbl_mmeas_status.grid(column=1, row=2)
        self.ent_mmeas_status.grid(column=2, row=2, sticky = (E, W))

        # mmeas sub-frames
        # mmeas major frames
        self.frm_mmeas_tm_panel.grid(column = 1, row = 1)

        ####################
        # MMEAS CYC tab
        # frm_mmeas_cyc
        self.lbl_mmeas_cyc_op.grid(column = 1, row = 1)
        self.lbl_mmeas_cyc_rtid.grid(column = 1, row = 2)
        self.sbox_mmeas_cyc_rtid.grid(column = 2, row = 2)
        self.lbl_mmeas_cyc_rmid.grid(column = 1, row = 3)
        self.sbox_mmeas_cyc_rmid.grid(column = 2, row = 3)
        # frm_mmeas_cyc_op
        self.frm_mmeas_cyc_op.grid(column = 2, row = 1)
        self.cbtn_mmeas_cyc_rst.grid(column=1, row=1)
        self.cbtn_mmeas_cyc_start.grid(column=2, row=1)
        self.cbtn_mmeas_cyc_end.grid(column=1, row=2)

        ####################
        # MMEAS FO, FZ tabs
        self.notebook_mmeas.grid(column = 2, row = 1)
        self.frm_mmeas_fo_cfmt.grid(column = 2, row = 2)
        self.frm_mmeas_fo_hfmt.grid(column = 2, row = 3)
        self.frm_mmeas_fz_cfmt.grid(column = 2, row = 2)
        self.frm_mmeas_fz_mfmt.grid(column = 2, row = 3)
        # frm_mmeas_fo
        self.lbl_mmeas_fo_log.grid(column = 1, row = 1)
        self.sbox_mmeas_fo_log.grid(column = 2, row = 1)
        self.lbl_mmeas_fo_cons.grid(column=1, row=2)
        self.lbl_mmeas_fo_host.grid(column=1, row=3)
        # frm_mmeas_fo_cfmt
        self.cbtn_mmeas_fo_cons.grid(column=1, row=1)
        self.cbtn_mmeas_fo_cfmt_c.grid(column=1, row=2)
        self.cbtn_mmeas_fo_cfmt_p.grid(column=2, row=2)
        self.cbtn_mmeas_fo_cfmt_b.grid(column=1, row=3)
        self.cbtn_mmeas_fo_cfmt_x.grid(column=2, row=3)
        self.cbtn_mmeas_fo_cfmt_xx.grid(column=1, row=4)
        self.cbtn_mmeas_fo_cfmt_xxx.grid(column=2, row=4)
        self.cbtn_mmeas_fo_cfmt_h.grid(column=1, row=5)
        self.cbtn_mmeas_fo_cfmt_d.grid(column=2, row=5)
        self.cbtn_mmeas_fo_cfmt_r.grid(column=1, row=6)
        self.cbtn_mmeas_fo_cfmt_v.grid(column=2, row=6)
        # frm_mmeas_fo_hfmt
        self.cbtn_mmeas_fo_host.grid(column=1, row=1)
        self.cbtn_mmeas_fo_hfmt_c.grid(column=1, row=2)
        self.cbtn_mmeas_fo_hfmt_p.grid(column=2, row=2)
        self.cbtn_mmeas_fo_hfmt_b.grid(column=1, row=3)
        self.cbtn_mmeas_fo_hfmt_x.grid(column=2, row=3)
        self.cbtn_mmeas_fo_hfmt_xx.grid(column=1, row=4)
        self.cbtn_mmeas_fo_hfmt_xxx.grid(column=2, row=4)
        self.cbtn_mmeas_fo_hfmt_h.grid(column=1, row=5)
        self.cbtn_mmeas_fo_hfmt_d.grid(column=2, row=5)
        self.cbtn_mmeas_fo_hfmt_r.grid(column=1, row=6)
        self.cbtn_mmeas_fo_hfmt_v.grid(column=2, row=6)

        # frm_mmeas_fz
        self.lbl_mmeas_fz_log.grid(column = 1, row = 1)
        self.sbox_mmeas_fz_log.grid(column = 2, row = 1)
        self.lbl_mmeas_fz_cbuf.grid(column=1, row=2)
        self.lbl_mmeas_fz_mbuf.grid(column=1, row=3)
        # frm_mmeas_fz_cfmt
        self.cbtn_mmeas_fz_cbuf_c.grid(column=1, row=1)
        self.cbtn_mmeas_fz_cbuf_h.grid(column=2, row=1)
        self.cbtn_mmeas_fz_cbuf_l.grid(column=1, row=2)
        self.cbtn_mmeas_fz_cbuf_q.grid(column=2, row=2)
        self.cbtn_mmeas_fz_cfmt_c.grid(column=1, row=3)
        self.cbtn_mmeas_fz_cfmt_p.grid(column=2, row=3)
        self.cbtn_mmeas_fz_cfmt_b.grid(column=1, row=4)
        self.cbtn_mmeas_fz_cfmt_x.grid(column=2, row=4)
        self.cbtn_mmeas_fz_cfmt_xx.grid(column=1, row=5)
        self.cbtn_mmeas_fz_cfmt_xxx.grid(column=2, row=5)
        self.cbtn_mmeas_fz_cfmt_h.grid(column=1, row=6)
        self.cbtn_mmeas_fz_cfmt_d.grid(column=2, row=6)
        self.cbtn_mmeas_fz_cfmt_r.grid(column=1, row=7)
        self.cbtn_mmeas_fz_cfmt_v.grid(column=2, row=7)
        # frm_mmeas_fz_mfmt
        self.cbtn_mmeas_fz_mbuf_c.grid(column=1, row=1)
        self.cbtn_mmeas_fz_mbuf_h.grid(column=2, row=1)
        self.cbtn_mmeas_fz_mbuf_l.grid(column=1, row=2)
        self.cbtn_mmeas_fz_mbuf_q.grid(column=2, row=2)
        self.cbtn_mmeas_fz_mfmt_c.grid(column=1, row=3)
        self.cbtn_mmeas_fz_mfmt_p.grid(column=2, row=3)
        self.cbtn_mmeas_fz_mfmt_b.grid(column=1, row=4)
        self.cbtn_mmeas_fz_mfmt_x.grid(column=2, row=4)
        self.cbtn_mmeas_fz_mfmt_xx.grid(column=1, row=5)
        self.cbtn_mmeas_fz_mfmt_xxx.grid(column=2, row=5)
        self.cbtn_mmeas_fz_mfmt_h.grid(column=1, row=6)
        self.cbtn_mmeas_fz_mfmt_d.grid(column=2, row=6)
        self.cbtn_mmeas_fz_mfmt_r.grid(column=1, row=7)
        self.cbtn_mmeas_fz_mfmt_v.grid(column=2, row=7)

        return

    def setSerialDeviceList(self,device_list):
        self.cbox_ser_device['values']=()
        for f_name in device_list:
            if f_name not in dir(self.cbox_ser_device['values']):
                self.cbox_ser_device['values'] = (*self.cbox_ser_device['values'],f_name)
        try:
            self.cbox_ser_device.current(0)
        except Exception as e:
            cwg_log.exception("cbox_ser_device error")
        return

    def setUploadSource(self, source):
        self.ent_up_src.delete(0,END)
        self.ent_up_src.insert(0,source)
        return

    def setUploadDest(self, cwg_files, selected_idx):
        self.cbox_up_dest['values'] = ()
        for f_name in cwg_files[0:] :
            if f_name not in self.cbox_up_dest['values']:
                self.cbox_up_dest['values'] = (*self.cbox_up_dest['values'],f_name)
            self.cbox_up_dest.current(selected_idx)
        return

    def setSelectUploadDest(self, dest):
        if dest not in self.cbox_up_dest['values']:
            self.cbox_up_dest['values'] = (*self.cbox_up_dest['values'],dest)
        index = self.cbox_up_dest['values'].index(dest)
        self.cbox_up_dest.current(index)
        return

    def setDownloadDest(self, dest):
        self.ent_down_dest.delete(0,END)
        self.ent_down_dest.insert(0,dest)
        return

    def setDownloadSource(self, cwg_files, selected_idx):
        self.cbox_down_src['values'] = ()
        for f_name in cwg_files[0:] :
            if f_name not in self.cbox_down_src['values']:
                self.cbox_down_src['values'] = (*self.cbox_down_src['values'],f_name)
        self.cbox_down_src.current(selected_idx)
        return

    def setSelectDownloadSource(self, src):
        if src not in self.cbox_down_src['values']:
            self.cbox_down_src['values'] = (*self.cbox_down_src['values'],src)
        index = self.cbox_down_src['values'].index(src)
        self.cbox_down_src.current(index)
        return

    def ClearPlots(self):
        i = 0
        while i < self.nplots:
            self.plot_controls[i].Reset()
            depth=int(self.ent_plot_depth.get())
            self.xs = [int(depth)]
#            if depth > 0 :
#                depth*=-1
#            self.xs = self.xs[int(depth):]
            i += 1
        # end while

    def UpdatePlot(self, dlist, test):
        rval = 0
#        frame = currentframe()
#        print("TRACE ",inspect.getframeinfo(frame).lineno)
        i=0
        try:
            # get timestamp
            tstamp=dlist[0]
            # append x-axis value
            self.xs.append(dt.datetime.fromisoformat(tstamp[0:19]).strftime('%Y-%m-%dT%H:%M:%S'))

            # Trim x point arrays to configured depth
            depth=int(self.ent_plot_depth.get())
            if depth > 0 :
                depth*=-1
            self.xs = self.xs[int(depth):]

            # update plots
            i = 0
            while i < self.nplots:
                field = self.plot_controls[i].GetField()
                if field >= 0:
                    self.plot_controls[i].SetDepth(depth)
                    if field < len(dlist)-1:
                        self.plot_controls[i].Update(self.xs,float(dlist[int(field)]))
                    else:
                        self.plot_controls[i].Clear()
                i += 1
            # end while
        except Exception as e:
            cwg_log.exception("UpdatePlot: field["+str(i)+"]:"+str(self.plot_controls[i].GetField()))
            rval = 1

        return rval
