// SettingDlg.cpp
//

#include "stdafx.h"
#include "WEB_IO_TEST.h"
#include "SettingDlg.h"
#include "WEB_IO_TESTDlg.h"


// CSettingDlg : Dialog Box

IMPLEMENT_DYNCREATE(CSettingDlg, CDialog)

CSettingDlg::CSettingDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSettingDlg::IDD, pParent)
	, m_rdOuputKindVal(FALSE)
{
}

CSettingDlg::~CSettingDlg()
{
}

void CSettingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_CHECK_OUTPUT1, m_chkOutput[0]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT2, m_chkOutput[1]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT3, m_chkOutput[2]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT4, m_chkOutput[3]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT5, m_chkOutput[4]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT6, m_chkOutput[5]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT7, m_chkOutput[6]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT8, m_chkOutput[7]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT9, m_chkOutput[8]);
	DDX_Control(pDX, IDC_CHECK_OUTPUT10, m_chkOutput[9]);

	DDX_Control(pDX, IDC_CHECK_INPUT1, m_chkInput[0]);
	DDX_Control(pDX, IDC_CHECK_INPUT2, m_chkInput[1]);
	DDX_Control(pDX, IDC_CHECK_INPUT3, m_chkInput[2]);
	DDX_Control(pDX, IDC_CHECK_INPUT4, m_chkInput[3]);
	DDX_Control(pDX, IDC_CHECK_INPUT5, m_chkInput[4]);
	DDX_Control(pDX, IDC_CHECK_INPUT6, m_chkInput[5]);
	DDX_Control(pDX, IDC_CHECK_INPUT7, m_chkInput[6]);
	DDX_Control(pDX, IDC_CHECK_INPUT8, m_chkInput[7]);
	DDX_Control(pDX, IDC_CHECK_INPUT9, m_chkInput[8]);
	DDX_Control(pDX, IDC_CHECK_INPUT10, m_chkInput[9]);
	DDX_Control(pDX, IDC_CHECK_INPUT11, m_chkInput[10]);
	DDX_Control(pDX, IDC_CHECK_INPUT12, m_chkInput[11]);
	DDX_Control(pDX, IDC_CHECK_INPUT13, m_chkInput[12]);
	DDX_Control(pDX, IDC_CHECK_INPUT14, m_chkInput[13]);
	DDX_Control(pDX, IDC_CHECK_INPUT15, m_chkInput[14]);
	DDX_Control(pDX, IDC_CHECK_INPUT16, m_chkInput[15]);

	DDX_Control(pDX, IDC_CHECK_ADC1, m_chkADC[0]);
	DDX_Control(pDX, IDC_CHECK_ADC2, m_chkADC[1]);
	DDX_Control(pDX, IDC_CHECK_ADC3, m_chkADC[2]);
	DDX_Control(pDX, IDC_CHECK_ADC4, m_chkADC[3]);

	DDX_Control(pDX, IDC_COMBO_ADC1, m_cmbADC[0]);
	DDX_Control(pDX, IDC_COMBO_ADC2, m_cmbADC[1]);
	DDX_Control(pDX, IDC_COMBO_ADC3, m_cmbADC[2]);
	DDX_Control(pDX, IDC_COMBO_ADC4, m_cmbADC[3]);

	DDX_Control(pDX, IDC_CHECK_PWR_STOP1, m_chkPwrStop[0]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP2, m_chkPwrStop[1]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP3, m_chkPwrStop[2]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP4, m_chkPwrStop[3]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP5, m_chkPwrStop[4]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP6, m_chkPwrStop[5]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP7, m_chkPwrStop[6]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP8, m_chkPwrStop[7]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP9, m_chkPwrStop[8]);
	DDX_Control(pDX, IDC_CHECK_PWR_STOP10, m_chkPwrStop[9]);
	DDX_Radio(pDX, IDC_RADIO_DELAY_ON, m_rdOuputKindVal);
}

BOOL CSettingDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	Init();
	return TRUE;  
}

