// AO_API_TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AO_API_Test.h"
#include "AO_API_TestDlg.h"
#include "..\..\..\Includes\ICPDAS_USBIO.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

ICPDAS_USBIO USBIO;
BYTE m_byDITotal;
BYTE m_byDOTotal;
BYTE m_byAITotal;
BYTE m_byAOTotal;
BYTE m_byPITotal;
BYTE m_byPOTotal;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAO_API_TestDlg dialog

CAO_API_TestDlg::CAO_API_TestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAO_API_TestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAO_API_TestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAO_API_TestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAO_API_TestDlg)
	DDX_Control(pDX, IDC_TXTPWRONCH_FLOAT, m_txtPwrOnCh_Float);
	DDX_Control(pDX, IDC_TXTPWRONVALUE_FLOAT, m_btnPwrOnValue_Float);
	DDX_Control(pDX, IDC_TXTSAFETYCH_FLOAT, m_txtSafetyCh_Float);
	DDX_Control(pDX, IDC_TXTSAFETYVALUE_FLOAT, m_txtSafetyValue_Float);
	DDX_Control(pDX, IDSETPWRONVALUE_FLOAT, m_btnSetPwrOnValue_Float);
	DDX_Control(pDX, IDSETSAFETYVALUE_FLOAT, m_btnSetSafetyValue_Float);
	DDX_Control(pDX, ID_BTNENABLE, m_btnEnable);
	DDX_Control(pDX, ID_BTNDISABLE, m_btnDisable);
	DDX_Control(pDX, IDC_TXTTYPEVALUE, m_txtTypeCode);
	DDX_Control(pDX, IDSETWDT, m_btnSetWDT);
	DDX_Control(pDX, IDSETBOARDID, m_btnSetBoardID);
	DDX_Control(pDX, IDSETTYPE, m_btnSetType);
	DDX_Control(pDX, IDSETSAFETYVALUE, m_btnSafetyValue);
	DDX_Control(pDX, IDSETSAFETYENABLE, m_btnSetSafetyEnable);
	DDX_Control(pDX, IDSETPWRONENABLE, m_btnSetPwrOnEnable);
	DDX_Control(pDX, IDSETPWRONVALUE, m_btnSetPwrOnValue);
	DDX_Control(pDX, IDC_TXTWDT, m_txtWDT);
	DDX_Control(pDX, IDC_TXTTYPECH, m_txtTypeCh);
	DDX_Control(pDX, IDC_TXTSETBOARDID, m_txtSetBoardID);
	DDX_Control(pDX, IDC_TXTSAFETYVALUE, m_txtSafetyValue);
	DDX_Control(pDX, IDC_TXTSAFETYENABLE, m_txtSafetyEnable);
	DDX_Control(pDX, IDC_TXTSAFETYCH, m_txtSafetyCh);
	DDX_Control(pDX, IDC_TXTPWRONVALUE, m_txtPwrOnValue);
	DDX_Control(pDX, IDC_TXTPWRONENABLECH, m_txtPwrOnEnableCh);
	DDX_Control(pDX, IDC_TXTPWRONCH, m_txtPwrOnCh);
	DDX_Control(pDX, IDC_TXTNICKNAME, m_txtNickname);
	DDX_Control(pDX, IDC_TXTCHENABLE, m_txtChEnable);
	DDX_Control(pDX, IDC_TXTBOARDID, m_txtBoardID);
	DDX_Control(pDX, IDC_LBINFORMATION, m_lbInformation);
	DDX_Control(pDX, IDC_CBXDEVID, m_cbxDeviceID);
	DDX_Control(pDX, IDC_BTNREFRESH, m_btnRefresh);
	DDX_Control(pDX, IDC_BTNOPEN, m_btnOpen);
	DDX_Control(pDX, IDC_BTNDEFAULT, m_btnDefault);
	DDX_Control(pDX, IDC_BTNCLOSE, m_btnClose);
	DDX_Control(pDX, IDSETNICKNAME, m_btnSetNickname);
	DDX_Control(pDX, IDSETCHENABLE, m_btnSetChEnable);
	DDX_Control(pDX, IDGETCHENABLE, m_btnGetChEnable);
	DDX_Control(pDX, IDGETPWRONENABLE, m_btnGetPwrOnEnable);
	DDX_Control(pDX, IDGETPWRONVLAUE, m_btnGetPwrOnValue);
	DDX_Control(pDX, IDGETSAFETYENABLE, m_btnGetSafetyEnable);
	DDX_Control(pDX, IDGETRESOLUTION, m_btnGetResolution);
	DDX_Control(pDX, IDGETSAFETYVALUE, m_btnGetSafetyValue);
	DDX_Control(pDX, IDGETSUPTYPECODE, m_btnGetSupportCode);
	DDX_Control(pDX, IDGETTYPECODE, m_btnGetTypeCode);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAO_API_TestDlg, CDialog)
	//{{AFX_MSG_MAP(CAO_API_TestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopen)
	ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclose)
	ON_BN_CLICKED(IDC_BTNDEFAULT, OnBtndefault)
	ON_BN_CLICKED(IDC_BTNREFRESH, OnBtnrefresh)
	ON_BN_CLICKED(IDSETNICKNAME, OnSetnickname)
	ON_BN_CLICKED(IDSETWDT, OnSetwdt)
	ON_BN_CLICKED(IDSETBOARDID, OnSetboardid)
	ON_BN_CLICKED(IDGETSUPTYPECODE, OnGetsuptypecode)
	ON_BN_CLICKED(IDGETTYPECODE, OnGettypecode)
	ON_BN_CLICKED(IDGETCHENABLE, OnGetchenable)
	ON_BN_CLICKED(IDGETRESOLUTION, OnGetresolution)
	ON_BN_CLICKED(IDGETPWRONENABLE, OnGetpwronenable)
	ON_BN_CLICKED(IDGETPWRONVLAUE, OnGetpwronvlaue)
	ON_BN_CLICKED(IDGETSAFETYENABLE, OnGetsafetyenable)
	ON_BN_CLICKED(IDGETSAFETYVALUE, OnGetsafetyvalue)
	ON_BN_CLICKED(IDSETTYPE, OnSettype)
	ON_BN_CLICKED(IDSETCHENABLE, OnSetchenable)
	ON_BN_CLICKED(ID_BTNENABLE, OnEnable)
	ON_BN_CLICKED(ID_BTNDISABLE, OnBtndisable)
	ON_BN_CLICKED(IDSETPWRONENABLE, OnSetpwronenable)
	ON_BN_CLICKED(IDSETSAFETYENABLE, OnSetsafetyenable)
	ON_BN_CLICKED(IDSETPWRONVALUE, OnSetpwronvalue)
	ON_BN_CLICKED(IDSETSAFETYVALUE, OnSetsafetyvalue)
	ON_BN_CLICKED(IDSETPWRONVALUE_FLOAT, OnSetPwrOnValueFloat)
	ON_BN_CLICKED(IDSETSAFETYVALUE_FLOAT, OnSetSafetyValueFloat)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAO_API_TestDlg message handlers

