﻿
// AoGeneratingOnceDevDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AoGeneratingOnceDev.h"
#include "AoGeneratingOnceDevDlg.h"
#include "afxdialogex.h"
#include "Caio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAoGeneratingOnceDevDlg dialog



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

void CAoGeneratingOnceDevDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_device_name);
	DDX_Control(pDX, IDC_COMBO_RANGE_VALUE, m_ao_range);
	DDX_Control(pDX, IDC_EDIT_CH_NUM, m_ao_channels);
	DDX_Control(pDX, IDC_EDIT_GENERATING_CLOCK, m_ao_generating_clock);
	DDX_Control(pDX, IDC_EDIT_GENERATING_TIMES, m_ao_generating_times);
	DDX_Control(pDX, IDC_EDIT_GENERATING_COUNT, m_ao_generating_count);
	DDX_Control(pDX, IDC_EDIT_STATUS, m_ao_status);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_return_code);
}

BEGIN_MESSAGE_MAP(CAoGeneratingOnceDevDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_AIO_INIT, &CAoGeneratingOnceDevDlg::OnBnClickedButtonAioInit)
	ON_BN_CLICKED(IDC_BUTTON_RESET_DEVICE, &CAoGeneratingOnceDevDlg::OnBnClickedButtonResetDevice)
	ON_BN_CLICKED(IDC_BUTTON_SET_AO_RANGE_ALL, &CAoGeneratingOnceDevDlg::OnBnClickedButtonSetAoRangeAll)
	ON_BN_CLICKED(IDC_BUTTON_SET_AO_CHANNELS, &CAoGeneratingOnceDevDlg::OnBnClickedButtonSetAoChannels)
	ON_BN_CLICKED(IDC_BUTTON_SET_AO_SAMPLING_CLOCK, &CAoGeneratingOnceDevDlg::OnBnClickedButtonSetAoSamplingClock)
	ON_BN_CLICKED(IDC_BUTTON_SMP_BASIC_SETTINGS, &CAoGeneratingOnceDevDlg::OnBnClickedButtonSmpBasicSettings)
	ON_BN_CLICKED(IDC_BUTTON_OUTPUT_DATA_GENERATE, &CAoGeneratingOnceDevDlg::OnBnClickedButtonOutputDataGenerate)
	ON_BN_CLICKED(IDC_BUTTON_SET_AO_SAMPLING_DATA_EX, &CAoGeneratingOnceDevDlg::OnBnClickedButtonSetAoSamplingDataEx)
	ON_BN_CLICKED(IDC_BUTTON_GENERATING_START, &CAoGeneratingOnceDevDlg::OnBnClickedButtonGeneratingStart)
	ON_BN_CLICKED(IDC_BUTTON_GET_AO_SAMPLING_COUNT, &CAoGeneratingOnceDevDlg::OnBnClickedButtonGetAoSamplingCount)
	ON_BN_CLICKED(IDC_BUTTON_GET_AO_STATUS, &CAoGeneratingOnceDevDlg::OnBnClickedButtonGetAoStatus)
	ON_BN_CLICKED(IDC_BUTTON_GENERATING_STOP, &CAoGeneratingOnceDevDlg::OnBnClickedButtonGeneratingStop)
	ON_BN_CLICKED(IDC_BUTTON_AIO_EXIT, &CAoGeneratingOnceDevDlg::OnBnClickedButtonAioExit)
	ON_BN_CLICKED(IDCLOSE, &CAoGeneratingOnceDevDlg::OnBnClickedClose)
END_MESSAGE_MAP()

//----------------------------------------
// Macro definition
//----------------------------------------
#define MAX_AO_RANGE_NUM	31								// Maximum number of elements in analog output range array
//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_id;												// ID for device access
TCHAR	g_file_name[] = _T("GeneratingData.csv");			// Generating data output file name
long	g_generating_count;									// Total generating count
float *	g_generating_data;									// Output data
short	g_ao_range[MAX_AO_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
											P1TO5 };		// 1 ~ 5V

// CAoGeneratingOnceDevDlg message handlers