BEGIN_MESSAGE_MAP(CSettingDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SET_OUTPUT, OnBnClickedButtonSetOutput)
	ON_BN_CLICKED(IDC_BUTTON_SET_INPUT, OnBnClickedButtonSetInput)
	ON_BN_CLICKED(IDC_BUTTON_SET_ENABLE, OnBnClickedButtonSetEnable)
	ON_BN_CLICKED(IDC_BUTTON_SET_PWR_STOP, OnBnClickedButtonSetPwrStop)
	ON_MESSAGE(WM_RHIO_RECEIVE, OnReceive)
	ON_BN_CLICKED(IDC_BUTTON_MONITOR_ENABLE, OnBnClickedButtonMonitorEnable)
	ON_BN_CLICKED(IDC_BUTTON_MONITOR_OUTPUT, OnBnClickedButtonMonitorOutput)
	ON_BN_CLICKED(IDC_BUTTON_MONITOR_INPUT, OnBnClickedButtonMonitorInput)
	ON_BN_CLICKED(IDC_BUTTON_MONITOR_PWR_STOP, OnBnClickedButtonMonitorPwrStop)
	ON_EN_CHANGE(IDC_EDIT_MACRO, OnEnChangeEditMacro)
	ON_BN_CLICKED(IDC_BUTTON_FACTORY_RESET, OnBnClickedButtonFactoryReset)
	ON_BN_CLICKED(IDC_BUTTON_SETMODE, OnBnClickedButtonSetmode)
	ON_BN_CLICKED(IDC_BUTTON_RUN_MODE, OnBnClickedButtonRunMode)
	ON_BN_CLICKED(IDC_RADIO_DELAY_ON, OnBnClickedRadioDelayOn)
	ON_BN_CLICKED(IDC_RADIO_DELAY_OFF, OnBnClickedRadioDelayOff)
	ON_BN_CLICKED(IDC_RADIO_Pulse, OnBnClickedRadioPulse)
	ON_BN_CLICKED(IDC_BUTTON_MONITOR_OUTPUT2, OnBnClickedButtonMonitorOutput2)
	ON_BN_CLICKED(IDC_BUTTON_SET_OUTPUT2, OnBnClickedButtonSetOutput2)
	ON_BN_CLICKED(IDC_CHECK_USE_MACRO, OnBnClickedCheckUseMacro)
	ON_BN_CLICKED(IDC_CHECK_USE_DELAY_PULSE, OnBnClickedCheckUseDelayPulse)
	ON_BN_CLICKED(IDC_BUTTON_MONITOR_SERIAL, OnBnClickedButtonMonitorSerial)
	ON_BN_CLICKED(IDC_BUTTON_SET_SERIAL, OnBnClickedButtonSetSerial)
END_MESSAGE_MAP()

//BEGIN_DHTML_EVENT_MAP(CSettingDlg)
//	ON_WM_SYSCOMMAND()
//	ON_WM_PAINT()
//	ON_WM_QUERYDRAGICON()
//END_DHTML_EVENT_MAP()



// Processing the CSettingDlg message.

void CSettingDlg::Init(void)
{
	InitOutput();
	InitADC();
	InitEnable();
	InitPwrStop();
	InitSerialNum();
	EnableSettingButton(FALSE);
	GetDlgItem(IDC_BUTTON_RUN_MODE)->EnableWindow(FALSE);
}

void CSettingDlg::InitOutput(void)
{
	((CEdit *)GetDlgItem(IDC_EDIT_PORT_NUM))->SetLimitText(2);
	SetDlgItemText(IDC_EDIT_PORT_NUM, "01");

	((CEdit *)GetDlgItem(IDC_EDIT_OUT_ONTIME))->SetLimitText(4);
	((CEdit *)GetDlgItem(IDC_EDIT_OUT_OFFTIME))->SetLimitText(4);

    OnBnClickedRadioDelayOn();
	OnBnClickedCheckUseMacro();
	OnBnClickedCheckUseDelayPulse();
}

void CSettingDlg::InitADC(void)
{
	((CEdit *)GetDlgItem(IDC_EDIT_ADC1))->SetLimitText(4);
	((CEdit *)GetDlgItem(IDC_EDIT_ADC2))->SetLimitText(4);
	((CEdit *)GetDlgItem(IDC_EDIT_ADC3))->SetLimitText(4);
	((CEdit *)GetDlgItem(IDC_EDIT_ADC4))->SetLimitText(4);

	SetDlgItemText(IDC_EDIT_ADC1, "1023");
	SetDlgItemText(IDC_EDIT_ADC2, "1023");
	SetDlgItemText(IDC_EDIT_ADC3, "1023");
	SetDlgItemText(IDC_EDIT_ADC4, "1023");

	for(int index = 0; index < 4; index++)
	{
		m_cmbADC[index].AddString("Level");
		m_cmbADC[index].AddString("Switch");
		m_cmbADC[index].SetCurSel(0);
	}

	((CComboBox *)GetDlgItem(IDC_COMBO_ADC_REFERENCE))->AddString("AVcc");
	((CComboBox *)GetDlgItem(IDC_COMBO_ADC_REFERENCE))->AddString("Internal");
	((CComboBox *)GetDlgItem(IDC_COMBO_ADC_REFERENCE))->AddString("External");
	((CComboBox *)GetDlgItem(IDC_COMBO_ADC_REFERENCE))->SetCurSel(0);
}

void CSettingDlg::InitEnable(void)
{
	for(int index = 0; index < 16; index++)
	{
		if(index < 10)
			m_chkOutput[index].SetCheck(1);

		if(index < 4)
			m_chkADC[index].SetCheck(1);

		m_chkInput[index].SetCheck(1);
	}
}

void CSettingDlg::InitPwrStop(void)
{
	for(int index = 0; index < 10; index++)
		m_chkPwrStop[index].SetCheck(1);
}

