﻿
// SyncBusSamplingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SyncBusSampling.h"
#include "SyncBusSamplingDlg.h"
#include "afxdialogex.h"
#include "Caio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSyncBusSamplingDlg dialog



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

void CSyncBusSamplingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_MASTER_DEVICE_NAME, m_master_device_name);
	DDX_Control(pDX, IDC_COMBO_MASTER_AI_RANGE_VALUE, m_master_ai_range);
	DDX_Control(pDX, IDC_COMBO_MASTER_AI_INP_METHOD, m_master_ai_input_method);
	DDX_Control(pDX, IDC_EDIT_MASTER_AI_CH_NUM, m_master_ai_channels);
	DDX_Control(pDX, IDC_EDIT_MASTER_INTERNAL_CLOCK, m_master_ai_sampling_clock);
	DDX_Control(pDX, IDC_EDIT_MASTER_STOP_TIMES, m_master_ai_stop_times);
	DDX_Control(pDX, IDC_LIST_MASTER_AI_SAMPLING_DATA, m_master_ai_sampling_data);
	DDX_Control(pDX, IDC_EDIT_MASTER_AI_SAMPLING_COUNT, m_master_ai_sampling_count);
	DDX_Control(pDX, IDC_EDIT_MASTER_AI_STATUS, m_master_ai_status);
	DDX_Control(pDX, IDC_EDIT_MASTER_RETURN_CODE, m_master_return_code);
	DDX_Control(pDX, IDC_EDIT_SLAVE_DEVICE_NAME, m_slave_device_name);
	DDX_Control(pDX, IDC_COMBO_SLAVE_AI_RANGE_VALUE, m_slave_ai_range);
	DDX_Control(pDX, IDC_COMBO_SLAVE_AI_INP_METHOD, m_slave_ai_input_method);
	DDX_Control(pDX, IDC_EDIT_SLAVE_AI_CH_NUM, m_slave_ai_channels);
	DDX_Control(pDX, IDC_LIST_SLAVE_AI_SAMPLING_DATA, m_slave_ai_sampling_data);
	DDX_Control(pDX, IDC_EDIT_SLAVE_AI_SAMPLING_COUNT, m_slave_ai_sampling_count);
	DDX_Control(pDX, IDC_EDIT_SLAVE_AI_STATUS, m_slave_ai_status);
	DDX_Control(pDX, IDC_EDIT_SLAVE_RETURN_CODE, m_slave_return_code);
}

BEGIN_MESSAGE_MAP(CSyncBusSamplingDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_AIO_INIT, &CSyncBusSamplingDlg::OnBnClickedButtonAioInit)
	ON_BN_CLICKED(IDC_BUTTON_AIO_RESET_DEV, &CSyncBusSamplingDlg::OnBnClickedButtonAioResetDev)
	ON_BN_CLICKED(IDC_BUTTON_AI_SETTING, &CSyncBusSamplingDlg::OnBnClickedButtonAiSetting)
	ON_BN_CLICKED(IDC_BUTTON_SAMPLING_SETTING, &CSyncBusSamplingDlg::OnBnClickedButtonSamplingSetting)
	ON_BN_CLICKED(IDC_BUTTON_SYNC_SETTING, &CSyncBusSamplingDlg::OnBnClickedButtonSyncSetting)
	ON_BN_CLICKED(IDC_BUTTON_SAMPLING_START, &CSyncBusSamplingDlg::OnBnClickedButtonSamplingStart)
	ON_BN_CLICKED(IDC_BUTTON_GET_STATUS, &CSyncBusSamplingDlg::OnBnClickedButtonGetStatus)
	ON_BN_CLICKED(IDC_BUTTON_SAMPLING_STOP, &CSyncBusSamplingDlg::OnBnClickedButtonSamplingStop)
	ON_BN_CLICKED(IDC_BUTTON_MASTER_OUTPUT_SAMPLING_DATA, &CSyncBusSamplingDlg::OnBnClickedButtonMasterOutputSamplingData)
	ON_BN_CLICKED(IDC_BUTTON_SLAVE_OUTPUT_SAMPLING_DATA, &CSyncBusSamplingDlg::OnBnClickedButtonSlaveOutputSamplingData)
	ON_BN_CLICKED(IDC_BUTTON_AIO_EXIT, &CSyncBusSamplingDlg::OnBnClickedButtonAioExit)
	ON_BN_CLICKED(IDCLOSE, &CSyncBusSamplingDlg::OnBnClickedClose)
END_MESSAGE_MAP()

//----------------------------------------
// Macro definition
//----------------------------------------
#define MAX_AI_RANGE_NUM			32					// Maximum number of elements in analog input range array
#define DISPLAY_SAMPLING_COUNT		1000				// Maximum sampling count of sampling data displayed in listbox
//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_master_id;									// [Master] ID for device access
short	g_slave_id;										// [Slave]  ID for device access
long	g_master_sampling_count;						// [Master] Total sampling count
long	g_slave_sampling_count;							// [Slave]  Total sampling count
float *	g_master_sampling_data = NULL;					// [Master] Pointer to sampling data storage area
float *	g_slave_sampling_data = NULL;					// [Slave]  Pointer to sampling data storage area
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

// CSyncBusSamplingDlg message handlers

