
// SyncDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Sync.h"
#include "SyncDlg.h"
#include "afxdialogex.h"

// Dialog header
#include "MSampMode.h"
#include "SSampMode.h"
#include "ModeDlg.h"

// Counter header
#include "ccnt.h"

short M_Id;					// Device ID of master
short S_Id;					// Device ID of slave
short M_IsRing = CNTS_WRITE_ONCE;
short S_IsRing = CNTS_WRITE_ONCE;


unsigned long	M_SampData[DATA_NUM][CHANNEL_NUM];
unsigned long	S_SampData[DATA_NUM][CHANNEL_NUM];

#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_EDIT_M_DEV_NAME, m_MDeviceName);
	DDX_Control(pDX, IDC_EDIT_S_DEV_NAME, m_SDeviceName);
	DDX_Control(pDX, IDC_EDIT_S_STATUS, m_SStatus);
	DDX_Control(pDX, IDC_EDIT_M_STATUS, m_MStatus);
	DDX_Control(pDX, IDC_EDIT_S_RET, m_SRet);
	DDX_Control(pDX, IDC_EDIT_M_RET, m_MRet);
	DDX_Control(pDX, IDC_EDIT_S_ERROR, m_SError);
	DDX_Control(pDX, IDC_EDIT_M_ERROR, m_MError);
	DDX_Control(pDX, IDC_EDIT_S_COUNT, m_SCount);
	DDX_Control(pDX, IDC_EDIT_M_COUNT, m_MCount);
}

BEGIN_MESSAGE_MAP(CSyncDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_M_INIT, &CSyncDlg::OnBnClickedButtonMInit)
	ON_BN_CLICKED(IDC_BUTTON_S_INIT, &CSyncDlg::OnBnClickedButtonSInit)
	ON_BN_CLICKED(IDC_BUTTON_M_CNTMODE, &CSyncDlg::OnBnClickedButtonMCntmode)
	ON_BN_CLICKED(IDC_BUTTON_S_CNTMODE, &CSyncDlg::OnBnClickedButtonSCntmode)
	ON_BN_CLICKED(IDC_BUTTON_M_SMPMODE, &CSyncDlg::OnBnClickedButtonMSmpmode)
	ON_BN_CLICKED(IDC_BUTTON_S_SMPMODE, &CSyncDlg::OnBnClickedButtonSSmpmode)
	ON_BN_CLICKED(IDC_BUTTON_M_START, &CSyncDlg::OnBnClickedButtonMStart)
	ON_BN_CLICKED(IDC_BUTTON_S_START, &CSyncDlg::OnBnClickedButtonSStart)
	ON_BN_CLICKED(IDC_BUTTON_M_STOP, &CSyncDlg::OnBnClickedButtonMStop)
	ON_BN_CLICKED(IDC_BUTTON_S_STOP, &CSyncDlg::OnBnClickedButtonSStop)
	ON_BN_CLICKED(IDC_BUTTON_M_DATA, &CSyncDlg::OnBnClickedButtonMData)
	ON_BN_CLICKED(IDC_BUTTON_S_DATA, &CSyncDlg::OnBnClickedButtonSData)
	ON_BN_CLICKED(IDC_BUTTON_M_COUNT, &CSyncDlg::OnBnClickedButtonMCount)
	ON_BN_CLICKED(IDC_BUTTON_S_COUNT, &CSyncDlg::OnBnClickedButtonSCount)
	ON_BN_CLICKED(IDC_BUTTON_M_STATUS, &CSyncDlg::OnBnClickedButtonMStatus)
	ON_BN_CLICKED(IDC_BUTTON_S_STATUS, &CSyncDlg::OnBnClickedButtonSStatus)
	ON_BN_CLICKED(IDC_BUTTON_M_EXIT, &CSyncDlg::OnBnClickedButtonMExit)
	ON_BN_CLICKED(IDC_BUTTON_S_EXIT, &CSyncDlg::OnBnClickedButtonSExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CSyncDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CSyncDlg message handlers

//===================================================
// Dialog initialization
//===================================================
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 dialog display
	//-----------------------------------
	m_MDeviceName.SetWindowText(_T("CNT000"));
	m_SDeviceName.SetWindowText(_T("CNT001"));

	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);
}

//===================================================
// Termination processing
//===================================================
void CSyncDlg::OnClose()
{
	long	ret;

	//-----------------------------------
	// Termination processing
	//-----------------------------------
	ret = CntExit(M_Id);
	ret = CntExit(S_Id);

	CDialogEx::OnClose();
}

