﻿
// AiSamplingInfiniteUserDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AiSamplingInfiniteUser.h"
#include "AiSamplingInfiniteUserDlg.h"
#include "ConditionDlg.h"
#include "afxdialogex.h"
#include "Caio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAiSamplingInfiniteUserDlg dialog



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

void CAiSamplingInfiniteUserDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_device_name);
	DDX_Control(pDX, IDC_COMBO_RANGE_VALUE, m_ai_range);
	DDX_Control(pDX, IDC_COMBO_INP_METHOD, m_ai_input_method);
	DDX_Control(pDX, IDC_EDIT_CH_NUM, m_ai_channels);
	DDX_Control(pDX, IDC_EDIT_SAMPLING_TIMES, m_ai_sampling_times);
	DDX_Control(pDX, IDC_EDIT_TRANSFER_TIMES, m_ai_transfer_times);
	DDX_Control(pDX, IDC_EDIT_TOTAL_TRANSFER_POSITION, m_ai_transfer_position);
	DDX_Control(pDX, IDC_EDIT_OVERWRITE_COUNT, m_ai_transfer_lap);
	DDX_Control(pDX, IDC_EDIT_STATUS, m_ai_status);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_return_code);
}

BEGIN_MESSAGE_MAP(CAiSamplingInfiniteUserDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_AIO_INIT, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonAioInit)
	ON_BN_CLICKED(IDC_BUTTON_AIO_RESET_DEV, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonAioResetDev)
	ON_BN_CLICKED(IDC_BUTTON_SET_AI_RANGE_ALL, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonSetAiRangeAll)
	ON_BN_CLICKED(IDC_BUTTON_SET_AI_INP_METHOD, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonSetAiInpMethod)
	ON_BN_CLICKED(IDC_BUTTON_SET_AI_CHANNELS, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonSetAiChannels)
	ON_BN_CLICKED(IDC_BUTTON_CONDITION_SETTINGS, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonConditionSettings)
	ON_BN_CLICKED(IDC_BUTTON_USER_BUFFER_SETTINGS, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonUserBufferSettings)
	ON_BN_CLICKED(IDC_BUTTON_SET_AI_EVENT_TRANSFER_TIMES, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonSetAiEventTransferTimes)
	ON_BN_CLICKED(IDC_BUTTON_SAMPLING_START, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonSamplingStart)
	ON_BN_CLICKED(IDC_BUTTON_GET_AI_TRANSFER_COUNT, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonGetAiTransferCount)
	ON_BN_CLICKED(IDC_BUTTON_GET_AI_TRANSFER_LAP, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonGetAiTransferLap)
	ON_BN_CLICKED(IDC_BUTTON_GET_AI_STATUS, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonGetAiStatus)
	ON_BN_CLICKED(IDC_BUTTON_SAMPLING_STOP, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonSamplingStop)
	ON_BN_CLICKED(IDC_BUTTON_AIO_EXIT, &CAiSamplingInfiniteUserDlg::OnBnClickedButtonAioExit)
	ON_BN_CLICKED(IDCLOSE, &CAiSamplingInfiniteUserDlg::OnBnClickedClose)
END_MESSAGE_MAP()

//----------------------------------------
// Macro definition
//----------------------------------------
#define MAX_AI_RANGE_NUM			32						// Maximum number of elements in analog input range array
//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_id;												// ID for device access
long *	g_sampling_buffer = NULL;							// User buffer
long	g_sampling_buffer_size;								// User buffer size
long	g_prev_count;										// Previous position
long	g_prev_lap;											// Previous overwrite count
TCHAR	g_file_name[] = _T("SamplingData.csv");				// Sampling data output file name
short	g_ai_range[MAX_AI_RANGE_NUM] = {	PM10,			// +/-10V
											PM5,			// +/-5V
											PM25,			// +/-2.5V
											PM125,			// +/-1.25V
											PM1,			// +/-1V
											PM0625,			// +/-0.625V
											PM05,			// +/-0.5V
											PM03125,		// +/-0.3125V
											PM025,			// +/-0.25V
											PM0125,			// +/-0.125V
											PM01,			// +/-0.1V
											PM005,			// +/-0.05V
											PM0025,			// +/-0.025V
											PM00125,		// +/-0.0125V
											PM001,			// +/-0.01V
											P10,			// 0 ~ 10V
											P5,				// 0 ~ 5V
											P4095,			// 0 ~ 4.095V
											P25,			// 0 ~ 2.5V
											P125,			// 0 ~ 1.25V
											P1,				// 0 ~ 1V
											P05,			// 0 ~ 0.5V
											P025,			// 0 ~ 0.25V
											P01,			// 0 ~ 0.1V
											P005,			// 0 ~ 0.05V
											P0025,			// 0 ~ 0.025V
											P00125,			// 0 ~ 0.0125V
											P001,			// 0 ~ 0.01V
											P20MA,			// 0 ~ 20mA
											P4TO20MA,		// 4 ~ 20mA
											PM20MA,			// +/-20mA
											P1TO5 };		// 1 ~ 5V

