// appkvdemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ftd2xx.h"
#include "appkvdemo.h"
#include "appkvdemoDlg.h"
#include "serial_link_hw.h"
#include "AppKV_PSU.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAppkvdemoDlg dialog

CAppkvdemoDlg::CAppkvdemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAppkvdemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAppkvdemoDlg)
	m_status = _T("");
	m_high_voltages_on = FALSE;
	m_demanded_voltage_0 = 0.0f;
	m_demanded_voltage_1 = 0.0f;
	m_demanded_voltage_2 = 0.0f;
	m_demanded_voltage_3 = 0.0f;
	m_demanded_voltage_4 = 0.0f;
	m_demanded_voltage_5 = 0.0f;
	m_demanded_voltage_6 = 0.0f;
	m_demanded_voltage_7 = 0.0f;
	m_demanded_voltage_8 = 0.0f;
	m_demanded_voltage_9 = 0.0f;
	m_demanded_voltage_10= 0.0f;
	m_demanded_voltage_11= 0.0f;
	m_demanded_voltage_12= 0.0f;
	m_demanded_voltage_13= 0.0f;
	m_demanded_voltage_14= 0.0f;
	m_demanded_voltage_15= 0.0f;
	m_readback_voltage_0 = 0 ;
	m_readback_voltage_1 = 0 ;
	m_readback_voltage_2 = 0 ;
	m_readback_voltage_3 = 0 ;
	m_readback_voltage_4 = 0 ;
	m_readback_voltage_5 = 0 ;
	m_readback_voltage_6 = 0 ;
	m_readback_voltage_7 = 0 ;
	m_readback_voltage_8 = 0 ;
	m_readback_voltage_9 = 0 ;
	m_readback_voltage_10= 0 ;
	m_readback_voltage_11= 0 ;
	m_readback_voltage_12= 0 ;
	m_readback_voltage_13= 0 ;
	m_readback_voltage_14= 0 ;
	m_readback_voltage_15= 0 ;
	m_readback_current_0 = 0 ;
	m_readback_current_1 = 0 ;
	m_readback_current_2 = 0 ;
	m_readback_current_3 = 0 ;
	m_readback_current_4 = 0 ;
	m_readback_current_5 = 0 ;
	m_readback_current_6 = 0 ;
	m_readback_current_7 = 0 ;
	m_readback_current_8 = 0 ;
	m_readback_current_9 = 0 ;
	m_readback_current_10= 0 ;
	m_readback_current_11= 0 ;
	m_readback_current_12= 0 ;
	m_readback_current_13= 0 ;
	m_readback_current_14= 0 ;
	m_readback_current_15= 0 ;
	m_present_0 = _T("");
	m_present_1 = _T("");
	m_present_2 = _T("");
	m_present_3 = _T("");
	m_present_4 = _T("");
	m_present_5 = _T("");
	m_present_6 = _T("");
	m_present_7 = _T("");
	m_present_8 = _T("");
	m_present_9 = _T("");
	m_present_10= _T("");
	m_present_11= _T("");
	m_present_12= _T("");
	m_present_13= _T("");
	m_present_14= _T("");
	m_present_15= _T("");
	m_pump_present_0 = _T("");
	m_pump_readback_voltage = 0;
	m_pump_0_enable = FALSE;
	m_negative_1 = FALSE;
	m_negative_2 = FALSE;
	m_negative_3 = FALSE;
	m_negative_4 = FALSE;
	m_negative_5 = FALSE;
	m_negative_6 = FALSE;
	m_negative_7 = FALSE;
	m_negative_8 = FALSE;
	m_negative_9 = FALSE;
	m_negative_10= FALSE;
	m_negative_11= FALSE;
	m_negative_12= FALSE;
	m_negative_13= FALSE;
	m_negative_14= FALSE;
	m_negative_15= FALSE;
	m_local_on_0 = FALSE;
	m_local_on_1 = FALSE;
	m_local_on_2 = FALSE;
	m_local_on_3 = FALSE;
	m_local_on_4 = FALSE;
	m_local_on_5 = FALSE;
	m_local_on_6 = FALSE;
	m_local_on_7 = FALSE;
	m_local_on_8 = FALSE;
	m_local_on_9 = FALSE;
	m_local_on_10= FALSE;
	m_local_on_11= FALSE;
	m_local_on_12= FALSE;
	m_local_on_13= FALSE;
	m_local_on_14= FALSE;
	m_local_on_15= FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	model_0 = 0 ;
	model_1 = 0 ;
	model_2 = 0 ;
	model_3 = 0 ;
	model_4 = 0 ;
	model_5 = 0 ;
	model_6 = 0 ;
	model_7 = 0 ;
	model_8 = 0 ;
	model_9 = 0 ;
	model_10 = 0;
	model_11 = 0;
	model_12 = 0;
	model_13 = 0;
	model_14 = 0;
	model_15 = 0;

}

void CAppkvdemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAppkvdemoDlg)
	DDX_Control(pDX, IDC_MODEL_NUMBER_0, m_model_number_0);
	DDX_Control(pDX, IDC_MODEL_NUMBER_1 ,m_model_number_1 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_2 ,m_model_number_2 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_3 ,m_model_number_3 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_4 ,m_model_number_4 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_5 ,m_model_number_5 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_6 ,m_model_number_6 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_7 ,m_model_number_7 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_8 ,m_model_number_8 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_9 ,m_model_number_9 );
	DDX_Control(pDX, IDC_MODEL_NUMBER_10,m_model_number_10);
	DDX_Control(pDX, IDC_MODEL_NUMBER_11,m_model_number_11);
	DDX_Control(pDX, IDC_MODEL_NUMBER_12,m_model_number_12);
	DDX_Control(pDX, IDC_MODEL_NUMBER_13,m_model_number_13);
	DDX_Control(pDX, IDC_MODEL_NUMBER_14,m_model_number_14);
	DDX_Control(pDX, IDC_MODEL_NUMBER_15,m_model_number_15);
	DDX_Text(pDX, IDC_STATUS, m_status);
	DDX_Check(pDX, IDC_HIGH_VOLTAGES_ON, m_high_voltages_on);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_0 , m_demanded_voltage_0);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_1 , m_demanded_voltage_1);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_2 , m_demanded_voltage_2);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_3 , m_demanded_voltage_3);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_4 , m_demanded_voltage_4);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_5 , m_demanded_voltage_5);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_6 , m_demanded_voltage_6);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_7 , m_demanded_voltage_7);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_8 , m_demanded_voltage_8);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_9 , m_demanded_voltage_9);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_10, m_demanded_voltage_10);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_11, m_demanded_voltage_11);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_12, m_demanded_voltage_12);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_13, m_demanded_voltage_13);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_14, m_demanded_voltage_14);
	DDX_Text(pDX, IDC_DEMANDED_VOLTAGE_15, m_demanded_voltage_15);
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_0 , m_readback_voltage_0 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_1 , m_readback_voltage_1 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_2 , m_readback_voltage_2 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_3 , m_readback_voltage_3 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_4 , m_readback_voltage_4 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_5 , m_readback_voltage_5 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_6 , m_readback_voltage_6 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_7 , m_readback_voltage_7 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_8 , m_readback_voltage_8 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_9 , m_readback_voltage_9 );
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_10, m_readback_voltage_10);
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_11, m_readback_voltage_11);
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_12, m_readback_voltage_12);
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_13, m_readback_voltage_13);
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_14, m_readback_voltage_14);
	DDX_Text(pDX, IDC_READBACK_VOLTAGE_15, m_readback_voltage_15);
	DDX_Text(pDX, IDC_READBACK_CURRENT_0 , m_readback_current_0 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_1 , m_readback_current_1 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_2 , m_readback_current_2 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_3 , m_readback_current_3 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_4 , m_readback_current_4 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_5 , m_readback_current_5 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_6 , m_readback_current_6 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_7 , m_readback_current_7 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_8 , m_readback_current_8 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_9 , m_readback_current_9 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_10 , m_readback_current_10 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_11 , m_readback_current_11 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_12 , m_readback_current_12 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_13 , m_readback_current_13 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_14 , m_readback_current_14 );
	DDX_Text(pDX, IDC_READBACK_CURRENT_15 , m_readback_current_15 );
	DDX_Text(pDX, IDC_PRESENT_0 , m_present_0 );
	DDX_Text(pDX, IDC_PRESENT_1 , m_present_1 );
	DDX_Text(pDX, IDC_PRESENT_2 , m_present_2 );
	DDX_Text(pDX, IDC_PRESENT_3 , m_present_3 );
	DDX_Text(pDX, IDC_PRESENT_4 , m_present_4 );
	DDX_Text(pDX, IDC_PRESENT_5 , m_present_5 );
	DDX_Text(pDX, IDC_PRESENT_6 , m_present_6 );
	DDX_Text(pDX, IDC_PRESENT_7 , m_present_7 );
	DDX_Text(pDX, IDC_PRESENT_8 , m_present_8 );
	DDX_Text(pDX, IDC_PRESENT_9 , m_present_9 );
	DDX_Text(pDX, IDC_PRESENT_10, m_present_10);
	DDX_Text(pDX, IDC_PRESENT_11, m_present_11);
	DDX_Text(pDX, IDC_PRESENT_12, m_present_12);
	DDX_Text(pDX, IDC_PRESENT_13, m_present_13);
	DDX_Text(pDX, IDC_PRESENT_14, m_present_14);
	DDX_Text(pDX, IDC_PRESENT_15, m_present_15);
	DDX_Control(pDX, IDC_NEGATIVE_0, m_negative_b0);
	DDX_Control(pDX, IDC_NEGATIVE_1, m_negative_b1);
	DDX_Control(pDX, IDC_NEGATIVE_2, m_negative_b2);
	DDX_Control(pDX, IDC_NEGATIVE_3, m_negative_b3);
	DDX_Control(pDX, IDC_NEGATIVE_4, m_negative_b4);
	DDX_Control(pDX, IDC_NEGATIVE_5, m_negative_b5);
	DDX_Control(pDX, IDC_NEGATIVE_6, m_negative_b6);
	DDX_Control(pDX, IDC_NEGATIVE_7, m_negative_b7);
	DDX_Control(pDX, IDC_NEGATIVE_8, m_negative_b8);
	DDX_Control(pDX, IDC_NEGATIVE_9, m_negative_b9);
	DDX_Control(pDX, IDC_NEGATIVE_10, m_negative_b10);
	DDX_Control(pDX, IDC_NEGATIVE_11, m_negative_b11);
	DDX_Control(pDX, IDC_NEGATIVE_12, m_negative_b12);
	DDX_Control(pDX, IDC_NEGATIVE_13, m_negative_b13);
	DDX_Control(pDX, IDC_NEGATIVE_14, m_negative_b14);
	DDX_Control(pDX, IDC_NEGATIVE_15, m_negative_b15);
	DDX_Text(pDX, IDC_PUMP_PRESENT_0, m_pump_present_0);
	DDX_Text(pDX, IDC_PUMP_READBACK_VOLTAGE_0, m_pump_readback_voltage);
	DDX_Check(pDX, IDC_PUMP_ENABLE, m_pump_0_enable);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_0, m_local_on_0);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_1, m_local_on_1);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_2, m_local_on_2);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_3, m_local_on_3);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_4, m_local_on_4);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_5, m_local_on_5);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_6, m_local_on_6);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_7, m_local_on_7);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_8, m_local_on_8);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_9, m_local_on_9);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_10, m_local_on_10);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_11, m_local_on_11);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_12, m_local_on_12);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_13, m_local_on_13);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_14, m_local_on_14);
	DDX_Check(pDX, IDC_LOCAL_HT_ON_15, m_local_on_15);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAppkvdemoDlg, CDialog)
	//{{AFX_MSG_MAP(CAppkvdemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_HIGH_VOLTAGES_ON, OnHighVoltagesOn)
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_0 , OnSetfocusDemandedVoltage0 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_1 , OnSetfocusDemandedVoltage1 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_2 , OnSetfocusDemandedVoltage2 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_3 , OnSetfocusDemandedVoltage3 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_4 , OnSetfocusDemandedVoltage4 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_5 , OnSetfocusDemandedVoltage5 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_6 , OnSetfocusDemandedVoltage6 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_7 , OnSetfocusDemandedVoltage7 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_8 , OnSetfocusDemandedVoltage8 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_9 , OnSetfocusDemandedVoltage9 )
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_10, OnSetfocusDemandedVoltage10)
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_11, OnSetfocusDemandedVoltage11)
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_12, OnSetfocusDemandedVoltage12)
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_13, OnSetfocusDemandedVoltage13)
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_14, OnSetfocusDemandedVoltage14)
	ON_EN_SETFOCUS(IDC_DEMANDED_VOLTAGE_15, OnSetfocusDemandedVoltage15)
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_0 , OnKillfocusDemandedVoltage0 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_1 , OnKillfocusDemandedVoltage1 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_2 , OnKillfocusDemandedVoltage2 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_3 , OnKillfocusDemandedVoltage3 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_4 , OnKillfocusDemandedVoltage4 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_5 , OnKillfocusDemandedVoltage5 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_6 , OnKillfocusDemandedVoltage6 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_7 , OnKillfocusDemandedVoltage7 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_8 , OnKillfocusDemandedVoltage8 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_9 , OnKillfocusDemandedVoltage9 )
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_10, OnKillfocusDemandedVoltage10)
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_11, OnKillfocusDemandedVoltage11)
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_12, OnKillfocusDemandedVoltage12)
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_13, OnKillfocusDemandedVoltage13)
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_14, OnKillfocusDemandedVoltage14)
	ON_EN_KILLFOCUS(IDC_DEMANDED_VOLTAGE_15, OnKillfocusDemandedVoltage15)
	ON_BN_CLICKED(IDC_MODEL_NUMBER_0, OnModelNumber0 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_1, OnModelNumber1 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_2, OnModelNumber2 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_3 ,OnModelNumber3 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_4 ,OnModelNumber4 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_5 ,OnModelNumber5 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_6 ,OnModelNumber6 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_7 ,OnModelNumber7 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_8 ,OnModelNumber8 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_9 ,OnModelNumber9 )
	ON_BN_CLICKED(IDC_MODEL_NUMBER_10,OnModelNumber10)
	ON_BN_CLICKED(IDC_MODEL_NUMBER_11,OnModelNumber11)
	ON_BN_CLICKED(IDC_MODEL_NUMBER_12,OnModelNumber12)
	ON_BN_CLICKED(IDC_MODEL_NUMBER_13,OnModelNumber13)
	ON_BN_CLICKED(IDC_MODEL_NUMBER_14,OnModelNumber14)
	ON_BN_CLICKED(IDC_MODEL_NUMBER_15,OnModelNumber15)
	ON_WM_MOUSEWHEEL()
	ON_BN_CLICKED(IDC_NEGATIVE_0 ,OnNegative0 )
	ON_BN_CLICKED(IDC_NEGATIVE_1 ,OnNegative1 )
	ON_BN_CLICKED(IDC_NEGATIVE_2 ,OnNegative2 )
	ON_BN_CLICKED(IDC_NEGATIVE_3 ,OnNegative3 )
	ON_BN_CLICKED(IDC_NEGATIVE_4 ,OnNegative4 )
	ON_BN_CLICKED(IDC_NEGATIVE_5 ,OnNegative5 )
	ON_BN_CLICKED(IDC_NEGATIVE_6 ,OnNegative6 )
	ON_BN_CLICKED(IDC_NEGATIVE_7 ,OnNegative7 )
	ON_BN_CLICKED(IDC_NEGATIVE_8 ,OnNegative8 )
	ON_BN_CLICKED(IDC_NEGATIVE_9 ,OnNegative9 )
	ON_BN_CLICKED(IDC_NEGATIVE_10,OnNegative10)
	ON_BN_CLICKED(IDC_NEGATIVE_11,OnNegative11)
	ON_BN_CLICKED(IDC_NEGATIVE_12,OnNegative12)
	ON_BN_CLICKED(IDC_NEGATIVE_13,OnNegative13)
	ON_BN_CLICKED(IDC_NEGATIVE_14,OnNegative14)
	ON_BN_CLICKED(IDC_NEGATIVE_15,OnNegative15)
	ON_WM_MOVE()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_PUMP_ENABLE, OnPumpEnable)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_0, OnLocalHtOn0)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_1, OnLocalHtOn1)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_2, OnLocalHtOn2)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_3, OnLocalHtOn3)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_4, OnLocalHtOn4)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_5, OnLocalHtOn5)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_6, OnLocalHtOn6)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_7, OnLocalHtOn7)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_8, OnLocalHtOn8)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_9, OnLocalHtOn9)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_10, OnLocalHtOn10)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_11, OnLocalHtOn11)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_12, OnLocalHtOn12)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_13, OnLocalHtOn13)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_14, OnLocalHtOn14)
	ON_BN_CLICKED(IDC_LOCAL_HT_ON_15, OnLocalHtOn15)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAppkvdemoDlg message handlers