void CSettingDlg::InitSerialNum(void)
{
	SetDlgItemText(IDC_EDIT_SERIAL1, "RHIO");
	((CEdit *)GetDlgItem(IDC_EDIT_SERIAL2))->LimitText(12);
}

void CSettingDlg::OnBnClickedButtonSetOutput()
{
	CString strMcrData, strStop, strPort, strMcr, strTemp;

	GetDlgItemText(IDC_EDIT_PORT_NUM, strPort);
	GetDlgItemText(IDC_EDIT_MACRO, strMcr);

	if(atoi(strPort) <= 0 || atoi(strPort) > 10) 
	{
		AfxMessageBox("The value of port number is not valid.", MB_OK | MB_ICONINFORMATION);
		return;
	}

	SRHIOSetting sData;
	strTemp = strMcr;
	strTemp.Trim();

	if(((CButton *)GetDlgItem(IDC_CHECK_USE_MACRO))->GetCheck() == BST_CHECKED)
	{
		if(strTemp == "")
		{
			AfxMessageBox("Input the MACRO.", MB_OK | MB_ICONINFORMATION);
			return;
		}

		sData.sOutput.eMacro = ESOF_SETTING;
		memset(sData.sOutput.cMacro, '\0', 106);
		memcpy(sData.sOutput.cMacro, strMcr, strMcr.GetLength());

		((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetMacro(sData, atoi(strPort));
	}
	else
	{
		sData.sOutput.eMacro = ESOF_CLEAR;
		((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetMacro(sData, atoi(strPort));
	}

	EnableSettingButton(FALSE);
	m_isSetting = true;

}

//ADC
void CSettingDlg::OnBnClickedButtonSetInput()
{
	int iADC[4];
	CString strTemp;

	iADC[0] = GetDlgItemInt(IDC_EDIT_ADC1);
	iADC[1] = GetDlgItemInt(IDC_EDIT_ADC2);
	iADC[2] = GetDlgItemInt(IDC_EDIT_ADC3);
	iADC[3] = GetDlgItemInt(IDC_EDIT_ADC4);

	SRHIOSetting sSettingData;

	for(int index = 0; index < 4; index++)
	{
		if(iADC[index] < 0 || iADC[index] > 1023)
		{
			AfxMessageBox("The value of port number is not valid." , MB_OK | MB_ICONINFORMATION);
			return;
		}
        
		strTemp.Format("%04d", iADC[index]);
		
		memset(sSettingData.sADC.sADCData[index].cADC, '\0', sizeof(sSettingData.sADC.sADCData[index].cADC));
		memcpy(sSettingData.sADC.sADCData[index].cADC, strTemp, strTemp.GetLength());
	}

	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetADC(sSettingData);
	EnableSettingButton(FALSE);
	m_isSetting = true;
}

void CSettingDlg::OnBnClickedButtonSetEnable()
{
	CString strData;
	/*strData.Format("11%d%d%d%d,%d%d%d%d,%d%d%d%d,%d%d%d%d,%d%d%d%d,%d%d%d%d,%d%d%d%d,%d%d",
		m_chkInput[0].GetCheck(),m_chkInput[1].GetCheck(),m_chkInput[2].GetCheck(),m_chkInput[3].GetCheck(),
		m_chkInput[4].GetCheck(),m_chkInput[5].GetCheck(),m_chkInput[6].GetCheck(),m_chkInput[7].GetCheck(),
		m_chkInput[8].GetCheck(),m_chkInput[9].GetCheck(),m_chkInput[10].GetCheck(),m_chkInput[11].GetCheck(),
		m_chkInput[12].GetCheck(),m_chkInput[13].GetCheck(),m_chkInput[14].GetCheck(),m_chkInput[15].GetCheck(),
		m_chkADC[0].GetCheck(),m_chkADC[1].GetCheck(),m_chkADC[2].GetCheck(),m_chkADC[3].GetCheck(),
		m_chkOutput[0].GetCheck(),m_chkOutput[1].GetCheck(),m_chkOutput[2].GetCheck(),m_chkOutput[3].GetCheck(),
		m_chkOutput[4].GetCheck(),m_chkOutput[5].GetCheck(),m_chkOutput[6].GetCheck(),m_chkOutput[7].GetCheck(),
		m_chkOutput[8].GetCheck(),m_chkOutput[9].GetCheck());*/

	SRHIOSetting sSettingData;
	
	for(int index = 0; index < 12; index++)
	{
		if(m_chkInput[index].GetCheck() == BST_CHECKED)
			sSettingData.sInput.isEnable[index] = true;
		else
			sSettingData.sInput.isEnable[index] = false;
	}

	for(int index = 0; index < 4; index++)
	{
		if(m_cmbADC[index].GetCurSel() == 0)
			sSettingData.sADC.eModeADC[index] = EAM_LEVEL;
		else
			sSettingData.sADC.eModeADC[index] = EAM_SWITCH;
	}

	for(int index = 0; index < 10; index++)
	{
		if(m_chkOutput[index].GetCheck() == BST_CHECKED)
			sSettingData.sOutput.isEnable[index] = true;
		else
			sSettingData.sOutput.isEnable[index] = false;
	}

	sSettingData.sADC.bReference = ((CComboBox *)GetDlgItem(IDC_COMBO_ADC_REFERENCE))->GetCurSel() + 0x30;


	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetEnable(sSettingData);
	EnableSettingButton(FALSE);
}

void CSettingDlg::OnBnClickedButtonSetPwrStop()
{
	SRHIOSetting sSetting;
	for(int index = 0; index < 10; index++)
	{
		if(m_chkPwrStop[index].GetCheck() == BST_CHECKED)
			sSetting.sOutput.isEnablePowerStop[index] = true;
		else
			sSetting.sOutput.isEnablePowerStop[index] = false;
	}

	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetPwrStop(sSetting);
	EnableSettingButton(FALSE);
	m_isSetting = true;
}


LRESULT CSettingDlg::OnReceive(WPARAM wParam, LPARAM lParam)
{
	SendStatusFlag eFlag = (SendStatusFlag)wParam;
	BOOL bVal = (BOOL)lParam;

	switch(eFlag)
	{
	case ESF_SET_MODE:		//A command response processing for the setting up mode change.
		{
			if(bVal == 0x31)
			{
				EnableSettingButton(TRUE);				
				GetDlgItem(IDC_BUTTON_SETMODE)->EnableWindow(FALSE);
				GetDlgItem(IDC_BUTTON_RUN_MODE)->EnableWindow(TRUE);
				GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
			}
			else
			{
				AfxMessageBox("It could not change a setting up mode",MB_OK | MB_ICONINFORMATION);
				GetDlgItem(IDC_BUTTON_SETMODE)->EnableWindow(TRUE);
			}

		}
		break;
	case ESF_RUN_MODE:		//A command response processing for the execution mode change.
		{
			if(bVal == 0x30)
			{
				EnableSettingButton(FALSE);				
				GetDlgItem(IDC_BUTTON_SETMODE)->EnableWindow(TRUE);
				GetDlgItem(IDC_BUTTON_RUN_MODE)->EnableWindow(FALSE);
				GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
			}
			else
			{
				AfxMessageBox("It could not change a setting up mode.",MB_OK | MB_ICONINFORMATION);
				GetDlgItem(IDC_BUTTON_RUN_MODE)->EnableWindow(TRUE);
			}

		}
		break;
	case ESF_SET_MACRO:		//A command response processing for the macro-mode change.
		if(bVal == 0x30)
		{
			SRHIOSetting sData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sData);
			OnBnClickedCheckUseMacro();
			SetDlgItemText(IDC_EDIT_MACRO, sData.sOutput.cMacro);
		}
		else
			AfxMessageBox("It could not complete an output port setting up.",
			MB_OK | MB_ICONINFORMATION);
		EnableSettingButton(TRUE);
		break;
	case ESF_MON_MACRO:		//A command response processing for the macro-mode check.
		SRHIOSetting sData;
		((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sData);

		if(bVal == 0x30)
		{
			((CButton *)GetDlgItem(IDC_CHECK_USE_MACRO))->SetCheck(BST_CHECKED);
			OnBnClickedCheckUseMacro();
			SetDlgItemText(IDC_EDIT_MACRO, sData.sOutput.cMacro);
		}
		else if(bVal == 0x32)
		{
			((CButton *)GetDlgItem(IDC_CHECK_USE_MACRO))->SetCheck(BST_UNCHECKED);
			OnBnClickedCheckUseMacro();
			SetDlgItemText(IDC_EDIT_MACRO, "");
		}
		else
			AfxMessageBox("It could not response the content of port setting up.",
				MB_OK | MB_ICONINFORMATION);
		EnableSettingButton(TRUE);
		break;
	case ESF_MON_DELAY_PULSE:	//A command response processing for the Delay/Pulse check. 
		{
			SRHIOSetting sData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sData);

			if(bVal == 0x30)
			{
				if(sData.sOutput.eDelayPulse == ESOF_CLEAR)
				{
					((CButton *)GetDlgItem(IDC_CHECK_USE_DELAY_PULSE))->SetCheck(BST_UNCHECKED);
					OnBnClickedCheckUseDelayPulse();
				}
				else
				{
					((CButton *)GetDlgItem(IDC_CHECK_USE_DELAY_PULSE))->SetCheck(BST_CHECKED);
					OnBnClickedCheckUseDelayPulse();

					CString strData;
					strData.Format("%010s", sData.sOutput.cDelayPulse);

					UpdateData();
					if(strData.Mid(0,5) != "00000" && strData.Mid(5,5) == "00000")
					{
						OnBnClickedRadioDelayOn();
						m_rdOuputKindVal = 0;
						SetDlgItemText(IDC_EDIT_OUT_ONTIME, strData.Mid(0,5));
					}
					else if(strData.Mid(0,5) == "00000" && strData.Mid(5,5) != "00000")
					{
						OnBnClickedRadioDelayOff();
						m_rdOuputKindVal = 1;
						SetDlgItemText(IDC_EDIT_OUT_OFFTIME, strData.Mid(5,5));
					}
					else if(strData.Mid(0,5) != "00000" && strData.Mid(5,5) != "00000")
					{
						OnBnClickedRadioPulse();
						m_rdOuputKindVal = 2;

						SetDlgItemText(IDC_EDIT_OUT_ONTIME, strData.Mid(0,5));
						SetDlgItemText(IDC_EDIT_OUT_OFFTIME, strData.Mid(5,5));
					}
					UpdateData(FALSE);
				}
			}
			else if(bVal == 0x32)
			{
				((CButton *)GetDlgItem(IDC_CHECK_USE_DELAY_PULSE))->SetCheck(BST_UNCHECKED);
				OnBnClickedCheckUseDelayPulse();
			}
			else
				AfxMessageBox("It could not response the content of port setting up.",
				MB_OK | MB_ICONINFORMATION);

			EnableSettingButton(TRUE);

		}
		break;
	case ESF_SET_DELAY_PULSE:	//A command response processing for the Delay/Pulse set up.
		{
			SRHIOSetting sData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sData);

			if(bVal == 0x30)
			{
				if(sData.sOutput.eDelayPulse == ESOF_CLEAR)
				{
					((CButton *)GetDlgItem(IDC_CHECK_USE_DELAY_PULSE))->SetCheck(BST_UNCHECKED);
					OnBnClickedCheckUseDelayPulse();
				}
				else
				{
					((CButton *)GetDlgItem(IDC_CHECK_USE_DELAY_PULSE))->SetCheck(BST_CHECKED);
					OnBnClickedCheckUseDelayPulse();

					CString strData;
					strData.Format("%010s", sData.sOutput.cDelayPulse);

					UpdateData();
					if(strData.Mid(0,5) != "00000" && strData.Mid(5,5) == "00000")
					{
						OnBnClickedRadioDelayOn();
						m_rdOuputKindVal = 0;
						SetDlgItemText(IDC_EDIT_OUT_ONTIME, strData.Mid(0,5));
					}
					else if(strData.Mid(0,5) == "00000" && strData.Mid(5,5) != "00000")
					{
						OnBnClickedRadioDelayOff();
						m_rdOuputKindVal = 1;
						SetDlgItemText(IDC_EDIT_OUT_OFFTIME, strData.Mid(5,5));
					}
					else if(strData.Mid(0,5) != "00000" && strData.Mid(5,5) != "00000")
					{
						OnBnClickedRadioPulse();
						m_rdOuputKindVal = 2;

						SetDlgItemText(IDC_EDIT_OUT_ONTIME, strData.Mid(0,5));
						SetDlgItemText(IDC_EDIT_OUT_OFFTIME, strData.Mid(5,5));
					}
					UpdateData(FALSE);
				}
			}
			else if(bVal == 0x32)
			{
				((CButton *)GetDlgItem(IDC_CHECK_USE_DELAY_PULSE))->SetCheck(BST_UNCHECKED);
				OnBnClickedCheckUseDelayPulse();
			}
			else
				AfxMessageBox("It could not response the content of port setting up.",
				MB_OK | MB_ICONINFORMATION);

			EnableSettingButton(TRUE);
		}
		break;
	case ESF_MON_ADC:	//A command response processing for the ADC-Level check.
		if(bVal == 0x30)
		{
			SRHIOSetting sSettingData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sSettingData);

			SetDlgItemText(IDC_EDIT_ADC1, sSettingData.sADC.sADCData[0].cADC);
			SetDlgItemText(IDC_EDIT_ADC2, sSettingData.sADC.sADCData[1].cADC);
			SetDlgItemText(IDC_EDIT_ADC3, sSettingData.sADC.sADCData[2].cADC);
			SetDlgItemText(IDC_EDIT_ADC4, sSettingData.sADC.sADCData[3].cADC);
		}
		else
		{
			AfxMessageBox("It could not response the content of ADC setting up.",
				MB_OK | MB_ICONINFORMATION);
		}
		EnableSettingButton(TRUE);
		break;
	case ESF_SET_ADC:	//A command response processing for the ADC-Level set up.
		if(bVal == 0x30)
		{
			SRHIOSetting sSettingData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sSettingData);

			SetDlgItemText(IDC_EDIT_ADC1, sSettingData.sADC.sADCData[0].cADC);
			SetDlgItemText(IDC_EDIT_ADC2, sSettingData.sADC.sADCData[1].cADC);
			SetDlgItemText(IDC_EDIT_ADC3, sSettingData.sADC.sADCData[2].cADC);
			SetDlgItemText(IDC_EDIT_ADC4, sSettingData.sADC.sADCData[3].cADC);
		}
		else
		{
			AfxMessageBox("It could not complete an ADC setting up.",
					MB_OK | MB_ICONINFORMATION );
		}
		EnableSettingButton(TRUE);
		break;
	case ESF_SET_ENABLE:	//A command response processing for Enable/Disable of each port set up.
		if(bVal == 0x30)
		{
			SRHIOSetting sSettingData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sSettingData);

			for(int index = 0; index < 12; index++)
			{
				if(index < 4)
				{
					if(sSettingData.sADC.eModeADC[index] == EAM_LEVEL)
						m_cmbADC[index].SetCurSel(0);
					else if(sSettingData.sADC.eModeADC[index] == EAM_SWITCH)
						m_cmbADC[index].SetCurSel(1);
				}

				if(index < 10)
				{
					if(sSettingData.sOutput.isEnable[index])
						m_chkOutput[index].SetCheck(BST_CHECKED);
					else
						m_chkOutput[index].SetCheck(BST_UNCHECKED);
				}

				if(sSettingData.sInput.isEnable[index])
					m_chkInput[index].SetCheck(BST_CHECKED);
				else
					m_chkInput[index].SetCheck(BST_UNCHECKED);
			}
			((CComboBox *)GetDlgItem(IDC_COMBO_ADC_REFERENCE))->SetCurSel(sSettingData.sADC.bReference - 0x30);
		}
		else
		{
			AfxMessageBox("It could not complete an Enable setting up.",
				MB_OK | MB_ICONINFORMATION);
		}
		EnableSettingButton(TRUE);
		break;
	case ESF_MON_ENABLE:	//A command response processing for Enable/Disable of each port check.
		if(bVal == 0x30)
		{
			SRHIOSetting sSettingData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sSettingData);

			for(int index = 0; index < 12; index++)
			{
				if(index < 4)
				{
					if(sSettingData.sADC.eModeADC[index] == EAM_LEVEL)
						m_cmbADC[index].SetCurSel(0);
					else if(sSettingData.sADC.eModeADC[index] == EAM_SWITCH)
						m_cmbADC[index].SetCurSel(1);
				}

				if(index < 10)
				{
					if(sSettingData.sOutput.isEnable[index])
						m_chkOutput[index].SetCheck(BST_CHECKED);
					else
						m_chkOutput[index].SetCheck(BST_UNCHECKED);
				}

				if(sSettingData.sInput.isEnable[index])
					m_chkInput[index].SetCheck(BST_CHECKED);
				else
					m_chkInput[index].SetCheck(BST_UNCHECKED);
			}
			((CComboBox *)GetDlgItem(IDC_COMBO_ADC_REFERENCE))->SetCurSel(sSettingData.sADC.bReference -0x30);
		}
		else
		{
			AfxMessageBox("It could not response the content of Enable setting up.",
				MB_OK | MB_ICONINFORMATION);
		}
		EnableSettingButton(TRUE);
		break;
	case ESF_SET_PWR_STOP:	//A command response processing for a power failure recovery set up.
		if(bVal == 0x30)
		{
			SRHIOSetting sSettingData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sSettingData);
		
			for(int index = 0; index < 10; index++)
			{
				if(sSettingData.sOutput.isEnablePowerStop[index])
					m_chkPwrStop[index].SetCheck(BST_CHECKED);
				else
					m_chkPwrStop[index].SetCheck(BST_UNCHECKED);
			}
			
		}
		else
		{
			AfxMessageBox("It could not complete a power failure recovery setting up.",
				MB_OK | MB_ICONINFORMATION);
		}
		EnableSettingButton(TRUE);
		break;
	case ESF_MON_PWR_STOP:	//A command response processing for a power failure recovery check.
		if(bVal == 0x30)
		{
			SRHIOSetting sSettingData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sSettingData);

			for(int index = 0; index < 10; index++)
			{
				if(sSettingData.sOutput.isEnablePowerStop[index])
					m_chkPwrStop[index].SetCheck(BST_CHECKED);
				else
					m_chkPwrStop[index].SetCheck(BST_UNCHECKED);
			}

		}
		else
		{
			AfxMessageBox("It could not response the content of a power failure recovery setting up.",
			MB_OK | MB_ICONINFORMATION);
		}
		EnableSettingButton(TRUE);
		break;
	case ESF_SET_FACTROY_RESET:	//A command response processing for the Factory Reset.
		if(bVal == 0x30)
			AfxMessageBox("It could complete the Factory-Reset.",
				MB_OK | MB_ICONINFORMATION);
		else
			AfxMessageBox("It could not complete the Factory-Reset.",
			MB_OK | MB_ICONINFORMATION);
		EnableSettingButton(TRUE);
		break;
	case ESF_SET_SERIAL:	//A command response processing for the serial number set up.
		if(bVal == 0x30)
		{
			SRHIOSetting sSettingData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sSettingData);

			CString strTemp = sSettingData.cGetSerial;
            SetDlgItemText(IDC_EDIT_SERIAL2, strTemp.Mid(strTemp.GetLength() - 12, 12));
		}
		else
			AfxMessageBox("It could not complete the serial number setting up.",
			MB_OK | MB_ICONINFORMATION);
		EnableSettingButton(TRUE);
		break;
	case ESF_MON_SERIAL:	//A command response processing for the serial number check.
		if(bVal == 0x30)
		{
			SRHIOSetting sSettingData;
			((CWEB_IO_TESTDlg *)GetParent())->m_rhGetSettingData(sSettingData);

			CString strTemp = sSettingData.cGetSerial;
			SetDlgItemText(IDC_EDIT_SERIAL2, strTemp.Mid(strTemp.GetLength() - 12, 12));
		}
		else
			AfxMessageBox("It could not response the content of the serial number",
			MB_OK | MB_ICONINFORMATION);
		EnableSettingButton(TRUE);
		break;
	}

