﻿
// CounterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Counter.h"
#include "CounterDlg.h"
#include "SettingDlg.h"
#include "afxdialogex.h"
#include "Caio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CCounterDlg dialog



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

void CCounterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_device_name);
	DDX_Control(pDX, IDC_EDIT_CHANNEL_NO, m_channel_no);
	DDX_Control(pDX, IDC_EDIT_PRESET_DATA, m_preset_data);
	DDX_Control(pDX, IDC_EDIT_COUNT_DATA, m_count);
	DDX_Control(pDX, IDC_EDIT_STATUS, m_status);
	DDX_Control(pDX, IDC_EDIT_EVENT_COUNT, m_event_count);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_return_code);
}

BEGIN_MESSAGE_MAP(CCounterDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_AIO_INIT, &CCounterDlg::OnBnClickedButtonAioInit)
	ON_BN_CLICKED(IDC_BUTTON_AIO_RESET_DEV, &CCounterDlg::OnBnClickedButtonAioResetDev)
	ON_EN_CHANGE(IDC_EDIT_CHANNEL_NO, &CCounterDlg::OnEnChangeEditChannelNo)
	ON_BN_CLICKED(IDC_BUTTON_SETTING, &CCounterDlg::OnBnClickedButtonSetting)
	ON_BN_CLICKED(IDC_BUTTON_PRESET_CNT, &CCounterDlg::OnBnClickedButtonPresetCnt)
	ON_BN_CLICKED(IDC_BUTTON_START, &CCounterDlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_GET_CNT_COUNT, &CCounterDlg::OnBnClickedButtonGetCntCount)
	ON_BN_CLICKED(IDC_BUTTON_GET_CNT_STATUS, &CCounterDlg::OnBnClickedButtonGetCntStatus)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CCounterDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_AIO_EXIT, &CCounterDlg::OnBnClickedButtonAioExit)
	ON_BN_CLICKED(IDCLOSE, &CCounterDlg::OnBnClickedClose)
END_MESSAGE_MAP()

//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_id;						// ID for device access
short	g_channel;					// Counter channel
long	g_event_count;				// Comparison count match event occurrence count
long	g_compare_data_num	= 1;	// Total number of comparison count data to be set
short	g_compare_repeat	= 0;	// Repeat setting of comparison count data array
long	g_compare_data_0	= 100;	// Comparison count data 0
long	g_compare_data_1	= 200;	// Comparison count data 1
long	g_compare_data_2	= 300;	// Comparison count data 2
long	g_compare_data_3	= 400;	// Comparison count data 3
long	g_compare_data_4	= 500;	// Comparison count data 4
long	g_preset_data_num	= 1;	// Total number of the preset data to be set
short	g_preset_repeat		= 0;	// Repeat setting of preset data array
long	g_preset_data_0		= 0;	// Counter preset data 0
long	g_preset_data_1		= 100;	// Counter preset data 1
long	g_preset_data_2		= 200;	// Counter preset data 2
long	g_preset_data_3		= 300;	// Counter preset data 3
long	g_preset_data_4		= 400;	// Counter preset data 4

// CCounterDlg message handlers

BOOL CCounterDlg::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_device_name.SetWindowTextW(_T("AIO000"));
	//----------------------------------------
	// Update the initial display of counter channel
	//----------------------------------------
	m_channel_no.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of counter preset value
	//----------------------------------------
	m_preset_data.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of current count value
	//----------------------------------------
	m_count.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of status
	//----------------------------------------
	m_status.SetWindowTextW(_T(""));
	//----------------------------------------
	// Updated the initial display of comparison count match event occurrence count
	//----------------------------------------
	m_event_count.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Initialize global variables
	//----------------------------------------
	g_id				= 0;
	g_channel			= 0;
	g_event_count		= 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 CCounterDlg::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 CCounterDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the AioInit button is clicked