BOOL CAppkvdemoDlg::OnInitDialog()
{
	HighVoltagesOn = FALSE ;
 
	CDialog::OnInitDialog();
	active_tune = 0 ;
	Loadem() ;
   for(UINT n = 0 ; n < 15; n++)
   {
		app_kv_psu[n] = new CAppKV_PSU() ;
		if(app_kv_psu[n] == 0){  
			AfxMessageBox("Could not create the psu object",MB_OK,0); 
			exit(1) ; 
		} 
   }

   UINT data = SetTimer(1025,500,NULL) ;

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	HP_DataStructure current_hp_struct ;
	CFile parameter_file ;
	if(!parameter_file.Open("app_kv.cfg",CFile::modeRead)) // check to see if file exists
	{
    
	}else{
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_0 = current_hp_struct.command_voltage ;
		m_negative_0		 = current_hp_struct.polarity ;
		model_0				 = current_hp_struct.power_supply_type ;
		m_local_on_0		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_1 = current_hp_struct.command_voltage ;
		m_negative_1		 = current_hp_struct.polarity ;
		model_1				 = current_hp_struct.power_supply_type ;
		m_local_on_1		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_2 = current_hp_struct.command_voltage ;
		m_negative_2		 = current_hp_struct.polarity ;
		model_2				 = current_hp_struct.power_supply_type ;
		m_local_on_2		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_3 = current_hp_struct.command_voltage ;
		m_negative_3		 = current_hp_struct.polarity ;
		model_3				 = current_hp_struct.power_supply_type ;
		m_local_on_3		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_4 = current_hp_struct.command_voltage ;
		m_negative_4		 = current_hp_struct.polarity ;
		model_4				 = current_hp_struct.power_supply_type ;
		m_local_on_4		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_5 = current_hp_struct.command_voltage ;
		m_negative_5		 = current_hp_struct.polarity ;
		model_5				 = current_hp_struct.power_supply_type ;
		m_local_on_5		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_6 = current_hp_struct.command_voltage ;
		m_negative_6		 = current_hp_struct.polarity ;
		model_6				 = current_hp_struct.power_supply_type ;
		m_local_on_6		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_7 = current_hp_struct.command_voltage ;
		m_negative_7		 = current_hp_struct.polarity ;
		model_7				 = current_hp_struct.power_supply_type ;
		m_local_on_7		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_8 = current_hp_struct.command_voltage ;
		m_negative_8		 = current_hp_struct.polarity ;
		model_8				 = current_hp_struct.power_supply_type ;
		m_local_on_8		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_9 = current_hp_struct.command_voltage ;
		m_negative_9		 = current_hp_struct.polarity ;
		model_9				 = current_hp_struct.power_supply_type ;
		m_local_on_9		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_10= current_hp_struct.command_voltage ;
		m_negative_10		 = current_hp_struct.polarity ;
		model_10			 = current_hp_struct.power_supply_type ;
		m_local_on_10		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_11= current_hp_struct.command_voltage ;
		m_negative_11		 = current_hp_struct.polarity ;
		model_11			 = current_hp_struct.power_supply_type ;
		m_local_on_11		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_12= current_hp_struct.command_voltage ;
		m_negative_12		 = current_hp_struct.polarity ;
		model_12			 = current_hp_struct.power_supply_type ;
		m_local_on_12		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_13= current_hp_struct.command_voltage ;
		m_negative_13		 = current_hp_struct.polarity ;
		model_13			 = current_hp_struct.power_supply_type ;
		m_local_on_13		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_14= current_hp_struct.command_voltage ;
		m_negative_14		 = current_hp_struct.polarity ;
		model_14			 = current_hp_struct.power_supply_type ;
		m_local_on_14		 = current_hp_struct.local_ht_on ;
		parameter_file.Read(&current_hp_struct,sizeof(current_hp_struct)) ;
		m_demanded_voltage_15= current_hp_struct.command_voltage ;
		m_negative_15	     = current_hp_struct.polarity ;
		model_15			 = current_hp_struct.power_supply_type ;
		m_local_on_15		 = current_hp_struct.local_ht_on ;
	}
 
	UpdateData(FALSE) ;
	UpdateAllCButtons() ;
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAppkvdemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAppkvdemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
 	 	CDialog::OnPaint();
 		CClientDC  dc(this ); 

		COLORREF fred = dc.GetBkColor( ) ;

	CRect rect;
	GetClientRect(&rect);
	CDC dcMemory;
	dcMemory.CreateCompatibleDC(&dc);
	dc.SetBkColor(RGB(0,0,0)) ;
	SetBkColor(dc.m_hDC ,RGB(0,0,0)) ;
   CBitmap bmp;
   BOOL data = bmp.LoadBitmap( IDB_BITMAP8) ;
 
    BITMAP bmpInfo; 
    bmp.GetBitmap(&bmpInfo);

	// Select the bitmap into the in-memory DC
	CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
 	// Find a centerpoint for the bitmap in the client area
	int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
	int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
	nX =  400 ;
	nY = 0 ;
	// Copy the bits from the in-memory DC into the on-
	// screen DC to actually do the painting. Use the centerpoint
	// we computed for the target offset.
 	UINT dataa = dc.BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 
    0, 0, SRCCOPY    );

	dcMemory.SelectObject(pOldBitmap);
 	  	CDialog::OnPaint();
 
	}
}




// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAppkvdemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


////////////////  driver bits ////////////

void CAppkvdemoDlg::LoadAppKVDll()
{
	m_app_kv_hmodule = LoadLibrary("app_kv_dll.dll");	
	if(m_app_kv_hmodule == NULL)
	{
		AfxMessageBox("Error: Can't Load app_kv_dll.dll");
		return;
	}
    m_pWrite = (PtrToWrite)GetProcAddress(m_hmodule, "FT_Write");
	if (m_pWrite == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_Write");
		return;
	}


}



void CAppkvdemoDlg::Loadem()
{
	unsigned char txbuf[25], rxbuf[25];
	DWORD ret_bytes;

	LoadDLL();
	FT_STATUS status;

	ULONG x=0;
 	status = Open((PVOID)x);//load default device 0
	ResetDevice();
	SetTimeouts(3000, 3000);//extend timeout while board DLP-USB2 finishes reset
	Purge(FT_PURGE_RX || FT_PURGE_TX);
	//test for presence of board
	txbuf[0] = 0x00;
	Write(txbuf, 1, &ret_bytes);
	Read(rxbuf, 1, &ret_bytes);
	board_present=1;
	if(ret_bytes!=1)
	{
 		board_present=0;
		AfxMessageBox("Error: Cannot find usb pod board");

	}else{
		if(rxbuf[0] & 0x1e) // test readback bits should be 0 on bits 1 2 3 4
		{
 			board_present = 0;
			AfxMessageBox("Error: wrong feedback data on usb pod board");
		}
	}
 	SetTimeouts(300, 300);
}
void CAppkvdemoDlg::LoadDLL()
{
	m_hmodule = LoadLibrary("Ftd2xx.dll");	
	if(m_hmodule == NULL)
	{
		AfxMessageBox("Error: Can't Load ft8u245.dll");
		return;
	}

	m_pWrite = (PtrToWrite)GetProcAddress(m_hmodule, "FT_Write");
	if (m_pWrite == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_Write");
		return;
	}

	m_pRead = (PtrToRead)GetProcAddress(m_hmodule, "FT_Read");
	if (m_pRead == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_Read");
		return;
	}

	m_pOpen = (PtrToOpen)GetProcAddress(m_hmodule, "FT_Open");
	if (m_pOpen == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_Open");
		return;
	}

	m_pOpenEx = (PtrToOpenEx)GetProcAddress(m_hmodule, "FT_OpenEx");
	if (m_pOpenEx == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_OpenEx");
		return;
	}

	m_pListDevices = (PtrToListDevices)GetProcAddress(m_hmodule, "FT_ListDevices");
	if(m_pListDevices == NULL)
		{
			AfxMessageBox("Error: Can't Find FT_ListDevices");
			return;
		}

	m_pClose = (PtrToClose)GetProcAddress(m_hmodule, "FT_Close");
	if (m_pClose == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_Close");
		return;
	}

	m_pResetDevice = (PtrToResetDevice)GetProcAddress(m_hmodule, "FT_ResetDevice");
	if (m_pResetDevice == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_ResetDevice");
		return;
	}

	m_pPurge = (PtrToPurge)GetProcAddress(m_hmodule, "FT_Purge");
	if (m_pPurge == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_Purge");
		return;
	}

	m_pSetTimeouts = (PtrToSetTimeouts)GetProcAddress(m_hmodule, "FT_SetTimeouts");
	if (m_pSetTimeouts == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_SetTimeouts");
		return;
	}

	m_pGetQueueStatus = (PtrToGetQueueStatus)GetProcAddress(m_hmodule, "FT_GetQueueStatus");
	if (m_pGetQueueStatus == NULL)
	{
		AfxMessageBox("Error: Can't Find FT_GetQueueStatus");
		return;
	}
}	
//****************************************************************************************
FT_STATUS CAppkvdemoDlg::ResetDevice()
{
	if (!m_pResetDevice)
	{
		AfxMessageBox("FT_ResetDevice is not valid!"); 
		return FT_INVALID_HANDLE;
	}
	
	return (*m_pResetDevice)(m_ftHandle);
}	



//****************************************************************************************
FT_STATUS CAppkvdemoDlg::Purge(ULONG dwMask)
{
	if (!m_pPurge)
	{
		AfxMessageBox("FT_Purge is not valid!"); 
		return FT_INVALID_HANDLE;
	}

	return (*m_pPurge)(m_ftHandle, dwMask);
}	



//****************************************************************************************
FT_STATUS CAppkvdemoDlg::SetTimeouts(ULONG dwReadTimeout, ULONG dwWriteTimeout)
{
	if (!m_pSetTimeouts)
	{
		AfxMessageBox("FT_SetTimeouts is not valid!"); 
		return FT_INVALID_HANDLE;
	}

	return (*m_pSetTimeouts)(m_ftHandle, dwReadTimeout, dwWriteTimeout);
}	


//****************************************************************************************
FT_STATUS CAppkvdemoDlg::GetQueueStatus(LPDWORD lpdwAmountInRxQueue)
{
	if (!m_pGetQueueStatus)
	{
		AfxMessageBox("FT_GetQueueStatus is not valid!"); 
		return FT_INVALID_HANDLE;
	}

	return (*m_pGetQueueStatus)(m_ftHandle, lpdwAmountInRxQueue);
}	