//===================================================
// CntInit() for master
//===================================================
void CSyncDlg::OnBnClickedButtonMInit()
{
	CString	str;
	char	buf[100], error_string[256];
	TCHAR	szBuf[100], szError_string[256];
	long	ret;

	//-----------------------------------
	// Get device name
	//-----------------------------------
	m_MDeviceName.GetWindowText(szBuf, sizeof(szBuf));
	sprintf_s(buf, "%S", szBuf);

	//-----------------------------------
	// Initialization
	//-----------------------------------
	ret = CntInit(buf, &M_Id);
	CntGetErrorString(ret, error_string);
	wsprintf(szError_string, _T("%S"), error_string);
	str.Format(_T("CntInit ret = %d: %s"), ret, szError_string);
	m_MRet.SetWindowText(str);
}

//===================================================
// CntInit() for slave
//===================================================
void CSyncDlg::OnBnClickedButtonSInit()
{
	CString	str;
	char	buf[100], error_string[256];
	TCHAR	szBuf[100], szError_string[256];
	long	ret;

	//-----------------------------------
	// Get device name
	//-----------------------------------
	m_SDeviceName.GetWindowText(szBuf, sizeof(szBuf));
	sprintf_s(buf, "%S", szBuf);

	//-----------------------------------
	// Initialization
	//-----------------------------------
	ret = CntInit(buf, &S_Id);
	CntGetErrorString(ret, error_string);
	wsprintf(szError_string, _T("%S"), error_string);
	str.Format(_T("CntInit ret = %d: %s"), ret, szError_string);
	m_SRet.SetWindowText(str);
}

//===================================================
// Condition setting for master
//===================================================
void CSyncDlg::OnBnClickedButtonMCntmode()
{
	CModeDlg	dlg;

	//-----------------------------------
	// Open "Counter Mode" dialog
	//-----------------------------------
	dlg.Id = M_Id;
	dlg.DoModal();
}

//===================================================
// Condition setting for slave
//===================================================
void CSyncDlg::OnBnClickedButtonSCntmode()
{
	CModeDlg	dlg;

	//-----------------------------------
	// Open "Counter Mode" dialog
	//-----------------------------------
	dlg.Id = S_Id;
	dlg.DoModal();
}

//===================================================
// Sampling mode setting for master
//===================================================
void CSyncDlg::OnBnClickedButtonMSmpmode()
{
	CMSampMode	dlg;

	dlg.m_hMainWnd = m_hWnd;
	dlg.DoModal();
}

//===================================================
// Sampling mode setting for slave
//===================================================
void CSyncDlg::OnBnClickedButtonSSmpmode()
{
	CSSampMode	dlg;
	dlg.DoModal();
}

//===================================================
// Master start
//===================================================
void CSyncDlg::OnBnClickedButtonMStart()
{
	long	ret;
	CString	csMsg;
	short	ChNo[CHANNEL_NUM];
	WORD	i;
	char	error_string[256];
	TCHAR	szError_string[256];
	CString	str;

	//-----------------------------------
	// Set notification
	//-----------------------------------
	ret = CntNotifySamplingStop(M_Id, m_hWnd);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntNotifySamplingStop ret = %d: %s"), ret, szError_string);
		m_MRet.SetWindowText(str);
		return;
	}

	ret = CntNotifySamplingCount(M_Id, M_NotifyCount, m_hWnd);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntNotifySamplingCount ret = %d: %s"), ret, szError_string);
		m_MRet.SetWindowText(str);
		return;
	}

	//-----------------------------------
	// Reset FIFO
	//-----------------------------------
	ret = CntSamplingFifoReset(M_Id);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntSamplingFifoReset ret = %d: %s"), ret, szError_string);
		m_MRet.SetWindowText(str);
		return;
	}

	//-----------------------------------
	// Set buffer
	//-----------------------------------
	ZeroMemory((PVOID)&M_SampData[0][0], CHANNEL_NUM * DATA_NUM * sizeof(DWORD));
	ret = CntSamplingSetBuffer(M_Id, &M_SampData[0][0], CHANNEL_NUM, DATA_NUM, M_IsRing);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntSamplingSetBuffer ret = %d: %s"), ret, szError_string);
		m_MRet.SetWindowText(str);
		return;
	}
	//-----------------------------------
	// Start counter
	//-----------------------------------
	for (i = 0; i<CHANNEL_NUM; i++) {
		ChNo[i] = i;
	}
	ret = CntStartCount(M_Id, &ChNo[0], CHANNEL_NUM);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntStartCount ret = %d: %s"), ret, szError_string);
		m_MRet.SetWindowText(str);
		return;
	}

	//-----------------------------------
	// Start sampling
	//-----------------------------------
	ret = CntSamplingStart(M_Id);
	csMsg.Format(_T("CntStartSampling : RetCode = %d"), ret);
	m_MRet.SetWindowText(csMsg);
	m_MCount.SetWindowText(_T(""));
	m_MStatus.SetWindowText(_T(""));
	m_MError.SetWindowText(_T(""));
	return;
}

