// AI_API_TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AI_API_Test.h"
#include "AI_API_TestDlg.h"
#include "..\..\..\Includes\ICPDAS_USBIO.h"
#include <SSTREAM>

#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;

/////////////////////////////////////////////////////////////////////////////
// CAI_API_TestDlg dialog

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

void CAI_API_TestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAI_API_TestDlg)
	DDX_Control(pDX, IDC_TXTBOARDID, m_txtUserDefinedBID);
	DDX_Control(pDX, IDC_TXTTIMEOUT, m_txtWDTTimeout);
	DDX_Control(pDX, IDC_TXTNICKNAME, m_txtNickname);
	DDX_Control(pDX, IDC_CHBXENABLEWIREDETECTION, m_chbxWireDetection);
	DDX_Control(pDX, IDC_CHBXENABLECJC, m_chbxEnableCJC);
	DDX_Control(pDX, IDC_TXTCJCOFFSET, m_txtCJCOffset);
	DDX_Control(pDX, IDC_TXTFILTERREJECTION, m_txtFilterRejection);
	DDX_Control(pDX, IDC_TXTCHMASK, m_txtChEnable);
	DDX_Control(pDX, IDC_TXTCH2, m_txtSetChCJCOffsetChannel);
	DDX_Control(pDX, IDC_TXTCH, m_txtSetTypeCodeChannel);
	DDX_Control(pDX, IDC_TXTCHOFFSET, m_txtChCJCOffset);
	DDX_Control(pDX, IDC_TXTTYPECODE, m_txtTypeCode);
	DDX_Control(pDX, IDC_LBMODULEINFO, m_lbModuleInfo);
	DDX_Control(pDX, IDOPEN, m_btnOpen);
	DDX_Control(pDX, IDCLOSE, m_btnClose);
	DDX_Control(pDX, IDC_TXTBID, m_txtBID);
	DDX_Control(pDX, IDC_CBXDID, m_cbxDID);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAI_API_TestDlg, CDialog)
	//{{AFX_MSG_MAP(CAI_API_TestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDEXIT, OnExit)
	ON_BN_CLICKED(IDOPEN, OnOpenDevice)
	ON_BN_CLICKED(IDCLOSE, OnCloseDevice)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDGETSUPTYPECODE, OnGetSupTypeCode)
	ON_BN_CLICKED(IDGETTYPECODE, OnGetChTypeCode)
	ON_BN_CLICKED(IDGETCHOFFSET, OnGetChOffset)
	ON_BN_CLICKED(IDGETCHMASK, OnGetChEnable)
	ON_BN_CLICKED(IDGETFILTERREJECTION, OnGetFilterRejection)
	ON_BN_CLICKED(IDGETCJCSETTING, OnGetCJCSetting)
	ON_BN_CLICKED(IDGETWIREDETECTION, OnGetWireDetection)
	ON_BN_CLICKED(IDSETTYPECODE, OnSetTypeCode)
	ON_BN_CLICKED(IDSETCHCJCOFFSET, OnSetChCJCOffset)
	ON_BN_CLICKED(IDSETCHENABLE, OnSetChEnable)
	ON_BN_CLICKED(IDSETFILTERREJECTION, OnSetFilterRejection)
	ON_BN_CLICKED(IDSETCJCOFFSET, OnSetCJCOffset)
	ON_BN_CLICKED(IDSETWIREDETECTION, OnSetWireDetection)
	ON_BN_CLICKED(IDREFRESHINFO, OnRefreshInfo)
	ON_BN_CLICKED(IDSETNICKNAME, OnSetNickname)
	ON_BN_CLICKED(IDSETWDTTIMEOUT, OnSetWDTTimeout)
	ON_BN_CLICKED(IDSETBOARDID, OnSetBoardID)
	ON_BN_CLICKED(IDLOADDEFAULT, OnLoadDefault)
	ON_BN_CLICKED(IDGETRESOLUTION, OnGetResolution)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAI_API_TestDlg message handlers