//****************************************************************************************
FT_STATUS CAppkvdemoDlg::Read(LPVOID lpvBuffer, DWORD dwBuffSize, LPDWORD lpdwBytesRead)
{
	if (!m_pRead)
	{
		AfxMessageBox("FT_Read is not valid!"); 
		return FT_INVALID_HANDLE;
	}

	return (*m_pRead)(m_ftHandle, lpvBuffer, dwBuffSize, lpdwBytesRead);
}	


//****************************************************************************************
FT_STATUS CAppkvdemoDlg::Write(LPVOID lpvBuffer, DWORD dwBuffSize, LPDWORD lpdwBytes)
{
	if (!m_pWrite)
	{
		AfxMessageBox("FT_Write is not valid!"); 
		return FT_INVALID_HANDLE;
	}
	
	return (*m_pWrite)(m_ftHandle, lpvBuffer, dwBuffSize, lpdwBytes);
}	



//****************************************************************************************
FT_STATUS CAppkvdemoDlg::Open(PVOID pvDevice)
{
	if (!m_pOpen)
	{
		AfxMessageBox("FT_Open is not valid!"); 
		return FT_INVALID_HANDLE;
	}
	
	return (*m_pOpen)(pvDevice, &m_ftHandle );
}	

//****************************************************************************************
FT_STATUS CAppkvdemoDlg::OpenEx(PVOID pArg1, DWORD dwFlags)
{
	if (!m_pOpenEx)
	{
		AfxMessageBox("FT_OpenEx is not valid!"); 
		return FT_INVALID_HANDLE;
	}
	
	return (*m_pOpenEx)(pArg1, dwFlags, &m_ftHandle);
}	


//****************************************************************************************
FT_STATUS CAppkvdemoDlg::ListDevices(PVOID pArg1, PVOID pArg2, DWORD dwFlags)
{
	if (!m_pListDevices)
	{
		AfxMessageBox("FT_ListDevices is not valid!"); 
		return FT_INVALID_HANDLE;
	}
	
	return (*m_pListDevices)(pArg1, pArg2, dwFlags);
}	


//****************************************************************************************
FT_STATUS CAppkvdemoDlg::Close()
{
	if (!m_pClose)
	{
		AfxMessageBox("FT_Close is not valid!"); 
		return FT_INVALID_HANDLE;
	}
	
	return (*m_pClose)(m_ftHandle);
}	

// end of hardware bits


/************************************************************************/




BOOL CAppkvdemoDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class

   for(UINT n = 0 ; n < 15 ; n++)
   {
		delete app_kv_psu[n] ;
   }
	
	Close() ;


	KillTimer(1025 ) ;
	return CDialog::DestroyWindow();
}

void CAppkvdemoDlg::OnTimer(UINT nIDEvent) 
{
	unsigned char txbuf[25], rxbuf[25];
	FT_STATUS return_val ;
//	goto temp ;
	if(!board_present)
	{
		m_status =  "usb is disconnected" ;
		UpdateData(FALSE) ;
		return ;
	}
	DWORD ret_bytes ;
	// TODO: Add your message handler code here and/or call default
	heartbeat_state = ! heartbeat_state ;
	BOOL present  ;
	BOOL faulty ;
	process_app_kv_unit(0,m_demanded_voltage_0, &m_readback_voltage_0 ,&m_readback_current_0 ,&status_int_0, HighVoltagesOn, m_local_on_0, m_negative_0, &present,model_0 , &faulty);
	if(present ){m_present_0 = "Present ";}else{m_present_0 = "Not Found ";}
	if(present&&(status_int_0&4)){m_present_0 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_0+="fault"  ;} ;
	if(present&&(status_int_0&1)&&!faulty){ m_present_0+="rl_pos ";}
	if(present&&(status_int_0&2)&&!faulty){ m_present_0+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_0,m_present_0) ;
 	process_app_kv_unit(1,m_demanded_voltage_1, &m_readback_voltage_1 ,&m_readback_current_1,&status_int_1, HighVoltagesOn, m_local_on_1, m_negative_1, &present,model_1 , &faulty);
	if(present ){m_present_1 = "Present ";}else{m_present_1 = "Not Found ";}
	if(present&&(status_int_1&4)){m_present_1 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_1+="fault"  ;} ;
	if(present&&(status_int_1&1)&&!faulty){ m_present_1+="rl_pos ";}
	if(present&&(status_int_1&2)&&!faulty){ m_present_1+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_1,m_present_1) ;
	process_app_kv_unit(2,m_demanded_voltage_2, &m_readback_voltage_2 ,&m_readback_current_2,&status_int_2, HighVoltagesOn, m_local_on_2, m_negative_2, &present,model_2 , &faulty);
	if(present ){m_present_2 = "Present ";}else{m_present_2 = "Not Found ";}
	if(present&&(status_int_2&4)){m_present_2 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_2+="fault"  ;} ;
	if(present&&(status_int_2&1)&&!faulty){ m_present_2+="rl_pos ";}
	if(present&&(status_int_2&2)&&!faulty){ m_present_2+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_2,m_present_2) ;
	process_app_kv_unit(3,m_demanded_voltage_3, &m_readback_voltage_3 ,&m_readback_current_3,&status_int_3, HighVoltagesOn, m_local_on_3, m_negative_3, &present,model_3 , &faulty);
	if(present ){m_present_3 = "Present ";}else{m_present_3 = "Not Found ";}
	if(present&&(status_int_3&4)){m_present_3 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_3+="fault"  ;} ;
	if(present&&(status_int_3&1)&&!faulty){ m_present_3+="rl_pos ";}
	if(present&&(status_int_3&2)&&!faulty){ m_present_3+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_3,m_present_3) ;
	process_app_kv_unit(4,m_demanded_voltage_4, &m_readback_voltage_4 ,&m_readback_current_4,&status_int_4, HighVoltagesOn, m_local_on_4, m_negative_4, &present,model_4 , &faulty);
	if(present ){m_present_4 = "Present ";}else{m_present_4 = "Not Found ";}
	if(present&&(status_int_4&4)){m_present_4 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_4+="fault"  ;} ;
	if(present&&(status_int_4&1)&&!faulty){ m_present_4+="rl_pos ";}
	if(present&&(status_int_4&2)&&!faulty){ m_present_4+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_4,m_present_4) ;
	process_app_kv_unit(5,m_demanded_voltage_5, &m_readback_voltage_5 ,&m_readback_current_5,&status_int_5, HighVoltagesOn, m_local_on_5, m_negative_5, &present,model_5 , &faulty);
	if(present ){m_present_5 = "Present ";}else{m_present_5 = "Not Found ";}
	if(present&&(status_int_5&4)){m_present_5 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_5+="fault"  ;} ;
	if(present&&(status_int_5&1)&&!faulty){ m_present_5+="rl_pos ";}
	if(present&&(status_int_5&2)&&!faulty){ m_present_5+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_5,m_present_5) ;
	process_app_kv_unit(6,m_demanded_voltage_6, &m_readback_voltage_6 ,&m_readback_current_6,&status_int_6, HighVoltagesOn, m_local_on_6, m_negative_6, &present,model_6 , &faulty);
	if(present ){m_present_6 = "Present ";}else{m_present_6 = "Not Found ";}
	if(present&&(status_int_6&4)){m_present_6 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_6+="fault"  ;} ;
	if(present&&(status_int_6&1)&&!faulty){ m_present_6+="rl_pos ";}
	if(present&&(status_int_6&2)&&!faulty){ m_present_6+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_6,m_present_6) ;
	process_app_kv_unit(7,m_demanded_voltage_7, &m_readback_voltage_7 ,&m_readback_current_7,&status_int_7, HighVoltagesOn, m_local_on_7, m_negative_7, &present,model_7 , &faulty);
	if(present ){m_present_7 = "Present ";}else{m_present_7 = "Not Found ";}
	if(present&&(status_int_7&4)){m_present_7 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_7+="fault"  ;} ;
	if(present&&(status_int_7&1)&&!faulty){ m_present_7+="rl_pos ";}
	if(present&&(status_int_7&2)&&!faulty){ m_present_7+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_7,m_present_7) ;
	process_app_kv_unit(8,m_demanded_voltage_8, &m_readback_voltage_8 ,&m_readback_current_8,&status_int_8, HighVoltagesOn, m_local_on_8, m_negative_8, &present,model_8 , &faulty);
	if(present ){m_present_8 = "Present ";}else{m_present_8 = "Not Found ";}
	if(present&&(status_int_8&4)){m_present_8 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_8+="fault"  ;} ;
	if(present&&(status_int_8&1)&&!faulty){ m_present_8+="rl_pos ";}
	if(present&&(status_int_8&2)&&!faulty){ m_present_8+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_8,m_present_8) ;
	process_app_kv_unit(9,m_demanded_voltage_9, &m_readback_voltage_9 ,&m_readback_current_9,&status_int_9, HighVoltagesOn, m_local_on_9, m_negative_9, &present,model_9 , &faulty);
	if(present ){m_present_9 = "Present ";}else{m_present_9 = "Not Found ";}
	if(present&&(status_int_9&4)){m_present_9 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_9+="fault"  ;} ;
	if(present&&(status_int_9&1)&&!faulty){ m_present_9+="rl_pos ";}
	if(present&&(status_int_9&2)&&!faulty){ m_present_9+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_9,m_present_9) ;
	process_app_kv_unit(10,m_demanded_voltage_10, &m_readback_voltage_10 ,&m_readback_current_10,&status_int_10, HighVoltagesOn, m_local_on_10, m_negative_10, &present,model_10 , &faulty);
	if(present ){m_present_10 = "Present ";}else{m_present_10 = "Not Found ";}
	if(present&&(status_int_10&4)){m_present_10 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_10+="fault"  ;} ;
	if(present&&(status_int_10&1)&&!faulty){ m_present_10+="rl_pos ";}
	if(present&&(status_int_10&2)&&!faulty){ m_present_10+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_10,m_present_10) ;
	process_app_kv_unit(11,m_demanded_voltage_11, &m_readback_voltage_11 ,&m_readback_current_11,&status_int_11, HighVoltagesOn, m_local_on_11, m_negative_11, &present,model_11 , &faulty);
	if(present ){m_present_11 = "Present ";}else{m_present_11 = "Not Found ";}
	if(present&&(status_int_11&4)){m_present_11 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_11+="fault"  ;} ;
	if(present&&(status_int_11&1)&&!faulty){ m_present_11+="rl_pos ";}
	if(present&&(status_int_11&2)&&!faulty){ m_present_11+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_11,m_present_11) ;
	process_app_kv_unit(12,m_demanded_voltage_12, &m_readback_voltage_12 ,&m_readback_current_12,&status_int_12, HighVoltagesOn, m_local_on_12, m_negative_12, &present,model_12 , &faulty);
	if(present ){m_present_12 = "Present ";}else{m_present_12 = "Not Found ";}
	if(present&&(status_int_12&4)){m_present_12 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_12+="fault"  ;} ;
	if(present&&(status_int_12&1)&&!faulty){ m_present_12+="rl_pos ";}
	if(present&&(status_int_12&2)&&!faulty){ m_present_12+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_12,m_present_12) ;
	process_app_kv_unit(13,m_demanded_voltage_13, &m_readback_voltage_13 ,&m_readback_current_13,&status_int_13, HighVoltagesOn, m_local_on_13, m_negative_13, &present,model_13 , &faulty);
	if(present ){m_present_13 = "Present ";}else{m_present_13 = "Not Found ";}
	if(present&&(status_int_13&4)){m_present_13 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_13+="fault"  ;} ;
	if(present&&(status_int_13&1)&&!faulty){ m_present_13+="rl_pos ";}
	if(present&&(status_int_13&2)&&!faulty){ m_present_13+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_13,m_present_13) ;
	process_app_kv_unit(14,m_demanded_voltage_14, &m_readback_voltage_14 ,&m_readback_current_14,&status_int_14, HighVoltagesOn, m_local_on_14, m_negative_14, &present,model_14 , &faulty);
	if(present ){m_present_14 = "Present ";}else{m_present_14 = "Not Found ";}
	if(present&&(status_int_14&4)){m_present_14 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_14+="fault"  ;} ;
	if(present&&(status_int_14&1)&&!faulty){ m_present_14+="rl_pos ";}
	if(present&&(status_int_14&2)&&!faulty){ m_present_14+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_14,m_present_14) ;
 	process_app_kv_unit(15,m_demanded_voltage_14, &m_readback_voltage_15 ,&m_readback_current_15,&status_int_15, HighVoltagesOn, m_local_on_15, m_negative_15, &present,model_15 , &faulty);
	if(present ){m_present_15 = "Present ";}else{m_present_15 = "Not Found ";}
	if(present&&(status_int_15&4)){m_present_15 +="O ";}	
	if(present&HighVoltagesOn&faulty ){m_present_15+="fault"  ;} ;
	if(present&&(status_int_15&1)&&!faulty){ m_present_15+="rl_pos ";}
	if(present&&(status_int_15&2)&&!faulty){ m_present_15+="rl_neg ";}
	SetDlgItemText(IDC_PRESENT_15,m_present_15) ;

 	
	process_app_kv_unit(0x10|0,0 /* no demanded voltage */, &m_pump_readback_voltage ,&m_pump_readback_dummy,&pump_status_int_0, HighVoltagesOn,m_pump_0_enable , 0, &present,PUMP_MODEL , &faulty);
	if(present ){m_pump_present_0= "Present ";}else{m_pump_present_0= "Not Found ";}
	if(present&HighVoltagesOn&faulty ){m_pump_present_0+="fault"  ;} ;
	SetDlgItemText(IDC_PUMP_PRESENT_0,m_pump_present_0) ;
	txbuf[0]  = 0x00; 
	if(HighVoltagesOn)
	{
		txbuf[0] |= OA_SERIAL_LINK_HT_ON; 
	}
	if(heartbeat_state)
	{
 		txbuf[0] |= OA_SERIAL_LINK_DATA; 
	}
	Purge(FT_PURGE_RX || FT_PURGE_TX);
	Write(txbuf, 1, &ret_bytes);
	return_val =  Read(rxbuf, 1, &ret_bytes);
	if(return_val != FT_OK)
	{
		board_present = 0 ;
		AfxMessageBox("USB board has been lost, Restart system"); 

	}
	m_status = "usb ok : " ;
	if(HighVoltagesOn)
	{
		m_status +=  "High Voltages On : "; 
	}


	SetDlgItemText(IDC_STATUS,m_status) ;

	SetDlgItemInt(IDC_READBACK_VOLTAGE_0 ,m_readback_voltage_0) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_1 ,m_readback_voltage_1) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_2 ,m_readback_voltage_2) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_3 ,m_readback_voltage_3) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_4 ,m_readback_voltage_4) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_5 ,m_readback_voltage_5) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_6 ,m_readback_voltage_6) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_7 ,m_readback_voltage_7) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_8 ,m_readback_voltage_8) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_9 ,m_readback_voltage_9) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_10,m_readback_voltage_10) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_11,m_readback_voltage_11) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_12,m_readback_voltage_12) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_13,m_readback_voltage_13) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_14,m_readback_voltage_14) ;
	SetDlgItemInt(IDC_READBACK_VOLTAGE_15,m_readback_voltage_15) ;
	
	SetDlgItemInt(IDC_READBACK_CURRENT_0 ,m_readback_current_0) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_1 ,m_readback_current_1) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_2 ,m_readback_current_2) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_3 ,m_readback_current_3) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_4 ,m_readback_current_4) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_5 ,m_readback_current_5) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_6 ,m_readback_current_6) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_7 ,m_readback_current_7) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_8 ,m_readback_current_8) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_9 ,m_readback_current_9) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_10,m_readback_current_10) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_11,m_readback_current_11) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_12,m_readback_current_12) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_13,m_readback_current_13) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_14,m_readback_current_14) ;
	SetDlgItemInt(IDC_READBACK_CURRENT_15,m_readback_current_15) ;

	SetDlgItemInt(IDC_PUMP_READBACK_VOLTAGE_0,m_pump_readback_voltage) ;
 
	CDialog::OnTimer(nIDEvent);
}