BOOL CAO_API_TestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here
	m_txtBoardID.SetWindowText("1");
	m_cbxDeviceID.SetCurSel(0);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAO_API_TestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CAO_API_TestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAO_API_TestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAO_API_TestDlg::OnBtnopen() 
{
  CString szBID;
  DWORD dwBID;
  int iSelIdx;
  int iErrCode;
  
  m_txtBoardID.GetWindowText(szBID);
  dwBID = _tcstoul(szBID, NULL, 10);

  iSelIdx = m_cbxDeviceID.GetCurSel();

  USBIO.SetCommTimeout(500);
  if(ERR_NO_ERR != (iErrCode = USBIO.OpenDevice(USBIO_MINPID+iSelIdx+26, 1)))// 2026
  {
    char szMessage[10];
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    char szItemString[200];
    DWORD dwSoftWDTTimeout;
    WORD wFWVer;
    BYTE byDeviceSN[USBIO_SN_LENGTH];
    BYTE byDeviceNickName[USBIO_NICKNAME_LENGTH];
    BYTE bySupIOMask;
    int iIdx;

    USBIO.GetSoftWDTTimeout(&dwSoftWDTTimeout);
    sprintf(szItemString, "SoftWDT: %d", dwSoftWDTTimeout);
    m_lbInformation.AddString(szItemString);
    
    USBIO.GetFwVer(&wFWVer);
    sprintf(szItemString, "Version: %d", wFWVer);
    m_lbInformation.AddString(szItemString);
    
    USBIO.GetDeviceNickName(byDeviceNickName);
    sprintf(szItemString, "Description: ");
    for(iIdx = 0; iIdx < USBIO_NICKNAME_LENGTH; iIdx++)
      sprintf(szItemString + strlen(szItemString), "%c", byDeviceNickName[iIdx]);
    m_lbInformation.AddString(szItemString);
    
    USBIO.GetDeviceSN(byDeviceSN);
    sprintf(szItemString, "SN: ");
    for(iIdx = 0; iIdx < USBIO_SN_LENGTH; iIdx++)
      sprintf(szItemString + strlen(szItemString), "%C", byDeviceSN[iIdx]);
    m_lbInformation.AddString(szItemString);

    USBIO.GetSupportIOMask(&bySupIOMask);
    sprintf(szItemString, "Support IO Mask: 0x%02X", bySupIOMask);
    m_lbInformation.AddString(szItemString);

    if(SUPPORT_DI(bySupIOMask))
    {
      USBIO.GetDITotal(&m_byDITotal);
      sprintf(szItemString, "DI channels: %d", m_byDITotal);
      m_lbInformation.AddString(szItemString);
    }
    
    if(SUPPORT_DO(bySupIOMask))
    {
      USBIO.GetDITotal(&m_byDOTotal);
      sprintf(szItemString, "DO channels: %d", m_byDOTotal);
      m_lbInformation.AddString(szItemString);
    }
    
    if(SUPPORT_AI(bySupIOMask))
    {
      USBIO.GetAITotal(&m_byAITotal);
      sprintf(szItemString, "AI channels: %d", m_byAITotal);
      m_lbInformation.AddString(szItemString);
    }
    
    if(SUPPORT_AO(bySupIOMask))
    {
      USBIO.GetAOTotal(&m_byAOTotal);
      sprintf(szItemString, "AO channels: %d", m_byAOTotal);
      m_lbInformation.AddString(szItemString);
    }
    
    if(SUPPORT_PI(bySupIOMask))
    {
      USBIO.GetPITotal(&m_byPITotal);
      sprintf(szItemString, "PI channels: %d", m_byPITotal);
      m_lbInformation.AddString(szItemString);
    }
    
    if(SUPPORT_PO(bySupIOMask))
    {
      USBIO.GetPOTotal(&m_byPOTotal);
      sprintf(szItemString, "PO channels: %d", m_byPOTotal);
      m_lbInformation.AddString(szItemString);
    }
  }
	
}