//    EnableSettingButton(TRUE);
	return TRUE;
}

void CSettingDlg::EnableSettingButton(bool bVal)
{
	GetDlgItem(IDC_BUTTON_SET_OUTPUT)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_SET_OUTPUT2)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_SET_INPUT)->EnableWindow(bVal);//ADC
	GetDlgItem(IDC_BUTTON_SET_ENABLE)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_SET_PWR_STOP)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_MONITOR_OUTPUT)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_MONITOR_OUTPUT2)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_MONITOR_INPUT)->EnableWindow(bVal);//ADC
	GetDlgItem(IDC_BUTTON_MONITOR_ENABLE)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_MONITOR_PWR_STOP)->EnableWindow(bVal);
	//GetDlgItem(IDCANCEL)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_FACTORY_RESET)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_SET_SERIAL)->EnableWindow(bVal);
	GetDlgItem(IDC_BUTTON_MONITOR_SERIAL)->EnableWindow(bVal);
}

void CSettingDlg::OnBnClickedButtonMonitorOutput()
{
	CString strMcrData, strPort , strTemp;
	GetDlgItemText(IDC_EDIT_PORT_NUM, strPort);
	
	if(atoi(strPort) <= 0 || atoi(strPort) > 10) 
	{
		AfxMessageBox("The value of port number is not valid.", MB_OK | MB_ICONINFORMATION);
		return;
	}
	
	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndGetMacro(atoi(strPort));



	EnableSettingButton(FALSE);
	m_isSetting = false;
}