// Initialize the device
//================================================================================
void CCounterDlg::OnBnClickedButtonAioInit()
{
	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_device_name.GetWindowTextW(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Device initialization
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioInit(device_name, &g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioInit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioResetDevice button is clicked
// Reset the device
//================================================================================
void CCounterDlg::OnBnClickedButtonAioResetDev()
{
	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	= AioResetDevice(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioResetDevice = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the ChannelNo textbox is changed
// Set the counter channel
//================================================================================
void CCounterDlg::OnEnChangeEditChannelNo()
{
	TCHAR	text_temp[256];		// For temporary storage of text

	//----------------------------------------
	// Get counter channel from textbox
	//----------------------------------------
	m_channel_no.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &g_channel);
}

//================================================================================
// It is the process when the Set button is clicked
// Open the counter setting dialog
//================================================================================
void CCounterDlg::OnBnClickedButtonSetting()
{
	CString	text_string;		// For updating the textbox

	//----------------------------------------
	// Open the condition setting dialog
	//----------------------------------------
	CSettingDlg	dlg;
	if(dlg.DoModal() == IDOK){
		//----------------------------------------
		// Display the message that setting is completed
		//----------------------------------------
		text_string.Format(_T("Set: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the AioPresetCnt button is clicked
// Preset the counter
//================================================================================
void CCounterDlg::OnBnClickedButtonPresetCnt()
{
	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
	long	preset_data;		// Counter preset value
	long	cnt_count;			// Count value

	//----------------------------------------
	// Get counter preset value from textbox
	//----------------------------------------
	m_preset_data.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%ld"), &preset_data);
	//----------------------------------------
	// Preset the counter
	//----------------------------------------
	ret	= AioPresetCnt(g_id, g_channel, preset_data);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioPresetCnt = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//----------------------------------------
	// When the count value preset function is completed successfully
	//----------------------------------------
	if(ret == 0){
		//--------------------
		// Get the count value
		//--------------------
		ret	= AioGetCntCount(g_id, g_channel, &cnt_count);
		//--------------------
		// Error process
		//--------------------
		if(ret != 0){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Display message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("AioGetCntCount = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return;
		}
		//--------------------
		// Update count value
		//--------------------
		text_string.Format(_T("%ld"), cnt_count);
		m_count.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the Count Start button is clicked
// Start the counter action
//================================================================================
void CCounterDlg::OnBnClickedButtonStart()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	CString	text_string_temp;	// For temporary storage of textbox
	long	cnt_status;			// Counter status
	long	ret;				// For return code
	long	cnt_count;			// Count value

	//----------------------------------------
	// Set the event factor to 'Comparison count match event', 'Counter operation error'
	//----------------------------------------
	ret	= AioSetCntEvent(g_id, g_channel, m_hWnd, CNTE_DATA_NUM | CNTE_ERR);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Count Start: AioSetCntEvent = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Reset status
	//----------------------------------------
	ret	= AioResetCntStatus(g_id, g_channel, CNTS_DATA_NUM | CNTS_ORERR);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Count Start: AioResetCntStatus = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Initialize comparison match count
	//----------------------------------------
	g_event_count	= 0;
	//----------------------------------------
	// Start the counter action
	//----------------------------------------
	ret	= AioStartCnt(g_id, g_channel);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Count Start: AioStartCnt = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get the counter status
	//----------------------------------------
	ret	= AioGetCntStatus(g_id, g_channel, &cnt_status);
	//----------------------------------------
	// Error process
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Count Start: AioGetCntStatus = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display status
	//----------------------------------------
	text_string.Format(_T("%08xH ("), cnt_status);
	//--------------------
	// When status 'Counter in operation' is ON
	//--------------------
	if((cnt_status & CNTS_BUSY) == CNTS_BUSY){
		text_string_temp.Format(_T("Operating"));
		text_string	+= text_string_temp;
	//--------------------
	// When status 'Counter in operation' is OFF
	//--------------------
	}else{
		text_string_temp.Format(_T("Stop"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Comparison count match' is ON
	//--------------------
	if((cnt_status & CNTS_DATA_NUM) == CNTS_DATA_NUM){
		text_string_temp.Format(_T(", Comparison count match"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Overrun' is ON
	//--------------------
	if((cnt_status & CNTS_ORERR) == CNTS_ORERR){
		text_string_temp.Format(_T(", Overrun"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Counter operation error' is ON
	//--------------------
	if((cnt_status & CNTS_ERR) == CNTS_ERR){
		text_string_temp.Format(_T(", Counter operation error"));
		text_string	+= text_string_temp;
	}
	text_string_temp.Format(_T(")"));
	text_string	+= text_string_temp;
	m_status.SetWindowText(text_string);
	//----------------------------------------
	// Get count value
	//----------------------------------------
	ret	= AioGetCntCount(g_id, g_channel, &cnt_count);
	//----------------------------------------
	// Error process
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Count Start: AioGetCntCount = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display count value
	//----------------------------------------
	text_string.Format(_T("%ld"), cnt_count);
	m_count.SetWindowText(text_string);
	//----------------------------------------
	// Update the comparison match count
	//----------------------------------------
	text_string.Format(_T("0"));
	m_event_count.SetWindowText(text_string);
	//----------------------------------------
	// Display the message that count start is completed
	//----------------------------------------
	text_string.Format(_T("Count Start: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioGetCntCount button is clicked
// Get the count and display it on the dialog
//================================================================================
void CCounterDlg::OnBnClickedButtonGetCntCount()
{
	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
	long	cnt_count;			// Count value

	//----------------------------------------
	// Get the count value
	//----------------------------------------
	ret	= AioGetCntCount(g_id, g_channel, &cnt_count);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioGetCntCount = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//----------------------------------------
	// When the count value acquisition function is completed successfully
	//----------------------------------------
	if(ret == 0){
		//--------------------
		// Update count value
		//--------------------
		text_string.Format(_T("%ld"), cnt_count);
		m_count.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the AioGetCntStatus button is clicked
// Get the counter status and display it on the dialog
//================================================================================
void CCounterDlg::OnBnClickedButtonGetCntStatus()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	CString	text_string_temp;	// For temporary storage of textbox
	long	ret;				// For return code
	long	cnt_status;			// Status

	//----------------------------------------
	// Get status
	//----------------------------------------
	ret	= AioGetCntStatus(g_id, g_channel, &cnt_status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioGetCntStatus = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//----------------------------------------
	// When the status acquisition function is completed successfully
	//----------------------------------------
	if(ret == 0){
		//----------------------------------------
		// Display message
		//----------------------------------------
		text_string.Format(_T("%08xH ("), cnt_status);
		//--------------------
		// When status 'Counter in operation' is ON
		//--------------------
		if((cnt_status & CNTS_BUSY) == CNTS_BUSY){
			text_string_temp.Format(_T("Operating"));
			text_string	+= text_string_temp;
		//--------------------
		// When status 'Counter in operation' is OFF
		//--------------------
		}else{
			text_string_temp.Format(_T("Stop"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Comparison count match' is ON
		//--------------------
		if((cnt_status & CNTS_DATA_NUM) == CNTS_DATA_NUM){
			text_string_temp.Format(_T(", Comparison count match"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Overrun' is ON
		//--------------------
		if((cnt_status & CNTS_ORERR) == CNTS_ORERR){
			text_string_temp.Format(_T(", Overrun"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Counter operation error' is ON
		//--------------------
		if((cnt_status & CNTS_ERR) == CNTS_ERR){
			text_string_temp.Format(_T(", Counter operation error"));
			text_string	+= text_string_temp;
		}
		text_string_temp.Format(_T(")"));
		text_string	+= text_string_temp;
		m_status.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the Counter Stop button is clicked
// Stop the counter action
//================================================================================
void CCounterDlg::OnBnClickedButtonStop()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	CString	text_string_temp;	// For temporary storage of textbox
	long	ret;				// For return code
	long	cnt_count;			// Count value
	long	cnt_status;			// Status

	//----------------------------------------
	// Stop the counter action
	//----------------------------------------
	ret	= AioStopCnt(g_id, g_channel);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Count Stop: AioStopCnt = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get the count value
	//----------------------------------------
	ret	= AioGetCntCount(g_id, g_channel, &cnt_count);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Count Stop: AioGetCntCount = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display count value
	//----------------------------------------
	text_string.Format(_T("%ld"), cnt_count);
	m_count.SetWindowText(text_string);
	//----------------------------------------
	// Get status
	//----------------------------------------
	ret	= AioGetCntStatus(g_id, g_channel, &cnt_status);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("Count Stop: AioGetCntStatus = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display status
	//----------------------------------------
	text_string.Format(_T("%08xH ("), cnt_status);
	//--------------------
	// When status 'Counter in operation' is ON
	//--------------------
	if((cnt_status & CNTS_BUSY) == CNTS_BUSY){
		text_string_temp.Format(_T("Operating"));
		text_string	+= text_string_temp;
	//--------------------
	// When status 'Counter in operation' is OFF
	//--------------------
	}else{
		text_string_temp.Format(_T("Stop"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Comparison count match' is ON
	//--------------------
	if((cnt_status & CNTS_DATA_NUM) == CNTS_DATA_NUM){
		text_string_temp.Format(_T(", Comparison count match"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Overrun' is ON
	//--------------------
	if((cnt_status & CNTS_ORERR) == CNTS_ORERR){
		text_string_temp.Format(_T(", Overrun"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Counter operation error' is ON
	//--------------------
	if((cnt_status & CNTS_ERR) == CNTS_ERR){
		text_string_temp.Format(_T(", Counter operation error"));
		text_string	+= text_string_temp;
	}
	text_string_temp.Format(_T(")"));
	text_string	+= text_string_temp;
	m_status.SetWindowText(text_string);
	//----------------------------------------
	// Display the message that count stop is completed
	//----------------------------------------
	text_string.Format(_T("Count Stop: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioExit button is clicked
// Perform the exit process of device
//================================================================================
void CCounterDlg::OnBnClickedButtonAioExit()
{
	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	= AioExit(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioExit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

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

//================================================================================
// Operation when event occurred
// Process for each event occurred
//================================================================================
LRESULT CCounterDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	CString	text_string_temp;	// For temporary storage of textbox
	long	ret;				// For return code
	long	cnt_count;			// Count value
	long	cnt_status;			// Status
	short	cnt_channel;		// Channel

	//----------------------------------------
	// Comparison count match event
	// Acquire and display data
	//----------------------------------------
	if(message == AIOM_CNTE_DATA_NUM){
		//----------------------------------------
		// Get counter channel
		//----------------------------------------
		cnt_channel	= (short)(wParam >> 16);
		if(cnt_channel == g_channel){
			//----------------------------------------
			// Get count value
			//----------------------------------------
			ret	= AioGetCntCount(g_id, cnt_channel, &cnt_count);
			//----------------------------------------
			// Error process
			//----------------------------------------
			//--------------------
			// Exit the process if not completed successfully
			//--------------------
			if(ret != 0){
				//--------------------
				// Get error string
				//--------------------
				AioGetErrorString(ret, error_string);
				//--------------------
				// Display message
				//--------------------
				wsprintf(text_temp, _T("%S"), error_string);
				text_string.Format(_T("Comparison Count Match Event Process: AioGetCntCount = %d : %s"), ret, text_temp);
				m_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Display count value
			//----------------------------------------
			text_string.Format(_T("%ld"), cnt_count);
			m_count.SetWindowText(text_string);
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetCntStatus(g_id, cnt_channel, &cnt_status);
			//----------------------------------------
			// Error process
			//----------------------------------------
			//--------------------
			// Exit the process if not completed successfully
			//--------------------
			if(ret != 0){
				//--------------------
				// Get error string
				//--------------------
				AioGetErrorString(ret, error_string);
				//--------------------
				// Display message
				//--------------------
				wsprintf(text_temp, _T("%S"), error_string);
				text_string.Format(_T("Comparison Count Match Event Process: AioGetCntStatus = %d : %s"), ret, text_temp);
				m_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Display status
			//----------------------------------------
			text_string.Format(_T("%08xH ("), cnt_status);
			//--------------------
			// When status 'Counter in operation' is ON
			//--------------------
			if((cnt_status & CNTS_BUSY) == CNTS_BUSY){
				text_string_temp.Format(_T("Operating"));
				text_string	+= text_string_temp;
			//--------------------
			// When status 'Counter in operation' is OFF
			//--------------------
			}else{
				text_string_temp.Format(_T("Stop"));
				text_string	+= text_string_temp;
			}
			//--------------------
			// When status 'Comparison count match' is ON
			//--------------------
			if((cnt_status & CNTS_DATA_NUM) == CNTS_DATA_NUM){
				text_string_temp.Format(_T(", Comparison count match"));
				text_string	+= text_string_temp;
			}
			//--------------------
			// When status 'Overrun' is ON
			//--------------------
			if((cnt_status & CNTS_ORERR) == CNTS_ORERR){
				text_string_temp.Format(_T(", Overrun"));
				text_string	+= text_string_temp;
			}
			//--------------------
			// When status 'Counter operation error' is ON
			//--------------------
			if((cnt_status & CNTS_ERR) == CNTS_ERR){
				text_string_temp.Format(_T(", Counter operation error"));
				text_string	+= text_string_temp;
			}
			text_string_temp.Format(_T(")"));
			text_string	+= text_string_temp;
			m_status.SetWindowText(text_string);
			//----------------------------------------
			// Display the comparison count match event occurrence count
			//----------------------------------------
			g_event_count++;
			text_string.Format(_T("%ld"), g_event_count);
			m_event_count.SetWindowText(text_string);
			//----------------------------------------
			// Reset status
			//----------------------------------------
			ret	= AioResetCntStatus(g_id, cnt_channel, CNTS_DATA_NUM);
			//----------------------------------------
			// Error process
			//----------------------------------------
			//--------------------
			// Exit the process if not completed successfully
			//--------------------
			if(ret != 0){
				//--------------------
				// Get error string
				//--------------------
				AioGetErrorString(ret, error_string);
				//--------------------
				// Display message
				//--------------------
				wsprintf(text_temp, _T("%S"), error_string);
				text_string.Format(_T("Comparison Count Match Event Process: AioResetCntStatus = %d : %s"), ret, text_temp);
				m_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Display the message that event processing is completed
			//----------------------------------------
			text_string.Format(_T("Comparison Count Match Event Process: Successful completion"));
			m_return_code.SetWindowText(text_string);
		}
		return TRUE;
	}
	//----------------------------------------
	// Counter operation error
	// Display the status
	//----------------------------------------
	if(message == AIOM_CNTE_ERR){
		//----------------------------------------
		// Get counter channel
		//----------------------------------------
		cnt_channel	= (short)(wParam >> 16);
		if(cnt_channel == g_channel){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetCntStatus(g_id, cnt_channel, &cnt_status);
			//----------------------------------------
			// Error process
			//----------------------------------------
			//--------------------
			// Exit the process if not completed successfully
			//--------------------
			if(ret != 0){
				//--------------------
				// Get error string
				//--------------------
				AioGetErrorString(ret, error_string);
				//--------------------
				// Display message
				//--------------------
				wsprintf(text_temp, _T("%S"), error_string);
				text_string.Format(_T("Counter Operation Error Occurrence Process: AioGetCntStatus = %d : %s"), ret, text_temp);
				m_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Display status
			//----------------------------------------
			text_string.Format(_T("%08xH ("), cnt_status);
			//--------------------
			// When status 'Counter in operation' is ON
			//--------------------
			if((cnt_status & CNTS_BUSY) == CNTS_BUSY){
				text_string_temp.Format(_T("Operating"));
				text_string	+= text_string_temp;
			//--------------------
			// When status 'Counter in operation' is OFF
			//--------------------
			}else{
				text_string_temp.Format(_T("Stop"));
				text_string	+= text_string_temp;
			}
			//--------------------
			// When status 'Comparison count match' is ON
			//--------------------
			if((cnt_status & CNTS_DATA_NUM) == CNTS_DATA_NUM){
				text_string_temp.Format(_T(", Comparison count match"));
				text_string	+= text_string_temp;
			}
			//--------------------
			// When status 'Overrun' is ON
			//--------------------
			if((cnt_status & CNTS_ORERR) == CNTS_ORERR){
				text_string_temp.Format(_T(", Overrun"));
				text_string	+= text_string_temp;
			}
			//--------------------
			// When status 'Counter operation error' is ON
			//--------------------
			if((cnt_status & CNTS_ERR) == CNTS_ERR){
				text_string_temp.Format(_T(", Counter operation error"));
				text_string	+= text_string_temp;
			}
			text_string_temp.Format(_T(")"));
			text_string	+= text_string_temp;
			m_status.SetWindowText(text_string);
			//----------------------------------------
			// Display the message that event processing is completed
			//----------------------------------------
			text_string.Format(_T("Counter Operation Error Occurrence Process: Successful completion"));
			m_return_code.SetWindowText(text_string);
		}
		return TRUE;
	}

	return CDialogEx::DefWindowProc(message, wParam, lParam);
}