void CAO_API_TestDlg::OnBtnclose() 
{
  int iErrCode;
  
  m_lbInformation.ResetContent();

  if(ERR_NO_ERR != (iErrCode = USBIO.CloseDevice()))
  {
    char szMessage[10];
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CAO_API_TestDlg::OnBtndefault() 
{
  int iErrCode;
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.LoadDefault()))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }	
}

void CAO_API_TestDlg::OnBtnrefresh() 
{
  int iErrCode;
  char szMessage[300];

  if(ERR_NO_ERR != (iErrCode = USBIO.RefreshDeviceInfo()))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }	
}

void CAO_API_TestDlg::OnSetnickname() 
{
  int iErrCode;
  char szMessage[300];
  BYTE byNickname[USBIO_NICKNAME_LENGTH];
  CString szNickname;

  m_txtNickname.GetWindowText(szNickname);
  memset(byNickname, 32, USBIO_NICKNAME_LENGTH);
  sscanf(szNickname, "%s", byNickname);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.SetDeviceNickName(byNickname)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }	
}

void CAO_API_TestDlg::OnSetwdt() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwWDTTimeout;
  CString szWDTTimeout;
  
  m_txtWDT.GetWindowText(szWDTTimeout);
  dwWDTTimeout = _tcstoul(szWDTTimeout, NULL, 10);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.SetSoftWDTTimeout(dwWDTTimeout)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }	
}