// CAiSamplingInfiniteUserDlg message handlers

BOOL CAiSamplingInfiniteUserDlg::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"));
	//----------------------------------------
	// Create the combobox for analog input range
	//----------------------------------------
	m_ai_range.AddString(_T("-10 - +10V"));
	m_ai_range.AddString(_T("-5 - +5V"));
	m_ai_range.AddString(_T("-2.5 - +2.5V"));
	m_ai_range.AddString(_T("-1.25 - +1.25V"));
	m_ai_range.AddString(_T("-1 - +1V"));
	m_ai_range.AddString(_T("-0.625 - +0.625V"));
	m_ai_range.AddString(_T("-0.5 - +0.5V"));
	m_ai_range.AddString(_T("-0.3125 - +0.3125V"));
	m_ai_range.AddString(_T("-0.25 - +0.25V"));
	m_ai_range.AddString(_T("-0.125 - +0.125V"));
	m_ai_range.AddString(_T("-0.1 - +0.1V"));
	m_ai_range.AddString(_T("-0.05 - +0.05V"));
	m_ai_range.AddString(_T("-0.025 - +0.025V"));
	m_ai_range.AddString(_T("-0.0125 - +0.0125V"));
	m_ai_range.AddString(_T("-0.01 - +0.01V"));
	m_ai_range.AddString(_T("0 - +10V"));
	m_ai_range.AddString(_T("0 - +5V"));
	m_ai_range.AddString(_T("0 - +4.095V"));
	m_ai_range.AddString(_T("0 - +2.5V"));
	m_ai_range.AddString(_T("0 - +1.25V"));
	m_ai_range.AddString(_T("0 - +1V"));
	m_ai_range.AddString(_T("0 - +0.5V"));
	m_ai_range.AddString(_T("0 - +0.25V"));
	m_ai_range.AddString(_T("0 - +0.1V"));
	m_ai_range.AddString(_T("0 - +0.05V"));
	m_ai_range.AddString(_T("0 - +0.025V"));
	m_ai_range.AddString(_T("0 - +0.0125V"));
	m_ai_range.AddString(_T("0 - +0.01V"));
	m_ai_range.AddString(_T("0 - +20mA"));
	m_ai_range.AddString(_T("4 - +20mA"));
	m_ai_range.AddString(_T("-20mA - +20mA"));
	m_ai_range.AddString(_T("1 - +5V"));
	//----------------------------------------
	// Change the selection of analog input range combobox to 0
	//----------------------------------------
	m_ai_range.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for analog input method
	//----------------------------------------
	m_ai_input_method.AddString(_T("Single-end input"));
	m_ai_input_method.AddString(_T("Differential input"));
	//----------------------------------------
	// Change the selection of analog input method combobox to 0
	//----------------------------------------
	m_ai_input_method.SetCurSel(0);
	//----------------------------------------
	// Update the initial display of number of channels for analog input
	//----------------------------------------
	m_ai_channels.SetWindowTextW(_T("1"));
	//----------------------------------------
	// Update the initial display of the sampling times
	//----------------------------------------
	m_ai_sampling_times.SetWindowTextW(_T("2000"));
	//----------------------------------------
	// Update the initial display of the data transfer times
	//----------------------------------------
	m_ai_transfer_times.SetWindowTextW(_T("1000"));
	//----------------------------------------
	// Initialize global variables
	//----------------------------------------
	g_id					= 0;
	g_sampling_buffer_size	= 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 CAiSamplingInfiniteUserDlg::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 CAiSamplingInfiniteUserDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the AioInit button is clicked