//===================================================
// Slave start
//===================================================
void CSyncDlg::OnBnClickedButtonSStart()
{
	long ret;
	CString	csMsg;
	short	ChNo[CHANNEL_NUM];
	WORD	i;
	char	error_string[256];
	TCHAR	szError_string[256];
	CString	str;

	//-----------------------------------
	// Set notification
	//-----------------------------------
	ret = CntNotifySamplingStop(S_Id, m_hWnd);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntNotifySamplingStop ret = %d: %s"), ret, szError_string);
		m_SRet.SetWindowText(str);
		return;
	}
	ret = CntNotifySamplingCount(S_Id, S_NotifyCount, m_hWnd);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntNotifySamplingCount ret = %d: %s"), ret, szError_string);
		m_SRet.SetWindowText(str);
		return;
	}

	//-----------------------------------
	// Reset FIFO
	//-----------------------------------
	ret = CntSamplingFifoReset(S_Id);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntSamplingFifoReset ret = %d: %s"), ret, szError_string);
		m_MRet.SetWindowText(str);
		return;
	}

	//-----------------------------------
	// Set buffer
	//-----------------------------------
	ZeroMemory((PVOID)&S_SampData[0][0], CHANNEL_NUM * DATA_NUM * sizeof(DWORD));
	ret = CntSamplingSetBuffer(S_Id, &S_SampData[0][0], CHANNEL_NUM, DATA_NUM, S_IsRing);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntSamplingSetBuffer ret = %d: %s"), ret, szError_string);
		m_SRet.SetWindowText(str);
		return;
	}
	//-----------------------------------
	// Start counter
	//-----------------------------------
	for (i = 0; i<CHANNEL_NUM; i++) {
		ChNo[i] = i;
	}
	ret = CntStartCount(S_Id, &ChNo[0], CHANNEL_NUM);
	if (ret != 0) {
		CntGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		str.Format(_T("CntStartCount ret = %d: %s"), ret, szError_string);
		m_SRet.SetWindowText(str);
		return;
	}
	//-----------------------------------
	// Start sampling
	//-----------------------------------
	ret = CntSamplingStart(S_Id);
	csMsg.Format(_T("CntStartSampling : RetCode = %d"), ret);
	m_SRet.SetWindowText(csMsg);
	m_SCount.SetWindowText(_T(""));
	m_SStatus.SetWindowText(_T(""));
	m_SError.SetWindowText(_T(""));
	return;
}

//===================================================
// Master stop
//===================================================
void CSyncDlg::OnBnClickedButtonMStop()
{
	long ret;
	char	error_string[256];
	TCHAR	szError_string[256];
	CString	str;

	ret = CntSamplingStop(M_Id);
	CntGetErrorString(ret, error_string);
	wsprintf(szError_string, _T("%S"), error_string);
	str.Format(_T("CntSamplingStop ret = %d: %s"), ret, szError_string);
	m_SRet.SetWindowText(str);
}

//===================================================
// Slave stop
//===================================================
void CSyncDlg::OnBnClickedButtonSStop()
{
	long ret;
	CString	csMsg;

	ret = CntSamplingStop(S_Id);
	csMsg.Format(_T("CntStopSampling : Ret = %d"), ret);
	m_SRet.SetWindowText(csMsg);
}