void CAO_API_TestDlg::OnSetboardid() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwBoardID;
  CString szBoardID;
  
  m_txtSetBoardID.GetWindowText(szBoardID);
  dwBoardID = _tcstoul(szBoardID, NULL, 10);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.SetUserDefinedBoardID(dwBoardID)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }	
}

void CAO_API_TestDlg::OnGetsuptypecode() 
{
  BYTE byTotalSupType;
  BYTE bySupTypeCode[USBIO_MAX_SUPPORT_TYPE];
  char szMessage[300];
  int iErrCode;
  BOOL bRet = true;

  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetTotalSupportType(&byTotalSupType)))
  {
    bRet = false;
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }

  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetSupportTypeCode(bySupTypeCode)))
  {
    bRet = false;
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }

  if(bRet)
  {
    sprintf(szMessage, "%d supported types for this module.\n", byTotalSupType);
    if(byTotalSupType > 0)
    {
      sprintf(szMessage + strlen(szMessage), "Support types is:\n");
      for(int iIdx = 0; iIdx < byTotalSupType; iIdx++)
        sprintf(szMessage + strlen(szMessage), "0x%02X\n", bySupTypeCode[iIdx]);
    }

    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }	
}

void CAO_API_TestDlg::OnGettypecode() 
{
  int iErrCode;
  BYTE byChTypeCode[USBIO_AO_MAX_CHANNEL];
  char szMessage[300];

  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetTypeCode(byChTypeCode)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module current channel type:\n");
    for(int iIdx = 0; iIdx < m_byAOTotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: 0x%02X\n", iIdx, byChTypeCode[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }	
}

void CAO_API_TestDlg::OnGetchenable() 
{
  int iErrCode;
  BYTE byChEnable[(USBIO_AO_MAX_CHANNEL + 7) / 8];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetChEnable(byChEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module channel enable:\n0x");
    for(int iIdx = 0; iIdx < (m_byAITotal + 7) / 8; iIdx++)
      sprintf(szMessage + strlen(szMessage), "%02X", byChEnable[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }	
}

void CAO_API_TestDlg::OnGetresolution() 
{
  int iErrCode;
  BYTE byResolution[USBIO_AO_MAX_CHANNEL];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetResolution(byResolution)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module current channel resolution:\n");
    for(int iIdx = 0; iIdx < m_byAOTotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: %d\n", iIdx, byResolution[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }	
}

void CAO_API_TestDlg::OnGetpwronenable() 
{
  int iErrCode;
  BYTE byPwrOnEnable[USBIO_AO_MAX_CHANNEL];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetPowerOnEnable(byPwrOnEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module power-on enable:\n");
    for(int iIdx = 0; iIdx < m_byAOTotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: 0x%02X\n", iIdx, byPwrOnEnable[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }	
}

void CAO_API_TestDlg::OnGetpwronvlaue() 
{
  int iErrCode;
  DWORD dwPwrOnValue[USBIO_AO_MAX_CHANNEL];
  float fPwrOnValue[USBIO_AO_MAX_CHANNEL];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetPowerOnValue(dwPwrOnValue)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module power-on value:\n");
    for(int iIdx = 0; iIdx < m_byAOTotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: 0x%04X\n", iIdx, dwPwrOnValue[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }

  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetPowerOnValue(fPwrOnValue)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module power-on value(float):\n");
    for(int iIdx = 0; iIdx < m_byAOTotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: %04f\n", iIdx, fPwrOnValue[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CAO_API_TestDlg::OnGetsafetyenable() 
{
  int iErrCode;
  BYTE bySafetyEnable[(USBIO_AO_MAX_CHANNEL + 7) >> 3];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetSafetyEnable(bySafetyEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module safety enable:\n0x");
    for(int iIdx = 0; iIdx < (m_byAOTotal + 7) >> 3; iIdx++)
      sprintf(szMessage + strlen(szMessage), "%02X", bySafetyEnable[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }	
}

void CAO_API_TestDlg::OnGetsafetyvalue() 
{
  int iErrCode;
  DWORD dwSafetyValue[USBIO_AO_MAX_CHANNEL];
  float fSafetyValue[USBIO_AO_MAX_CHANNEL];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetSafetyValue(dwSafetyValue)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module safety value:\n");
    for(int iIdx = 0; iIdx < m_byAOTotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: 0x%04X\n", iIdx, dwSafetyValue[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }

  if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetSafetyValue(fSafetyValue)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module safety value(float):\n");
    for(int iIdx = 0; iIdx < m_byAOTotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: %04f\n", iIdx, fSafetyValue[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CAO_API_TestDlg::OnSettype() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwChToSet;
  DWORD dwTypeCode;
  BYTE byTypeCodes[USBIO_AO_MAX_CHANNEL];
  CString szChToSet, szTypeCode;
  
  m_txtTypeCode.GetWindowText(szTypeCode);
  dwTypeCode = _tcstoul(szTypeCode, NULL, 16);
  
  m_txtTypeCh.GetWindowText(szChToSet);
  dwChToSet = _tcstoul(szChToSet, NULL, 10);
 
  if(szChToSet == "")
  {
	for(int idx = 0; idx < m_byAOTotal; idx ++)
		byTypeCodes[idx] = (BYTE)dwTypeCode;

	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetTypeCode(byTypeCodes)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);

	}
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}

  }
  else
  {
	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetTypeCode((BYTE)dwChToSet, (BYTE)dwTypeCode)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
	 }
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}	
  }

}

void CAO_API_TestDlg::OnSetchenable() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwChEnable;
  CString szChEnable;
  BYTE byChEnable[(USBIO_AO_MAX_CHANNEL + 7) / 8];
  int iIdx;
  
  m_txtChEnable.GetWindowText(szChEnable);
  dwChEnable = _tcstoul(szChEnable, NULL, 16);

  for(iIdx = 0; iIdx < (USBIO_AO_MAX_CHANNEL + 7) / 8; iIdx++)
    byChEnable[iIdx] = (BYTE)((dwChEnable >> (iIdx * 8)) & 0xFF);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetChEnable(byChEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }	
}

void CAO_API_TestDlg::OnEnable() 
{
  m_btnEnable.EnableWindow(false);
  m_btnDisable.EnableWindow(true);
}

void CAO_API_TestDlg::OnBtndisable() 
{
  m_btnEnable.EnableWindow(true);
  m_btnDisable.EnableWindow(false);
}

void CAO_API_TestDlg::OnSetpwronenable() 
{
	bool blEnabled;
	char szMessage[300];
	BYTE byPwrOnEnable[USBIO_AO_MAX_CHANNEL];
	CString szChToSet;
	BYTE byCh;
	int iErrCode;
	
	
	blEnabled = m_btnDisable.IsWindowEnabled();
	m_txtPwrOnEnableCh.GetWindowText(szChToSet);

	if(szChToSet == "")
	{
		for(int idx = 0; idx < m_byAOTotal; idx ++)
			byPwrOnEnable[idx] = blEnabled;

		if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetPowerOnEnable(byPwrOnEnable)))
		{
			sprintf(szMessage, "[%d]", iErrCode);
    
			MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
		}
		else
		{
			MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
		}	
	}
	else
	{
		byCh = _tcstoul(szChToSet, NULL, 10);

		if(ERR_NO_ERR != (iErrCode = USBIO.AO_GetPowerOnEnable(byPwrOnEnable)))
		{
			sprintf(szMessage, "[%d]", iErrCode);
    
			MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
		}
		
		byPwrOnEnable[byCh] = (BYTE)blEnabled;

		if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetPowerOnEnable(byPwrOnEnable)))
		{
			sprintf(szMessage, "[%d]", iErrCode);
    
			MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
		}
		else
		{
			MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
		}	
	}
	
}

void CAO_API_TestDlg::OnSetsafetyenable() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwSafetyEnable;
  CString szSafetyEnable;
  BYTE bySafetyEnable[(USBIO_AO_MAX_CHANNEL + 7) / 8];
  int iIdx;
  
  m_txtSafetyEnable.GetWindowText(szSafetyEnable);
  dwSafetyEnable = _tcstoul(szSafetyEnable, NULL, 16);

  for(iIdx = 0; iIdx < (USBIO_AO_MAX_CHANNEL + 7) / 8; iIdx++)
    bySafetyEnable[iIdx] = (BYTE)((dwSafetyEnable >> (iIdx * 8)) & 0xFF);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetSafetyEnable(bySafetyEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }	
}

void CAO_API_TestDlg::OnSetpwronvalue() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwChToSet;
  DWORD dwPwrOnValue;
  DWORD dwPwrOnValues[USBIO_AO_MAX_CHANNEL];
  CString szChToSet, szPwrOnValue;
  
  m_txtPwrOnValue.GetWindowText(szPwrOnValue);
  dwPwrOnValue = _tcstoul(szPwrOnValue, NULL, 16);
  
  m_txtPwrOnCh.GetWindowText(szChToSet);
  dwChToSet = _tcstoul(szChToSet, NULL, 10);
 
  if(szChToSet == "")
  {
	for(int idx = 0; idx < m_byAOTotal; idx ++)
		dwPwrOnValues[idx] = dwPwrOnValue;

	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetPowerOnValue(dwPwrOnValues)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);

	}
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}

  }
  else
  {
	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetPowerOnValue((BYTE)dwChToSet, dwPwrOnValue)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
	 }
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}	
  }	
}