void CSettingDlg::OnBnClickedButtonMonitorInput()
{
	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndGetADC();
	EnableSettingButton(FALSE);
	m_isSetting = false;
}

void CSettingDlg::OnBnClickedButtonMonitorEnable()
{
	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndGetEnable();
	EnableSettingButton(FALSE);
	m_isSetting = false;
}

void CSettingDlg::OnBnClickedButtonMonitorPwrStop()
{
	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndGetPwrStop();
	EnableSettingButton(FALSE);
	m_isSetting = false;
}

void CSettingDlg::OnBnClickedButtonFactoryReset()
{
	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetFactory();
	EnableSettingButton(FALSE);
	m_isSetting = false;
}


void CSettingDlg::OnEnChangeEditMacro()
{	
	CString strData;
	GetDlgItemText(IDC_EDIT_MACRO, strData);
	
	for(int index = 0; index < strData.GetLength(); index++)
	{
		char cTemp = strData.GetAt(index);
		
		if(cTemp != 'I' && cTemp != 'O' && cTemp != '&' && cTemp != '|' && cTemp != '!' &&
			!(cTemp <= '9' && cTemp >= '0'))
		{
			strData.Delete(index, 1);
			SetDlgItemText(IDC_EDIT_MACRO, strData);
			GetDlgItem(IDC_EDIT_MACRO)->PostMessage(WM_KEYUP, VK_SHIFT);
			GetDlgItem(IDC_EDIT_MACRO)->PostMessage(WM_KEYDOWN, VK_END);
			break;
		}
	}	
}