//===================================================
// Get master data
//===================================================
void CSyncDlg::OnBnClickedButtonMData()
{
	unsigned long	data_count, ch_no;
	FILE			*fp;
	static TCHAR	szFileName[] = _T("Sampling1.txt");
	char			buf[256];
	TCHAR			szLine[500], szTmp[100];
	CString			csMsg;

	//-----------------------------------
	// Open file
	//-----------------------------------
	if ((_tfopen_s(&fp, szFileName, _T("w"))) != 0) {
		csMsg.Format(_T("fopen : file open error."));
		m_MRet.SetWindowText(csMsg);
		return;
	}
	//-----------------------------------
	// Write data to file
	//-----------------------------------
	for (data_count = 0; data_count < DATA_NUM; data_count++) {
		wsprintf(szLine, _T("%8d : "), data_count);
		for (ch_no = 0; ch_no < CHANNEL_NUM; ch_no++) {
			wsprintf(szTmp, _T("%08X,"), M_SampData[data_count][ch_no]);
			lstrcat(szLine, szTmp);
		}
		lstrcat(szLine, _T("\n"));
		_fputts(szLine, fp);
	}
	//-----------------------------------
	// Close file
	//-----------------------------------
	fclose(fp);

	//-----------------------------------
	// notepad
	//-----------------------------------
	sprintf_s(buf, sizeof(buf), "notepad.exe %S", szFileName);
	WinExec(buf, SW_SHOW);
}

//===================================================
// Get slave data
//===================================================
void CSyncDlg::OnBnClickedButtonSData()
{
	DWORD			data_count, ch_no;
	FILE			*fp;
	static TCHAR	szFileName[] = _T("Sampling2.txt");
	char			buf[256];
	TCHAR			szLine[500], szTmp[100];
	CString			csMsg;

	//-----------------------------------
	// Open file
	//-----------------------------------
	if ((_tfopen_s(&fp, szFileName, _T("w"))) != 0) {
		csMsg.Format(_T("fopen : file open error."));
		m_SRet.SetWindowText(csMsg);
		return;
	}
	//-----------------------------------
	// Write data to file
	//-----------------------------------
	for (data_count = 0; data_count < DATA_NUM; data_count++) {
		wsprintf(szLine, _T("%8d : "), data_count);
		for (ch_no = 0; ch_no < CHANNEL_NUM; ch_no++) {
			wsprintf(szTmp, _T("%08X,"), S_SampData[data_count][ch_no]);
			lstrcat(szLine, szTmp);
		}
		lstrcat(szLine, _T("\n"));
		_fputts(szLine, fp);
	}

	//-----------------------------------
	// Close file
	//-----------------------------------
	fclose(fp);

	//-----------------------------------
	// notepad
	//-----------------------------------
	sprintf_s(buf, sizeof(buf), "notepad.exe %S", szFileName);
	WinExec(buf, SW_SHOW);
}

//===================================================
// Get master count value
//===================================================
void CSyncDlg::OnBnClickedButtonMCount()
{
	long ret;
	unsigned long	Count;
	unsigned long	ScanCarry;
	CString	csMsg;

	//-----------------------------------
	// Get count value
	//-----------------------------------
	ret = CntGetSamplingCount(M_Id, &Count, &ScanCarry);
	csMsg.Format(_T("CntGetSamplingCount : Ret = %d"), ret);
	m_MRet.SetWindowText(csMsg);
	csMsg.Format(_T("%lu"), Count);
	m_MCount.SetWindowText(csMsg);
}

//===================================================
// Get slave count value
//===================================================
void CSyncDlg::OnBnClickedButtonSCount()
{
	long ret;
	unsigned long	Count;
	unsigned long	ScanCarry;
	CString	csMsg;

	//-----------------------------------
	// Get count value
	//-----------------------------------
	ret = CntGetSamplingCount(S_Id, &Count, &ScanCarry);
	csMsg.Format(_T("CntGetSamplingCount : Ret = %d"), ret);
	m_SRet.SetWindowText(csMsg);
	csMsg.Format(_T("%lu"), Count);
	m_SCount.SetWindowText(csMsg);
}