BOOL CAoGeneratingOnceDevDlg::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 number of channels for analog output
	//----------------------------------------
	m_ao_channels.SetWindowTextW(_T("1"));
	//----------------------------------------
	// Update the initial display of generating clock
	//----------------------------------------
	m_ao_generating_clock.SetWindowTextW(_T("1000"));
	//----------------------------------------
	// Update the initial display of output data generating times
	//----------------------------------------
	m_ao_generating_times.SetWindowTextW(_T("1000"));
	//----------------------------------------
	// Create the combobox for analog output range
	//----------------------------------------
	m_ao_range.AddString(_T("-10 - +10V"));
	m_ao_range.AddString(_T("-5 - +5V"));
	m_ao_range.AddString(_T("-2.5 - +2.5V"));
	m_ao_range.AddString(_T("-1.25 - +1.25V"));
	m_ao_range.AddString(_T("-1 - +1V"));
	m_ao_range.AddString(_T("-0.625 - +0.625V"));
	m_ao_range.AddString(_T("-0.5 - +0.5V"));
	m_ao_range.AddString(_T("-0.3125 - +0.3125V"));
	m_ao_range.AddString(_T("-0.25 - +0.25V"));
	m_ao_range.AddString(_T("-0.125 - +0.125V"));
	m_ao_range.AddString(_T("-0.1 - +0.1V"));
	m_ao_range.AddString(_T("-0.05 - +0.05V"));
	m_ao_range.AddString(_T("-0.025 - +0.025V"));
	m_ao_range.AddString(_T("-0.0125 - +0.0125V"));
	m_ao_range.AddString(_T("-0.01 - +0.01V"));
	m_ao_range.AddString(_T("0 - +10V"));
	m_ao_range.AddString(_T("0 - +5V"));
	m_ao_range.AddString(_T("0 - +4.095V"));
	m_ao_range.AddString(_T("0 - +2.5V"));
	m_ao_range.AddString(_T("0 - +1.25V"));
	m_ao_range.AddString(_T("0 - +1V"));
	m_ao_range.AddString(_T("0 - +0.5V"));
	m_ao_range.AddString(_T("0 - +0.25V"));
	m_ao_range.AddString(_T("0 - +0.1V"));
	m_ao_range.AddString(_T("0 - +0.05V"));
	m_ao_range.AddString(_T("0 - +0.025V"));
	m_ao_range.AddString(_T("0 - +0.0125V"));
	m_ao_range.AddString(_T("0 - +0.01V"));
	m_ao_range.AddString(_T("0 - +20mA"));
	m_ao_range.AddString(_T("4 - +20mA"));
	m_ao_range.AddString(_T("1 - +5V"));
	//----------------------------------------
	// Change the selection of analog output range combobox to 0
	//----------------------------------------
	m_ao_range.SetCurSel(0);
	//----------------------------------------
	// Initialize global variables
	//----------------------------------------
	g_id				= 0;
	g_generating_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 CAoGeneratingOnceDevDlg::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 CAoGeneratingOnceDevDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the AioInit button is clicked
