
// SyncDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Sync.h"
#include "SyncDlg.h"
#include "afxdialogex.h"
//-------------------------------------
// Include API-DIG(WDM) header file
//-------------------------------------
#include "cdig.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSyncDlg dialog



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

void CSyncDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATUS2, m_Status2);
	DDX_Control(pDX, IDC_STATUS1, m_Status1);
	DDX_Control(pDX, IDC_RICHEDIT_DATA2, m_EditData2);
	DDX_Control(pDX, IDC_RICHEDIT_DATA1, m_EditData1);
	DDX_Control(pDX, IDC_EDIT_INFO2, m_Info2);
	DDX_Control(pDX, IDC_EDIT_INFO1, m_Info1);
	DDX_Control(pDX, IDC_EDIT_DEVICE2, m_DeviceName2);
	DDX_Control(pDX, IDC_EDIT_DEVICE1, m_DeviceName1);
	DDX_Control(pDX, IDC_COUNT2, m_Count2);
	DDX_Control(pDX, IDC_COUNT1, m_Count1);
	DDX_Control(pDX, IDC_COMBO_RANGE2, m_Range2);
	DDX_Control(pDX, IDC_COMBO_RANGE1, m_Range1);
}

BEGIN_MESSAGE_MAP(CSyncDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_INIT, &CSyncDlg::OnBnClickedButtonInit)
	ON_CBN_SELCHANGE(IDC_COMBO_RANGE1, &CSyncDlg::OnCbnSelchangeComboRange1)
	ON_CBN_SELCHANGE(IDC_COMBO_RANGE2, &CSyncDlg::OnCbnSelchangeComboRange2)
	ON_BN_CLICKED(IDC_BUTTON_START, &CSyncDlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_INFO, &CSyncDlg::OnBnClickedButtonInfo)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CSyncDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CSyncDlg::OnBnClickedButtonExit)
	ON_WM_DESTROY()
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CSyncDlg message handlers

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

	//---------------------------------
	// Initialize device name
	//---------------------------------
	m_DeviceName1.SetWindowText(_T("DIG000"));
	m_DeviceName2.SetWindowText(_T("DIG001"));

	//---------------------------------
	// Initialize input range
	//---------------------------------
	m_Range1.AddString(_T("+/-20V"));
	m_Range1.AddString(_T("+/-10V"));
	m_Range1.AddString(_T("+/-5V"));
	m_Range1.AddString(_T("+/-2V"));
	m_Range1.AddString(_T("+/-1V"));
	m_Range1.AddString(_T("+/-0.5V"));
	m_Range1.AddString(_T("+/-0.2V"));
	m_Range1.AddString(_T("+/-0.1V"));
	m_Range1.AddString(_T("+/-0.05V"));
	m_Range1.AddString(_T("+/-0.02V"));

	m_Range2.AddString(_T("+/-20V"));
	m_Range2.AddString(_T("+/-10V"));
	m_Range2.AddString(_T("+/-5V"));
	m_Range2.AddString(_T("+/-2V"));
	m_Range2.AddString(_T("+/-1V"));
	m_Range2.AddString(_T("+/-0.5V"));
	m_Range2.AddString(_T("+/-0.2V"));
	m_Range2.AddString(_T("+/-0.1V"));
	m_Range2.AddString(_T("+/-0.05V"));
	m_Range2.AddString(_T("+/-0.02V"));

	//---------------------------------
	// Initialize the number of acquisition samples
	//---------------------------------
	m_Samples1 = 0;
	m_Samples2 = 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 CSyncDlg::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 CSyncDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//===============================================