// Initialize the device
//================================================================================
void CAiSamplingInfiniteUserDlg::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 CAiSamplingInfiniteUserDlg::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 AioSetAiRangeAll button is clicked
// Set the input range of analog input
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonSetAiRangeAll()
{
	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
	short	index;				// Index of combobox

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the index of range setting value from combobox
	//----------------------------------------
	index	= m_ai_range.GetCurSel();
	//----------------------------------------
	// Set the analog input range (all channels)
	//----------------------------------------
	ret	= AioSetAiRangeAll(g_id, g_ai_range[index]);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAiRangeAll = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioSetAiInputMethod button is clicked
// Set the input method of analog input
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonSetAiInpMethod()
{
	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
	short	input_method;		// Input method

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the index of input method setting value from combobox
	//----------------------------------------
	input_method	= m_ai_input_method.GetCurSel();
	//----------------------------------------
	// Set the analog input method
	//----------------------------------------
	ret	= AioSetAiInputMethod(g_id, input_method);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAiInputMethod = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioSetAiChannels button is clicked
// Set the number of analog input channels used for sampling
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonSetAiChannels()
{
	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
	short	ai_channels;		// Number of channels

	//----------------------------------------
	// Get the number of channels from textbox
	//----------------------------------------
	m_ai_channels.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &ai_channels);
	//----------------------------------------
	// Set the number of channels for the analog input conversion.
	//----------------------------------------
	ret	= AioSetAiChannels(g_id, ai_channels);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAiChannels = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Condition Set button is clicked
// Open the condition setting dialog
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonConditionSettings()
{
	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

	//----------------------------------------
	// Open the condition setting dialog
	//----------------------------------------
	CConditionDlg	dlg;
	if(dlg.DoModal() == IDOK){
		//----------------------------------------
		// Set the event factor to 'Event that AD conversion start', 'Event that device operation end',
		// 'Event that the specified number of data are transferred', 'Event that overflow',
		// 'Event that sampling clock error', and 'Event that AD conversion error'.
		// Caution for modify this function (if modified, the application may not work)
		//----------------------------------------
		ret	= AioSetAiEvent(g_id, m_hWnd, (AIE_START | AIE_END | AIE_DATA_TSF | AIE_OFERR | AIE_SCERR | AIE_ADERR));
		//----------------------------------------
		// 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("Condition Set: AioSetAiEvent = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Display the message that condition setting is completed
		//----------------------------------------
		text_string.Format(_T("Condition Set: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the User Buffer Size Set button is clicked
// Set the user buffer that stores the conversion data
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonUserBufferSettings()
{
	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
	short	data_size;			// Data size (Number of packets)
	long	sampling_times;		// Sampling times

	//----------------------------------------
	// Set the analog input conversion data transfer mode to user buffer mode
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiTransferMode(g_id, 1);
	//----------------------------------------
	// 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("User Buffer Size Set: AioSetAiTransferMode = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// When the user buffer mode is used to transfer data, retrieves data size that is necessary for one sampling
	//----------------------------------------
	ret	= AioGetAiSamplingDataSize(g_id, &data_size);
	//----------------------------------------
	// 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("User Buffer Size Set: AioGetAiSamplingDataSize = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get the sampling times from textbox
	//----------------------------------------
	m_ai_sampling_times.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%ld"), &sampling_times);
	//----------------------------------------
	// Allocate the user buffer for storing conversion data
	//----------------------------------------
	if(g_sampling_buffer != NULL){
		free(g_sampling_buffer);
		g_sampling_buffer	= NULL;
	}
	//--------------------
	// User buffer size: number of packets that is necessary for one sampling x sampling times
	//--------------------
	g_sampling_buffer_size	= data_size * sampling_times;
	g_sampling_buffer		= (long *)malloc(sizeof(long) * g_sampling_buffer_size);
	if(g_sampling_buffer == NULL){
		text_string.Format(_T("User Buffer Size Set: Failed to allocate the user buffer"));
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Set user buffer
	//----------------------------------------
	ret	= AioSetAiTransferData(g_id, g_sampling_buffer_size, g_sampling_buffer);
	//----------------------------------------
	// 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("User Buffer Size Set: AioSetAiTransferData = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display the message that user buffer setting is completed
	//----------------------------------------
	text_string.Format(_T("User Buffer Size Set: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioSetAiEventTransferTimes button is clicked
// Set the sampling times when "Event that the specified number of data are transferred" is used
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonSetAiEventTransferTimes()
{
	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	transfer_times;		// Data transfer times

	//----------------------------------------
	// Get the data transfer times from textbox
	//----------------------------------------
	m_ai_transfer_times.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%ld"), &transfer_times);
	//----------------------------------------
	// Set the sampling times when "Event that the specified number of data are transferred" is used
	//----------------------------------------
	ret	= AioSetAiEventTransferTimes(g_id, transfer_times);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAiEventTransferTimes = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Sampling Start button is clicked
// After clearing the memory and status, start sampling.
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonSamplingStart()
{
	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	ai_status;			// Analog input status
	long	ret;				// For return code

	//----------------------------------------
	// Clear status
	//----------------------------------------
	ret	= AioResetAiStatus(g_id);
	//----------------------------------------
	// 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("Sampling Start: AioResetAiStatus = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Initialize the transfer count
	//----------------------------------------
	g_prev_count	= 0;
	g_prev_lap		= 0;
	//----------------------------------------
	// Delete the data file
	//----------------------------------------
	_tremove(g_file_name);
	//----------------------------------------
	// Start sampling
	//----------------------------------------
	ret	= AioStartAi(g_id);
	//----------------------------------------
	// 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("Sampling Start: AioStartAi = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get status
	//----------------------------------------
	ret	= AioGetAiStatus(g_id, &ai_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("Sampling Start: AioGetAiStatus = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display status
	//----------------------------------------
	text_string.Format(_T("%08xH ("), ai_status);
	//--------------------
	// When status 'Device is running' is ON
	//--------------------
	if((ai_status & AIS_BUSY) == AIS_BUSY){
		text_string_temp.Format(_T("Operating"));
		text_string	+= text_string_temp;
	//--------------------
	// When status 'Device is running' is OFF
	//--------------------
	}else{
		text_string_temp.Format(_T("Stop"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Wait the start trigger' is ON
	//--------------------
	if((ai_status & AIS_START_TRG) == AIS_START_TRG){
		text_string_temp.Format(_T(", Wait the start trigger"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'The specified number of data are stored' is ON
	//--------------------
	if((ai_status & AIS_DATA_NUM) == AIS_DATA_NUM){
		text_string_temp.Format(_T(", The specified number of data are stored"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Overflow' is ON
	//--------------------
	if((ai_status & AIS_OFERR) == AIS_OFERR){
		text_string_temp.Format(_T(", Overflow"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Sampling clock error' is ON
	//--------------------
	if((ai_status & AIS_SCERR) == AIS_SCERR){
		text_string_temp.Format(_T(", Sampling clock error"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'AD conversion error' is ON
	//--------------------
	if((ai_status & AIS_AIERR) == AIS_AIERR){
		text_string_temp.Format(_T(", AD conversion error"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Driver spec error' is ON
	//--------------------
	if((ai_status & AIS_DRVERR) == AIS_DRVERR){
		text_string_temp.Format(_T(", Driver spec error"));
		text_string	+= text_string_temp;
	}
	text_string_temp.Format(_T(")"));
	text_string	+= text_string_temp;
	m_ai_status.SetWindowText(text_string);
	//----------------------------------------
	// Update the transfer count
	//----------------------------------------
	text_string.Format(_T("0"));
	m_ai_transfer_position.SetWindowText(text_string);
	//----------------------------------------
	// Update the overwrite count
	//----------------------------------------
	text_string.Format(_T("0"));
	m_ai_transfer_lap.SetWindowText(text_string);
	//----------------------------------------
	// Display the message that sampling start is completed
	//----------------------------------------
	text_string.Format(_T("Sampling Start: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioGetAiTransferCount button is clicked
// Get the transfer count and display it on the dialog
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonGetAiTransferCount()
{
	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	ai_transfer_count;	// Transfer count

	//----------------------------------------
	// Get the number of data that are transferred from the user memory
	//----------------------------------------
	ret	= AioGetAiTransferCount(g_id, &ai_transfer_count);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioGetAiTransferCount = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//----------------------------------------
	// When the transfer count acquisition function is completed successfully
	//----------------------------------------
	if(ret == 0){
		//--------------------
		// Update the transfer count
		//--------------------
		text_string.Format(_T("%ld"), ai_transfer_count);
		m_ai_transfer_position.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the AioGetAiTransferLap button is clicked
// Get the overwrite count and display it on the dialog
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonGetAiTransferLap()
{
	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	ai_transfer_lap;	// Overwrite count

	//----------------------------------------
	// Get the number of overwrite if the user buffer is used in "Overwrite the memory"
	//----------------------------------------
	ret	= AioGetAiTransferLap(g_id, &ai_transfer_lap);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioGetAiTransferLap = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//----------------------------------------
	// When the overwrite count acquisition function is completed successfully
	//----------------------------------------
	if(ret == 0){
		//--------------------
		// Update the overwrite count
		//--------------------
		text_string.Format(_T("%ld"), ai_transfer_lap);
		m_ai_transfer_lap.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the AioGetAiStatus button is clicked
// Get the analog input status and display it on the dialog
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonGetAiStatus()
{
	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	ai_status;			// Analog input status
	long	ret;				// For return code

	//----------------------------------------
	// Get status
	//----------------------------------------
	ret	= AioGetAiStatus(g_id, &ai_status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioGetAiStatus = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//----------------------------------------
	// When the status acquisition function is completed successfully
	//----------------------------------------
	if(ret == 0){
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ai_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ai_status & AIS_BUSY) == AIS_BUSY){
			text_string_temp.Format(_T("Operating"));
		//--------------------
		// When status 'Device is running' is OFF
		//--------------------
		}else{
			text_string_temp.Format(_T("Stop"));
		}
		text_string	+= text_string_temp;
		//--------------------
		// When status 'Wait the start trigger' is ON
		//--------------------
		if((ai_status & AIS_START_TRG) == AIS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are stored' is ON
		//--------------------
		if((ai_status & AIS_DATA_NUM) == AIS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are stored"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Overflow' is ON
		//--------------------
		if((ai_status & AIS_OFERR) == AIS_OFERR){
			text_string_temp.Format(_T(", Overflow"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ai_status & AIS_SCERR) == AIS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'AD conversion error' is ON
		//--------------------
		if((ai_status & AIS_AIERR) == AIS_AIERR){
			text_string_temp.Format(_T(", AD conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ai_status & AIS_DRVERR) == AIS_DRVERR){
			text_string_temp.Format(_T(", Driver spec error"));
			text_string	+= text_string_temp;
		}
		text_string_temp.Format(_T(")"));
		text_string	+= text_string_temp;
		m_ai_status.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the Sampling Stop button is clicked
// Stop sampling
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedButtonSamplingStop()
{
	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

	//----------------------------------------
	// Stop sampling
	//----------------------------------------
	ret	= AioStopAi(g_id);
	//----------------------------------------
	// 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("Sampling Stop: AioStopAi = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display the message that sampling stop is completed
	//----------------------------------------
	text_string.Format(_T("Sampling 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 CAiSamplingInfiniteUserDlg::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);
	//----------------------------------------
	// Release the user buffer for storing conversion data
	//----------------------------------------
	if(g_sampling_buffer != NULL){
		free(g_sampling_buffer);
		g_sampling_buffer	= NULL;
	}
}

//================================================================================
// It is the process when the Close button is clicked
// Perform the exit process of device and terminate the application
//================================================================================
void CAiSamplingInfiniteUserDlg::OnBnClickedClose()
{
	//----------------------------------------
	// Exit process
	//----------------------------------------
	AioExit(g_id);
	//----------------------------------------
	// Release the user buffer for storing conversion data
	//----------------------------------------
	if(g_sampling_buffer != NULL){
		free(g_sampling_buffer);
		g_sampling_buffer	= NULL;
	}
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}

//================================================================================
// Operation when event occurred
// Process for each event occurred
//================================================================================
LRESULT CAiSamplingInfiniteUserDlg::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	ai_transfer_count;	// Transfer count
	long	ai_transfer_lap;	// Overwrite count
	long	ai_status;			// Status
	long	ret;				// For return code
	FILE *	fp;					// File pointer
	errno_t	file_ret;			// For return code of the file operation function
	long	i;					// Loop count

	//----------------------------------------
	// Event that AD conversion start
	// Display the status
	//----------------------------------------
	if(message == AIOM_AIE_START){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAiStatus(g_id, &ai_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("Event that AD conversion start Process: AioGetAiStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ai_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ai_status & AIS_BUSY) == AIS_BUSY){
			text_string_temp.Format(_T("Operating"));
		//--------------------
		// When status 'Device is running' is OFF
		//--------------------
		}else{
			text_string_temp.Format(_T("Stop"));
		}
		text_string	+= text_string_temp;
		//--------------------
		// When status 'Wait the start trigger' is ON
		//--------------------
		if((ai_status & AIS_START_TRG) == AIS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are stored' is ON
		//--------------------
		if((ai_status & AIS_DATA_NUM) == AIS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are stored"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Overflow' is ON
		//--------------------
		if((ai_status & AIS_OFERR) == AIS_OFERR){
			text_string_temp.Format(_T(", Overflow"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ai_status & AIS_SCERR) == AIS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'AD conversion error' is ON
		//--------------------
		if((ai_status & AIS_AIERR) == AIS_AIERR){
			text_string_temp.Format(_T(", AD conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ai_status & AIS_DRVERR) == AIS_DRVERR){
			text_string_temp.Format(_T(", Driver spec error"));
			text_string	+= text_string_temp;
		}
		text_string_temp.Format(_T(")"));
		text_string	+= text_string_temp;
		m_ai_status.SetWindowText(text_string);
		//----------------------------------------
		// Display the message that event processing is completed
		//----------------------------------------
		text_string.Format(_T("Event that AD conversion start: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}
	//----------------------------------------
	// Event that the specified number of data are transferred
	// Acquire and display data
	//----------------------------------------
	if(message == AIOM_AIE_DATA_TSF){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAiStatus(g_id, &ai_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("Event that the specified number of data are transferred Process: AioGetAiStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ai_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ai_status & AIS_BUSY) == AIS_BUSY){
			text_string_temp.Format(_T("Operating"));
		//--------------------
		// When status 'Device is running' is OFF
		//--------------------
		}else{
			text_string_temp.Format(_T("Stop"));
		}
		text_string	+= text_string_temp;
		//--------------------
		// When status 'Wait the start trigger' is ON
		//--------------------
		if((ai_status & AIS_START_TRG) == AIS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are stored' is ON
		//--------------------
		if((ai_status & AIS_DATA_NUM) == AIS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are stored"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Overflow' is ON
		//--------------------
		if((ai_status & AIS_OFERR) == AIS_OFERR){
			text_string_temp.Format(_T(", Overflow"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ai_status & AIS_SCERR) == AIS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'AD conversion error' is ON
		//--------------------
		if((ai_status & AIS_AIERR) == AIS_AIERR){
			text_string_temp.Format(_T(", AD conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ai_status & AIS_DRVERR) == AIS_DRVERR){
			text_string_temp.Format(_T(", Driver spec error"));
			text_string	+= text_string_temp;
		}
		text_string_temp.Format(_T(")"));
		text_string	+= text_string_temp;
		m_ai_status.SetWindowText(text_string);
		//----------------------------------------
		// Get the number of data that are transferred from the user memory
		//----------------------------------------
		ret	= AioGetAiTransferCount(g_id, &ai_transfer_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("Event that the specified number of data are transferred Process: AioGetAiTransferCount = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Update the transfer count
		//----------------------------------------
		text_string.Format(_T("%ld"), ai_transfer_count);
		m_ai_transfer_position.SetWindowText(text_string);
		//----------------------------------------
		// Get the number of overwrite if the user buffer is used in "Overwrite the memory"
		//----------------------------------------
		ret	= AioGetAiTransferLap(g_id, &ai_transfer_lap);
		//----------------------------------------
		// 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("Event that the specified number of data are transferred Process: AioGetAiTransferLap = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Update the overwrite count
		//----------------------------------------
		text_string.Format(_T("%ld"), ai_transfer_lap);
		m_ai_transfer_lap.SetWindowText(text_string);
		//----------------------------------------
		// Check overrun error
		//----------------------------------------
		if((ai_transfer_lap - g_prev_lap >= 2) || ((ai_transfer_lap - g_prev_lap == 1) && (ai_transfer_count >= g_prev_count))){
			text_string.Format(_T("Event that the specified number of data are transferred Process: Overrun Error!(count = %d)"), (g_sampling_buffer_size * ai_transfer_lap) + ai_transfer_count);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Output sampling data to file
		//----------------------------------------
		//--------------------
		// Open file
		//--------------------
		file_ret	= _wfopen_s(&fp, g_file_name, _T("a"));
		if(file_ret != 0){
			text_string.Format(_T("Event that the specified number of data are transferred Process: fopen = file open error."));
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//--------------------
		// If the transfer position is greater than the previous position
		//--------------------
		if(ai_transfer_count > g_prev_count){
			//--------------------
			// Repeat the process from the previous position to the transfer position
			//--------------------
			for(i = g_prev_count; i < ai_transfer_count; i++){
				text_string.Empty();
				//--------------------
				// Generate display string
				//--------------------
				text_string_temp.Format(_T("%5d,"), (i + 1) + (g_sampling_buffer_size * ai_transfer_lap));
				text_string	+= text_string_temp;
				text_string_temp.Format(_T(" % 08X\n"), g_sampling_buffer[i]);
				text_string	+= text_string_temp;
				//--------------------
				// Write data
				//--------------------
				fputws(text_string, fp);
			}
		//--------------------
		// If the transfer position is smaller than the previous position
		//--------------------
		}else{
			//--------------------
			// Repeat process from previous position to user buffer size
			//--------------------
			for(i = g_prev_count; i < g_sampling_buffer_size; i++){
				text_string.Empty();
				//--------------------
				// Generate display string
				//--------------------
				text_string_temp.Format(_T("%5d,"), (i + 1) + (g_sampling_buffer_size * g_prev_lap));
				text_string	+= text_string_temp;
				text_string_temp.Format(_T(" % 08X\n"), g_sampling_buffer[i]);
				text_string	+= text_string_temp;
				//--------------------
				// Write data
				//--------------------
				fputws(text_string, fp);
			}
			//--------------------
			// Repeat the process from 0 to transfer position
			//--------------------
			for(i = 0; i < ai_transfer_count; i++){
				text_string.Empty();
				//--------------------
				// Generate display string
				//--------------------
				text_string_temp.Format(_T("%5d,"), (i + 1) + (g_sampling_buffer_size * ai_transfer_lap));
				text_string	+= text_string_temp;
				text_string_temp.Format(_T(" % 08X\n"), g_sampling_buffer[i]);
				text_string	+= text_string_temp;
				//--------------------
				// Write data
				//--------------------
				fputws(text_string, fp);
			}
		}
		//--------------------
		// Close file
		//--------------------
		fclose(fp);
		//----------------------------------------
		// Update previous position
		//----------------------------------------
		g_prev_count	= ai_transfer_count;
		g_prev_lap		= ai_transfer_lap;
		//----------------------------------------
		// Display the message that event processing is completed
		//----------------------------------------
		text_string.Format(_T("Event that the specified number of data are transferred Process: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}
	//----------------------------------------
	// Event that device operation end
	// Acquire and display data
	//----------------------------------------
	if(message == AIOM_AIE_END){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAiStatus(g_id, &ai_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("Device Operation End Event Process: AioGetAiStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ai_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ai_status & AIS_BUSY) == AIS_BUSY){
			text_string_temp.Format(_T("Operating"));
		//--------------------
		// When status 'Device is running' is OFF
		//--------------------
		}else{
			text_string_temp.Format(_T("Stop"));
		}
		text_string	+= text_string_temp;
		//--------------------
		// When status 'Wait the start trigger' is ON
		//--------------------
		if((ai_status & AIS_START_TRG) == AIS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are stored' is ON
		//--------------------
		if((ai_status & AIS_DATA_NUM) == AIS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are stored"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Overflow' is ON
		//--------------------
		if((ai_status & AIS_OFERR) == AIS_OFERR){
			text_string_temp.Format(_T(", Overflow"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ai_status & AIS_SCERR) == AIS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'AD conversion error' is ON
		//--------------------
		if((ai_status & AIS_AIERR) == AIS_AIERR){
			text_string_temp.Format(_T(", AD conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ai_status & AIS_DRVERR) == AIS_DRVERR){
			text_string_temp.Format(_T(", Driver spec error"));
			text_string	+= text_string_temp;
		}
		text_string_temp.Format(_T(")"));
		text_string	+= text_string_temp;
		m_ai_status.SetWindowText(text_string);
		//----------------------------------------
		// Get the number of data that are transferred from the user memory
		//----------------------------------------
		ret	= AioGetAiTransferCount(g_id, &ai_transfer_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("Device Operation End Event Process: AioGetAiTransferCount = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Update the transfer count
		//----------------------------------------
		text_string.Format(_T("%d"), ai_transfer_count);
		m_ai_transfer_position.SetWindowText(text_string);
		//----------------------------------------
		// Get the number of overwrite if the user buffer is used in "Overwrite the memory"
		//----------------------------------------
		ret	= AioGetAiTransferLap(g_id, &ai_transfer_lap);
		//----------------------------------------
		// 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("Device Operation End Event Process: AioGetAiTransferLap = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Update the overwrite count
		//----------------------------------------
		text_string.Format(_T("%d"), ai_transfer_lap);
		m_ai_transfer_lap.SetWindowText(text_string);
		//----------------------------------------
		// Check overrun error
		//----------------------------------------
		if((ai_transfer_lap - g_prev_lap >= 2) || ((ai_transfer_lap - g_prev_lap == 1) && (ai_transfer_count >= g_prev_count))){
			text_string.Format(_T("Device Operation End Event Process: Overrun Error!(count = %d)"), (g_sampling_buffer_size * ai_transfer_lap) + ai_transfer_count);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Output sampling data to file
		//----------------------------------------
		//--------------------
		// Open file
		//--------------------
		file_ret	= _wfopen_s(&fp, g_file_name, _T("a"));
		if(file_ret != 0){
			text_string.Format(_T("Device Operation End Event Process: fopen = file open error."));
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//--------------------
		// If the transfer position is greater than the previous position
		//--------------------
		if(ai_transfer_count > g_prev_count){
			//--------------------
			// Repeat the process from the previous position to the transfer position
			//--------------------
			for(i = g_prev_count; i < ai_transfer_count; i++){
				text_string.Empty();
				//--------------------
				// Generate display string
				//--------------------
				text_string_temp.Format(_T("%5d,"), (i + 1) + (g_sampling_buffer_size * ai_transfer_lap));
				text_string	+= text_string_temp;
				text_string_temp.Format(_T(" % 08X\n"), g_sampling_buffer[i]);
				text_string	+= text_string_temp;
				//--------------------
				// Write data
				//--------------------
				fputws(text_string, fp);
			}
		//--------------------
		// If the transfer position is smaller than the previous position
		//--------------------
		}else{
			//--------------------
			// Repeat process from previous position to user buffer size
			//--------------------
			for(i = g_prev_count; i < g_sampling_buffer_size; i++){
				text_string.Empty();
				//--------------------
				// Generate display string
				//--------------------
				text_string_temp.Format(_T("%5d,"), (i + 1) + (g_sampling_buffer_size * g_prev_lap));
				text_string	+= text_string_temp;
				text_string_temp.Format(_T(" % 08X\n"), g_sampling_buffer[i]);
				text_string	+= text_string_temp;
				//--------------------
				// Write data
				//--------------------
				fputws(text_string, fp);
			}
			//--------------------
			// Repeat the process from 0 to transfer position
			//--------------------
			for(i = 0; i < ai_transfer_count; i++){
				text_string.Empty();
				//--------------------
				// Generate display string
				//--------------------
				text_string_temp.Format(_T("%5d,"), (i + 1) + (g_sampling_buffer_size * ai_transfer_lap));
				text_string	+= text_string_temp;
				text_string_temp.Format(_T(" % 08X\n"), g_sampling_buffer[i]);
				text_string	+= text_string_temp;
				//--------------------
				// Write data
				//--------------------
				fputws(text_string, fp);
			}
		}
		//--------------------
		// Close file
		//--------------------
		fclose(fp);
		//----------------------------------------
		// Update previous position
		//----------------------------------------
		g_prev_count	= ai_transfer_count;
		g_prev_lap		= ai_transfer_lap;
		//----------------------------------------
		// Display the message that event processing is completed
		//----------------------------------------
		text_string.Format(_T("Device Operation End Event Process: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}
	//----------------------------------------
	// Event that overflow
	// Display the status
	//----------------------------------------
	if(message == AIOM_AIE_OFERR){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAiStatus(g_id, &ai_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("Overflow Occurrence Process: AioGetAiStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ai_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ai_status & AIS_BUSY) == AIS_BUSY){
			text_string_temp.Format(_T("Operating"));
		//--------------------
		// When status 'Device is running' is OFF
		//--------------------
		}else{
			text_string_temp.Format(_T("Stop"));
		}
		text_string	+= text_string_temp;
		//--------------------
		// When status 'Wait the start trigger' is ON
		//--------------------
		if((ai_status & AIS_START_TRG) == AIS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are stored' is ON
		//--------------------
		if((ai_status & AIS_DATA_NUM) == AIS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are stored"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Overflow' is ON
		//--------------------
		if((ai_status & AIS_OFERR) == AIS_OFERR){
			text_string_temp.Format(_T(", Overflow"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ai_status & AIS_SCERR) == AIS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'AD conversion error' is ON
		//--------------------
		if((ai_status & AIS_AIERR) == AIS_AIERR){
			text_string_temp.Format(_T(", AD conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ai_status & AIS_DRVERR) == AIS_DRVERR){
			text_string_temp.Format(_T(", Driver spec error"));
			text_string	+= text_string_temp;
		}
		text_string_temp.Format(_T(")"));
		text_string	+= text_string_temp;
		m_ai_status.SetWindowText(text_string);
		//----------------------------------------
		// Display the message that event processing is completed
		//----------------------------------------
		text_string.Format(_T("Overflow Occurrence Process: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}
	//----------------------------------------
	// Event that sampling clock error
	// Display the status
	//----------------------------------------
	if(message == AIOM_AIE_SCERR){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAiStatus(g_id, &ai_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("Sampling Clock Error Occurrence Process: AioGetAiStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ai_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ai_status & AIS_BUSY) == AIS_BUSY){
			text_string_temp.Format(_T("Operating"));
		//--------------------
		// When status 'Device is running' is OFF
		//--------------------
		}else{
			text_string_temp.Format(_T("Stop"));
		}
		text_string	+= text_string_temp;
		//--------------------
		// When status 'Wait the start trigger' is ON
		//--------------------
		if((ai_status & AIS_START_TRG) == AIS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are stored' is ON
		//--------------------
		if((ai_status & AIS_DATA_NUM) == AIS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are stored"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Overflow' is ON
		//--------------------
		if((ai_status & AIS_OFERR) == AIS_OFERR){
			text_string_temp.Format(_T(", Overflow"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ai_status & AIS_SCERR) == AIS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'AD conversion error' is ON
		//--------------------
		if((ai_status & AIS_AIERR) == AIS_AIERR){
			text_string_temp.Format(_T(", AD conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ai_status & AIS_DRVERR) == AIS_DRVERR){
			text_string_temp.Format(_T(", Driver spec error"));
			text_string	+= text_string_temp;
		}
		text_string_temp.Format(_T(")"));
		text_string	+= text_string_temp;
		m_ai_status.SetWindowText(text_string);
		//----------------------------------------
		// Display the message that event processing is completed
		//----------------------------------------
		text_string.Format(_T("Sampling Clock Error Occurrence Process: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}
	//----------------------------------------
	// Event that AD conversion error
	// Display the status
	//----------------------------------------
	if(message == AIOM_AIE_ADERR){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAiStatus(g_id, &ai_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("AD Conversion Error Occurrence Process: AioGetAiStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ai_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ai_status & AIS_BUSY) == AIS_BUSY){
			text_string_temp.Format(_T("Operating"));
		//--------------------
		// When status 'Device is running' is OFF
		//--------------------
		}else{
			text_string_temp.Format(_T("Stop"));
		}
		text_string	+= text_string_temp;
		//--------------------
		// When status 'Wait the start trigger' is ON
		//--------------------
		if((ai_status & AIS_START_TRG) == AIS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are stored' is ON
		//--------------------
		if((ai_status & AIS_DATA_NUM) == AIS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are stored"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Overflow' is ON
		//--------------------
		if((ai_status & AIS_OFERR) == AIS_OFERR){
			text_string_temp.Format(_T(", Overflow"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ai_status & AIS_SCERR) == AIS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'AD conversion error' is ON
		//--------------------
		if((ai_status & AIS_AIERR) == AIS_AIERR){
			text_string_temp.Format(_T(", AD conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ai_status & AIS_DRVERR) == AIS_DRVERR){
			text_string_temp.Format(_T(", Driver spec error"));
			text_string	+= text_string_temp;
		}
		text_string_temp.Format(_T(")"));
		text_string	+= text_string_temp;
		m_ai_status.SetWindowText(text_string);
		//----------------------------------------
		// Display the message that event processing is completed
		//----------------------------------------
		text_string.Format(_T("AD Conversion Error Occurrence Process: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}

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