BOOL CSyncBusSamplingDlg::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

	//----------------------------------------
	// [Master] Update the initial display of device name
	//----------------------------------------
	m_master_device_name.SetWindowTextW(_T("AIO000"));
	//----------------------------------------
	// [Master] Update the initial display of number of channels for analog input
	//----------------------------------------
	m_master_ai_channels.SetWindowTextW(_T("1"));
	//----------------------------------------
	// [Master] Update the initial display of sampling clock
	//----------------------------------------
	m_master_ai_sampling_clock.SetWindowTextW(_T("1000"));
	//----------------------------------------
	// [Master] Update the initial display of sampling stop times
	//----------------------------------------
	m_master_ai_stop_times.SetWindowTextW(_T("1000"));
	//----------------------------------------
	// [Master] Create the combobox for analog input method
	//----------------------------------------
	m_master_ai_input_method.AddString(_T("Single-end input"));
	m_master_ai_input_method.AddString(_T("Differential input"));
	//----------------------------------------
	// [Master] Change the selection of analog input method combobox to 0
	//----------------------------------------
	m_master_ai_input_method.SetCurSel(0);
	//----------------------------------------
	// [Master] Create the combobox for analog input range
	//----------------------------------------
	m_master_ai_range.AddString(_T("-10 - +10V"));
	m_master_ai_range.AddString(_T("-5 - +5V"));
	m_master_ai_range.AddString(_T("-2.5 - +2.5V"));
	m_master_ai_range.AddString(_T("-1.25 - +1.25V"));
	m_master_ai_range.AddString(_T("-1 - +1V"));
	m_master_ai_range.AddString(_T("-0.625 - +0.625V"));
	m_master_ai_range.AddString(_T("-0.5 - +0.5V"));
	m_master_ai_range.AddString(_T("-0.3125 - +0.3125V"));
	m_master_ai_range.AddString(_T("-0.25 - +0.25V"));
	m_master_ai_range.AddString(_T("-0.125 - +0.125V"));
	m_master_ai_range.AddString(_T("-0.1 - +0.1V"));
	m_master_ai_range.AddString(_T("-0.05 - +0.05V"));
	m_master_ai_range.AddString(_T("-0.025 - +0.025V"));
	m_master_ai_range.AddString(_T("-0.0125 - +0.0125V"));
	m_master_ai_range.AddString(_T("-0.01 - +0.01V"));
	m_master_ai_range.AddString(_T("0 - +10V"));
	m_master_ai_range.AddString(_T("0 - +5V"));
	m_master_ai_range.AddString(_T("0 - +4.095V"));
	m_master_ai_range.AddString(_T("0 - +2.5V"));
	m_master_ai_range.AddString(_T("0 - +1.25V"));
	m_master_ai_range.AddString(_T("0 - +1V"));
	m_master_ai_range.AddString(_T("0 - +0.5V"));
	m_master_ai_range.AddString(_T("0 - +0.25V"));
	m_master_ai_range.AddString(_T("0 - +0.1V"));
	m_master_ai_range.AddString(_T("0 - +0.05V"));
	m_master_ai_range.AddString(_T("0 - +0.025V"));
	m_master_ai_range.AddString(_T("0 - +0.0125V"));
	m_master_ai_range.AddString(_T("0 - +0.01V"));
	m_master_ai_range.AddString(_T("0 - +20mA"));
	m_master_ai_range.AddString(_T("4 - +20mA"));
	m_master_ai_range.AddString(_T("-20mA - +20mA"));
	m_master_ai_range.AddString(_T("1 - +5V"));
	//----------------------------------------
	// [Master] Change the selection of analog input range combobox to 0
	//----------------------------------------
	m_master_ai_range.SetCurSel(0);
	//----------------------------------------
	// [Slave]  Update the initial display of device name
	//----------------------------------------
	m_slave_device_name.SetWindowTextW(_T("AIO001"));
	//----------------------------------------
	// [Slave]  Update the initial display of number of channels for analog input
	//----------------------------------------
	m_slave_ai_channels.SetWindowTextW(_T("1"));
	//----------------------------------------
	// [Slave]  Create the combobox for analog input method
	//----------------------------------------
	m_slave_ai_input_method.AddString(_T("Single-end input"));
	m_slave_ai_input_method.AddString(_T("Differential input"));
	//----------------------------------------
	// [Slave]  Change the selection of analog input method combobox to 0
	//----------------------------------------
	m_slave_ai_input_method.SetCurSel(0);
	//----------------------------------------
	// [Slave]  Create the combobox for analog input range
	//----------------------------------------
	m_slave_ai_range.AddString(_T("-10 - +10V"));
	m_slave_ai_range.AddString(_T("-5 - +5V"));
	m_slave_ai_range.AddString(_T("-2.5 - +2.5V"));
	m_slave_ai_range.AddString(_T("-1.25 - +1.25V"));
	m_slave_ai_range.AddString(_T("-1 - +1V"));
	m_slave_ai_range.AddString(_T("-0.625 - +0.625V"));
	m_slave_ai_range.AddString(_T("-0.5 - +0.5V"));
	m_slave_ai_range.AddString(_T("-0.3125 - +0.3125V"));
	m_slave_ai_range.AddString(_T("-0.25 - +0.25V"));
	m_slave_ai_range.AddString(_T("-0.125 - +0.125V"));
	m_slave_ai_range.AddString(_T("-0.1 - +0.1V"));
	m_slave_ai_range.AddString(_T("-0.05 - +0.05V"));
	m_slave_ai_range.AddString(_T("-0.025 - +0.025V"));
	m_slave_ai_range.AddString(_T("-0.0125 - +0.0125V"));
	m_slave_ai_range.AddString(_T("-0.01 - +0.01V"));
	m_slave_ai_range.AddString(_T("0 - +10V"));
	m_slave_ai_range.AddString(_T("0 - +5V"));
	m_slave_ai_range.AddString(_T("0 - +4.095V"));
	m_slave_ai_range.AddString(_T("0 - +2.5V"));
	m_slave_ai_range.AddString(_T("0 - +1.25V"));
	m_slave_ai_range.AddString(_T("0 - +1V"));
	m_slave_ai_range.AddString(_T("0 - +0.5V"));
	m_slave_ai_range.AddString(_T("0 - +0.25V"));
	m_slave_ai_range.AddString(_T("0 - +0.1V"));
	m_slave_ai_range.AddString(_T("0 - +0.05V"));
	m_slave_ai_range.AddString(_T("0 - +0.025V"));
	m_slave_ai_range.AddString(_T("0 - +0.0125V"));
	m_slave_ai_range.AddString(_T("0 - +0.01V"));
	m_slave_ai_range.AddString(_T("0 - +20mA"));
	m_slave_ai_range.AddString(_T("4 - +20mA"));
	m_slave_ai_range.AddString(_T("-20mA - +20mA"));
	m_slave_ai_range.AddString(_T("1 - +5V"));
	//----------------------------------------
	// [Slave]  Change the selection of analog input range combobox to 0
	//----------------------------------------
	m_slave_ai_range.SetCurSel(0);
	//----------------------------------------
	// Initialize global variables
	//----------------------------------------
	g_master_id				= 0;
	g_slave_id				= 0;
	g_master_sampling_count	= 0;
	g_slave_sampling_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 CSyncBusSamplingDlg::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 CSyncBusSamplingDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the AioInit button is clicked