//===================================================
// Get master status
//===================================================
void CSyncDlg::OnBnClickedButtonMStatus()
{
	long ret;
	unsigned long	Status, Err;
	CString	csMsg;

	//-----------------------------------
	// Get status information
	//-----------------------------------
	ret = CntGetSamplingStatus(M_Id, &Status, &Err);
	csMsg.Format(_T("CntGetSamplingStatus : Ret = %d"), ret);
	m_MRet.SetWindowText(csMsg);
	// Status
	csMsg = "";
	if (Status & CNTS_STATUS_BMSTOP)	csMsg += "BmStop,";
	if (Status & CNTS_STATUS_CNTSTART)	csMsg += "CntStart,";
	if (Status & CNTS_STATUS_CNTSTOP)	csMsg += "CntStop,";
	if (Status & CNTS_STATUS_TRGIN)	csMsg += "TrgIn,";
	if (Status & CNTS_STATUS_OVERRUN)	csMsg += "Overrun";
	m_MStatus.SetWindowText(csMsg);
	// Error
	csMsg = "";
	if (Err & CNTS_STATUS_FIFOFULL)	csMsg += "FifoFull,";
	if (Err & CNTS_STATUS_SGOVERIN)	csMsg += "S/GOverIn,";
	if (Err & CNTS_STATUS_TRGERR)		csMsg += "TrgErr,";
	if (Err & CNTS_STATUS_CLKERR)		csMsg += "ClkErr,";
	if (Err & CNTS_STATUS_SLAVEHALT)	csMsg += "SlaveHalt,";
	if (Err & CNTS_STATUS_MASTERHALT)	csMsg += "MasterHalt";
	m_MError.SetWindowText(csMsg);
}

//===================================================
// Get slave status
//===================================================
void CSyncDlg::OnBnClickedButtonSStatus()
{
	long ret;
	unsigned long	Status, Err;
	CString	csMsg;

	//-----------------------------------
	// Get status information
	//-----------------------------------
	ret = CntGetSamplingStatus(S_Id, &Status, &Err);
	csMsg.Format(_T("CntGetSamplingStatus : Ret = %d"), ret);
	m_SRet.SetWindowText(csMsg);
	// Status
	csMsg = "";
	if (Status & CNTS_STATUS_BMSTOP)	csMsg += "BmStop,";
	if (Status & CNTS_STATUS_CNTSTART)	csMsg += "CntStart,";
	if (Status & CNTS_STATUS_CNTSTOP)	csMsg += "CntStop,";
	if (Status & CNTS_STATUS_TRGIN)	csMsg += "TrgIn,";
	if (Status & CNTS_STATUS_OVERRUN)	csMsg += "Overrun";
	m_SStatus.SetWindowText(csMsg);
	// Error
	csMsg = "";
	if (Err & CNTS_STATUS_FIFOFULL)	csMsg += "FifoFull,";
	if (Err & CNTS_STATUS_SGOVERIN)	csMsg += "S/GOverIn,";
	if (Err & CNTS_STATUS_TRGERR)		csMsg += "TrgErr,";
	if (Err & CNTS_STATUS_CLKERR)		csMsg += "ClkErr,";
	if (Err & CNTS_STATUS_SLAVEHALT)	csMsg += "SlaveHalt,";
	if (Err & CNTS_STATUS_MASTERHALT)	csMsg += "MasterHalt";
	m_SError.SetWindowText(csMsg);
}

//===================================================
// Exit master
//===================================================
void CSyncDlg::OnBnClickedButtonMExit()
{
	TCHAR	szMsg[100];
	long	ret;

	//------------------------------
	// Exit processing
	//------------------------------
	ret = CntExit(M_Id);
	wsprintf(szMsg, _T("%S : Ret = %X h"), "CntClose", ret);
	m_MRet.SetWindowText(szMsg);
}

//===================================================
// Exit slave
//===================================================
void CSyncDlg::OnBnClickedButtonSExit()
{
	TCHAR	szMsg[100];
	long	ret;

	//------------------------------
	// Exit processing
	//------------------------------
	ret = CntExit(S_Id);
	wsprintf(szMsg, _T("%S : Ret = %X h"), "CntClose", ret);
	m_SRet.SetWindowText(szMsg);
}

//===================================================
// Exit program
//===================================================
void CSyncDlg::OnBnClickedButtonExit()
{
	long ret;
	ret = CntExit(M_Id);
	ret = CntExit(S_Id);
	CDialogEx::OnCancel();
}

//===================================================
// Processing of counter event notification
//===================================================
LRESULT CSyncDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message){
	case CNTM_SAMPLING_COUNT:
		if (LOWORD(wParam) == M_Id){
			OnBnClickedButtonMCount();
		}
		else{
			OnBnClickedButtonSCount();
		}
		break;
	case CNTM_SAMPLING_STOP:
		if (LOWORD(wParam) == M_Id){
			OnBnClickedButtonMCount();
			m_MStatus.SetWindowText(_T("Stopped!"));
		}
		else{
			OnBnClickedButtonSCount();
			m_SStatus.SetWindowText(_T("Stopped!"));
		}
		break;
	}

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