void CAO_API_TestDlg::OnSetsafetyvalue() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwChToSet;
  DWORD dwSafetyValue;
  DWORD dwSafetyValues[USBIO_AO_MAX_CHANNEL];
  CString szChToSet, szSafetyValue;
  
  m_txtSafetyValue.GetWindowText(szSafetyValue);
  dwSafetyValue = _tcstoul(szSafetyValue, NULL, 16);
  
  m_txtSafetyCh.GetWindowText(szChToSet);
  dwChToSet = _tcstoul(szChToSet, NULL, 10);
 
  if(szChToSet == "")
  {
	for(int idx = 0; idx < m_byAOTotal; idx ++)
		dwSafetyValues[idx] = dwSafetyValue;

	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetSafetyValue(dwSafetyValues)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);

	}
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}

  }
  else
  {
	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetSafetyValue((BYTE)dwChToSet, dwSafetyValue)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
	 }
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}	
  }	
}

void CAO_API_TestDlg::OnSetPwrOnValueFloat() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwChToSet;
  float fPwrOnValue;
  float fPwrOnValues[USBIO_AO_MAX_CHANNEL];
  CString szChToSet, szPwrOnValue;
  
  m_btnPwrOnValue_Float.GetWindowText(szPwrOnValue);
  fPwrOnValue = _tcstoul(szPwrOnValue, NULL, 10);
  
  m_txtPwrOnCh_Float.GetWindowText(szChToSet);
  dwChToSet = _tcstoul(szChToSet, NULL, 10);
 
  if(szChToSet == "")
  {
	for(int idx = 0; idx < m_byAOTotal; idx ++)
		fPwrOnValues[idx] = fPwrOnValue;

	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetPowerOnValue(fPwrOnValues)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);

	}
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}

  }
  else
  {
	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetPowerOnValue((BYTE)dwChToSet, fPwrOnValue)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
	 }
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}	
  }
}

void CAO_API_TestDlg::OnSetSafetyValueFloat() 
{
  	int iErrCode;
  char szMessage[300];
  DWORD dwChToSet;
  float fSafetyValue;
  float fSafetyValues[USBIO_AO_MAX_CHANNEL];
  CString szChToSet, szSafetyValue;
  
  m_txtSafetyValue_Float.GetWindowText(szSafetyValue);
  fSafetyValue = _tcstoul(szSafetyValue, NULL, 10);
  
  m_txtSafetyCh_Float.GetWindowText(szChToSet);
  dwChToSet = _tcstoul(szChToSet, NULL, 10);
 
  if(szChToSet == "")
  {
	for(int idx = 0; idx < m_byAOTotal; idx ++)
		fSafetyValues[idx] = fSafetyValue;

	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetSafetyValue(fSafetyValues)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);

	}
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}

  }
  else
  {
	if(ERR_NO_ERR != (iErrCode = USBIO.AO_SetSafetyValue((BYTE)dwChToSet, fSafetyValue)))
	{
		sprintf(szMessage, "[%d]", iErrCode);
    
		MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
	 }
	else
	{
		MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
	}	
  }
}