// Init handling
//===============================================
void CSyncDlg::OnBnClickedButtonInit()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	char	device_name[256];	// Device name
	TCHAR	szDevice_name[256];
	long	ret;				// Return value

	//-----------------------------------------
	// Device1
	//-----------------------------------------
	//---------------------------------
	// Init handling
	//---------------------------------
	m_DeviceName1.GetWindowText(szDevice_name, sizeof(szDevice_name));
	sprintf_s(device_name, "%S", szDevice_name);
	ret = DigInit(device_name, &m_Id1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigInit = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}

	//---------------------------------
	// Reset device
	//---------------------------------
	ret = DigResetDevice(m_Id1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigResetDevice = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}

	m_Info1.SetWindowText(_T("Init : Normality completion"));

	//-----------------------------------------
	// Device2
	//-----------------------------------------
	//---------------------------------
	// Init handling
	//---------------------------------
	m_DeviceName2.GetWindowText(szDevice_name, sizeof(szDevice_name));
	sprintf_s(device_name, "%S", szDevice_name);
	ret = DigInit(device_name, &m_Id2);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigInit = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Reset device
	//---------------------------------
	ret = DigResetDevice(m_Id2);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigResetDevice = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}

	m_Info2.SetWindowText(_T("Init : Normality completion"));
}

//===============================================
// Set input range for device1
//===============================================
void CSyncDlg::OnCbnSelchangeComboRange1()
{
	CString	cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		index;				// The selected index value of input range combobox
	float	range;				// Input range
	int		ret;				// Return value

	//---------------------------------
	// Get the input range from combobox
	//---------------------------------
	index = m_Range1.GetCurSel();
	switch (index){
	case 0:	range = DIG_RANGE_40VPP;	break;
	case 1:	range = DIG_RANGE_20VPP;	break;
	case 2:	range = DIG_RANGE_10VPP;	break;
	case 3:	range = DIG_RANGE_4VPP;		break;
	case 4:	range = DIG_RANGE_2VPP;		break;
	case 5:	range = DIG_RANGE_1VPP;		break;
	case 6:	range = DIG_RANGE_400MVPP;	break;
	case 7: range = DIG_RANGE_200MVPP;	break;
	case 8: range = DIG_RANGE_100MVPP;	break;
	case 9:	range = DIG_RANGE_40MVPP;	break;
	default: break;
	}

	//---------------------------------
	// Set input range of channel0
	//---------------------------------
	ret = DigSetRange(m_Id1, DIG_CHANNEL0, range);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRange = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}

	m_Info1.SetWindowText(_T("Set input range : Normality completion"));
}

//===============================================
// Set input range for device2
//===============================================
void CSyncDlg::OnCbnSelchangeComboRange2()
{
	CString	cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		index;				// The selected index value of input range combobox
	float	range;				// Input range
	int		ret;				// Return value

	//---------------------------------
	// Get the input range from combobox
	//---------------------------------
	index = m_Range2.GetCurSel();
	switch (index){
	case 0:	range = DIG_RANGE_40VPP;	break;
	case 1:	range = DIG_RANGE_20VPP;	break;
	case 2:	range = DIG_RANGE_10VPP;	break;
	case 3:	range = DIG_RANGE_4VPP;		break;
	case 4:	range = DIG_RANGE_2VPP;		break;
	case 5:	range = DIG_RANGE_1VPP;		break;
	case 6:	range = DIG_RANGE_400MVPP;	break;
	case 7: range = DIG_RANGE_200MVPP;	break;
	case 8: range = DIG_RANGE_100MVPP;	break;
	case 9:	range = DIG_RANGE_40MVPP;	break;
	default: break;
	}

	//---------------------------------
	// Set input range of channel0
	//---------------------------------
	ret = DigSetRange(m_Id2, DIG_CHANNEL0, range);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRange = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}

	m_Info2.SetWindowText(_T("Set input range : Normality completion"));
}