void CSettingDlg::OnBnClickedButtonSetmode()
{
	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetSettingMode();
	GetDlgItem(IDC_BUTTON_SETMODE)->EnableWindow(FALSE);
}

void CSettingDlg::OnBnClickedButtonRunMode()
{
	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetRunMode();
	GetDlgItem(IDC_BUTTON_RUN_MODE)->EnableWindow(FALSE);
}

void CSettingDlg::OnBnClickedRadioDelayOn()
{
	GetDlgItem(IDC_EDIT_OUT_ONTIME)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_OUT_OFFTIME)->EnableWindow(FALSE);
}

void CSettingDlg::OnBnClickedRadioDelayOff()
{
	GetDlgItem(IDC_EDIT_OUT_ONTIME)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_OUT_OFFTIME)->EnableWindow(TRUE);
}

void CSettingDlg::OnBnClickedRadioPulse()
{
	GetDlgItem(IDC_EDIT_OUT_ONTIME)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_OUT_OFFTIME)->EnableWindow(TRUE);
}


void CSettingDlg::OnBnClickedButtonMonitorOutput2()
{
	CString strMcrData, strPort , strTemp;
	GetDlgItemText(IDC_EDIT_PORT_NUM, strPort);

	if(atoi(strPort) <= 0 || atoi(strPort) > 10) 
	{
		AfxMessageBox("The value of port number is not valid.", MB_OK | MB_ICONINFORMATION);
		return;
	}

	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndGetDelayPulse(atoi(strPort));
	EnableSettingButton(FALSE);
	m_isSetting = false;
}