BOOL CAI_API_TestDlg::OnInitDialog()
{
	CDialog::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

  m_txtBID.SetWindowText("1");
  m_cbxDID.SetCurSel(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 CAI_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 CAI_API_TestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAI_API_TestDlg::OnExit() 
{
  USBIO.CloseDevice();

  CDialog::OnCancel();
}

void CAI_API_TestDlg::OnOpenDevice() 
{
  CString szBID;
  DWORD dwBID;
  int iSelIdx;
  int iErrCode;
  
  m_txtBID.GetWindowText(szBID);
  dwBID = _tcstoul(szBID, NULL, 10);

  iSelIdx = m_cbxDID.GetCurSel();

  USBIO.SetCommTimeout(500);
  if(ERR_NO_ERR != (iErrCode = USBIO.OpenDevice(USBIO_MINPID+iSelIdx+19, (BYTE)dwBID)))
  {
    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_lbModuleInfo.AddString(szItemString);
    
    USBIO.GetFwVer(&wFWVer);
    sprintf(szItemString, "Version: %d", wFWVer);
    m_lbModuleInfo.AddString(szItemString);
    
    USBIO.GetDeviceNickName(byDeviceNickName);
    sprintf(szItemString, "Description: ");
    for(iIdx = 0; iIdx < USBIO_NICKNAME_LENGTH; iIdx++)
      sprintf(szItemString + strlen(szItemString), "%c", byDeviceNickName[iIdx]);
    m_lbModuleInfo.AddString(szItemString);
    
    USBIO.GetDeviceSN(byDeviceSN);
    sprintf(szItemString, "SN: ");
    for(iIdx = 0; iIdx < USBIO_SN_LENGTH; iIdx++)
      sprintf(szItemString + strlen(szItemString), "%C", byDeviceSN[iIdx]);
    m_lbModuleInfo.AddString(szItemString);

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

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

void CAI_API_TestDlg::OnCloseDevice() 
{
  int iErrCode;
  
  m_lbModuleInfo.ResetContent();

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

void CAI_API_TestDlg::OnClose() 
{
  USBIO.CloseDevice();

	CDialog::OnClose();
}

void CAI_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.AI_GetTotalSupportType(&byTotalSupType)))
  {
    bRet = false;
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_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 CAI_API_TestDlg::OnGetChTypeCode() 
{
  int iErrCode;
  BYTE byChTypeCode[USBIO_AI_MAX_CHANNEL];
  char szMessage[300];

  if(ERR_NO_ERR != (iErrCode = USBIO.AI_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_byAITotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: 0x%02X\n", iIdx, byChTypeCode[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CAI_API_TestDlg::OnGetChOffset() 
{
  int iErrCode;
  float fChCJCOffset[USBIO_AI_MAX_CHANNEL];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_GetChCJCOffset(fChCJCOffset)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module channel CJC offset:\n");
    for(int iIdx = 0; iIdx < m_byAITotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: %f\n", iIdx, fChCJCOffset[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CAI_API_TestDlg::OnGetChEnable() 
{
  int iErrCode;
  BYTE byChEnable[(USBIO_AI_MAX_CHANNEL + 7) / 8];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_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 CAI_API_TestDlg::OnGetFilterRejection() 
{
  int iErrCode;
  BYTE byFilterRejection;
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_GetFilterRejection(&byFilterRejection)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    sprintf(szMessage, "Module filter rejection: 0x%02X", byFilterRejection);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CAI_API_TestDlg::OnGetCJCSetting() 
{
  BYTE byCJCEnable;
  float fCJCOffset;
  float fCJCValue;
  char szMessage[300];
  int iErrCode;
  BOOL bRet = true;
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_GetCJCEnable(&byCJCEnable)))
  {
    bRet = false;
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_GetCJCOffset(&fCJCOffset)))
  {
    bRet = false;
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_ReadCJCValue(&fCJCValue)))
  {
    bRet = false;
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  
  if(bRet)
  {
    if(byCJCEnable == 1)
    {
      sprintf(szMessage, "Enable CJC offset.");
    }
    else
    {
      sprintf(szMessage, "Disable CJC offset. ");
      sprintf(szMessage + strlen(szMessage), "CJC offset is %f", fCJCOffset);
    }
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);

    sprintf(szMessage, "CJC value is %f", fCJCValue);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CAI_API_TestDlg::OnGetWireDetection() 
{
  int iErrCode;
  BYTE byWireDetectEnable;
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_GetWireDetectEnable(&byWireDetectEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    if(byWireDetectEnable == 1)
      sprintf(szMessage, "Module wire detection enable");
    else
      sprintf(szMessage, "Module wire detection disable");
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}

void CAI_API_TestDlg::OnSetTypeCode() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwChToSet;
  DWORD dwTypeCode;
  CString szChToSet, szTypeCode;
  
  m_txtTypeCode.GetWindowText(szTypeCode);
  dwTypeCode = _tcstoul(szTypeCode, NULL, 16);
  
  m_txtSetTypeCodeChannel.GetWindowText(szChToSet);
  dwChToSet = _tcstoul(szChToSet, NULL, 10);

  if(ERR_NO_ERR != (iErrCode = USBIO.AI_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 CAI_API_TestDlg::OnSetChCJCOffset() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwChToSet;
  float fChCJCOffset;
  CString szChToSet, szChCJCOffset;
  
  m_txtChCJCOffset.GetWindowText(szChCJCOffset);
  sscanf(szChCJCOffset, "%f", &fChCJCOffset);
  
  m_txtSetChCJCOffsetChannel.GetWindowText(szChToSet);
  dwChToSet = _tcstoul(szChToSet, NULL, 10);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_SetChCJCOffset((BYTE)dwChToSet, fChCJCOffset)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }
}

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

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

void CAI_API_TestDlg::OnSetFilterRejection() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwFilterRejection;
  CString szFilterRejection;
  
  m_txtFilterRejection.GetWindowText(szFilterRejection);
  dwFilterRejection = _tcstoul(szFilterRejection, NULL, 10);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_SetFilterRejection((BYTE)dwFilterRejection)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }
}

void CAI_API_TestDlg::OnSetCJCOffset() 
{
  int iErrCode;
  char szMessage[300];
  BYTE byCJCEnable;
  float fCJCOffset;
  CString szFilterRejection, szCJCOffset;
  BOOL bRet = true;
  
  if(m_chbxEnableCJC.GetCheck() == BST_CHECKED)
    byCJCEnable = 1;
  else
    byCJCEnable = 0;

  m_txtCJCOffset.GetWindowText(szCJCOffset);
  sscanf(szCJCOffset, "%f", &fCJCOffset);
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_SetCJCEnable(byCJCEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_SetCJCOffset(fCJCOffset)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }
}

void CAI_API_TestDlg::OnSetWireDetection() 
{
  int iErrCode;
  char szMessage[300];
  BYTE byWireDetectEnable;
  CString szFilterRejection;

  if(m_chbxWireDetection.GetCheck() == BST_CHECKED)
    byWireDetectEnable = 1;
  else
    byWireDetectEnable = 0;
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_SetWireDetectEnable(byWireDetectEnable)))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
  else
  {
    MessageBox("Success.", "Error", MB_ICONINFORMATION | MB_OK);
  }
}

void CAI_API_TestDlg::OnRefreshInfo() 
{
  int iErrCode;
  char szMessage[300];

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

void CAI_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 CAI_API_TestDlg::OnSetWDTTimeout() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwWDTTimeout;
  CString szWDTTimeout;
  
  m_txtWDTTimeout.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 CAI_API_TestDlg::OnSetBoardID() 
{
  int iErrCode;
  char szMessage[300];
  DWORD dwBoardID;
  CString szBoardID;
  
  m_txtUserDefinedBID.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 CAI_API_TestDlg::OnLoadDefault() 
{
  int iErrCode;
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.LoadDefault()))
  {
    sprintf(szMessage, "[%d]", iErrCode);
    
    MessageBox(szMessage, "Error", MB_ICONERROR | MB_OK);
  }
}

void CAI_API_TestDlg::OnGetResolution() 
{	
  int iErrCode;
  BYTE byResolution[USBIO_AI_MAX_CHANNEL];
  char szMessage[300];
  
  if(ERR_NO_ERR != (iErrCode = USBIO.AI_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_byAITotal; iIdx++)
      sprintf(szMessage + strlen(szMessage), "Ch%d: %d\n", iIdx, byResolution[iIdx]);
    
    MessageBox(szMessage, "Information", MB_ICONINFORMATION | MB_OK);
  }
}