//===============================================
// Start acquisition
//===============================================
void CSyncDlg::OnBnClickedButtonStart()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value
	//---------------------------------
	// Initialize variable
	//---------------------------------
	m_Samples1 = 0;
	m_Samples2 = 0;

	//-----------------------------------------
	// Set deivce1
	//-----------------------------------------
	//---------------------------------
	// Set operation mode : Data logger mode
	//---------------------------------
	ret = DigSetDigitizerMode(m_Id1, DIG_MODE_LOGGER);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetDigitizerMode = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set Event Controller Unit : Synchronous bus signal line 1 - pre-trigger satisfaction
	//---------------------------------
	ret = DigSetEcuSignal(m_Id1, DIG_ECU_DEST_SYNC1, DIG_ECU_SRC_PRETRG);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetEcuSignal = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set sampling clock source : Crystal oscillator of board
	//---------------------------------
	ret = DigSetClockSource(m_Id1, DIG_ECU_SRC_CLOCK);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetClockSource = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set sampling clock : 1000usec  (set value * 10 + 10 (nsec))
	//---------------------------------
	ret = DigSetSampleClock(m_Id1, 99999);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetSampleClock = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the number of acquiring records : 1
	//---------------------------------
	ret = DigSetRecordNumber(m_Id1, 1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRecordNumber = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the number of pre-trigger samples : 1000
	//---------------------------------
	ret = DigSetPreSampleTimes(m_Id1, MAX_SAMPLES);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPreSampleTimes = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the number of post-trigger samples : 0
	//---------------------------------
	ret = DigSetPostSampleTimes(m_Id1, 0);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPostSampleTimes = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set channel : Enable channel0 only
	//---------------------------------
	// Enable channel0
	ret = DigSetChannel(m_Id1, DIG_CHANNEL0, DIG_ENABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetChannel = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	// Disable channel1
	ret = DigSetChannel(m_Id1, DIG_CHANNEL1, DIG_DISABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetChannel = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Sets the type of pre-trigger : Auto
	//---------------------------------
	ret = DigSetPreTriggerType(m_Id1, DIG_TRIGGERTYPE_AUTO);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPreTriggerType = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}

	//-----------------------------------------
	// Set deivce2
	//-----------------------------------------
	//---------------------------------
	// Set operation mode : Data logger mode
	//---------------------------------
	ret = DigSetDigitizerMode(m_Id2, DIG_MODE_LOGGER);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetDigitizerMode = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set sampling clock source : Crystal oscillator of board 
	//---------------------------------
	ret = DigSetClockSource(m_Id2, DIG_ECU_SRC_CLOCK);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetClockSource = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set sampling clock : 1000usec  (set value * 10 + 10 (nsec))
	//---------------------------------
	ret = DigSetSampleClock(m_Id2, 99999);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetSampleClock = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the number of acquiring records : 1
	//---------------------------------
	ret = DigSetRecordNumber(m_Id2, 1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRecordNumber = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the number of pre-trigger samples : 1000
	//---------------------------------
	ret = DigSetPreSampleTimes(m_Id2, MAX_SAMPLES);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPreSampleTimes = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the number of post-trigger samples : 0
	//---------------------------------
	ret = DigSetPostSampleTimes(m_Id2, 0);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPostSampleTimes = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set channel : Enable channel0 only
	//---------------------------------
	// Enable channel0
	ret = DigSetChannel(m_Id2, DIG_CHANNEL0, DIG_ENABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetChannel = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	// Disable channel1
	ret = DigSetChannel(m_Id2, DIG_CHANNEL1, DIG_DISABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetChannel = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the type of pre-trigger : Digital edge
	//---------------------------------
	ret = DigSetPreTriggerType(m_Id2, DIG_TRIGGERTYPE_DIEDGE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPreTriggerType = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set pre-trigger : Signal source = Synchronous bus 1, Direction = Rising edge, Count = 1
	//---------------------------------
	ret = DigSetPreTriggerDiEdge(m_Id2, DIG_ECU_SRC_SYNC1, 0, 1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPreTriggerDiEdge = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}


	//---------------------------------
	// Operation start : Start device2 first
	//---------------------------------
	ret = DigStart(m_Id2);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigStart = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	m_Info2.SetWindowText(_T("Start acquisition : Normality completion"));

	ret = DigStart(m_Id1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigStart = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	m_Info1.SetWindowText(_T("Start acquisition : Normality completion"));

	//---------------------------------
	// Polling until the operation status is changed to be OFF
	//---------------------------------
	long status1, status2;

	do{
		//---------------------------------
		// In order to enable the dialog operation
		//---------------------------------
		MSG msg;
		while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)){
			if (!GetMessage(&msg, NULL, NULL, NULL)){
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		//------------------------------------------------------------
		// Device1
		//------------------------------------------------------------
		//---------------------------------
		// Get the number of samples stored in driver memory
		//---------------------------------
		ret = DigGetDriverMemorySamples(m_Id1, &m_Samples1);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigGetDriverMemorySamples = %d : %s"), ret, szError_string);
			m_Info1.SetWindowText(cstr);
			return;
		}
		cstr.Format(_T("%d"), m_Samples1);
		m_Count1.SetWindowText(cstr);
		//---------------------------------
		// Get the operation status
		//---------------------------------
		ret = DigGetStatus(m_Id1, &status1);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigGetStatus = %d : %s"), ret, szError_string);
			m_Info1.SetWindowText(cstr);
			return;
		}
		cstr.Format(_T("%xH"), status1);
		m_Status1.SetWindowText(cstr);

		//------------------------------------------------------------
		// Device2
		//------------------------------------------------------------
		//---------------------------------
		// Get the number of samples stored in driver memory
		//---------------------------------
		ret = DigGetDriverMemorySamples(m_Id2, &m_Samples2);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigGetDriverMemorySamples = %d : %s"), ret, szError_string);
			m_Info1.SetWindowText(cstr);
			return;
		}
		cstr.Format(_T("%d"), m_Samples2);
		m_Count2.SetWindowText(cstr);
		//---------------------------------
		// Get the operation status
		//---------------------------------
		ret = DigGetStatus(m_Id2, &status2);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigGetStatus = %d : %s"), ret, szError_string);
			m_Info2.SetWindowText(cstr);
			return;
		}
		cstr.Format(_T("%xH"), status2);
		m_Status2.SetWindowText(cstr);
	} while (((status1 | status2) & DIG_STATUS_BUSY) == DIG_STATUS_BUSY);

	long trigger_point;			// Position of the first data after the establishment of the post-trigger
	//------------------------------------------------------------
	// Device1
	//------------------------------------------------------------
	//---------------------------------
	// After operation stop, get the number of samples which are being stored in driver memory
	//---------------------------------
	ret = DigGetDriverMemorySamples(m_Id1, &m_Samples1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetDriverMemorySamples = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Retrieve the acquisition data
	//---------------------------------
	if (m_Samples1 > 0){
		// Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
		if (m_Samples1 > MAX_SAMPLES){
			m_Samples1 = MAX_SAMPLES;
		}
		ret = DigReadSamples(m_Id1, &m_Samples1, &m_Data1[0], &trigger_point);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigReadSamples = %d : %s"), ret, szError_string);
			m_Info1.SetWindowText(cstr);
			return;
		}
		//---------------------------------
		// Display the retrieved data
		//---------------------------------
		CString temp;
		for (int i = 0; i < m_Samples1; i++){
			cstr.Format(_T("%d\t%6.3f\r\n"), i + 1, m_Data1[i]);
			temp += cstr;
		}
		m_EditData1.SetWindowText(temp);
	}

	//------------------------------------------------------------
	// Device2
	//------------------------------------------------------------
	//---------------------------------
	// After operation stop, get the number of samples which are being stored in driver memory
	//---------------------------------
	ret = DigGetDriverMemorySamples(m_Id2, &m_Samples2);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetDriverMemorySamples = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Retrieve the acquisition data
	//---------------------------------
	if (m_Samples2 > 0){
		// Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
		if (m_Samples2 > MAX_SAMPLES){
			m_Samples2 = MAX_SAMPLES;
		}
		ret = DigReadSamples(m_Id2, &m_Samples2, &m_Data2[0], &trigger_point);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigReadSamples = %d : %s"), ret, szError_string);
			m_Info2.SetWindowText(cstr);
			return;
		}
		//---------------------------------
		// Display the retrieved data
		//---------------------------------
		CString temp;
		for (int i = 0; i < m_Samples2; i++){
			cstr.Format(_T("%d\t%6.3f\r\n"), i + 1, m_Data2[i]);
			temp += cstr;
		}
		m_EditData2.SetWindowText(temp);
	}
}

//===============================================
// Get status
//===============================================
void CSyncDlg::OnBnClickedButtonInfo()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value
	long	status1, status2;	// Status

	//------------------------------------------------------------
	// Device1
	//------------------------------------------------------------
	//---------------------------------
	// Get the number of samples stored in driver memory
	//---------------------------------
	ret = DigGetDriverMemorySamples(m_Id1, &m_Samples1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetDriverMemorySamples = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	cstr.Format(_T("%d"), m_Samples1);
	m_Count1.SetWindowText(cstr);
	//---------------------------------
	// Get the operation status
	//---------------------------------
	ret = DigGetStatus(m_Id1, &status1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetStatus = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	cstr.Format(_T("%xH"), status1);
	m_Status1.SetWindowText(cstr);
	m_Info1.SetWindowText(_T("Get status : Normality completion"));

	//------------------------------------------------------------
	// Device2
	//------------------------------------------------------------
	//---------------------------------
	// Get the number of samples stored in driver memory
	//---------------------------------
	ret = DigGetDriverMemorySamples(m_Id2, &m_Samples2);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetDriverMemorySamples = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	cstr.Format(_T("%d"), m_Samples2);
	m_Count2.SetWindowText(cstr);
	//---------------------------------
	// Get the operation status
	//---------------------------------
	ret = DigGetStatus(m_Id2, &status2);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetStatus = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	cstr.Format(_T("%xH"), status2);
	m_Status2.SetWindowText(cstr);
	m_Info2.SetWindowText(_T("Get status : Normality completion"));
}

//===============================================
// Stop acquisition
//===============================================
void CSyncDlg::OnBnClickedButtonStop()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value
	//------------------------------------------------------------
	// Device1
	//------------------------------------------------------------
	ret = DigStop(m_Id1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigStop = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	m_Info1.SetWindowText(_T("Stop acquisition : Normality completion"));

	//------------------------------------------------------------
	// Device2
	//------------------------------------------------------------
	ret = DigStop(m_Id2);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigStop = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	m_Info2.SetWindowText(_T("Stop acquisition : Normality completion"));
}

//===============================================
// Exit handling
//===============================================
void CSyncDlg::OnBnClickedButtonExit()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value
	//------------------------------------------------------------
	// Device1
	//------------------------------------------------------------
	ret = DigExit(m_Id1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigExit = %d : %s"), ret, szError_string);
		m_Info1.SetWindowText(cstr);
		return;
	}
	m_Info1.SetWindowText(_T("Exit : Normality completion"));

	//------------------------------------------------------------
	// Device2
	//------------------------------------------------------------
	ret = DigExit(m_Id2);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigExit = %d : %s"), ret, szError_string);
		m_Info2.SetWindowText(cstr);
		return;
	}
	m_Info2.SetWindowText(_T("Exit : Normality completion"));
}


void CSyncDlg::OnDestroy()
{
	//-------------------------------
	// Exit handling
	//-------------------------------
	DigExit(m_Id1);
	DigExit(m_Id2);
	CDialogEx::OnDestroy();
}


void CSyncDlg::OnClose()
{
	//-------------------------------
	// Exit handling
	//-------------------------------
	DigExit(m_Id1);
	DigExit(m_Id2);
	CDialogEx::OnClose();
}