void CSettingDlg::OnBnClickedButtonSetOutput2()
{
	int iOnTime = GetDlgItemInt(IDC_EDIT_OUT_ONTIME);
	int iOffTime = GetDlgItemInt(IDC_EDIT_OUT_OFFTIME);
	int iPort = GetDlgItemInt(IDC_EDIT_PORT_NUM);

	if(iPort < 1 || iPort > 10) 
	{
		AfxMessageBox("The value of port number is not valid.", MB_OK | MB_ICONINFORMATION);
		return;
	}

	UpdateData();

	CString strData;
	SRHIOSetting sData;
	if(((CButton *)GetDlgItem(IDC_CHECK_USE_DELAY_PULSE))->GetCheck() == BST_CHECKED)
	{
		if(m_rdOuputKindVal == 0)
		{
			if(iOnTime < 0 || iOnTime > 1023)
			{
				AfxMessageBox("The value of On-Time is not valid.(0~1023)", MB_OK | MB_ICONINFORMATION);
				return;
			}
			strData.Format("%05d%05d", iOnTime, 0);
		}
		else if(m_rdOuputKindVal == 1)
		{
			if(iOffTime < 0 || iOffTime > 1023)
			{
				AfxMessageBox("The value of Off-Time is not valid.(0~1023)", MB_OK | MB_ICONINFORMATION);
				return;
			}
			strData.Format("%05d%05d", 0, iOffTime);
		}
		else if(m_rdOuputKindVal == 2)
		{
			if(iOnTime < 0 || iOnTime > 1023)
			{
				AfxMessageBox("The value of On-Time is not valid.(0~1023)", MB_OK | MB_ICONINFORMATION);
				return;
			}

			if(iOffTime < 0 || iOffTime > 1023)
			{
				AfxMessageBox("The value of Off-Time is not valid.(0~1023)", MB_OK | MB_ICONINFORMATION);
				return;
			}
			strData.Format("%05d%05d", iOnTime, iOffTime);
		}
		sData.sOutput.eDelayPulse = ESOF_SETTING;
		memset(sData.sOutput.cDelayPulse, '\0', sizeof(sData.sOutput.cDelayPulse));
		memcpy(sData.sOutput.cDelayPulse, strData, strData.GetLength());
	}
	else
		sData.sOutput.eDelayPulse = ESOF_CLEAR;

	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetDelayPulse(sData, iPort);
	EnableSettingButton(FALSE);
	m_isSetting = false;	
}