void CAppkvdemoDlg::OnHighVoltagesOn() 
{
	UpdateData(TRUE) ;
	 HighVoltagesOn = m_high_voltages_on ;

	
}

void CAppkvdemoDlg::OnSetfocusDemandedVoltage0 () {active_tune = 0 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage1 () {active_tune = 1 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage2 () {active_tune = 2 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage3 () {active_tune = 3 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage4 () {active_tune = 4 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage5 () {active_tune = 5 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage6 () {active_tune = 6 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage7 () {active_tune = 7 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage8 () {active_tune = 8 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage9 () {active_tune = 9 ;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage10() {active_tune = 10;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage11() {active_tune = 11;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage12() {active_tune = 12;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage13() {active_tune = 13;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage14() {active_tune = 14;}
void CAppkvdemoDlg::OnSetfocusDemandedVoltage15() {active_tune = 15;}

void CAppkvdemoDlg::OnKillfocusDemandedVoltage0 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage1 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage2 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage3 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage4 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage5 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage6 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage7 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage8 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage9 () {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage10() {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage11() {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage12() {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage13() {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage14() {	UpdateData(TRUE) ;}
void CAppkvdemoDlg::OnKillfocusDemandedVoltage15() {	UpdateData(TRUE) ;}

void CAppkvdemoDlg::OnModelNumber0() 
{
	model_0++ ;
	if(model_0>LAST_MODEL){ model_0 = 1 ;}
	CString string ="error" ;
	switch(model_0)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_0.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber1() 
{
	model_1++ ;
	if(model_1>LAST_MODEL){ model_1 = 1 ;}
	CString string ="error" ;
	switch(model_1)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_1.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber2() 
{
	model_2++ ;
	if(model_2>LAST_MODEL){ model_2 = 1 ;}
	CString string ="error" ;
	switch(model_2)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_2.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber3() 
{
	model_3++ ;
	if(model_3>LAST_MODEL){ model_3 = 1 ;}
	CString string ="error" ;
	switch(model_3)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_3.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber4() 
{
	model_4++ ;
	if(model_4>LAST_MODEL){ model_4 = 1 ;}
	CString string ="error" ;
	switch(model_4)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_4.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber5() 
{
	model_5++ ;
	if(model_5>LAST_MODEL){ model_5 = 1 ;}
	CString string ="error" ;
	switch(model_5)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_5.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber6() 
{
	model_6++ ;
	if(model_6>LAST_MODEL){ model_6 = 1 ;}
	CString string ="error" ;
	switch(model_6)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_6.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber7() 
{
	model_7++ ;
	if(model_7>LAST_MODEL){ model_7 = 1 ;}
	CString string ="error" ;
	switch(model_7)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_7.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber8() 
{
	model_8++ ;
	if(model_8>LAST_MODEL){ model_8 = 1 ;}
	CString string ="error" ;
	switch(model_8)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_8.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber9() 
{
	model_9++ ;
	if(model_9>LAST_MODEL){ model_9 = 1 ;}
	CString string ="error" ;
	switch(model_9)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_9.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber10() 
{
	model_10++ ;
	if(model_10>LAST_MODEL){ model_10 = 1 ;}
	CString string ="error" ;
	switch(model_10)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_10.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber11() 
{
	model_11++ ;
	if(model_11>LAST_MODEL){ model_11 = 1 ;}
	CString string ="error" ;
	switch(model_11)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_11.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber12() 
{
	model_12++ ;
	if(model_12>LAST_MODEL){ model_12 = 1 ;}
	CString string ="error" ;
	switch(model_12)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_12.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber13() 
{
	model_13++ ;
	if(model_13>LAST_MODEL){ model_13 = 1 ;}
	CString string ="error" ;
	switch(model_13)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_13.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber14() 
{
	model_14++ ;
	if(model_14>LAST_MODEL){ model_14 = 1 ;}
	CString string ="error" ;
	switch(model_14)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_14.SetWindowText(string) ; 
	
}
void CAppkvdemoDlg::OnModelNumber15() 
{
	model_15++ ;
	if(model_15>LAST_MODEL){ model_15 = 1 ;}
	CString string ="error" ;
	switch(model_15)
	{
	case HP1_MODEL :string = "HPl " ;
		break ;
	case  HP2_MODEL :string = "HP2.5" ;
		break ;
	case  HP5_MODEL :string = "HP5" ;
		break ;
	case  HP10_MODEL :string = "HP10" ;
		break ;
	case  HP20_MODEL :string = "HP20" ;
		break ;
	case  HP30_MODEL :string = "HP30" ;
		break ;
	case  HP40_MODEL :string = "HP60" ;
		break ;
	default : string = "model error" ;
		break ;
		
	}
	m_model_number_15.SetWindowText(string) ; 
	
}


BOOL CAppkvdemoDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
{
	// TODO: Add your message handler code here and/or call default

	float amount = (float)zDelta/120.0f ;
	if(	nFlags & MK_SHIFT )
	{
		amount *=100.f ;
	}
 	switch(active_tune)
	{
		case 0 : 
			   m_demanded_voltage_0 +=   amount ;
			if(m_demanded_voltage_0 <0.F){m_demanded_voltage_0 = 0.F ;}
			break ;
		case 1 : 
			   m_demanded_voltage_1 +=   amount ;
			if(m_demanded_voltage_1 <0.F){m_demanded_voltage_1 = 0.F ;}
			break ;
		case 2 : 
			   m_demanded_voltage_2 +=   amount ;
			if(m_demanded_voltage_2 <0.F){m_demanded_voltage_2 = 0.F ;}
			break ;
		case 3 : 
			   m_demanded_voltage_3 +=   amount ;
			if(m_demanded_voltage_3 <0.F){m_demanded_voltage_3 = 0.F ;}
			break ;
		case 4 : 
			   m_demanded_voltage_4 +=   amount ;
			if(m_demanded_voltage_4 <0.F){m_demanded_voltage_4 = 0.F ;}
			break ;
		case 5 : 
			   m_demanded_voltage_5 +=   amount ;
			if(m_demanded_voltage_5 <0.F){m_demanded_voltage_5 = 0.F ;}
			break ;
		case 6 : 
			   m_demanded_voltage_6 +=   amount ;
			if(m_demanded_voltage_6 <0.F){m_demanded_voltage_6 = 0.F ;}
			break ;
		case 7 : 
			   m_demanded_voltage_7 +=   amount ;
			if(m_demanded_voltage_7 <0.F){m_demanded_voltage_7 = 0.F ;}
			break ;
		case 8 : 
			   m_demanded_voltage_8 +=   amount ;
			if(m_demanded_voltage_8 <0.F){m_demanded_voltage_8 = 0.F ;}
			break ;
		case 9 : 
			   m_demanded_voltage_9 +=   amount ;
			if(m_demanded_voltage_9 <0.F){m_demanded_voltage_9 = 0.F ;}
			break ;
		case 10: 
			   m_demanded_voltage_10+=   amount ;
			if(m_demanded_voltage_10<0.F){m_demanded_voltage_10= 0.F ;}
			break ;
		case 11: 
			   m_demanded_voltage_11+=   amount ;
			if(m_demanded_voltage_11<0.F){m_demanded_voltage_11= 0.F ;}
			break ;
		case 12: 
			   m_demanded_voltage_12+=   amount ;
			if(m_demanded_voltage_12<0.F){m_demanded_voltage_12= 0.F ;}
			break ;
		case 13: 
			   m_demanded_voltage_13+=   amount ;
			if(m_demanded_voltage_13<0.F){m_demanded_voltage_13= 0.F ;}
			break ;
		case 14: 
			   m_demanded_voltage_14+=   amount ;
			if(m_demanded_voltage_14<0.F){m_demanded_voltage_14= 0.F ;}
			break ;
		case 15: 
			   m_demanded_voltage_15+=   amount ;
			if(m_demanded_voltage_15<0.F){m_demanded_voltage_15= 0.F ;}
			break ;

		default :
			break ;
	}
	UpdateData(FALSE) ;

	

	return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}
 
BOOL CAppkvdemoDlg::OnCommand(WPARAM wParam, LPARAM lParam) 
{
 	if((wParam!=1)||(lParam!=0))
	{
		if(lParam == 0x00110306)
		{
			UpdateData(TRUE) ;
			return 1 ;
		}
		return CDialog::OnCommand(wParam, lParam);
	}
	return 1 ;
}

void CAppkvdemoDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE) ;
	//CDialog::OnOK();
}


/*0   address 7		main board type address
"1   address 6		main board type address
"2   address 5		main board type address
"3   address 4		main board type address
"4   address 3		sub addresses
"5   address 2		sub addresses
"6   address 1		sub addresses
"7   address 0		sub addresses
"8   control bit 0	adc_select
"9   control bit 1	ht on
"10  control bit 2	synth clk
"11  control bit 3	synch data
"12  control bit 4	sycn cs
"13  dac data 0 ;
"14  dac data 1 ;
"15  dac data 2 ;
"16  dac data 3 ;
"17  dac data 4 ;
"18  dac data 5 ;
"19  dac data 6 ;
"20  dac data 7 ;
"21  dac data 8 ;
"22  dac data 9 ;
"23  dac data 10;
"24  dac data 11;
"25  dac data 12;
"26  dac data 13;
"27  dac data 14;
"28  dac data 15 */

/*CLock and cs have to be high clock a clock occurs when clock is low and goes away when cs is low
so bring clock high first and last on formating packet */



void  CAppkvdemoDlg::process_app_kv_unit(UINT address,float voltage , int* actual_voltge,int * actual_current,int * status_bit_readback,BOOL global_ht_on,BOOL ht_on, BOOL negative_ion, BOOL * present, UINT model,BOOL *fault  ) 
{
	unsigned char serial_packet[200], rxbuf[200];
	DWORD ret_bytes;
	unsigned char  t = 0 ;
	float full_scale ;
	switch(model)
	{
		case DEFAULT_MODEL	:full_scale = 1000.f  ; break ;
		case HP1_MODEL		:full_scale = 1000.f  ; break ;
		case HP2_MODEL		:full_scale = 2500.f  ; break ;
		case HP5_MODEL		:full_scale = 5000.f  ; break ;
		case HP10_MODEL		:full_scale = 10000.f ; break ;
		case HP20_MODEL		:full_scale = 20000.f ; break ;
		case HP30_MODEL		:full_scale = 30000.f ; break ;
		case HP40_MODEL		:full_scale = 40000.f ; break ;
		case PUMP_MODEL		:full_scale = 100.f ; break ;
		default :			 full_scale = 1000.f  ; break ;
	}
	float dac_value = voltage/full_scale * (float)0xffff *40/43 ;
	if(dac_value >(float)0xffff){dac_value = (float)0xffff ;}
	UINT i_d_v = (UINT)dac_value ;
	if(global_ht_on) 
	{
		t = OA_SERIAL_LINK_HT_ON ;
	}
	for( UINT n = 0 ; n<200;n++) // set the high voltage on.
	{
		serial_packet[n]  = t ;
	}

// packet starts at index 4 

	UINT length_of_packet = 29 ;
	serial_packet[0]  |= OA_SERIAL_LINK_CLOCK ; 
	serial_packet[1]  |= OA_SERIAL_LINK_CLOCK ;  
	serial_packet[2]  |= OA_SERIAL_LINK_CLOCK|  OA_SERIAL_LINK_CS ;  
	serial_packet[3]  |= OA_SERIAL_LINK_CLOCK|  OA_SERIAL_LINK_CS ;  

	for(   n = 4 ; n<((length_of_packet*4)+4);n=n+4) // set the high voltage on.
	{
		serial_packet[n]   |= OA_SERIAL_LINK_CLOCK|  OA_SERIAL_LINK_CS ; // set data
		serial_packet[n+1] |=                        OA_SERIAL_LINK_CS ; // set clock
		serial_packet[n+2] |= OA_SERIAL_LINK_CLOCK|  OA_SERIAL_LINK_CS ;
		serial_packet[n+3] |= OA_SERIAL_LINK_CLOCK					   ; // clear clock
	}

  

	if(address&0x80){for(n = 0 ;n<4; n++){serial_packet[ 4+n] |= OA_SERIAL_LINK_DATA ;}}
	if(address&0x40){for(n = 0 ;n<4; n++){serial_packet[ 8+n] |= OA_SERIAL_LINK_DATA ;}}
	if(address&0x20){for(n = 0 ;n<4; n++){serial_packet[12+n] |= OA_SERIAL_LINK_DATA ;}}
	if(address&0x10){for(n = 0 ;n<4; n++){serial_packet[16+n] |= OA_SERIAL_LINK_DATA ;}}
	if(address&0x08){for(n = 0 ;n<4; n++){serial_packet[20+n] |= OA_SERIAL_LINK_DATA ;}}
	if(address&0x04){for(n = 0 ;n<4; n++){serial_packet[24+n] |= OA_SERIAL_LINK_DATA ;}}
	if(address&0x02){for(n = 0 ;n<4; n++){serial_packet[28+n] |= OA_SERIAL_LINK_DATA ;}}
	if(address&0x01){for(n = 0 ;n<4; n++){serial_packet[32+n] |= OA_SERIAL_LINK_DATA ;}}
	if(1           ){for(n = 0 ;n<4; n++){serial_packet[36+n] |= 0                   ;}} // adc_sel 
	if(ht_on       ){for(n = 0 ;n<4; n++){serial_packet[40+n] |= OA_SERIAL_LINK_DATA ;}}
 	if(negative_ion){for(n = 0 ;n<4; n++){serial_packet[44+n] |= OA_SERIAL_LINK_DATA ;}}
	if(1           ){for(n = 0 ;n<4; n++){serial_packet[48+n] |= 0                   ;}} // adc_sel 
	if(1           ){for(n = 0 ;n<4; n++){serial_packet[52+n] |= 0                   ;}} // adc_sel 

	if(i_d_v&0x8000){for(n = 0 ;n<4; n++){serial_packet[56+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x4000){for(n = 0 ;n<4; n++){serial_packet[60+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x2000){for(n = 0 ;n<4; n++){serial_packet[64+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x1000){for(n = 0 ;n<4; n++){serial_packet[68+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0800){for(n = 0 ;n<4; n++){serial_packet[72+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0400){for(n = 0 ;n<4; n++){serial_packet[76+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0200){for(n = 0 ;n<4; n++){serial_packet[80+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0100){for(n = 0 ;n<4; n++){serial_packet[84+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0080){for(n = 0 ;n<4; n++){serial_packet[88+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0040){for(n = 0 ;n<4; n++){serial_packet[92+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0020){for(n = 0 ;n<4; n++){serial_packet[96+n] |=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0010){for(n = 0 ;n<4; n++){serial_packet[100+n]|=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0008){for(n = 0 ;n<4; n++){serial_packet[104+n]|=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0004){for(n = 0 ;n<4; n++){serial_packet[108+n]|=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0002){for(n = 0 ;n<4; n++){serial_packet[112+n]|=OA_SERIAL_LINK_DATA ;}} 
	if(i_d_v&0x0001){for(n = 0 ;n<4; n++){serial_packet[116+n]|=OA_SERIAL_LINK_DATA ;}} 
 
	serial_packet[120]  |=	OA_SERIAL_LINK_CLOCK   ; 
	serial_packet[121]  |=	OA_SERIAL_LINK_CLOCK ;  
	serial_packet[122]  |= 0 ;  
	serial_packet[123]  |= 0 ;  




 	UINT total_length = (length_of_packet*4)+8 ;
  	Write( serial_packet , total_length, &ret_bytes);
	Read ( rxbuf , total_length, &ret_bytes);

	if(rxbuf[40]&OA_SERIAL_LINK_DATA_IN)
	{
		*present = FALSE ;
	}else{
		*present = TRUE ;
	}
	BOOL on_opto =  FALSE ;
	UINT read_mask = OA_SERIAL_LINK_DATA_IN ;
	if(*present == FALSE)
	{

		if((rxbuf[40]&OA_OPTIICAL_SERIAL_LINK_DATA_IN))
		{
			*present = FALSE ;
		}else{
			*present = TRUE ;
			read_mask = OA_OPTIICAL_SERIAL_LINK_DATA_IN ; 
			on_opto = TRUE ;
		}
	}

	*fault = FALSE ;
	UINT dac_data = 0 ;
	UINT dac_mask = 0x80 ;
	UINT offset = 55 ;
	for(n = 0 ; n<8 ; n++)
	{
		if(!(rxbuf[offset+(4*n)]&read_mask))
		{
			dac_data |= dac_mask ; 
		}
		dac_mask >>=1 ;

	}
	UINT idac_data = 0 ;
	UINT idac_mask = 0x80 ;
	UINT ioffset = 55+(4*8) ;
	for(n = 0 ; n<8 ; n++)
	{
		if(!(rxbuf[ioffset+(4*n)]&read_mask))
		{
			idac_data |= idac_mask ; 
		}
		idac_mask >>=1 ;

	}
	CString sbit_pattern ;
/*	for(   n = 0 ; n<200;n++) // set the high voltage on.
	{
		rxbuf[n]  &= OA_SERIAL_LINK_DATA_IN ;
	}*/
	UINT doff = 39 ;
	
	for(   n = 0 ; n<33;n++) // set the high voltage on.
	{
		if(rxbuf[(4*n)+doff] & read_mask )
		{
			sbit_pattern +="0" ;
		}else
		{
			sbit_pattern +="1" ;
		}
	}
//	TRACE(" %s\n",sbit_pattern) ;
	
	*actual_voltge = (UINT)(dac_data * (UINT)full_scale /256 *500./382./5.23*5.*1000/977); 
	*actual_current = idac_data * (UINT)100 /256 *500/382;  ;
	*status_bit_readback = 0 ;
	if(!(rxbuf[44]&read_mask))
	{
		*status_bit_readback |= 1 ;
	}
	if(!(rxbuf[48]&read_mask))
	{
		*status_bit_readback |= 2 ;
	}
	if(on_opto)
	{
		*status_bit_readback |= 4 ;
	}
	if(model == PUMP_MODEL) 
	{
		if(!(rxbuf[44]&OA_SERIAL_LINK_DATA_IN))
		{
			*fault = TRUE ;
		}else
		{
			*fault = FALSE ;
		}

	}
 
}


void CAppkvdemoDlg::OnNegative0 (){	UpdateData(TRUE) ;m_negative_0 = !m_negative_0 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative1 (){	UpdateData(TRUE) ;m_negative_1 = !m_negative_1 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative2 (){	UpdateData(TRUE) ;m_negative_2 = !m_negative_2 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative3 (){	UpdateData(TRUE) ;m_negative_3 = !m_negative_3 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative4 (){	UpdateData(TRUE) ;m_negative_4 = !m_negative_4 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative5 (){	UpdateData(TRUE) ;m_negative_5 = !m_negative_5 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative6 (){	UpdateData(TRUE) ;m_negative_6 = !m_negative_6 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative7 (){	UpdateData(TRUE) ;m_negative_7 = !m_negative_7 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative8 (){	UpdateData(TRUE) ;m_negative_8 = !m_negative_8 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative9 (){	UpdateData(TRUE) ;m_negative_9 = !m_negative_9 ;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative10(){	UpdateData(TRUE) ;m_negative_10= !m_negative_10;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative11(){	UpdateData(TRUE) ;m_negative_11= !m_negative_11;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative12(){	UpdateData(TRUE) ;m_negative_12= !m_negative_12;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative13(){	UpdateData(TRUE) ;m_negative_13= !m_negative_13;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative14(){	UpdateData(TRUE) ;m_negative_14= !m_negative_14;UpdateAllCButtons();}
void CAppkvdemoDlg::OnNegative15(){	UpdateData(TRUE) ;m_negative_15= !m_negative_15;UpdateAllCButtons();}

void CAppkvdemoDlg::OnMove(int x, int y) 
{
	
	CDialog::OnMove(x, y);
 	// TODO: Add your message handler code here
	
}

void CAppkvdemoDlg::OnClose() 
{
	HP_DataStructure current_hp_struct ;

 	CFile parameter_file ;
	if(!parameter_file.Open("app_kv.cfg",CFile::modeWrite|CFile::modeCreate)) // check to see if file exists
	{
    
	}else{
		current_hp_struct.command_voltage	= m_demanded_voltage_0   ;
		current_hp_struct.polarity			= m_negative_0		   ;
		current_hp_struct.power_supply_type	= model_0				  ;
		current_hp_struct.local_ht_on		= m_local_on_0;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_1   ;
		current_hp_struct.polarity			= m_negative_1		   ;
		current_hp_struct.power_supply_type	= model_1				   ;
		current_hp_struct.local_ht_on		= m_local_on_1;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_2   ;
		current_hp_struct.polarity			= m_negative_2		   ;
		current_hp_struct.power_supply_type	= model_2				   ;
		current_hp_struct.local_ht_on		= m_local_on_2;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_3   ;
		current_hp_struct.polarity			= m_negative_3		   ;
		current_hp_struct.power_supply_type	= model_3				   ;
		current_hp_struct.local_ht_on		= m_local_on_3;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_4   ;
		current_hp_struct.polarity			= m_negative_4		   ;
		current_hp_struct.power_supply_type	= model_4				  ;
		current_hp_struct.local_ht_on		= m_local_on_4;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_5   ;
		current_hp_struct.polarity			= m_negative_5		   ;
		current_hp_struct.power_supply_type	= model_5				   ;
		current_hp_struct.local_ht_on		= m_local_on_5;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_6   ;
		current_hp_struct.polarity			= m_negative_6		   ;
		current_hp_struct.power_supply_type	= model_6				   ;
		current_hp_struct.local_ht_on		= m_local_on_6;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_7   ;
		current_hp_struct.polarity			= m_negative_7		  ;
		current_hp_struct.power_supply_type	= model_7				   ;
		current_hp_struct.local_ht_on		= m_local_on_7;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_8   ;
		current_hp_struct.polarity			= m_negative_8		   ;
		current_hp_struct.power_supply_type	= model_8				   ;
		current_hp_struct.local_ht_on		= m_local_on_8;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_9   ;
		current_hp_struct.polarity			= m_negative_9		   ;
		current_hp_struct.power_supply_type	= model_9				  ;
		current_hp_struct.local_ht_on		= m_local_on_9;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_10  ;
		current_hp_struct.polarity			= m_negative_10		  ;
		current_hp_struct.power_supply_type	= model_10			   ;
		current_hp_struct.local_ht_on		= m_local_on_10;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_11  ;
		current_hp_struct.polarity			= m_negative_11		  ;
		current_hp_struct.power_supply_type	= model_11			   ;
		current_hp_struct.local_ht_on		= m_local_on_11;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_12  ;
		current_hp_struct.polarity			= m_negative_12		   ;
		current_hp_struct.power_supply_type	= model_12			   ;
		current_hp_struct.local_ht_on		= m_local_on_12;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_13  ;
		current_hp_struct.polarity			= m_negative_13		   ;
		current_hp_struct.power_supply_type	= model_13			   ;
		current_hp_struct.local_ht_on		= m_local_on_13;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_14  ;
		current_hp_struct.polarity			= m_negative_14		   ;
		current_hp_struct.power_supply_type	= model_14			   ;
		current_hp_struct.local_ht_on		= m_local_on_14;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
		current_hp_struct.command_voltage	= m_demanded_voltage_15  ;
		current_hp_struct.polarity			= m_negative_15	      ;
		current_hp_struct.power_supply_type	= model_15		 ;
		current_hp_struct.local_ht_on		= m_local_on_15;
		parameter_file.Write(&current_hp_struct,sizeof(current_hp_struct)) ;
	}
 	BOOL present  ;
	BOOL faulty ;
	process_app_kv_unit(0,m_demanded_voltage_0, &m_readback_voltage_0 ,&m_readback_current_0 ,&status_int_0, FALSE, FALSE, m_negative_0, &present,model_0 , &faulty);
 	// this turns all units off
	CDialog::OnClose();
}


void CAppkvdemoDlg::UpdateAllCButtons()
{
	if(m_negative_0 ){m_negative_b0.SetWindowText("Negative") ;}else{m_negative_b0.SetWindowText("Positive") ; }
	if(m_negative_1 ){m_negative_b1.SetWindowText("Negative") ;}else{m_negative_b1.SetWindowText("Positive") ; }
	if(m_negative_2 ){m_negative_b2.SetWindowText("Negative") ;}else{m_negative_b2.SetWindowText("Positive") ; }
	if(m_negative_3 ){m_negative_b3.SetWindowText("Negative") ;}else{m_negative_b3.SetWindowText("Positive") ; }
	if(m_negative_4 ){m_negative_b4.SetWindowText("Negative") ;}else{m_negative_b4.SetWindowText("Positive") ; }
	if(m_negative_5 ){m_negative_b5.SetWindowText("Negative") ;}else{m_negative_b5.SetWindowText("Positive") ; }
	if(m_negative_6 ){m_negative_b6.SetWindowText("Negative") ;}else{m_negative_b6.SetWindowText("Positive") ; }
	if(m_negative_7 ){m_negative_b7.SetWindowText("Negative") ;}else{m_negative_b7.SetWindowText("Positive") ; }
	if(m_negative_8 ){m_negative_b8.SetWindowText("Negative") ;}else{m_negative_b8.SetWindowText("Positive") ; }
	if(m_negative_9 ){m_negative_b9.SetWindowText("Negative") ;}else{m_negative_b9.SetWindowText("Positive") ; }
	if(m_negative_10){m_negative_b10.SetWindowText("Negative") ;}else{m_negative_b10.SetWindowText("Positive") ; }
	if(m_negative_11){m_negative_b11.SetWindowText("Negative") ;}else{m_negative_b11.SetWindowText("Positive") ; }
	if(m_negative_12){m_negative_b12.SetWindowText("Negative") ;}else{m_negative_b12.SetWindowText("Positive") ; }
	if(m_negative_13){m_negative_b13.SetWindowText("Negative") ;}else{m_negative_b13.SetWindowText("Positive") ; }
	if(m_negative_14){m_negative_b14.SetWindowText("Negative") ;}else{m_negative_b14.SetWindowText("Positive") ; }
	if(m_negative_15){m_negative_b15.SetWindowText("Negative") ;}else{m_negative_b15.SetWindowText("Positive") ; }
	CString string ;
	ConvertModelNumberToString(model_0,  &string) ;	m_model_number_0.SetWindowText(string) ; 
	ConvertModelNumberToString(model_1,  &string) ;	m_model_number_1.SetWindowText(string) ; 
	ConvertModelNumberToString(model_2,  &string) ;	m_model_number_2.SetWindowText(string) ; 
	ConvertModelNumberToString(model_3,  &string) ;	m_model_number_3.SetWindowText(string) ; 
	ConvertModelNumberToString(model_4,  &string) ;	m_model_number_4.SetWindowText(string) ; 
	ConvertModelNumberToString(model_5,  &string) ;	m_model_number_5.SetWindowText(string) ; 
	ConvertModelNumberToString(model_6,  &string) ;	m_model_number_6.SetWindowText(string) ; 
	ConvertModelNumberToString(model_7,  &string) ;	m_model_number_7.SetWindowText(string) ; 
	ConvertModelNumberToString(model_8,  &string) ;	m_model_number_8.SetWindowText(string) ; 
	ConvertModelNumberToString(model_9,  &string) ;	m_model_number_9.SetWindowText(string) ; 
	ConvertModelNumberToString(model_10, &string) ;	m_model_number_10.SetWindowText(string) ; 
	ConvertModelNumberToString(model_11, &string) ;	m_model_number_11.SetWindowText(string) ; 
	ConvertModelNumberToString(model_12, &string) ;	m_model_number_12.SetWindowText(string) ; 
	ConvertModelNumberToString(model_13, &string) ;	m_model_number_13.SetWindowText(string) ; 
	ConvertModelNumberToString(model_14, &string) ;	m_model_number_14.SetWindowText(string) ; 
	ConvertModelNumberToString(model_15, &string) ;	m_model_number_15.SetWindowText(string) ; 



}

void CAppkvdemoDlg::ConvertModelNumberToString(UINT model, CString * string)
{
	*string ="error" ;
	switch(model)
	{
	case HP1_MODEL :*string = "HPl " ;
		break ;
	case  HP2_MODEL :*string = "HP2.5" ;
		break ;
	case  HP5_MODEL :*string = "HP5" ;
		break ;
	case  HP10_MODEL :*string = "HP10" ;
		break ;
	case  HP20_MODEL :*string = "HP20" ;
		break ;
	case  HP30_MODEL :*string = "HP30" ;
		break ;
	case  HP40_MODEL :*string = "HP60" ;
		break ;
	default : *string = "model error" ;
		break ;
		
	}
}

void CAppkvdemoDlg::OnPumpEnable() 
{
	UpdateData(TRUE) ;	
}

void CAppkvdemoDlg::OnLocalHtOn0()  {	m_local_on_0 = !m_local_on_0 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn1()  {	m_local_on_1 = !m_local_on_1 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn2()  {	m_local_on_2 = !m_local_on_2 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn3()  {	m_local_on_3 = !m_local_on_3 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn4()  {	m_local_on_4 = !m_local_on_4 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn5()  {	m_local_on_5 = !m_local_on_5 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn6()  {	m_local_on_6 = !m_local_on_6 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn7()  {	m_local_on_7 = !m_local_on_7 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn8()  {	m_local_on_8 = !m_local_on_8 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn9()  {	m_local_on_9 = !m_local_on_9 ;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn10() {	m_local_on_10= !m_local_on_10;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn11() {	m_local_on_11= !m_local_on_11;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn12() {	m_local_on_12= !m_local_on_12;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn13() {	m_local_on_13= !m_local_on_13;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn14() {	m_local_on_14= !m_local_on_14;UpdateData(FALSE) ;}
void CAppkvdemoDlg::OnLocalHtOn15() {	m_local_on_15= !m_local_on_15;UpdateData(FALSE) ;}
 