// Initialize the device
//================================================================================
void CSyncBusSamplingDlg::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

	//----------------------------------------
	// [Master] Get device name from textbox
	//----------------------------------------
	m_master_device_name.GetWindowTextW(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// [Master] Device initialization
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioInit(device_name, &g_master_id);
	//----------------------------------------
	// [Master] Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// [Master] Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioInit = %d : %s"), ret, text_temp);
	m_master_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Get device name from textbox
	//----------------------------------------
	m_slave_device_name.GetWindowTextW(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// [Slave]  Device initialization
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioInit(device_name, &g_slave_id);
	//----------------------------------------
	// [Slave]  Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// [Slave]  Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioInit = %d : %s"), ret, text_temp);
	m_slave_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioResetDevice button is clicked
// Reset the device
//================================================================================
void CSyncBusSamplingDlg::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

	//----------------------------------------
	// [Master] Reset device
	//----------------------------------------
	ret	= AioResetDevice(g_master_id);
	//----------------------------------------
	// [Master] Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// [Master] Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioResetDevice = %d : %s"), ret, text_temp);
	m_master_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Reset device
	//----------------------------------------
	ret	= AioResetDevice(g_slave_id);
	//----------------------------------------
	// [Slave]  Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// [Slave]  Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioResetDevice = %d : %s"), ret, text_temp);
	m_slave_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Analog Input Set button is clicked
// Set the analog input settings
//================================================================================
void CSyncBusSamplingDlg::OnBnClickedButtonAiSetting()
{
	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
	short	input_method;		// Input method

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// [Master] Get the index of analog input range setting value from combobox
	//----------------------------------------
	index	= m_master_ai_range.GetCurSel();
	//----------------------------------------
	// [Master] Set the analog input range (all channels)
	//----------------------------------------
	ret	= AioSetAiRangeAll(g_master_id, g_ai_range[index]);
	//----------------------------------------
	// [Master] 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("Analog Input Set: AioSetAiRangeAll = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Get the index of input method setting value from combobox
	//----------------------------------------
	input_method	= m_master_ai_input_method.GetCurSel();
	//----------------------------------------
	// [Master] Set the analog input method
	//----------------------------------------
	ret	= AioSetAiInputMethod(g_master_id, input_method);
	//----------------------------------------
	// [Master] 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("Analog Input Set: AioSetAiInputMethod = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Display the message that analog input settings is completed
	//----------------------------------------
	text_string.Format(_T("Analog Input Set: Successful completion"));
	m_master_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Get the index of analog input range setting value from combobox
	//----------------------------------------
	index	= m_slave_ai_range.GetCurSel();
	//----------------------------------------
	// [Slave]  Set the analog input range (all channels)
	//----------------------------------------
	ret	= AioSetAiRangeAll(g_slave_id, g_ai_range[index]);
	//----------------------------------------
	// [Slave]  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("Analog Input Set: AioSetAiRangeAll = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Get the index of input method setting value from combobox
	//----------------------------------------
	input_method	= m_slave_ai_input_method.GetCurSel();
	//----------------------------------------
	// [Slave]  Set the analog input method
	//----------------------------------------
	ret	= AioSetAiInputMethod(g_slave_id, input_method);
	//----------------------------------------
	// [Slave]  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("Analog Input Set: AioSetAiInputMethod = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Display the message that analog input settings is completed
	//----------------------------------------
	text_string.Format(_T("Analog Input Set: Successful completion"));
	m_slave_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Sampling Set button is clicked
// Set the sampling settings
//================================================================================
void CSyncBusSamplingDlg::OnBnClickedButtonSamplingSetting()
{
	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
	short	ai_channels;		// Number of analog input channels
	float	sampling_clock;		// Internal clock conversion speed
	long	stop_times;			// Sampling stop times

	//----------------------------------------
	// [Slave]  Get the number of channels
	//----------------------------------------
	m_slave_ai_channels.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &ai_channels);
	//----------------------------------------
	// [Slave]  Set the number of analog input channels used for conversion
	//----------------------------------------
	ret	= AioSetAiChannels(g_slave_id, ai_channels);
	//----------------------------------------
	// [Slave]  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 Set: AioSetAiChannels = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Set the analog input conversion data transfer mode to device buffer mode
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiTransferMode(g_slave_id, 0);
	//----------------------------------------
	// [Slave]  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 Set: AioSetAiTransferMode = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Set the data storage memory type to FIFO
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiMemoryType(g_slave_id, 0);
	//----------------------------------------
	// [Slave]  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 Set: AioSetAiMemoryType = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Set the sampling start condition to Event controller output
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiStartTrigger(g_slave_id, 10);
	//----------------------------------------
	// [Slave]  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 Set: AioSetAiStartTrigger = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Set the clock type to Event controller output
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiClockType(g_slave_id, 10);
	//----------------------------------------
	// [Slave]  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 Set: AioSetAiClockType = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Set the sampling stop condition to Event controller output
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiStopTrigger(g_slave_id, 10);
	//----------------------------------------
	// [Slave]  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 Set: AioSetAiStopTrigger = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Set the event factor to 'Event that AD conversion start', 'Event that device operation end', '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_slave_id, m_hWnd, (AIE_START | AIE_END | AIE_OFERR | AIE_SCERR | AIE_ADERR));
	//----------------------------------------
	// [Slave]  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 Set: AioSetAiEvent = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Display the message that sampling settings is completed
	//----------------------------------------
	text_string.Format(_T("Sampling Set: Successful completion"));
	m_slave_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Master] Get the number of channels
	//----------------------------------------
	m_master_ai_channels.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &ai_channels);
	//----------------------------------------
	// [Master] Set the number of analog input channels used for conversion
	//----------------------------------------
	ret	= AioSetAiChannels(g_master_id, ai_channels);
	//----------------------------------------
	// [Master] 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 Set: AioSetAiChannels = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Set the analog input conversion data transfer mode to device buffer mode
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiTransferMode(g_master_id, 0);
	//----------------------------------------
	// [Master] 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 Set: AioSetAiTransferMode = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Set the data storage memory type to FIFO
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiMemoryType(g_master_id, 0);
	//----------------------------------------
	// [Master] 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 Set: AioSetAiMemoryType = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Set the sampling start condition to Software
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiStartTrigger(g_master_id, 0);
	//----------------------------------------
	// [Master] 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 Set: AioSetAiStartTrigger = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Set the clock type to Internal Clock
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiClockType(g_master_id, 0);
	//----------------------------------------
	// [Master] 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 Set: AioSetAiClockType = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Get the internal clock conversion speed from textbox
	//----------------------------------------
	m_master_ai_sampling_clock.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%f"), &sampling_clock);
	//----------------------------------------
	// [Master] Set the internal clock conversion speed
	//----------------------------------------
	ret	= AioSetAiSamplingClock(g_master_id, sampling_clock);
	//----------------------------------------
	// 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 Set: AioSetAiSamplingClock = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Set the sampling stop condition to Stop conversion by the specified times
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAiStopTrigger(g_master_id, 0);
	//----------------------------------------
	// [Master] 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 Set: AioSetAiStopTrigger = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Get the sampling stop times from textbox
	//----------------------------------------
	m_master_ai_stop_times.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%ld"), &stop_times);
	//----------------------------------------
	// [Master] Set the sampling stop times
	//----------------------------------------
	ret	= AioSetAiStopTimes(g_master_id, stop_times);
	//----------------------------------------
	// 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 Set: AioSetAiStopTimes = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Set the event factor to 'Event that device operation end', '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_master_id, m_hWnd, (AIE_END | AIE_OFERR | AIE_SCERR | AIE_ADERR));
	//----------------------------------------
	// [Master] 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 Set: AioSetAiEvent = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Display the message that sampling settings is completed
	//----------------------------------------
	text_string.Format(_T("Sampling Set: Successful completion"));
	m_master_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Sync Operation Set button is clicked
// Set the synchronous operation settings
//================================================================================
void CSyncBusSamplingDlg::OnBnClickedButtonSyncSetting()
{
	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

	//----------------------------------------
	// [Master] Event Controller Setting: Connect Analog input internal clock signal to Synchronization bus master signal 1
	//----------------------------------------
	ret	= AioSetEcuSignal(g_master_id, AIOECU_DEST_MASTER1, AIOECU_SRC_AI_CLK);
	//----------------------------------------
	// [Master] 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("Sync Operation Set: AioSetEcuSignal = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Event Controller Setting: Connect Ai software start signal to Synchronization bus master signal 2
	//----------------------------------------
	ret	= AioSetEcuSignal(g_master_id, AIOECU_DEST_MASTER2, AIOECU_SRC_START);
	//----------------------------------------
	// [Master] 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("Sync Operation Set: AioSetEcuSignal = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Event Controller Setting: Connect Analog input signal that data of the specified sampling times have been converted (No delay) to Synchronization bus master signal 3
	//----------------------------------------
	ret	= AioSetEcuSignal(g_master_id, AIOECU_DEST_MASTER3, AIOECU_SRC_AI_STOP);
	//----------------------------------------
	// [Master] 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("Sync Operation Set: AioSetEcuSignal = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Display the message that synchronous operation settings is completed
	//----------------------------------------
	text_string.Format(_T("Sync Operation Set: Successful completion"));
	m_master_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Event Controller Setting: Connect Synchronization bus slave signal 1 to Analog input sampling clock
	//----------------------------------------
	ret	= AioSetEcuSignal(g_slave_id, AIOECU_DEST_AI_CLK, AIOECU_SRC_SLAVE1);
	//----------------------------------------
	// [Slave]  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("Sync Operation Set: AioSetEcuSignal = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Event Controller Setting: Connect Synchronization bus slave signal 2 to Analog input converting start signal
	//----------------------------------------
	ret	= AioSetEcuSignal(g_slave_id, AIOECU_DEST_AI_START, AIOECU_SRC_SLAVE2);
	//----------------------------------------
	// [Slave]  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("Sync Operation Set: AioSetEcuSignal = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Event Controller Setting: Connect Synchronization bus slave signal 3 to Analog input converting stop signal
	//----------------------------------------
	ret	= AioSetEcuSignal(g_slave_id, AIOECU_DEST_AI_STOP, AIOECU_SRC_SLAVE3);
	//----------------------------------------
	// [Slave]  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("Sync Operation Set: AioSetEcuSignal = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Display the message that synchronous operation settings is completed
	//----------------------------------------
	text_string.Format(_T("Sync Operation Set: Successful completion"));
	m_slave_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 CSyncBusSamplingDlg::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

	//----------------------------------------
	// [Slave]  Clear memory
	//----------------------------------------
	ret	= AioResetAiMemory(g_slave_id);
	//----------------------------------------
	// [Slave]  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: AioResetAiMemory = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Clear status
	//----------------------------------------
	ret	= AioResetAiStatus(g_slave_id);
	//----------------------------------------
	// [Slave]  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_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Initialize the total sampling count
	//----------------------------------------
	g_slave_sampling_count	= 0;
	//----------------------------------------
	// [Slave]  Start sampling
	//----------------------------------------
	ret	= AioStartAi(g_slave_id);
	//----------------------------------------
	// [Slave]  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_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Get status
	//----------------------------------------
	ret	= AioGetAiStatus(g_slave_id, &ai_status);
	//----------------------------------------
	// [Slave]  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_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  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_slave_ai_status.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Update the sampling count
	//----------------------------------------
	text_string.Format(_T("0"));
	m_slave_ai_sampling_count.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Clear the listbox for sampling data display
	//----------------------------------------
	m_slave_ai_sampling_data.ResetContent();
	//----------------------------------------
	// [Slave]  Display the message that sampling start is completed
	//----------------------------------------
	text_string.Format(_T("Sampling Start: Successful completion"));
	m_slave_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Master] Clear memory
	//----------------------------------------
	ret	= AioResetAiMemory(g_master_id);
	//----------------------------------------
	// [Master] 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: AioResetAiMemory = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Clear status
	//----------------------------------------
	ret	= AioResetAiStatus(g_master_id);
	//----------------------------------------
	// [Master] 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_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Initialize the total sampling count
	//----------------------------------------
	g_master_sampling_count	= 0;
	//----------------------------------------
	// [Master] Start sampling
	//----------------------------------------
	ret	= AioStartAi(g_master_id);
	//----------------------------------------
	// [Master] 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_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Get status
	//----------------------------------------
	ret	= AioGetAiStatus(g_master_id, &ai_status);
	//----------------------------------------
	// [Master] 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_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] 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_master_ai_status.SetWindowText(text_string);
	//----------------------------------------
	// [Master] Update the sampling count
	//----------------------------------------
	text_string.Format(_T("0"));
	m_master_ai_sampling_count.SetWindowText(text_string);
	//----------------------------------------
	// [Master] Clear the listbox for sampling data display
	//----------------------------------------
	m_master_ai_sampling_data.ResetContent();
	//----------------------------------------
	// [Master] Display the message that sampling start is completed
	//----------------------------------------
	text_string.Format(_T("Sampling Start: Successful completion"));
	m_master_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Get Status button is clicked
// Get the count and status of sampling and display it on the dialog
//================================================================================
void CSyncBusSamplingDlg::OnBnClickedButtonGetStatus()
{
	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_sampling_count;		// Sampling count
	long	ai_status;				// Sampling status
	long	ret;					// For return code

	//----------------------------------------
	// [Slave]  Get the sampling count
	//----------------------------------------
	ret	= AioGetAiSamplingCount(g_slave_id, &ai_sampling_count);
	//----------------------------------------
	// [Slave]  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("Get Status: AioGetAiSamplingCount = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Update the sampling count
	//----------------------------------------
	text_string.Format(_T("%d"), ai_sampling_count);
	m_slave_ai_sampling_count.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Get status
	//----------------------------------------
	ret	= AioGetAiStatus(g_slave_id, &ai_status);
	//----------------------------------------
	// [Slave]  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("Get Status: AioGetAiStatus = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  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_slave_ai_status.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Display the message that get status is completed
	//----------------------------------------
	text_string.Format(_T("Get Status: Successful completion"));
	m_slave_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Master] Get the sampling count
	//----------------------------------------
	ret	= AioGetAiSamplingCount(g_master_id, &ai_sampling_count);
	//----------------------------------------
	// [Master] 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("Get Status: AioGetAiSamplingCount = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Update the sampling count
	//----------------------------------------
	text_string.Format(_T("%d"), ai_sampling_count);
	m_master_ai_sampling_count.SetWindowText(text_string);
	//----------------------------------------
	// [Master] Get status
	//----------------------------------------
	ret	= AioGetAiStatus(g_master_id, &ai_status);
	//----------------------------------------
	// [Master] 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("Get Status: AioGetAiStatus = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] 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_master_ai_status.SetWindowText(text_string);
	//----------------------------------------
	// [Master] Display the message that get status is completed
	//----------------------------------------
	text_string.Format(_T("Get Status: Successful completion"));
	m_master_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Sampling Stop button is clicked
// Stop sampling
//================================================================================
void CSyncBusSamplingDlg::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

	//----------------------------------------
	// [Master] Stop sampling
	//----------------------------------------
	ret	= AioStopAi(g_master_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("Stop sampling: AioStopAi = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Master] Display the message that sampling stop is completed
	//----------------------------------------
	text_string.Format(_T("Stop sampling: Successful completion"));
	m_master_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Slave]  Stop sampling
	//----------------------------------------
	ret	= AioStopAi(g_slave_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("Stop sampling: AioStopAi = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// [Slave]  Display the message that sampling stop is completed
	//----------------------------------------
	text_string.Format(_T("Stop sampling: Successful completion"));
	m_slave_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Master Device Output Acquired Data to File button is clicked
// Output the globally held sampling data to a CSV file
//================================================================================
void CSyncBusSamplingDlg::OnBnClickedButtonMasterOutputSamplingData()
{
	TCHAR	text_temp[256];								// For temporary storage of text
	char	error_string[256];							// Error code string
	TCHAR	file_name[] = _T("MasterSamplingData.csv");	// Sampling data output file name
	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
	short	ai_channels;								// Number of channels

	//----------------------------------------
	// Open file
	//----------------------------------------
	file_ret	= _wfopen_s(&fp, file_name, _T("w"));
	if(file_ret != 0){
		text_string.Format(_T("Output Acquired Data to File: fopen = file open error."));
		m_master_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get the number of channels
	//----------------------------------------
	ret	= AioGetAiChannels(g_master_id, &ai_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("Output Acquired Data to File: AioGetAiChannels = %d : %s"), ret, text_temp);
		m_master_return_code.SetWindowText(text_string);
		//----------------------------------------
		// Close file
		//----------------------------------------
		fclose(fp);
		return;
	}
	//----------------------------------------
	// Create sampling data for file output
	//----------------------------------------
	//--------------------
	// If the total sampling count is 1 or more and pointer to sampling data storage area is not NULL, output the file
	//--------------------
	if((g_master_sampling_count > 0) && (g_master_sampling_data != NULL)){
		//--------------------
		// Repeat the process up to the total sampling count
		//--------------------
		for(i = 0; i < g_master_sampling_count; i++){
			text_string.Empty();
			//--------------------
			// Generate display string
			//--------------------
			text_string_temp.Format(_T("%5d,"), (i + 1));
			text_string	+= text_string_temp;
			//--------------------
			// Repeat the process for the specified number of channels
			//--------------------
			for(j = 0; j < ai_channels; j++){
				text_string_temp.Format(_T(" % 10.5f"), g_master_sampling_data[i * ai_channels + j]);
				text_string	+= text_string_temp;
				//--------------------
				// Add newline character for the last channel
				//--------------------
				if(j == (ai_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 the message that file output is completed
	//----------------------------------------
	text_string.Format(_T("Output Acquired Data to File: Save data to \"%s\"."), file_name);
	m_master_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Slave Device Output Acquired Data to File button is clicked
// Output the globally held sampling data to a CSV file
//================================================================================
void CSyncBusSamplingDlg::OnBnClickedButtonSlaveOutputSamplingData()
{
	TCHAR	text_temp[256];								// For temporary storage of text
	char	error_string[256];							// Error code string
	TCHAR	file_name[] = _T("SlaveSamplingData.csv");	// Sampling data output file name
	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
	short	ai_channels;								// Number of channels

	//----------------------------------------
	// Open file
	//----------------------------------------
	file_ret	= _wfopen_s(&fp, file_name, _T("w"));
	if(file_ret != 0){
		text_string.Format(_T("Output Acquired Data to File: fopen = file open error."));
		m_slave_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get the number of channels
	//----------------------------------------
	ret	= AioGetAiChannels(g_slave_id, &ai_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("Output Acquired Data to File: AioGetAiChannels = %d : %s"), ret, text_temp);
		m_slave_return_code.SetWindowText(text_string);
		//----------------------------------------
		// Close file
		//----------------------------------------
		fclose(fp);
		return;
	}
	//----------------------------------------
	// Create sampling data for file output
	//----------------------------------------
	//--------------------
	// If the total sampling count is 1 or more and pointer to sampling data storage area is not NULL, output the file
	//--------------------
	if((g_slave_sampling_count > 0) && (g_slave_sampling_data != NULL)){
		//--------------------
		// Repeat the process up to the total sampling count
		//--------------------
		for(i = 0; i < g_slave_sampling_count; i++){
			text_string.Empty();
			//--------------------
			// Generate display string
			//--------------------
			text_string_temp.Format(_T("%5d,"), (i + 1));
			text_string	+= text_string_temp;
			//--------------------
			// Repeat the process for the specified number of channels
			//--------------------
			for(j = 0; j < ai_channels; j++){
				text_string_temp.Format(_T(" % 10.5f"), g_slave_sampling_data[i * ai_channels + j]);
				text_string	+= text_string_temp;
				//--------------------
				// Add newline character for the last channel
				//--------------------
				if(j == (ai_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 the message that file output is completed
	//----------------------------------------
	text_string.Format(_T("Output Acquired Data to File: Save data to \"%s\"."), file_name);
	m_slave_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioExit button is clicked
// Perform the exit process of device
//================================================================================
void CSyncBusSamplingDlg::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

	//----------------------------------------
	// [Master] Release the array for storing AD conversion data
	//----------------------------------------
	if(g_master_sampling_data != NULL){
		free(g_master_sampling_data);
		g_master_sampling_data = NULL;
	}
	//----------------------------------------
	// [Master] Exit process
	//----------------------------------------
	ret	= AioExit(g_master_id);
	//----------------------------------------
	// [Master] Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// [Master] Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioExit = %d : %s"), ret, text_temp);
	m_master_return_code.SetWindowText(text_string);
	//----------------------------------------
	// [Slave] Release the array for storing AD conversion data
	//----------------------------------------
	if(g_slave_sampling_data != NULL){
		free(g_slave_sampling_data);
		g_slave_sampling_data = NULL;
	}
	//----------------------------------------
	// [Slave]  Exit process
	//----------------------------------------
	ret	= AioExit(g_slave_id);
	//----------------------------------------
	// [Slave]  Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// [Slave]  Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioExit = %d : %s"), ret, text_temp);
	m_slave_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 CSyncBusSamplingDlg::OnBnClickedClose()
{
	//----------------------------------------
	// Release the array for storing AD conversion data
	//----------------------------------------
	if(g_master_sampling_data != NULL){
		free(g_master_sampling_data);
		g_master_sampling_data = NULL;
	}
	if(g_slave_sampling_data != NULL){
		free(g_slave_sampling_data);
		g_slave_sampling_data = NULL;
	}
	//----------------------------------------
	// Exit process
	//----------------------------------------
	AioExit(g_slave_id);
	AioExit(g_master_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}

//================================================================================
// Operation when event occurred
// Process for each event occurred
//================================================================================
LRESULT CSyncBusSamplingDlg::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
	CDC				*device_context;		// Device context of ListBox for sampling data display
	CFont			*old_font;				// Font settings of ListBox for sampling data display
	unsigned long	string_length;			// String width of ListBox for sampling data display
	unsigned long	max_string_length;		// Maximum string width of ListBox for displaying sampling data
	long			ai_sampling_count;		// Sampling count
	long			ai_status;				// Sampling status
	long			ret;					// For return code
	long			i, j;					// Loop count
	long			loop_max_count;			// Maximum loop count
	long			top_address;			// Top address of buffer for sampling data display
	short			ai_channels;			// Number of channels
	short			id;						// ID for device access

	//----------------------------------------
	// Get ID for device access
	//----------------------------------------
	id	= wParam & 0x0000FFFF;
	//----------------------------------------
	// Event that AD conversion start
	// Display the status
	//----------------------------------------
	if(message == AIOM_AIE_START){
		//----------------------------------------
		// Slave ID
		//----------------------------------------
		if(id == g_slave_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_slave_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_slave_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_slave_ai_status.SetWindowText(text_string);
			//----------------------------------------
			// Display the message that event processing is completed
			//----------------------------------------
			text_string.Format(_T("Event that AD conversion start Process: Successful completion"));
			m_slave_return_code.SetWindowText(text_string);
		}
	}
	//----------------------------------------
	// Event that device operation end
	// Acquire and display data
	//----------------------------------------
	if(message == AIOM_AIE_END){
		//----------------------------------------
		// Master ID
		//----------------------------------------
		if(id == g_master_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_master_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_master_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_master_ai_status.SetWindowText(text_string);
			//----------------------------------------
			// Get the current sampling count
			//----------------------------------------
			ret	= AioGetAiSamplingCount(g_master_id, &ai_sampling_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: AioGetAiSamplingCount = %d : %s"), ret, text_temp);
				m_master_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Display sampling count
			//----------------------------------------
			text_string.Format(_T("%d"), ai_sampling_count);
			m_master_ai_sampling_count.SetWindowText(text_string);
			//----------------------------------------
			// Get the number of channels
			//----------------------------------------
			ret	= AioGetAiChannels(g_master_id, &ai_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("Device Operation End Event Process: AioGetAiChannels = %d : %s"), ret, text_temp);
				m_master_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Get sampling data process
			//----------------------------------------
			//--------------------
			// Allocate an array for storing data
			//--------------------
			if(g_master_sampling_data != NULL){
				free(g_master_sampling_data);
				g_master_sampling_data = NULL;
			}
			g_master_sampling_data	= (float *)malloc(sizeof(float) * ai_sampling_count * ai_channels);
			if(g_master_sampling_data == NULL){
				text_string.Format(_T("Device Operation End Event Process: Failed to allocate an array for storing data"));
				m_master_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Get sampling data
			//----------------------------------------
			ret	= AioGetAiSamplingDataEx(g_master_id, &ai_sampling_count, g_master_sampling_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("Device Operation End Event Process: AioGetAiSamplingDataEx = %d : %s"), ret, text_temp);
				m_master_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Update the total sampling count
			//----------------------------------------
			g_master_sampling_count	= ai_sampling_count;
			//----------------------------------------
			// Set the loop count for the sampling data display loop
			//----------------------------------------
			//--------------------
			// When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
			//--------------------
			if(ai_sampling_count > DISPLAY_SAMPLING_COUNT){
				//--------------------
				// Limit the maximum number of loops to the data display maximum sampling count
				//--------------------
				loop_max_count	= DISPLAY_SAMPLING_COUNT;
				//--------------------
				// Set the top address of the buffer to be referenced to the address 
				// before the maximum sampling count for data display from the end of the acquired data
				//--------------------
				top_address		= ai_sampling_count - DISPLAY_SAMPLING_COUNT;
			//--------------------
			// When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
			//--------------------
			}else{
				//--------------------
				// Set the number of total sampling data to the maximum loop number
				//--------------------
				loop_max_count	= ai_sampling_count;
				//--------------------
				// Set the top address of the referenced buffer to 0
				//--------------------
				top_address		= 0;
			}
			//----------------------------------------
			// Display sampling data
			// (Display from the end of acquired data to the maximum number of data display sampling count)
			//----------------------------------------
			//--------------------
			// When the sampling count of the data acquired this time is 1 or more
			//--------------------
			if(ai_sampling_count > 0){
				//--------------------
				// Get device context for listbox
				//--------------------
				device_context		= m_master_ai_sampling_data.GetDC();
				old_font			= device_context->SelectObject(m_master_ai_sampling_data.GetFont());
				max_string_length	= 0;
				string_length		= 0;
				//--------------------
				// Clear the listbox for sampling data display
				//--------------------
				m_master_ai_sampling_data.ResetContent();
				//--------------------
				// Repeat the process up to the maximum sampling count of the display data
				//--------------------
				for(i = top_address; i < (loop_max_count + top_address); i++){
					//--------------------
					// Clear display string storage variable
					//--------------------
					text_string.Empty();
					//--------------------
					// Generate display string
					//--------------------
					text_string_temp.Format(_T("%5d:  "), (i + 1));
					text_string	+= text_string_temp;
					//--------------------
					// Repeat the process for the specified number of channels
					//--------------------
					for(j = 0; j < ai_channels; j++){
						text_string_temp.Format(_T(" % 10.5f"), g_master_sampling_data[i * ai_channels + j]);
						text_string	+= text_string_temp;
					}
					//----------------------------------------
					// Adjust horizontal width of listbox
					//----------------------------------------
					string_length	= device_context->GetTextExtent(text_string).cx;
					//--------------------
					// If the horizontal width of the string to be added is greater than the current maximum
					//--------------------
					if(string_length > max_string_length){
						//--------------------
						// Set horizontal width
						//--------------------
						m_master_ai_sampling_data.SetHorizontalExtent(string_length);
						//--------------------
						// Update maximum horizontal width
						//--------------------
						max_string_length	= string_length;
					}
					//----------------------------------------
					// Display sampling start data
					//----------------------------------------
					m_master_ai_sampling_data.AddString(text_string);
				}
				//--------------------
				// Release device context of listbox
				//--------------------
				device_context->SelectObject(old_font);
				ReleaseDC(device_context);
			}
			//----------------------------------------
			// Display the message that event processing is completed
			//----------------------------------------
			text_string.Format(_T("Device Operation End Event Process: Successful completion"));
			m_master_return_code.SetWindowText(text_string);
		//----------------------------------------
		// Slave ID
		//----------------------------------------
		}else if(id == g_slave_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_slave_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_slave_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_slave_ai_status.SetWindowText(text_string);
			//----------------------------------------
			// Get the current sampling count
			//----------------------------------------
			ret	= AioGetAiSamplingCount(g_slave_id, &ai_sampling_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: AioGetAiSamplingCount = %d : %s"), ret, text_temp);
				m_slave_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Display sampling count
			//----------------------------------------
			text_string.Format(_T("%d"), ai_sampling_count);
			m_slave_ai_sampling_count.SetWindowText(text_string);
			//----------------------------------------
			// Get the number of channels
			//----------------------------------------
			ret	= AioGetAiChannels(g_slave_id, &ai_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("Device Operation End Event Process: AioGetAiChannels = %d : %s"), ret, text_temp);
				m_slave_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Get sampling data process
			//----------------------------------------
			//--------------------
			// Allocate an array for storing data
			//--------------------
			if(g_slave_sampling_data != NULL){
				free(g_slave_sampling_data);
				g_slave_sampling_data = NULL;
			}
			g_slave_sampling_data	= (float *)malloc(sizeof(float) * ai_sampling_count * ai_channels);
			if(g_slave_sampling_data == NULL){
				text_string.Format(_T("Device Operation End Event Process: Failed to allocate an array for storing data"));
				m_slave_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Get sampling data
			//----------------------------------------
			ret	= AioGetAiSamplingDataEx(g_slave_id, &ai_sampling_count, g_slave_sampling_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("Device Operation End Event Process: AioGetAiSamplingDataEx = %d : %s"), ret, text_temp);
				m_slave_return_code.SetWindowText(text_string);
				return TRUE;
			}
			//----------------------------------------
			// Update the total sampling count
			//----------------------------------------
			g_slave_sampling_count	= ai_sampling_count;
			//----------------------------------------
			// Set the loop count for the sampling data display loop
			//----------------------------------------
			//--------------------
			// When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
			//--------------------
			if(ai_sampling_count > DISPLAY_SAMPLING_COUNT){
				//--------------------
				// Limit the maximum number of loops to the data display maximum sampling count
				//--------------------
				loop_max_count	= DISPLAY_SAMPLING_COUNT;
				//--------------------
				// Set the top address of the buffer to be referenced to the address 
				// before the maximum sampling count for data display from the end of the acquired data
				//--------------------
				top_address		= ai_sampling_count - DISPLAY_SAMPLING_COUNT;
			//--------------------
			// When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
			//--------------------
			}else{
				//--------------------
				// Set the number of total sampling data to the maximum loop number
				//--------------------
				loop_max_count	= ai_sampling_count;
				//--------------------
				// Set the top address of the referenced buffer to 0
				//--------------------
				top_address		= 0;
			}
			//----------------------------------------
			// Display sampling data
			// (Display from the end of acquired data to the maximum number of data display sampling count)
			//----------------------------------------
			//--------------------
			// When the sampling count of the data acquired this time is 1 or more
			//--------------------
			if(ai_sampling_count > 0){
				//--------------------
				// Get device context for listbox
				//--------------------
				device_context		= m_slave_ai_sampling_data.GetDC();
				old_font			= device_context->SelectObject(m_slave_ai_sampling_data.GetFont());
				max_string_length	= 0;
				string_length		= 0;
				//--------------------
				// Clear the listbox for sampling data display
				//--------------------
				m_slave_ai_sampling_data.ResetContent();
				//--------------------
				// Repeat the process up to the maximum sampling count of the display data
				//--------------------
				for(i = top_address; i < (loop_max_count + top_address); i++){
					//--------------------
					// Clear display string storage variable
					//--------------------
					text_string.Empty();
					//--------------------
					// Generate display string
					//--------------------
					text_string_temp.Format(_T("%5d:  "), (i + 1));
					text_string	+= text_string_temp;
					//--------------------
					// Repeat the process for the specified number of channels
					//--------------------
					for(j = 0; j < ai_channels; j++){
						text_string_temp.Format(_T(" % 10.5f"), g_slave_sampling_data[i * ai_channels + j]);
						text_string	+= text_string_temp;
					}
					//----------------------------------------
					// Adjust horizontal width of listbox
					//----------------------------------------
					string_length	= device_context->GetTextExtent(text_string).cx;
					//--------------------
					// If the horizontal width of the string to be added is greater than the current maximum
					//--------------------
					if(string_length > max_string_length){
						//--------------------
						// Set horizontal width
						//--------------------
						m_slave_ai_sampling_data.SetHorizontalExtent(string_length);
						//--------------------
						// Update maximum horizontal width
						//--------------------
						max_string_length	= string_length;
					}
					//----------------------------------------
					// Display sampling start data
					//----------------------------------------
					m_slave_ai_sampling_data.AddString(text_string);
				}
				//--------------------
				// Release device context of listbox
				//--------------------
				device_context->SelectObject(old_font);
				ReleaseDC(device_context);
			}
			//----------------------------------------
			// Display the message that event processing is completed
			//----------------------------------------
			text_string.Format(_T("Device Operation End Event Process: Successful completion"));
			m_slave_return_code.SetWindowText(text_string);
		}
	}
	//----------------------------------------
	// Event that overflow
	// Display the status
	//----------------------------------------
	if(message == AIOM_AIE_OFERR){
		//----------------------------------------
		// Master ID
		//----------------------------------------
		if(id == g_master_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_master_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_master_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_master_ai_status.SetWindowText(text_string);
			//----------------------------------------
			// Display the message that event processing is completed
			//----------------------------------------
			text_string.Format(_T("Overflow Occurrence Process: Successful completion"));
			m_master_return_code.SetWindowText(text_string);
		//----------------------------------------
		// Slave ID
		//----------------------------------------
		}else if(id == g_slave_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_slave_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_slave_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_slave_ai_status.SetWindowText(text_string);
			//----------------------------------------
			// Display the message that event processing is completed
			//----------------------------------------
			text_string.Format(_T("Overflow Occurrence Process: Successful completion"));
			m_slave_return_code.SetWindowText(text_string);
		}
	}
	//----------------------------------------
	// Event that sampling clock error
	// Display the status
	//----------------------------------------
	if(message == AIOM_AIE_SCERR){
		//----------------------------------------
		// Master ID
		//----------------------------------------
		if(id == g_master_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_master_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_master_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_master_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_master_return_code.SetWindowText(text_string);
		//----------------------------------------
		// Slave ID
		//----------------------------------------
		}else if(id == g_slave_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_slave_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_slave_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_slave_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_slave_return_code.SetWindowText(text_string);
		}
	}
	//----------------------------------------
	// Event that AD conversion error
	// Display the status
	//----------------------------------------
	if(message == AIOM_AIE_ADERR){
		//----------------------------------------
		// Master ID
		//----------------------------------------
		if(id == g_master_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_master_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_master_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_master_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_master_return_code.SetWindowText(text_string);
		//----------------------------------------
		// Slave ID
		//----------------------------------------
		}else if(id == g_slave_id){
			//----------------------------------------
			// Get status
			//----------------------------------------
			ret	= AioGetAiStatus(g_slave_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_slave_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_slave_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_slave_return_code.SetWindowText(text_string);
		}
	}

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