void CSettingDlg::OnBnClickedCheckUseMacro()
{
	if(((CButton *)GetDlgItem(IDC_CHECK_USE_MACRO))->GetCheck() == BST_CHECKED)
		GetDlgItem(IDC_EDIT_MACRO)->EnableWindow(TRUE);
	else
		GetDlgItem(IDC_EDIT_MACRO)->EnableWindow(FALSE);
}

void CSettingDlg::OnBnClickedCheckUseDelayPulse()
{
	if(((CButton *)GetDlgItem(IDC_CHECK_USE_DELAY_PULSE))->GetCheck() == BST_CHECKED)
	{
		GetDlgItem(IDC_RADIO_DELAY_ON)->EnableWindow(TRUE);
		GetDlgItem(IDC_RADIO_DELAY_OFF)->EnableWindow(TRUE);
		GetDlgItem(IDC_RADIO_PULSE)->EnableWindow(TRUE);

		UpdateData();
		switch(m_rdOuputKindVal)
		{
		case 0:
			OnBnClickedRadioDelayOn();
			break;
		case 1:
			OnBnClickedRadioDelayOff();
			break;
		case 2:
			OnBnClickedRadioPulse();
			break;
		}
	}
	else
	{
		GetDlgItem(IDC_RADIO_DELAY_ON)->EnableWindow(FALSE);
		GetDlgItem(IDC_RADIO_DELAY_OFF)->EnableWindow(FALSE);
		GetDlgItem(IDC_RADIO_PULSE)->EnableWindow(FALSE);

		GetDlgItem(IDC_EDIT_OUT_ONTIME)->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_OUT_OFFTIME)->EnableWindow(FALSE);
	}
}

void CSettingDlg::OnBnClickedButtonMonitorSerial()
{
	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndGetSerial();
	EnableSettingButton(FALSE);	
}

void CSettingDlg::OnBnClickedButtonSetSerial()
{
	CString strTemp, strData;

	GetDlgItemText(IDC_EDIT_SERIAL2, strTemp);
	strData.Format("%012s", strTemp);

	((CWEB_IO_TESTDlg *)GetParent())->m_rhSndSetSerial(strData);
	EnableSettingButton(FALSE);
}