// Initialize the device
//================================================================================
void CAoGeneratingOnceDevDlg::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 CAoGeneratingOnceDevDlg::OnBnClickedButtonResetDevice()
{
	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 AioSetAoRangeAll button is clicked
// Set the output range of analog output
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonSetAoRangeAll()
{
	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_ao_range.GetCurSel();
	//----------------------------------------
	// Set the analog output range (all channels)
	//----------------------------------------
	ret	= AioSetAoRangeAll(g_id, g_ao_range[index]);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAoRangeAll = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioSetAoChannels button is clicked
// Set the number of analog output channels used for generating
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonSetAoChannels()
{
	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	ao_channels;		// Number of channels

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

//================================================================================
// It is the process when the AioSetAoSamplingClock button is clicked
// Set the internal clock conversion speed used for generating
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonSetAoSamplingClock()
{
	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
	float	generating_clock;	// Internal clock conversion speed

	//----------------------------------------
	// Get the internal clock conversion speed from textbox
	//----------------------------------------
	m_ao_generating_clock.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%f"), &generating_clock);
	//----------------------------------------
	// Set the internal clock conversion speed
	//----------------------------------------
	ret	= AioSetAoSamplingClock(g_id, generating_clock);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAoSamplingClock = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Basic Settings button is clicked
// Performs the basic settings required for generating
// (Basic setting must be performed after initialization processing/reset processing and before starting the first generating)
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonSmpBasicSettings()
{
	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

	//----------------------------------------
	// Set the analog output conversion data transfer mode to device buffer mode
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAoTransferMode(g_id, 0);
	//----------------------------------------
	// 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("Basic Settings: AioSetAoTransferMode = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Set the data storage memory type to FIFO
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAoMemoryType(g_id, 0);
	//----------------------------------------
	// 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("Basic Settings: AioSetAoMemoryType = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Set the generating start condition to Software
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAoStartTrigger(g_id, 0);
	//----------------------------------------
	// 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("Basic Settings: AioSetAoStartTrigger = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Set the clock type to Internal Clock
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAoClockType(g_id, 0);
	//----------------------------------------
	// 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("Basic Settings: AioSetAoClockType = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Set the generating stop condition to Stop conversion by the specified times
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAoStopTrigger(g_id, 0);
	//----------------------------------------
	// 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("Basic Settings: AioSetAoStopTrigger = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Set the event factor to 'Event that device operation end', 
	// 'Event that sampling clock error', and 'Event that DA conversion error'.
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAoEvent(g_id, m_hWnd, (AOE_END | AOE_SCERR | AOE_DAERR));
	//----------------------------------------
	// 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("Basic Settings: AioSetAoEvent = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display the message that basic setting is completed
	//----------------------------------------
	text_string.Format(_T("Basic Settings: Successful completion"));
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Generate Output Data button is clicked
// Generate output data file
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonOutputDataGenerate()
{
	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
	FILE	*fp;				// File pointer
	long	i, j;				// Loop count
	long	ret;				// For return code
	errno_t	file_ret;			// For return code of the file operation function
	long	generating_times;	// Generating times of output data
	short	ao_channels;		// Number of channels
	short	ao_range;			// Range
	float	gain, offset;		// Coefficient of sine waveform

	//----------------------------------------
	// Get the number of channels
	//----------------------------------------
	ret	= AioGetAoChannels(g_id, &ao_channels);
	//----------------------------------------
	// 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("Generate Output Data: AioGetAoChannels = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get range (channel 0 only)
	//----------------------------------------
	ret	= AioGetAoRange(g_id, 0, &ao_range);
	//----------------------------------------
	// 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("Generate Output Data: AioGetAoRange = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get generating times from textbox
	//----------------------------------------
	m_ao_generating_times.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%ld"), &generating_times);
	//----------------------------------------
	// Output data process
	//----------------------------------------
	//--------------------
	// Allocate an array for data storage
	//--------------------
	if(g_generating_data != NULL){
		free(g_generating_data);
		g_generating_data	= NULL;
	}
	g_generating_data	= (float *)malloc(sizeof(float) * generating_times * ao_channels);
	if(g_generating_data == NULL){
		text_string.Format(_T("Generate Output Data: Failed to allocate an array for storing data"));
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Create analog output data: sine waveform
	//----------------------------------------
	switch(ao_range){
	case PM10:
		gain	= 10;
		offset	= 0;
		break;
	case PM5:
		gain	= 5;
		offset	= 0;
		break;
	case PM25:
		gain	= 2.5f;
		offset	= 0;
		break;
	case PM125:
		gain	= 1.25f;
		offset	= 0;
		break;
	case PM1:
		gain	= 1;
		offset	= 0;
		break;
	case PM0625:
		gain	= 0.625f;
		offset	= 0;
		break;
	case PM05:
		gain	= 0.5f;
		offset	= 0;
		break;
	case PM03125:
		gain	= 0.3125f;
		offset	= 0;
		break;
	case PM025:
		gain	= 0.25f;
		offset	= 0;
		break;
	case PM0125:
		gain	= 0.125f;
		offset	= 0;
		break;
	case PM01:
		gain	= 0.1f;
		offset	= 0;
		break;
	case PM005:
		gain	= 0.05f;
		offset	= 0;
		break;
	case PM0025:
		gain	= 0.025f;
		offset	= 0;
		break;
	case PM00125:
		gain	= 0.0125f;
		offset	= 0;
		break;
	case PM001:
		gain	= 0.01f;
		offset	= 0;
		break;
	case P10:
		gain	= 5;
		offset	= 5;
		break;
	case P5:
		gain	= 2.5f;
		offset	= 2.5f;
		break;
	case P4095:
		gain	= 2.0475f;
		offset	= 2.0475f;
		break;
	case P25:
		gain	= 1.25f;
		offset	= 1.25f;
		break;
	case P125:
		gain	= 0.625f;
		offset	= 0.625f;
		break;
	case P1:
		gain	= 0.5f;
		offset	= 0.5f;
		break;
	case P05:
		gain	= 0.25f;
		offset	= 0.25f;
		break;
	case P025:
		gain	= 0.125f;
		offset	= 0.125f;
		break;
	case P01:
		gain	= 0.05f;
		offset	= 0.05f;
		break;
	case P005:
		gain	= 0.025f;
		offset	= 0.025f;
		break;
	case P0025:
		gain	= 0.0125f;
		offset	= 0.0125f;
		break;
	case P00125:
		gain	= 0.00625f;
		offset	= 0.00625f;
		break;
	case P001:
		gain	= 0.005f;
		offset	= 0.005f;
		break;
	case P20MA:
		gain	= 10;
		offset	= 10;
		break;
	case P4TO20MA:
		gain	= 8;
		offset	= 12;
		break;
	case P1TO5:
		gain	= 2;
		offset	= 3;
		break;
	default:
		gain	= 10;
		offset	= 0;
		break;
	}
	for(i = 0; i < generating_times; i++){
		for(j = 0; j < ao_channels; j++){
			g_generating_data[i * ao_channels + j]	= (float)(gain * sin(2 * 3.14159265358979 * i / generating_times + 3.14159265358979 / 2 * (j % 4)) + offset);
		}
	}
	//----------------------------------------
	// Open file
	//----------------------------------------
	file_ret	= _wfopen_s(&fp, g_file_name, _T("w"));
	if(file_ret != 0){
		text_string.Format(_T("Generate Output Data: fopen = file open error."));
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Create file
	//----------------------------------------
	for(i = 0; i < generating_times; i++){
		text_string.Empty();
		//--------------------
		// Generate display string
		//--------------------
		text_string_temp.Format(_T("%5ld,"), (i + 1));
		text_string	+= text_string_temp;
		//--------------------
		// Repeat the process for the specified number of channels
		//--------------------
		for(j = 0; j < ao_channels; j++){
			text_string_temp.Format(_T(" %10.5f"), g_generating_data[i * ao_channels + j]);
			text_string	+= text_string_temp;
			//--------------------
			// Add newline character for the last channel
			//--------------------
			if(j == (ao_channels - 1)){
				text_string_temp.Format(_T("\n"));
				text_string	+= text_string_temp;
			//--------------------
			// Add comma for channels other than the last channel
			//--------------------
			}else{
				text_string_temp.Format(_T(","));
				text_string	+= text_string_temp;
			}
		}
		//----------------------------------------
		// Write data
		//----------------------------------------
		fputws(text_string, fp);
	}
	//----------------------------------------
	// Close file
	//----------------------------------------
	fclose(fp);
	//----------------------------------------
	// Display message
	//----------------------------------------
	m_return_code.SetWindowText(_T("Generate Output Data: Successful completion"));
}

//================================================================================
// It is the process when the AioSetAoSamplingDataEx button is clicked
// Set the output data
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonSetAoSamplingDataEx()
{
	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	generating_times;	// Output data generating times

	//----------------------------------------
	// Get generating times from textbox
	//----------------------------------------
	m_ao_generating_times.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%ld"), &generating_times);
	//----------------------------------------
	// Set analog output data
	//----------------------------------------
	ret	= AioSetAoSamplingDataEx(g_id, generating_times, g_generating_data);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAoSamplingDataEx = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Generating Start button is clicked
// After clearing the memory and status, start generating.
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonGeneratingStart()
{
	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			ao_status;			// Analog output status
	long			i, j;				// Loop count
	long			ret;				// For return code
	HANDLE			h_find;				// For the return code of the file search function
	WIN32_FIND_DATA	find_file_data;		// File information
	long			generating_times;	// Output data generating times
	short			ao_channels;		// Number of channels
	short			ao_range;			// Range
	float			gain, offset;		// Coefficient of sine waveform

	//----------------------------------------
	// Reset the analog output status
	//----------------------------------------
	ret	= AioResetAoStatus(g_id);
	//----------------------------------------
	// 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("Generating Start: AioResetAoStatus = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// If the output data file does not exist
	//----------------------------------------
	if((h_find = FindFirstFile(g_file_name, &find_file_data)) == INVALID_HANDLE_VALUE){
		//----------------------------------------
		// Get the number of channels
		//----------------------------------------
		ret	= AioGetAoChannels(g_id, &ao_channels);
		//----------------------------------------
		// 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("Generating Start: AioGetAoChannels = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Get range (channel 0 only)
		//----------------------------------------
		ret	= AioGetAoRange(g_id, 0, &ao_range);
		//----------------------------------------
		// 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("Generating Start: AioGetAoRange = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Get generating times from textbox
		//----------------------------------------
		m_ao_generating_times.GetWindowTextW(text_temp, _countof(text_temp));
		_stscanf_s(text_temp, _T("%ld"), &generating_times);
		//----------------------------------------
		// Output data process
		//----------------------------------------
		//--------------------
		// Allocate an array for data storage
		//--------------------
		if(g_generating_data != NULL){
			free(g_generating_data);
			g_generating_data	= NULL;
		}
		g_generating_data	= (float *)malloc(sizeof(float) * generating_times * ao_channels);
		if(g_generating_data == NULL){
			text_string.Format(_T("Generating Start: Failed to allocate an array for storing data"));
			m_return_code.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Create analog output data: sine waveform
		//----------------------------------------
		switch(ao_range){
		case PM10:
			gain	= 10;
			offset	= 0;
			break;
		case PM5:
			gain	= 5;
			offset	= 0;
			break;
		case PM25:
			gain	= 2.5f;
			offset	= 0;
			break;
		case PM125:
			gain	= 1.25f;
			offset	= 0;
			break;
		case PM1:
			gain	= 1;
			offset	= 0;
			break;
		case PM0625:
			gain	= 0.625f;
			offset	= 0;
			break;
		case PM05:
			gain	= 0.5f;
			offset	= 0;
			break;
		case PM03125:
			gain	= 0.3125f;
			offset	= 0;
			break;
		case PM025:
			gain	= 0.25f;
			offset	= 0;
			break;
		case PM0125:
			gain	= 0.125f;
			offset	= 0;
			break;
		case PM01:
			gain	= 0.1f;
			offset	= 0;
			break;
		case PM005:
			gain	= 0.05f;
			offset	= 0;
			break;
		case PM0025:
			gain	= 0.025f;
			offset	= 0;
			break;
		case PM00125:
			gain	= 0.0125f;
			offset	= 0;
			break;
		case PM001:
			gain	= 0.01f;
			offset	= 0;
			break;
		case P10:
			gain	= 5;
			offset	= 5;
			break;
		case P5:
			gain	= 2.5f;
			offset	= 2.5f;
			break;
		case P4095:
			gain	= 2.0475f;
			offset	= 2.0475f;
			break;
		case P25:
			gain	= 1.25f;
			offset	= 1.25f;
			break;
		case P125:
			gain	= 0.625f;
			offset	= 0.625f;
			break;
		case P1:
			gain	= 0.5f;
			offset	= 0.5f;
			break;
		case P05:
			gain	= 0.25f;
			offset	= 0.25f;
			break;
		case P025:
			gain	= 0.125f;
			offset	= 0.125f;
			break;
		case P01:
			gain	= 0.05f;
			offset	= 0.05f;
			break;
		case P005:
			gain	= 0.025f;
			offset	= 0.025f;
			break;
		case P0025:
			gain	= 0.0125f;
			offset	= 0.0125f;
			break;
		case P00125:
			gain	= 0.00625f;
			offset	= 0.00625f;
			break;
		case P001:
			gain	= 0.005f;
			offset	= 0.005f;
			break;
		case P20MA:
			gain	= 10;
			offset	= 10;
			break;
		case P4TO20MA:
			gain	= 8;
			offset	= 12;
			break;
		case P1TO5:
			gain	= 2;
			offset	= 3;
			break;
		default:
			gain	= 10;
			offset	= 0;
			break;
		}
		for(i = 0; i < generating_times; i++){
			for(j = 0; j < ao_channels; j++){
				g_generating_data[i * ao_channels + j]	= (float)(gain * sin(2 * 3.14159265358979 * i / generating_times + 3.14159265358979 / 2 * (j % 4)) + offset);
			}
		}
		//----------------------------------------
		// Set analog output data
		//----------------------------------------
		ret	= AioSetAoSamplingDataEx(g_id, generating_times, g_generating_data);
		//----------------------------------------
		// 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("Generating Start: AioSetAoSamplingDataEx = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return;
		}
	}else{
		//----------------------------------------
		// Close file search handle
		//----------------------------------------
		FindClose(h_find);
	}
	//----------------------------------------
	// Initialize the total generating count
	//----------------------------------------
	g_generating_count	= 0;
	//----------------------------------------
	// Start generating
	//----------------------------------------
	ret	= AioStartAo(g_id);
	//----------------------------------------
	// 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("Generating Start: AioStartAo = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get status
	//----------------------------------------
	ret	= AioGetAoStatus(g_id, &ao_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("Generating Start: AioGetAoStatus = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display status
	//----------------------------------------
	text_string.Format(_T("%08xH ("), ao_status);
	//--------------------
	// When status 'Device is running' is ON
	//--------------------
	if((ao_status & AOS_BUSY) == AOS_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((ao_status & AOS_START_TRG) == AOS_START_TRG){
		text_string_temp.Format(_T(", Wait the start trigger"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'The specified number of data are output' is ON
	//--------------------
	if((ao_status & AOS_DATA_NUM) == AOS_DATA_NUM){
		text_string_temp.Format(_T(", The specified number of data are output"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Sampling clock error' is ON
	//--------------------
	if((ao_status & AOS_SCERR) == AOS_SCERR){
		text_string_temp.Format(_T(", Sampling clock error"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'DA conversion error' is ON
	//--------------------
	if((ao_status & AOS_AOERR) == AOS_AOERR){
		text_string_temp.Format(_T(", DA conversion error"));
		text_string	+= text_string_temp;
	}
	//--------------------
	// When status 'Driver spec error' is ON
	//--------------------
	if((ao_status & AOS_DRVERR) == AOS_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_ao_status.SetWindowText(text_string);
	//----------------------------------------
	// Update generating count
	//----------------------------------------
	text_string.Format(_T("0"));
	m_ao_generating_count.SetWindowText(text_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	m_return_code.SetWindowText(_T("Generating Start: Successful completion"));
}

//================================================================================
// It is the process when the AioGetAoSamplingCount button is clicked
// Get the generating count and display it on the dialog
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonGetAoSamplingCount()
{
	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	ao_generating_count;	// Generating count

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

//================================================================================
// It is the process when the AioGetAoStatus button is clicked
// Get the analog output status and display it on the dialog
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonGetAoStatus()
{
	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	ao_status;				// Analog output status
	long	ret;					// For return code

	//----------------------------------------
	// Get status
	//----------------------------------------
	ret	= AioGetAoStatus(g_id, &ao_status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioGetAoStatus = %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 ("), ao_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ao_status & AOS_BUSY) == AOS_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((ao_status & AOS_START_TRG) == AOS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are output' is ON
		//--------------------
		if((ao_status & AOS_DATA_NUM) == AOS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are output"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ao_status & AOS_SCERR) == AOS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'DA conversion error' is ON
		//--------------------
		if((ao_status & AOS_AOERR) == AOS_AOERR){
			text_string_temp.Format(_T(", DA conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ao_status & AOS_DRVERR) == AOS_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_ao_status.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the Generating Stop button is clicked
// Stop generating
//================================================================================
void CAoGeneratingOnceDevDlg::OnBnClickedButtonGeneratingStop()
{
	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 generating
	//----------------------------------------
	ret	= AioStopAo(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("Generating Stop: AioStopAo = %d : %s"), ret, text_temp);
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Display the message that generating stop is completed
	//----------------------------------------
	text_string.Format(_T("Generating 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 CAoGeneratingOnceDevDlg::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

	//----------------------------------------
	// Release the output data buffer
	//----------------------------------------
	if(g_generating_data != NULL){
		free(g_generating_data);
		g_generating_data	= NULL;
	}
	//----------------------------------------
	// 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 CAoGeneratingOnceDevDlg::OnBnClickedClose()
{
	//----------------------------------------
	// Release the output data buffer
	//----------------------------------------
	if(g_generating_data != NULL){
		free(g_generating_data);
		g_generating_data	= NULL;
	}
	//----------------------------------------
	// Exit process
	//----------------------------------------
	AioExit(g_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}

//================================================================================
// Operation when event occurred
// Process for each event occurred
//================================================================================
LRESULT CAoGeneratingOnceDevDlg::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	ao_generating_count;	// Generating count
	long	ao_status;				// Status
	long	ret;					// For return code

	//----------------------------------------
	// Event that device operation end
	// Acquire and display data
	//----------------------------------------
	if(message == AIOM_AOE_END){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAoStatus(g_id, &ao_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: AioGetAoStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ao_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ao_status & AOS_BUSY) == AOS_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((ao_status & AOS_START_TRG) == AOS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are output' is ON
		//--------------------
		if((ao_status & AOS_DATA_NUM) == AOS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are output"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ao_status & AOS_SCERR) == AOS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'DA conversion error' is ON
		//--------------------
		if((ao_status & AOS_AOERR) == AOS_AOERR){
			text_string_temp.Format(_T(", DA conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ao_status & AOS_DRVERR) == AOS_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_ao_status.SetWindowText(text_string);
		//----------------------------------------
		// Get the current generating count
		//----------------------------------------
		ret	= AioGetAoSamplingCount(g_id, &ao_generating_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: AioGetAoSamplingCount = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display generating count
		//----------------------------------------
		text_string.Format(_T("%d"), ao_generating_count);
		m_ao_generating_count.SetWindowText(text_string);
		//----------------------------------------
		// Update the total generating count
		//----------------------------------------
		g_generating_count	= ao_generating_count;
		//----------------------------------------
		// 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 sampling clock error
	// Display the status
	//----------------------------------------
	if(message == AIOM_AOE_SCERR){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAoStatus(g_id, &ao_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: AioGetAoStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ao_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ao_status & AOS_BUSY) == AOS_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((ao_status & AOS_START_TRG) == AOS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are output' is ON
		//--------------------
		if((ao_status & AOS_DATA_NUM) == AOS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are output"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ao_status & AOS_SCERR) == AOS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'DA conversion error' is ON
		//--------------------
		if((ao_status & AOS_AOERR) == AOS_AOERR){
			text_string_temp.Format(_T(", DA conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ao_status & AOS_DRVERR) == AOS_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_ao_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 DA conversion error
	// Display the status
	//----------------------------------------
	if(message == AIOM_AOE_DAERR){
		//----------------------------------------
		// Get status
		//----------------------------------------
		ret	= AioGetAoStatus(g_id, &ao_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("DA Conversion Error Occurrence Process: AioGetAoStatus = %d : %s"), ret, text_temp);
			m_return_code.SetWindowText(text_string);
			return TRUE;
		}
		//----------------------------------------
		// Display status
		//----------------------------------------
		text_string.Format(_T("%08xH ("), ao_status);
		//--------------------
		// When status 'Device is running' is ON
		//--------------------
		if((ao_status & AOS_BUSY) == AOS_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((ao_status & AOS_START_TRG) == AOS_START_TRG){
			text_string_temp.Format(_T(", Wait the start trigger"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'The specified number of data are output' is ON
		//--------------------
		if((ao_status & AOS_DATA_NUM) == AOS_DATA_NUM){
			text_string_temp.Format(_T(", The specified number of data are output"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Sampling clock error' is ON
		//--------------------
		if((ao_status & AOS_SCERR) == AOS_SCERR){
			text_string_temp.Format(_T(", Sampling clock error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'DA conversion error' is ON
		//--------------------
		if((ao_status & AOS_AOERR) == AOS_AOERR){
			text_string_temp.Format(_T(", DA conversion error"));
			text_string	+= text_string_temp;
		}
		//--------------------
		// When status 'Driver spec error' is ON
		//--------------------
		if((ao_status & AOS_DRVERR) == AOS_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_ao_status.SetWindowText(text_string);
		//----------------------------------------
		// Display the message that event processing is completed
		//----------------------------------------
		text_string.Format(_T("DA Conversion Error Occurrence Process: Successful completion"));
		m_return_code.SetWindowText(text_string);
	}

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