
// CapacitanceSingleMeasureDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CapacitanceSingleMeasure.h"
#include "CapacitanceSingleMeasureDlg.h"
#include "afxdialogex.h"
#include "czm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CCapacitanceSingleMeasureDlg dialog



CCapacitanceSingleMeasureDlg::CCapacitanceSingleMeasureDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_CAPACITANCESINGLEMEASURE_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCapacitanceSingleMeasureDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_edit_device_name);
	DDX_Control(pDX, IDC_COMBO_CHANNEL_NUMBER, m_combo_channel_number);
	DDX_Control(pDX, IDC_COMBO_FREQUENCY, m_combo_frequency);
	DDX_Control(pDX, IDC_COMBO_LEVEL, m_combo_level);
	DDX_Control(pDX, IDC_COMBO_SPEED, m_combo_speed);
	DDX_Control(pDX, IDC_EDIT_AVERAGE_COUNT, m_edit_average_count);
	DDX_Control(pDX, IDC_COMBO_RANGE, m_combo_range);
	DDX_Control(pDX, IDC_COMBO_CIRCUIT, m_combo_circuit);
	DDX_Control(pDX, IDC_EDIT_START_DELAY, m_edit_start_delay);
	DDX_Control(pDX, IDC_COMBO_TRIGGER_SYNC_ENABLE, m_combo_trigger_sync_enable);
	DDX_Control(pDX, IDC_EDIT_SIGNAL_OUTPUT_DELAY, m_edit_signal_output_delay);
	DDX_Control(pDX, IDC_COMBO_CONTACT_CHECK, m_combo_contact_check);
	DDX_Control(pDX, IDC_EDIT_CAPACITANCE, m_edit_capacitance);
	DDX_Control(pDX, IDC_EDIT_DISSIPATION, m_edit_dissipation);
	DDX_Control(pDX, IDC_EDIT_CIRCUIT, m_edit_circuit);
	DDX_Control(pDX, IDC_EDIT_BEFORE_L, m_edit_before_l);
	DDX_Control(pDX, IDC_EDIT_BEFORE_H, m_edit_before_h);
	DDX_Control(pDX, IDC_EDIT_AFTER_L, m_edit_after_l);
	DDX_Control(pDX, IDC_EDIT_AFTER_H, m_edit_after_h);
	DDX_Control(pDX, IDC_EDIT_RETURN_VALUE, m_edit_return_value);
}

BEGIN_MESSAGE_MAP(CCapacitanceSingleMeasureDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ZMINIT, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZminit)
	ON_BN_CLICKED(IDC_BUTTON_ZMRESETDEVICE, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmresetdevice)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETFREQUENCY, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetfrequency)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETLEVEL, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetlevel)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETSPEED, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetspeed)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETRANGE, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetrange)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETCIRCUIT, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetcircuit)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETSTARTDELAY, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetstartdelay)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETTRIGGERSYNCENABLE, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsettriggersyncenable)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETSIGNALOUTPUTDELAY, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetsignaloutputdelay)
	ON_BN_CLICKED(IDC_BUTTON_ZMSETCONTACTCHECK, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetcontactcheck)
	ON_BN_CLICKED(IDC_BUTTON_ZMSINGLECMEASURE, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsinglecmeasure)
	ON_BN_CLICKED(IDC_BUTTON_ZMEXIT, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmexit)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CCapacitanceSingleMeasureDlg::OnBnClickedButtonClose)
END_MESSAGE_MAP()


//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_id;												// ID for device access

// CCapacitanceSingleMeasureDlg message handlers

BOOL CCapacitanceSingleMeasureDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 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

	//----------------------------------------
	// Update the initial display of device name
	//----------------------------------------
	m_edit_device_name.SetWindowTextW(_T("ZM000"));
	//----------------------------------------
	// Create the combobox for channel No.
	//----------------------------------------
	m_combo_channel_number.AddString(_T("CH1"));
	m_combo_channel_number.AddString(_T("CH2"));
	//----------------------------------------
	// Change the selection of channel No. combobox to 0
	//----------------------------------------
	m_combo_channel_number.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for frequency
	//----------------------------------------
	m_combo_frequency.AddString(_T("120 Hz"));
	m_combo_frequency.AddString(_T("1 kHz"));
	//----------------------------------------
	// Change the selection of frequency combobox to 0
	//----------------------------------------
	m_combo_frequency.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for signal level
	//----------------------------------------
	m_combo_level.AddString(_T("0.5 V"));
	m_combo_level.AddString(_T("1 V"));
	//----------------------------------------
	// Change the selection of signal level combobox to 0
	//----------------------------------------
	m_combo_level.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for measurement speed
	//----------------------------------------
	m_combo_speed.AddString(_T("FAST"));
	m_combo_speed.AddString(_T("NORMAL"));
	m_combo_speed.AddString(_T("SLOW"));
	//----------------------------------------
	// Change the selection of measurement speed combobox to 0
	//----------------------------------------
	m_combo_speed.SetCurSel(0);
	//----------------------------------------
	// Update the initial display of average count
	//----------------------------------------
	m_edit_average_count.SetWindowTextW(_T("1"));
	//----------------------------------------
	// Create the combobox for measurement range
	//----------------------------------------
	m_combo_range.AddString(_T("4 nF"));
	m_combo_range.AddString(_T("40 nF"));
	m_combo_range.AddString(_T("400 nF"));
	m_combo_range.AddString(_T("4 uF"));
	m_combo_range.AddString(_T("40 uF"));
	m_combo_range.AddString(_T("400 uF"));
	m_combo_range.AddString(_T("1.4 mF"));
	//----------------------------------------
	// Change the selection of measurement range combobox to 0
	//----------------------------------------
	m_combo_range.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for equivalent circuit mode
	//----------------------------------------
	m_combo_circuit.AddString(_T("AUTO"));
	m_combo_circuit.AddString(_T("SERIAL"));
	m_combo_circuit.AddString(_T("PARALLEL"));
	//----------------------------------------
	// Change the selection of equivalent circuit mode combobox to 0
	//----------------------------------------
	m_combo_circuit.SetCurSel(0);
	//----------------------------------------
	// Update the initial display of measurement start delay
	//----------------------------------------
	m_edit_start_delay.SetWindowTextW(_T("2"));
	//----------------------------------------
	// Create the combobox for trigger synchronous output
	//----------------------------------------
	m_combo_trigger_sync_enable.AddString(_T("DISABLE"));
	m_combo_trigger_sync_enable.AddString(_T("ENABLE"));
	//----------------------------------------
	// Change the selection of trigger synchronous output combobox to 0
	//----------------------------------------
	m_combo_trigger_sync_enable.SetCurSel(0);
	//----------------------------------------
	// Update the initial display of measurement signal output delay
	//----------------------------------------
	m_edit_signal_output_delay.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Create the combobox for contact check
	//----------------------------------------
	m_combo_contact_check.AddString(_T("DISABLE"));
	m_combo_contact_check.AddString(_T("BEFORE"));
	m_combo_contact_check.AddString(_T("AFTER"));
	m_combo_contact_check.AddString(_T("BOTH"));
	//----------------------------------------
	// Change the selection of contact check combobox to 0
	//----------------------------------------
	m_combo_contact_check.SetCurSel(0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CCapacitanceSingleMeasureDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCapacitanceSingleMeasureDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the ZmInit button is clicked
// Initialize the device
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZminit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	device_name[256];	// Device name
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_edit_device_name.GetWindowTextW(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Device initialization
	//----------------------------------------
	ret	= ZmInit(device_name, &g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmInit = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmResetDevice button is clicked
// Reset the device
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmresetdevice()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Reset device
	//----------------------------------------
	ret	= ZmResetDevice(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmResetDevice = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetFrequency button is clicked
// Set the frequency
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetfrequency()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	CString			text_string;		// For updating the textbox
	long			ret;				// For return code
	unsigned short	channel_number;		// Channel No.
	unsigned short	frequency;			// Frequency

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the frequency to set from combobox
	//----------------------------------------
	frequency	= m_combo_frequency.GetCurSel();
	//----------------------------------------
	// Set the frequency
	//----------------------------------------
	ret	= ZmSetFrequency(g_id, channel_number, frequency);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetFrequency = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetLevel button is clicked
// Set the signal level
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetlevel()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	CString			text_string;		// For updating the textbox
	long			ret;				// For return code
	unsigned short	channel_number;		// Channel No.
	unsigned short	level;				// Signal level

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the signal level to set from combobox
	//----------------------------------------
	level	= m_combo_level.GetCurSel();
	//----------------------------------------
	// Set the signal level
	//----------------------------------------
	ret	= ZmSetLevel(g_id, channel_number, level);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetLevel = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetSpeed button is clicked
// Set the measurement speed and the average count
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetspeed()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	CString			text_string;		// For updating the textbox
	long			ret;				// For return code
	unsigned short	channel_number;		// Channel No.
	unsigned short	speed;				// Measurement speed
	unsigned long	average_count;		// Average count

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the measurement speed to set from combobox
	//----------------------------------------
	speed	= m_combo_speed.GetCurSel();
	//----------------------------------------
	// Get the average count to set from textbox
	//----------------------------------------
	m_edit_average_count.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%lu"), &average_count);
	//----------------------------------------
	// Set the measurement speed and the average count
	//----------------------------------------
	ret	= ZmSetSpeed(g_id, channel_number, speed, average_count);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetSpeed = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetRange button is clicked
// Set the measurement range
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetrange()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	CString			text_string;		// For updating the textbox
	long			ret;				// For return code
	unsigned short	channel_number;		// Channel No.
	unsigned short	range;				// Measurement range

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the measurement range to set from combobox
	//----------------------------------------
	range	= m_combo_range.GetCurSel() + 3;
	//----------------------------------------
	// Set the measurement range
	//----------------------------------------
	ret	= ZmSetRange(g_id, channel_number, range);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetRange = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetCircuit button is clicked
// Set the equivalent circuit mode
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetcircuit()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	CString			text_string;		// For updating the textbox
	long			ret;				// For return code
	unsigned short	channel_number;		// Channel No.
	unsigned short	circuit;			// Equivalent circuit mode

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the equivalent circuit mode to set from combobox
	//----------------------------------------
	circuit	= m_combo_circuit.GetCurSel();
	//----------------------------------------
	// Set the equivalent circuit mode
	//----------------------------------------
	ret	= ZmSetCircuit(g_id, channel_number, circuit);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetCircuit = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetStartDelay button is clicked
// Set the measurement start delay
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetstartdelay()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	CString			text_string;		// For updating the textbox
	long			ret;				// For return code
	unsigned short	channel_number;		// Channel No.
	float			start_delay;		// Measurement start delay

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the measurement start delay to set from textbox
	//----------------------------------------
	m_edit_start_delay.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%f"), &start_delay);
	//----------------------------------------
	// Set the measurement start delay
	//----------------------------------------
	ret	= ZmSetStartDelay(g_id, channel_number, start_delay);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetStartDelay = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetTriggerSyncEnable button is clicked
// Set the trigger synchronous output
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsettriggersyncenable()
{
	TCHAR			text_temp[256];			// For temporary storage of text
	char			error_string[256];		// Error code string
	CString			text_string;			// For updating the textbox
	long			ret;					// For return code
	unsigned short	channel_number;			// Channel No.
	unsigned short	trigger_sync_enable;	// Trigger synchronous output

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the trigger synchronous output to set from combobox
	//----------------------------------------
	trigger_sync_enable	= m_combo_trigger_sync_enable.GetCurSel();
	//----------------------------------------
	// Set the trigger synchronous output
	//----------------------------------------
	ret	= ZmSetTriggerSyncEnable(g_id, channel_number, trigger_sync_enable);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetTriggerSyncEnable = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetSignalOutputDelay button is clicked
// Set the measurement signal output delay
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetsignaloutputdelay()
{
	TCHAR			text_temp[256];			// For temporary storage of text
	char			error_string[256];		// Error code string
	CString			text_string;			// For updating the textbox
	long			ret;					// For return code
	unsigned short	channel_number;			// Channel No.
	float			signal_output_delay;	// Measurement signal output delay

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the measurement signal output delay to set from textbox
	//----------------------------------------
	m_edit_signal_output_delay.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%f"), &signal_output_delay);
	//----------------------------------------
	// Set the measurement signal output delay
	//----------------------------------------
	ret	= ZmSetSignalOutputDelay(g_id, channel_number, signal_output_delay);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetSignalOutputDelay = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSetContactCheck button is clicked
// Set the contact check
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsetcontactcheck()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	CString			text_string;		// For updating the textbox
	long			ret;				// For return code
	unsigned short	channel_number;		// Channel No.
	unsigned short	contact_check;		// Contack check

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to set from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Get the contact check to set from combobox
	//----------------------------------------
	contact_check	= m_combo_contact_check.GetCurSel();
	//----------------------------------------
	// Set the contact check
	//----------------------------------------
	ret	= ZmSetContactCheck(g_id, channel_number, contact_check);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSetContactCheck = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmSingleCMeasure button is clicked
// Measure
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmsinglecmeasure()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	CString			text_string;		// For updating the textbox
	long			ret;				// For return code
	unsigned short	channel_number;		// Channel No.
	float			capacitance;		// Capacitance
	float			dissipation;		// Dissipation factor
	unsigned long	status;				// Status
	unsigned short	contact_check;		// Contact check

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the channel number to measure from combobox
	//----------------------------------------
	channel_number	= m_combo_channel_number.GetCurSel() + 1;
	//----------------------------------------
	// Measure
	//----------------------------------------
	ret	= ZmSingleCMeasure(g_id, channel_number, &capacitance, &dissipation, &status);
	//----------------------------------------
	// Update the display when the ZmSingleCMeasure function ends normally
	//----------------------------------------
	if(ret == ZM_ERR_SUCCESS){
		//----------------------------------------
		// Update the display of capacitance
		//----------------------------------------
		text_string.Format(_T("%e"), capacitance);
		m_edit_capacitance.SetWindowText(text_string);
		//----------------------------------------
		// Update the display of dissipation factor
		//----------------------------------------
		text_string.Format(_T("%f"), dissipation);
		m_edit_dissipation.SetWindowText(text_string);
		//----------------------------------------
		// Update the display of equivalent circuit mode
		//----------------------------------------
		if(((status >> 24) & 0x03) == ZM_CIRCUIT_SERIAL){
			//----------------------------------------
			// Update the display to serial equivalent circuit
			//----------------------------------------
			m_edit_circuit.SetWindowText(_T("SERIAL"));
		}else if(((status >> 24) & 0x03) == ZM_CIRCUIT_PARALLEL){
			//----------------------------------------
			// Update the display to parallel equivalent circuit
			//----------------------------------------
			m_edit_circuit.SetWindowText(_T("PARALLEL"));
		}
		//----------------------------------------
		// Update the display of contact check error
		// Update the display to '-' when contact check is disabled
		//----------------------------------------
		ZmGetContactCheck(g_id, channel_number, &contact_check);
		//----------------------------------------
		// The contact check is disabled so update all display to '-'
		//----------------------------------------
		if(contact_check == ZM_CONTACTCHECK_DISABLE){
			m_edit_before_l.SetWindowText(_T("-"));
			m_edit_before_h.SetWindowText(_T("-"));
			m_edit_after_l.SetWindowText(_T("-"));
			m_edit_after_h.SetWindowText(_T("-"));
		//----------------------------------------
		// The contact check is 'BEFORE' so update 'after measurement' to '-'
		//----------------------------------------
		}else if(contact_check == ZM_CONTACTCHECK_BEFORE){
			text_string.Format(_T("%d"), (status >> 16) & 0x01);
			m_edit_before_l.SetWindowText(text_string);
			text_string.Format(_T("%d"), (status >> 17) & 0x01);
			m_edit_before_h.SetWindowText(text_string);
			m_edit_after_l.SetWindowText(_T("-"));
			m_edit_after_h.SetWindowText(_T("-"));
		//----------------------------------------
		// The contact check is 'AFTER' so update 'berore measurement' to '-'
		//----------------------------------------
		}else if(contact_check == ZM_CONTACTCHECK_AFTER){
			m_edit_before_l.SetWindowText(_T("-"));
			m_edit_before_h.SetWindowText(_T("-"));
			text_string.Format(_T("%d"), (status >> 18) & 0x01);
			m_edit_after_l.SetWindowText(text_string);
			text_string.Format(_T("%d"), (status >> 19) & 0x01);
			m_edit_after_h.SetWindowText(text_string);
		//----------------------------------------
		// The contact check is 'BOTH' so update all display to status value
		//----------------------------------------
		}else if(contact_check == ZM_CONTACTCHECK_BOTH){
			text_string.Format(_T("%d"), (status >> 16) & 0x01);
			m_edit_before_l.SetWindowText(text_string);
			text_string.Format(_T("%d"), (status >> 17) & 0x01);
			m_edit_before_h.SetWindowText(text_string);
			text_string.Format(_T("%d"), (status >> 18) & 0x01);
			m_edit_after_l.SetWindowText(text_string);
			text_string.Format(_T("%d"), (status >> 19) & 0x01);
			m_edit_after_h.SetWindowText(text_string);
		}
	}
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmSingleCMeasure = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the ZmExit button is clicked
// Perform the exit process of device
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonZmexit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Exit process
	//----------------------------------------
	ret	= ZmExit(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	ZmGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("ZmExit = %d : %s"), ret, text_temp);
	m_edit_return_value.SetWindowText(text_string);
}


//================================================================================
// It is the process when the Close button is clicked
// Perform the exit process of device and terminate the application
//================================================================================
void CCapacitanceSingleMeasureDlg::OnBnClickedButtonClose()
{
	//----------------------------------------
	// Exit process
	//----------------------------------------
	ZmExit(g_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}