www.pudn.com > DalsaNetlink.rar > CyDeviceDlg.cpp


// CyDeviceDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include  
#include "CyApp.h" 
#include "resource.h" 
#include "CyDeviceDlg.h" 
#include "CyAssert.h" 
#include "CyDeviceFinder.h" 
#include "CyCameraRegistry.h" 
#include "CyCameraInterface.h" 
#include "CyXMLDocument.h" 
#include "CyPropertySheet.h" 
#include "CyMulticastingDialog.h" 
#include "CyTimeoutDialog.h" 
#include "CyAcquisitionPage.h" 
#include "CySerialConfigDialog.h" 
#include  
 
#include  
#include  
using namespace std; 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
 
// Internal Constants 
///////////////////////////////////////////////////////////////////////////// 
 
// ----- Formats ----- 
static const char	*CONFIG_EXT			= "XML"; 
static const char	*CONFIG_FILE_TYPE	= "Configuration Files (*.xml)|*.xml|All files (*.*)|*.*||"; 
 
 
// Utilities 
///////////////////////////////////////////////////////////////////////////// 
static int FindStringInCombo( CComboBox& aBox, const std::string& aString ) 
{ 
    for ( int lIndex = 0; lIndex < aBox.GetCount(); ++lIndex ) 
    { 
        // Get the string at the current index in the combo box 
        CString lString; 
        aBox.GetLBText( lIndex, lString ); 
 
        // compare with the wanted string, if it is a match, then 
        // return the index 
        if ( aString == (LPCTSTR) lString ) 
            return lIndex; 
    } 
 
    return -1; 
} 
 
static unsigned long GetConnectFlag( CyDeviceDlg& aDialog, const CyConfig& aConfig ) 
{ 
    char lAddressIP[ 256 ]; 
    char lAddressMAC[ 256 ]; 
    int lA, lB, lC, lD, lE, lF; 
    unsigned long lResult = 0; 
 
    // We will return CyDevice::FLAG_SET_IP_ON_CONNECT if the IP and the MAC are 
    // set in the config and that they are valid, as far as we know 
 
    if ( ( aDialog.mForceIPOnConnect ) && 
         ( aConfig.GetAddress( lAddressIP, sizeof( lAddressIP ) ) == CY_RESULT_OK ) && 
         ( aConfig.GetMACAddress( lAddressMAC, sizeof( lAddressMAC ) ) == CY_RESULT_OK ) && 
         ( sscanf( lAddressIP, "[%d.%d.%d.%d]", &lA, &lB, &lC, &lD ) == 4 ) && 
         ( sscanf( lAddressMAC, "%02X:%02X:%02X:%02X:%02X:%02X", &lA, &lB, &lC, &lD, &lE, &lF ) == 6 ) ) 
    { 
        lResult = CyDevice::FLAG_SET_IP_ON_CONNECT; 
    } 
 
    reinterpret_cast( AfxGetApp() )->mMulticastMasterNoData = false; 
    if ( aDialog.mMulticastEnabled && 
         aDialog.mMulticastMaster && 
         aDialog.mMulticastNoData ) 
    { 
        reinterpret_cast( AfxGetApp() )->mMulticastMasterNoData = true; 
        lResult |= CyDevice::FLAG_MULTICAST_NO_DATA; 
    } 
          
    return lResult; 
} 
 
static void SetMACToControls( CyDeviceDlg& aDialog, const char * aMAC ) 
{ 
    int lA, lB, lC, lD, lE, lF; 
    if ( ( sscanf( aMAC, "%02X:%02X:%02X:%02X:%02X:%02X", &lA, &lB, &lC, &lD, &lE, &lF ) == 6 ) || 
         ( sscanf( aMAC, "%02X-%02X-%02X-%02X-%02X-%02X", &lA, &lB, &lC, &lD, &lE, &lF ) == 6 ) ) 
    { 
        aDialog.mDeviceMAC1.Format( "%02X", lA ); 
        aDialog.mDeviceMAC2.Format( "%02X", lB ); 
        aDialog.mDeviceMAC3.Format( "%02X", lC ); 
        aDialog.mDeviceMAC4.Format( "%02X", lD ); 
        aDialog.mDeviceMAC5.Format( "%02X", lE ); 
        aDialog.mDeviceMAC6.Format( "%02X", lF ); 
    } 
} 
 
static const char* GetMACFromControls( CyDeviceDlg& aDialog ) 
{ 
    static CyAdapterID sID; 
    sID = CyAdapterID(); 
    CString lMAC1, lMAC2, lMAC3, lMAC4, lMAC5, lMAC6; 
 
    aDialog.GetDlgItem( IDC_DEVICE_MAC1 )->GetWindowText( lMAC1 ); 
    aDialog.GetDlgItem( IDC_DEVICE_MAC2 )->GetWindowText( lMAC2 ); 
    aDialog.GetDlgItem( IDC_DEVICE_MAC3 )->GetWindowText( lMAC3 ); 
    aDialog.GetDlgItem( IDC_DEVICE_MAC4 )->GetWindowText( lMAC4 ); 
    aDialog.GetDlgItem( IDC_DEVICE_MAC5 )->GetWindowText( lMAC5 ); 
    aDialog.GetDlgItem( IDC_DEVICE_MAC6 )->GetWindowText( lMAC6 ); 
     
    if ( ( lMAC1.GetLength() > 0 ) && 
         ( lMAC2.GetLength() > 0 ) && 
         ( lMAC3.GetLength() > 0 ) && 
         ( lMAC4.GetLength() > 0 ) && 
         ( lMAC5.GetLength() > 0 ) && 
         ( lMAC6.GetLength() > 0 ) ) 
    { 
        CyAssert( lMAC1.GetLength() ); 
        CyAssert( lMAC2.GetLength() ); 
        CyAssert( lMAC3.GetLength() ); 
        CyAssert( lMAC4.GetLength() ); 
        CyAssert( lMAC5.GetLength() ); 
        CyAssert( lMAC6.GetLength() ); 
 
        int lA, lB, lC, lD, lE, lF; 
        if ( ( sscanf( lMAC1, "%02X", &lA ) == 1 ) && 
             ( sscanf( lMAC2, "%02X", &lB ) == 1 ) && 
             ( sscanf( lMAC3, "%02X", &lC ) == 1 ) && 
             ( sscanf( lMAC4, "%02X", &lD ) == 1 ) && 
             ( sscanf( lMAC5, "%02X", &lE ) == 1 ) && 
             ( sscanf( lMAC6, "%02X", &lF ) == 1 ) && 
             ( lA >= 0 ) && ( lA <= 255 ) && 
             ( lB >= 0 ) && ( lB <= 255 ) && 
             ( lC >= 0 ) && ( lC <= 255 ) && 
             ( lD >= 0 ) && ( lD <= 255 ) && 
             ( lE >= 0 ) && ( lE <= 255 ) && 
             ( lF >= 0 ) && ( lF <= 255 ) ) 
        { 
            sID = CyAdapterID( lA, lB, lC, lD, lE, lF ); 
        } 
    } 
 
    if ( sID != CyAdapterID() ) 
        return sID.GetIdentifier().c_str(); 
    else 
        return ""; 
} 
 
 
static bool CanForceIPOnConnect( CyDeviceDlg& aDialog ) 
{ 
    BYTE lIP1, lIP2, lIP3, lIP4; 
    return ( ( string( "") != GetMACFromControls( aDialog ) ) && 
             ( aDialog.mDeviceIP.GetAddress( lIP1, lIP2, lIP3, lIP4 ) == 4 ) ); 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CyDeviceDlg property page 
 
IMPLEMENT_DYNCREATE(CyDeviceDlg, CPropertyPage) 
 
CyDeviceDlg::CyDeviceDlg() 
    : CPropertyPage(CyDeviceDlg::IDD) 
    , mDoApply( true ) 
    , mMulticastEnabled( FALSE ) 
    , mMulticastMaster( FALSE ) 
    , mMulticastNoData( FALSE ) 
    , mCurrentDeviceIndex( (unsigned short) -1 ) 
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
 
	//{{AFX_DATA_INIT(CyDeviceDlg) 
	mDeviceMode = -1; 
	mPacketTimeout = 0; 
	mRequestTimeout = 0; 
	mIPName = _T(""); 
	mDeviceName = _T(""); 
	mCameraName = _T(""); 
	mPacketSize = 0; 
	mDeviceMAC1 = _T(""); 
    mDeviceMAC2 = _T(""); 
    mDeviceMAC3 = _T(""); 
    mDeviceMAC4 = _T(""); 
    mDeviceMAC5 = _T(""); 
    mDeviceMAC6 = _T(""); 
	mForceIPOnConnect = FALSE; 
	//}}AFX_DATA_INIT 
 
    mDeviceMode = 0; 
	mAnswerTimeout = lApp->mDefaultAnswerTimeout; 
	mFirstPacketTimeout = lApp->mDefaultFirstPacketTimeout; 
	mPacketTimeout = lApp->mDefaultPacketTimeout; 
	mRequestTimeout = lApp->mDefaultRequestTimeout; 
    mPacketSize = lApp->mDefaultPacketSize; 
 
    mForceIPOnConnect = AfxGetApp()->GetProfileInt( "", "ForceIPOnConnect", false ) != 0; 
 
    m_psp.dwFlags |= PSP_HASHELP; 
} 
 
CyDeviceDlg::~CyDeviceDlg() 
{ 
    AfxGetApp()->WriteProfileInt( "", "ForceIPOnConnect", mForceIPOnConnect ); 
} 
 
void CyDeviceDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CPropertyPage::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CyDeviceDlg) 
	DDX_Control(pDX, IDC_DEVICE_ADAPTER_ID, mAdapterIDControl); 
	DDX_Control(pDX, IDC_DEVICE_PACKET_SIZE_SPIN, mPacketSizeSpin); 
	DDX_Control(pDX, IDC_DEVICE_MODE, mDeviceModeControl); 
	DDX_Control(pDX, IDC_DEVICE_PACKET_SIZE, mPacketSizeControl); 
	DDX_Control(pDX, IDC_DEVICE_INFO, mDeviceInfo); 
	DDX_Control(pDX, IDC_DEVICE_STATUS, mDeviceStatus); 
	DDX_Control(pDX, IDC_CURRENT_DEVICE, mCurrentDevice); 
	DDX_Control(pDX, IDC_DEVICE_IP, mDeviceIP); 
	DDX_Text(pDX, IDC_DEVICE_ANSWER_TIMEOUT, mAnswerTimeout); 
	DDV_MinMaxUInt(pDX, mAnswerTimeout, 1, 32767); 
	DDX_Text(pDX, IDC_DEVICE_FIRST_PACKET_TIMEOUT, mFirstPacketTimeout); 
	DDV_MinMaxUInt(pDX, mFirstPacketTimeout, 0, 600000000); 
	DDX_CBIndex(pDX, IDC_DEVICE_MODE, mDeviceMode); 
	DDX_Text(pDX, IDC_DEVICE_PACKET_TIMEOUT, mPacketTimeout); 
	DDV_MinMaxUInt(pDX, mPacketTimeout, 0, 60000000); 
	DDX_Text(pDX, IDC_DEVICE_REQUEST_TIMEOUT, mRequestTimeout); 
	DDV_MinMaxUInt(pDX, mRequestTimeout, 1, 600000000); 
	DDX_Text(pDX, IDC_DEVICE_IP_NAME, mIPName); 
	DDX_Text(pDX, IDC_DEVICE_NAME, mDeviceName); 
	DDX_Text(pDX, IDC_CAMERA_NAME, mCameraName); 
    DDX_Text(pDX, IDC_DEVICE_PACKET_SIZE, mPacketSize); 
	DDX_Text(pDX, IDC_DEVICE_MAC1, mDeviceMAC1); 
    DDX_Text(pDX, IDC_DEVICE_MAC2, mDeviceMAC2); 
    DDX_Text(pDX, IDC_DEVICE_MAC3, mDeviceMAC3); 
    DDX_Text(pDX, IDC_DEVICE_MAC4, mDeviceMAC4); 
    DDX_Text(pDX, IDC_DEVICE_MAC5, mDeviceMAC5); 
    DDX_Text(pDX, IDC_DEVICE_MAC6, mDeviceMAC6); 
	DDX_Check(pDX, IDC_DEVICE_FORCE_IP, mForceIPOnConnect); 
	//}}AFX_DATA_MAP 
 
    if ( pDX->m_bSaveAndValidate ) 
    { 
        pDX->PrepareEditCtrl( IDC_DEVICE_PACKET_SIZE ); 
        if ( mPacketSize %32 ) 
        { 
            MessageBox( "Packet size must be a multiple of 32" ); 
            pDX->Fail(); 
        } 
        DDV_MinMaxUInt(pDX, mPacketSize, 32, 16224 ); 
 
        pDX->PrepareEditCtrl( IDC_DEVICE_NAME ); 
        if ( mDeviceName == "" ) 
        { 
            MessageBox( "Invalid Device Name" ); 
            pDX->Fail(); 
        } 
 
        if ( ( mDeviceMAC1.GetLength() > 0 ) || 
             ( mDeviceMAC2.GetLength() > 0 ) || 
             ( mDeviceMAC3.GetLength() > 0 ) || 
             ( mDeviceMAC4.GetLength() > 0 ) || 
             ( mDeviceMAC5.GetLength() > 0 ) || 
             ( mDeviceMAC6.GetLength() > 0 ) ) 
        { 
            int lTemp; 
 
            pDX->PrepareEditCtrl( IDC_DEVICE_MAC1 ); 
            if ( ( sscanf( (LPCTSTR) mDeviceMAC1, "%02X", &lTemp ) != 1 ) || 
                 ( lTemp < 0 ) || 
                 ( lTemp > 255 ) ) 
            { 
                MessageBox( "Invalid Device MAC address" ); 
                pDX->Fail(); 
            } 
 
            pDX->PrepareEditCtrl( IDC_DEVICE_MAC2 ); 
            if ( ( sscanf( (LPCTSTR) mDeviceMAC2, "%02X", &lTemp ) != 1 ) || 
                 ( lTemp < 0 ) || 
                 ( lTemp > 255 ) ) 
            { 
                MessageBox( "Invalid Device MAC address" ); 
                pDX->Fail(); 
            } 
 
            pDX->PrepareEditCtrl( IDC_DEVICE_MAC3 ); 
            if ( ( sscanf( (LPCTSTR) mDeviceMAC3, "%02X", &lTemp ) != 1 ) || 
                 ( lTemp < 0 ) || 
                 ( lTemp > 255 ) ) 
            { 
                MessageBox( "Invalid Device MAC address" ); 
                pDX->Fail(); 
            } 
 
            pDX->PrepareEditCtrl( IDC_DEVICE_MAC4 ); 
            if ( ( sscanf( (LPCTSTR) mDeviceMAC4, "%02X", &lTemp ) != 1 ) || 
                 ( lTemp < 0 ) || 
                 ( lTemp > 255 ) ) 
            { 
                MessageBox( "Invalid Device MAC address" ); 
                pDX->Fail(); 
            } 
 
            pDX->PrepareEditCtrl( IDC_DEVICE_MAC5 ); 
            if ( ( sscanf( (LPCTSTR) mDeviceMAC5, "%02X", &lTemp ) != 1 ) || 
                 ( lTemp < 0 ) || 
                 ( lTemp > 255 ) ) 
            { 
                MessageBox( "Invalid Device MAC address" ); 
                pDX->Fail(); 
            } 
 
            pDX->PrepareEditCtrl( IDC_DEVICE_MAC6 ); 
            if ( ( sscanf( (LPCTSTR) mDeviceMAC6, "%02X", &lTemp ) != 1 ) || 
                 ( lTemp < 0 ) || 
                 ( lTemp > 255 ) ) 
            { 
                MessageBox( "Invalid Device MAC address" ); 
                pDX->Fail(); 
            } 
        } 
    } 
} 
 
 
BEGIN_MESSAGE_MAP(CyDeviceDlg, CPropertyPage) 
	//{{AFX_MSG_MAP(CyDeviceDlg) 
	ON_BN_CLICKED(IDC_DEVICE_FIND, OnDeviceFind) 
	ON_BN_CLICKED(ID_DEVICE_DELETE, OnDeviceDelete) 
	ON_BN_CLICKED(ID_DEVICE_TEST, OnDeviceTest) 
	ON_BN_CLICKED(ID_DEVICE_ADD, OnDeviceAdd) 
	ON_CBN_SELCHANGE(IDC_CURRENT_DEVICE, OnSelchangeCurrentDevice) 
	ON_BN_CLICKED(IDC_CAMERA_BROWSE, OnCameraBrowse) 
	ON_NOTIFY(IPN_FIELDCHANGED, IDC_DEVICE_IP, OnFieldchangedDeviceIp) 
	ON_EN_CHANGE(IDC_DEVICE_IP_NAME, OnChangeDeviceIpName) 
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen) 
	ON_COMMAND(ID_FILE_NEW, OnFileNew) 
	ON_NOTIFY(UDN_DELTAPOS, IDC_DEVICE_PACKET_SIZE_SPIN, OnDeltaposDevicePacketSizeSpin) 
	ON_CBN_SELCHANGE(IDC_DEVICE_MODE, OnSelchangeDeviceMode) 
	ON_BN_CLICKED(ID_DEVICE_RECONNECT, OnDeviceReconnect) 
	ON_BN_CLICKED(IDC_DEVICE_MULTICAST, OnDeviceMulticast) 
	ON_BN_CLICKED(IDC_DEVICE_TIMEOUT, OnDeviceTimeout) 
	ON_COMMAND(ID_FILE_SAVE, OnFileSave) 
	ON_COMMAND(ID_FILE_SAVEAS, OnFileSaveas) 
	ON_BN_CLICKED(IDC_SYSTEM_SERIAL, OnSystemSerialPort) 
	ON_CBN_SELCHANGE(IDC_DEVICE_ADAPTER_ID, OnSelchangeDeviceAdapterId) 
	ON_EN_CHANGE(IDC_DEVICE_MAC1, OnChangeDeviceMac) 
    ON_EN_CHANGE(IDC_DEVICE_MAC2, OnChangeDeviceMac) 
    ON_EN_CHANGE(IDC_DEVICE_MAC3, OnChangeDeviceMac) 
    ON_EN_CHANGE(IDC_DEVICE_MAC4, OnChangeDeviceMac) 
    ON_EN_CHANGE(IDC_DEVICE_MAC5, OnChangeDeviceMac) 
    ON_EN_CHANGE(IDC_DEVICE_MAC6, OnChangeDeviceMac) 
	ON_BN_CLICKED(IDC_DEVICE_FORCE_IP, OnDeviceForceIp) 
	//}}AFX_MSG_MAP 
 
    ON_WM_DROPFILES() 
	ON_EN_CHANGE(IDC_CAMERA_NAME, OnControlChanged) 
    ON_EN_CHANGE(IDC_DEVICE_PACKET_SIZE, OnControlChanged) 
	ON_EN_CHANGE(IDC_DEVICE_ANSWER_TIMEOUT, OnControlChanged) 
	ON_EN_CHANGE(IDC_DEVICE_FIRST_PACKET_TIMEOUT, OnControlChanged) 
	ON_EN_CHANGE(IDC_DEVICE_NAME, OnControlChanged) 
	ON_EN_CHANGE(IDC_DEVICE_PACKET_TIMEOUT, OnControlChanged) 
	ON_EN_CHANGE(IDC_DEVICE_REQUEST_TIMEOUT, OnControlChanged) 
    ON_EN_CHANGE(IDC_DEVICE_ADAPTER_INDEX, OnControlChanged) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CyDeviceDlg message handlers 
 
void CyDeviceDlg::SetModified(BOOL bChanged ) 
{ 
    CPropertyPage::SetModified( bChanged ); 
 
    mModified = bChanged != 0; 
} 
 
void CyDeviceDlg::OnDeviceFind()  
{ 
    CyDeviceFinder lFinder; 
    CyDeviceFinder::DeviceEntry lEntry; 
 
    if ( lFinder.SelectDevice( lEntry, GetSafeHwnd() ) == CY_RESULT_OK ) 
    { 
        CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
 
        if ( lApp->GetConfig().GetCount() == 0 ) 
            OnDeviceAdd(); 
 
        if ( !lEntry.mDeviceName.empty() ) 
            mDeviceName = lEntry.mDeviceName.c_str(); 
 
        SetMACToControls( *this, lEntry.mAddressMAC.c_str() ); 
 
        switch( lEntry.mMode ) 
        { 
            case CyConfig::MODE_DRV    : mDeviceMode = 0; break; 
            case CyConfig::MODE_UDP    : mDeviceMode = 1; break; 
            case CyConfig::MODE_FILTER : mDeviceMode = 2; break; 
            default : CyAssert( false ); 
        } 
 
        if ( lEntry.mAddressIP != "" ) 
        { 
            std::string lAddress( lEntry.mAddressIP.c_str() + 1 ); 
            lAddress.resize( lAddress.size() - 1 ); 
  		    mDeviceIP.SetWindowText( lAddress.c_str() ); 
            mIPName = ""; 
        } 
        else 
        { 
			mDeviceIP.SetWindowText( "" ); 
            mIPName = ""; 
        } 
 
        if ( ( ( lEntry.mSoftVerMaj > 1 ) || ( lEntry.mSoftVerMin > 3 ) ) &&  
             !lEntry.mMulticastAddress.empty() ) 
        { 
            // we set the current application not-master because we found it 
            // so another app has made it master 
            mMulticastEnabled = TRUE; 
            mMulticastMaster  = FALSE; 
            mMulticastNoData  = FALSE; 
            mMulticastAddress = lEntry.mMulticastAddress.c_str(); 
        } 
        else 
        { 
            mMulticastEnabled = FALSE; 
            mMulticastMaster  = FALSE; 
            mMulticastNoData  = FALSE; 
            mMulticastAddress = ""; 
        } 
 
        // Generate the default device info text 
        CString lTemp; 
        lTemp.Format( "Device %u, Module %u, Sub-Module %u", 
                      lEntry.mDeviceID, 
                      lEntry.mModuleID, 
                      lEntry.mSubID ); 
 
        // Ask the device list if there is a description available. 
        // If so, use it instead of the default 
        string lDeviceDesc, lModuleDesc, lSubDesc; 
        if ( CyDeviceList::GetInformation( lEntry.mDeviceID, 
                                           lEntry.mModuleID, 
                                           lEntry.mSubID, 
                                           lDeviceDesc, 
                                           lModuleDesc, 
                                           lSubDesc ) == CY_RESULT_OK ) 
        { 
            lTemp.Format( "%s, %s", lDeviceDesc.c_str(), lModuleDesc.c_str() ); 
            if ( !lSubDesc.empty() ) 
            { 
                lTemp += ", "; 
                lTemp += lSubDesc.c_str(); 
            } 
        } 
 
        // Set the information in the page 
        mDeviceInfo.SetWindowText( lTemp ); 
        mDeviceID = lEntry.mDeviceID; 
        mModuleID = lEntry.mModuleID; 
        mSubID    = lEntry.mSubID; 
 
        // Verify how many camera matche that ID, if there is only 
        // one, write it in the camera name space 
        CyCameraRegistry lReg; 
        CyCameraRegistry::CameraID lID = CyCameraRegistry::GetCameraID( mModuleID, mSubID ); 
        if ( ( lReg.CountCameras( lID ) == 1 ) && 
             ( lReg.FindCamera( lID) == CY_RESULT_OK ) ) 
        { 
            string lName; 
            if ( lReg.GetName( lName ) == CY_RESULT_OK ) 
                mCameraName = lName.c_str(); 
        } 
 
        mAnswerTimeout = lApp->mDefaultAnswerTimeout; 
        mFirstPacketTimeout = lApp->mDefaultFirstPacketTimeout; 
        mPacketTimeout = lApp->mDefaultPacketTimeout; 
        mRequestTimeout = lApp->mDefaultRequestTimeout; 
		if ( ( lEntry.mMode == CyConfig::MODE_UDP ) || 
		     ( lEntry.mMode == CyConfig::MODE_FILTER ) ) 
		{ 
			if ( lApp->mDefaultPacketSize <= CyConfig::DEFAULT_PACKET_SIZE_UDP ) 
				mPacketSize = lApp->mDefaultPacketSize; 
			else 
				mPacketSize = CyConfig::DEFAULT_PACKET_SIZE_UDP; 
		} 
		else 
			mPacketSize = lApp->mDefaultPacketSize; 
 
        UpdateData( FALSE ); 
        SetModified(); 
        EnableControls( true ); 
 
        int lAdapterIndex = FindStringInCombo(  mAdapterIDControl, lEntry.mAdapterID.GetIdentifier() ); 
        mAdapterIDControl.SetCurSel( lAdapterIndex ); 
 
        // Move the focus to the camera selection button 
        GetDlgItem( IDC_CAMERA_BROWSE )->SetFocus(); 
 
        // Select the camera if required 
        if ( lApp->mSelectCameraAfterDetect ) 
            PostMessage( WM_COMMAND, IDC_CAMERA_BROWSE, (LPARAM)GetDlgItem( IDC_CAMERA_BROWSE )->GetSafeHwnd() ); 
    } 
} 
 
void CyDeviceDlg::OnDeviceAdd()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lConfig = lApp->GetConfig(); 
 
    // verify that the current camera settings have beed save 
    if ( lApp->mConfigChanged ) 
    { 
        switch ( MessageBox( "Camera configuration has changed, but not saved.\n" 
                             "Continue without saving?", 
                             "Camera Interface Application", 
                             MB_OKCANCEL ) ) 
        { 
            case IDCANCEL: 
                return; 
                break; 
        } 
    } 
 
    // at this point, the user clicked ok, so we don't save 
    lApp->mConfigChanged = false; 
 
    if ( ( lConfig.GetCount() == 0 ) ||  
         MessageBox( "Do you want to create a new camera entry?",  
                     "Camera Interface Application", 
                     MB_OK | MB_YESNO ) == IDYES ) 
    { 
		lConfig.AddDevice( ); 
        lConfig.GoLast(); 
 
        // Get the name of the new device 
        char lTemp[128]; 
        lConfig.GetName( lTemp, sizeof( lTemp ) ); 
        GetDlgItem( IDC_DEVICE_NAME )->SetWindowText( lTemp ); 
 
        CString lStr; 
        lStr.Format( "%d", mCurrentDevice.GetCount() ); 
        mCurrentDevice.AddString( lStr ); 
        mCurrentDevice.SetCurSel( mCurrentDevice.GetCount() - 1 ); 
         
        mDoApply = false; 
        OnSelchangeCurrentDevice(); 
        mDoApply = true; 
 
        EnableControls( true ); 
	} 
} 
 
void CyDeviceDlg::OnDeviceDelete()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lConfig = lApp->GetConfig(); 
    CyDevice& lDevice = lApp->GetDevice(); 
 
    if ( mCurrentDevice.GetCount() > 0 ) 
    { 
        CString lStr; 
	    lStr.Format( "Deleting last camera (number %d)", mCurrentDevice.GetCount() ); 
	    if ( MessageBox( lStr, "Camera Interface Application", MB_YESNO ) == IDYES ) 
	    { 
            lConfig.GoLast(); 
		    lConfig.DeleteDevice( ); 
 
            mCurrentDevice.DeleteString( mCurrentDevice.GetCount() - 1 ); 
            SetModified(); 
	    } 
 
        if ( mCurrentDevice.GetCount() > 0 ) 
        { 
            EnableControls( true ); 
            mCurrentDevice.SetCurSel( mCurrentDevice.GetCount() - 1 ); 
 
            mDoApply = false; 
            OnSelchangeCurrentDevice(); 
            mDoApply = true; 
        } 
        else 
        { 
            mDeviceName = ""; 
            lApp->mPropertySheet.SetWindowText( "Camera Interface Application" ); 
            mDeviceMode = 0; 
            mIPName = ""; 
            mCameraName = ""; 
            mDeviceInfo.SetWindowText( "" ); 
            mDeviceID = 1; 
            mModuleID = 0; 
            mSubID    = 0; 
            lApp->SetCamera( 0 ); 
            mDeviceIP.SetWindowText( "" ); 
            mDeviceMAC1 = ""; 
            mDeviceMAC2 = ""; 
            mDeviceMAC3 = ""; 
            mDeviceMAC4 = ""; 
            mDeviceMAC5 = ""; 
            mDeviceMAC6 = ""; 
            UpdateData( FALSE ); 
            mMulticastEnabled = FALSE; 
            mMulticastMaster  = FALSE; 
            mMulticastNoData  = FALSE; 
            mMulticastAddress = ""; 
 
            // Reset the current device index 
            mCurrentDeviceIndex = (unsigned short) -1; 
 
            EnableControls( false ); 
            mCurrentDevice.SetCurSel( -1 ); 
 
            // Make sure the device is disconnected 
            lDevice.Disconnect(); 
        } 
    } 
} 
 
void CyDeviceDlg::OnDeviceTest()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyGrabber& lGrabber = lApp->GetGrabber(); 
 
    // update the data from the dialog 
    if ( ! UpdateData() ) 
        return; 
 
    // Disconnect the grabber 
	if ( lGrabber.IsConnected( ) ) 
    { 
		lGrabber.Disconnect( ); 
	    Sleep ( 500 ); 
    } 
 
    // Create a dummy CyConfig for testing 
    CyConfig lConfig; 
    lConfig.AddDevice(); 
    lConfig.SetName( (LPCTSTR)mDeviceName ); 
    lConfig.SetDeviceType( (LPCTSTR)mCameraName ); 
 
    CString lAddress; 
    mDeviceIP.GetWindowText( lAddress ); 
    if ( lAddress != "0.0.0.0" ) 
    { 
        string lIP( "[" ); 
        lIP += (LPCTSTR)lAddress; 
        lIP += "]"; 
         
        lConfig.SetAddress( lIP.c_str() ); 
    } 
    else 
    { 
        lConfig.SetAddress( (LPCTSTR) mIPName ); 
    } 
 
    lConfig.SetMACAddress( GetMACFromControls( *this ) ); 
 
    switch( mDeviceMode ) 
    { 
        case 0: lConfig.SetAccessMode( CyConfig::MODE_DRV    ); break; 
        case 1: lConfig.SetAccessMode( CyConfig::MODE_UDP    ); break; 
        case 2: lConfig.SetAccessMode( CyConfig::MODE_FILTER ); break; 
        default : CyAssert( false ); 
    } 
     
    lConfig.SetAnswerTimeOut( mAnswerTimeout ); 
    lConfig.SetFirstPacketTimeOut( mFirstPacketTimeout ); 
    lConfig.SetPacketTimeOut( mPacketTimeout ); 
    lConfig.SetRequestTimeOut( mRequestTimeout ); 
    lConfig.SetPacketSize( mPacketSize ); 
    CString lAdapterID; 
    mAdapterIDControl.GetLBText( mAdapterIDControl.GetCurSel(), lAdapterID ); 
    lConfig.SetAdapterID( CyAdapterID( (LPCTSTR)lAdapterID ) ); 
 
    if ( lApp->HasCamera() ) 
        lGrabber.UnlinkFrom( lApp->GetCamera() ); 
 
    mDeviceStatus.SetWindowText( "Not connected..." ); 
	if ( lGrabber.Connect( lConfig, GetConnectFlag( *this, lConfig ) ) == CY_RESULT_OK) 
	{ 
		MessageBox( "Connected to the device..." ); 
 
		mDeviceStatus.SetWindowText( "Connected to device..." ); 
        lGrabber.Disconnect( ); 
	} 
	else 
	{ 
        CyErrorInfo lError; 
        lGrabber.GetErrorInfo( &lError ); 
 
        ostringstream lMessage; 
        lMessage << "Could not connect to device because " << lError.mMessage; 
        AfxMessageBox( lMessage.str().c_str() ); 
        return; 
	} 
 
    if ( lApp->HasCamera() ) 
        lGrabber.LinkTo( lApp->GetCamera() );    
 
    SetModified(); 
} 
 
BOOL CyDeviceDlg::OnInitDialog()  
{ 
	CPropertyPage::OnInitDialog(); 
 
    DragAcceptFiles( TRUE ); 
    EnableControls( false ); 
 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lConfig = lApp->GetConfig(); 
 
    if ( lConfig.GetCount() > 0 ) 
    { 
        CString lStr; 
        for ( unsigned int i = 0; i < lConfig.GetCount(); ++i ) 
        { 
            lStr.Format( "%d", i ); 
            mCurrentDevice.AddString( lStr ); 
        } 
 
        mCurrentDevice.SetCurSel( lConfig.GetIndex() ); 
        OnSelchangeCurrentDevice(); 
        EnableControls( TRUE ); 
    } 
    else 
        lApp->SetCamera( 0 ); 
 
    mPacketSizeSpin.SetRange( 32, 16224 ); 
 
    // Move the focus to the detect button 
    GetDlgItem( IDC_DEVICE_FIND )->SetFocus(); 
 
    // invoked the detect dialog if required 
    if ( lApp->mDetectOnStartup ) 
        PostMessage( WM_COMMAND, IDC_DEVICE_FIND, (LPARAM)GetDlgItem( IDC_DEVICE_FIND )->GetSafeHwnd() ); 
 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
 
void CyDeviceDlg::EnableControls( bool aEnabled ) 
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyGrabber& lGrabber = lApp->GetGrabber(); 
     
    bool lEnableConfigure = aEnabled && ( lApp->HasCamera() ); 
    bool lEnableReconnect = aEnabled && ( lGrabber.IsConnected() ); 
     
    CString lValue; 
    mDeviceIP.GetWindowText( lValue ); 
    bool lEnableIPControl = aEnabled && ( mIPName == "" );  
    bool lEnableIPName    = aEnabled && ( lValue == "0.0.0.0" );  
 
	GetDlgItem( IDC_DEVICE_NAME )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_CAMERA_NAME )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_DEVICE_IP )->EnableWindow( lEnableIPControl ); 
    GetDlgItem( IDC_DEVICE_FORCE_IP )->EnableWindow( lEnableIPControl && CanForceIPOnConnect( *this ) ); 
    GetDlgItem( IDC_DEVICE_MAC1 )->EnableWindow( lEnableIPControl ); 
    GetDlgItem( IDC_DEVICE_MAC2 )->EnableWindow( lEnableIPControl ); 
    GetDlgItem( IDC_DEVICE_MAC3 )->EnableWindow( lEnableIPControl ); 
    GetDlgItem( IDC_DEVICE_MAC4 )->EnableWindow( lEnableIPControl ); 
    GetDlgItem( IDC_DEVICE_MAC5 )->EnableWindow( lEnableIPControl ); 
    GetDlgItem( IDC_DEVICE_MAC6 )->EnableWindow( lEnableIPControl ); 
    GetDlgItem( IDC_DEVICE_IP_NAME )->EnableWindow( lEnableIPName ); 
    GetDlgItem( IDC_DEVICE_MODE )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_DEVICE_ADAPTER_INDEX )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_DEVICE_MULTICAST )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_SYSTEM_SERIAL )->EnableWindow( aEnabled && lGrabber.GetDevice().IsConnected() ); 
    GetDlgItem( ID_DEVICE_RECONNECT )->EnableWindow( lEnableReconnect ); 
    GetDlgItem( IDC_DEVICE_ANSWER_TIMEOUT )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_DEVICE_FIRST_PACKET_TIMEOUT )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_DEVICE_PACKET_TIMEOUT )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_DEVICE_REQUEST_TIMEOUT )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_DEVICE_TIMEOUT )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_DEVICE_PACKET_SIZE )->EnableWindow( aEnabled ); 
    GetDlgItem( ID_DEVICE_DELETE )->EnableWindow( aEnabled ); 
    GetDlgItem( ID_DEVICE_TEST )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_CURRENT_DEVICE )->EnableWindow( aEnabled ); 
    GetDlgItem( IDC_CAMERA_BROWSE )->EnableWindow( aEnabled ); 
 
    if ( lGrabber.GetDevice().IsSystemSerialPortEnabled() ) 
        SetDlgItemText( IDC_SYSTEM_SERIAL, "Disable Serial Port Link" ); 
    else 
        SetDlgItemText( IDC_SYSTEM_SERIAL, "Enable Serial Port Link..." ); 
 
    lApp->mPropertySheet.EnableConfigure( lEnableConfigure ); 
 
    UpdateAdapterIDs(); 
} 
 
void CyDeviceDlg::OnSelchangeCurrentDevice()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lConfig = lApp->GetConfig(); 
 
    unsigned int lIndex = mCurrentDevice.GetCurSel(); 
    if ( lIndex != mCurrentDeviceIndex && 
         ( lConfig.SetIndex( lIndex ) == CY_RESULT_OK ) ) 
    { 
 
        // verify that the current camera settings have beed save 
        if ( lApp->mConfigChanged ) 
        { 
            switch ( MessageBox( "Camera configuration has changed, but not saved.\n" 
                                 "Continue without saving?", 
                                 "Camera Interface Application", 
                                 MB_YESNOCANCEL ) ) 
            { 
                case IDNO: 
                    lApp->OnFileSave(); 
                    break; 
 
                case IDCANCEL: 
                    mCurrentDevice.SetCurSel( mCurrentDeviceIndex ); 
                    return; 
                    break; 
            } 
        } 
 
        // at this point, the user clicked ok, so we don't save 
        lApp->mConfigChanged = false; 
 
        char lTemp[128]; 
        unsigned long lULong; 
        bool lEnabled, lMaster; 
        CyConfig::AccessMode lMode; 
        CyAdapterID lAdapterID; 
 
        lConfig.GetName( lTemp, sizeof( lTemp ) ); 
	    mDeviceName = lTemp; 
 
        lConfig.GetDeviceType( lTemp, sizeof( lTemp ) ); 
        mCameraName = lTemp; 
 
        mDeviceInfo.SetWindowText( "" ); 
        mDeviceID = 1; 
        mModuleID = 0; 
        mSubID    = 0; 
 
        lConfig.GetAddress( lTemp, sizeof( lTemp ) ); 
        if ( lTemp[0] == '[' ) 
        { 
            string lIP( lTemp + 1 ); 
            while ( lIP[ lIP.length() - 1 ] == ']' ) 
                lIP.resize( lIP.length() - 1 ); 
            mDeviceIP.SetWindowText( lIP.c_str() ); 
        } 
        else 
        { 
            mDeviceIP.SetWindowText( "" ); 
            mIPName = lTemp; 
        } 
 
        lConfig.GetMACAddress( lTemp, sizeof( lTemp ) ); 
        SetMACToControls( *this, lTemp ); 
 
        lConfig.GetMulticast( &lEnabled, &lMaster ); 
        lConfig.GetMulticastAddress( lTemp, sizeof( lTemp ) ); 
        mMulticastEnabled = lEnabled; 
        mMulticastMaster  = lMaster; 
        mMulticastNoData  = FALSE; // TODO: we do not really know... 
        mMulticastAddress = lTemp; 
 
        lConfig.GetAccessMode( &lMode ); 
        switch( lMode ) 
        { 
            case CyConfig::MODE_DRV    : mDeviceMode = 0; break; 
            case CyConfig::MODE_UDP    : mDeviceMode = 1; break; 
            case CyConfig::MODE_FILTER : mDeviceMode = 2; break; 
            default : CyAssert( false ); 
        } 
         
        lConfig.GetAnswerTimeOut( &lULong ); 
        mAnswerTimeout = lULong; 
        lConfig.GetFirstPacketTimeOut( &lULong ); 
        mFirstPacketTimeout = lULong; 
        lConfig.GetPacketTimeOut( &lULong ); 
        mPacketTimeout = lULong; 
        lConfig.GetRequestTimeOut( &lULong ); 
        mRequestTimeout = lULong; 
        lConfig.GetPacketSize( &lULong ); 
        mPacketSize = lULong; 
        lConfig.GetAdapterID( &lAdapterID ); 
 
        UpdateData( FALSE ); 
 
        UpdateAdapterIDs(); 
        int lAdapterIndex = FindStringInCombo(  mAdapterIDControl, lAdapterID.GetIdentifier() ); 
        mAdapterIDControl.SetCurSel( lAdapterIndex ); 
 
        if ( mDoApply ) 
            InternalApply(); 
        mCurrentDeviceIndex = lIndex; 
    } 
} 
 
void CyDeviceDlg::OnCameraBrowse()  
{ 
    CyCameraRegistry lReg; 
    CyCameraRegistry::CameraID lID; 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
 
    CString lName; 
    GetDlgItem( IDC_CAMERA_NAME )->GetWindowText( lName ); 
    if ( lName.GetLength() > 0 ) 
        lReg.FindCamera( (LPCTSTR)lName ); 
 
    if ( mModuleID == 0x04 ) // module is test module, so any camera will be available 
        lID = 0; 
    else 
        lID = CyCameraRegistry::GetCameraID( mModuleID, mSubID ); 
    if ( lReg.SelectCamera( GetSafeHwnd(), lID ) == CY_RESULT_OK ) 
    { 
        string lName; 
        lReg.GetName( lName ); 
        GetDlgItem( IDC_CAMERA_NAME )->SetWindowText( lName.c_str() ); 
 
        EnableControls( TRUE ); 
        SetModified(); 
 
        // Move the focus on the "Apply" button of the property sheet 
        GetParent()->GetDlgItem( ID_APPLY_NOW )->SetFocus(); 
 
        // Connect if required 
        if ( lApp->mConnectAfterCameraSelection ) 
            GetParent()->PostMessage( WM_COMMAND, 
                                      ID_APPLY_NOW, 
                                      (LPARAM)GetParent()->GetDlgItem( ID_APPLY_NOW )->GetSafeHwnd() ); 
    } 
} 
 
void CyDeviceDlg::OnControlChanged()  
{ 
    SetModified( ); 
} 
 
void CyDeviceDlg::OnFieldchangedDeviceIp(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	SetModified(); 
 
    CString lValue; 
    mDeviceIP.GetWindowText( lValue ); 
    GetDlgItem( IDC_DEVICE_IP_NAME )->EnableWindow( lValue == "0.0.0.0" ); 
    GetDlgItem( IDC_DEVICE_FORCE_IP )->EnableWindow( CanForceIPOnConnect( *this ) ); 
	 
	*pResult = 0; 
} 
 
 
void CyDeviceDlg::OnChangeDeviceIpName()  
{ 
    SetModified(); 
 
    CString lValue; 
    GetDlgItem( IDC_DEVICE_IP_NAME )->GetWindowText( lValue ); 
    GetDlgItem( IDC_DEVICE_IP )->EnableWindow( lValue == "" ); 
    GetDlgItem( IDC_DEVICE_FORCE_IP )->EnableWindow( CanForceIPOnConnect( *this ) ); 
} 
 
 
BOOL CyDeviceDlg::OnKillActive()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lConfig = lApp->GetConfig(); 
 
    if ( lConfig.GetCount() == 0 ) 
        return FALSE; 
 
    if ( mModified ) 
    { 
        if ( !InternalApply() ) 
            return FALSE; 
    } 
 
	return CPropertyPage::OnKillActive(); 
} 
 
 
bool CyDeviceDlg::InternalApply() 
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyGrabber& lGrabber = lApp->GetGrabber(); 
    CyConfig& lConfig = lApp->GetConfig(); 
    CyDevice& lDevice= lApp->GetDevice(); 
    unsigned short lID; 
 
    lApp->SetCamera( 0 ); 
 
    if ( !UpdateData() ) 
        return false; 
 
    BeginWaitCursor(); 
 
    // State of grabbing and displaying for channels 
    std::map< CyChannelID, bool > 
        lDisplaying; 
    std::map< CyChannelID, bool > 
        lGrabbing; 
 
    // check the state of the displays and grabbing for all channels 
    for ( lID = 0; lID < lDevice.GetChannelCount(); ++lID ) 
    { 
        // Get the channel from the config 
        const CyChannel lChannel( lDevice.GetChannel( lID ) ); 
 
        // Check the grabbing and stop it for the current channel 
        lGrabbing[ lID ] = lGrabber.IsStarted( lChannel ); 
        if ( lGrabbing[ lID ] ) 
            lGrabber.StopGrabbing( lChannel ); 
 
        // Check the display and stop it for the current channel 
        lDisplaying[ lID ] = lApp->GetDisplay().IsDisplaying( lID ); 
        if ( lDisplaying[ lID ] ) 
            lApp->GetDisplay().StopDisplaying( lID ); 
    } 
 
 
    if ( lGrabber.IsConnected() ) 
    { 
        lGrabber.Disconnect(); 
        ::Sleep( 500 ); 
    } 
 
    if ( lConfig.GetCount() == 0 ) 
    { 
        EndWaitCursor(); 
        return false; 
    } 
 
	int lIndex = mCurrentDevice.GetCurSel(); 
    if ( lConfig.SetIndex( lIndex ) != CY_RESULT_OK ) 
    { 
        CyAssert( false ); 
        EndWaitCursor(); 
        return false; 
    } 
 
    lConfig.SetName( (LPCTSTR)mDeviceName ); 
    lConfig.SetDeviceType( (LPCTSTR)mCameraName ); 
 
    CString lAddress; 
    mDeviceIP.GetWindowText( lAddress ); 
    if ( lAddress != "0.0.0.0" ) 
    { 
        string lIP( "[" ); 
        lIP += (LPCTSTR)lAddress; 
        lIP += "]"; 
         
        lConfig.SetAddress( lIP.c_str() ); 
    } 
    else 
    { 
        lConfig.SetAddress( (LPCTSTR) mIPName ); 
    } 
 
    lConfig.SetMACAddress( GetMACFromControls( *this ) ); 
 
    switch( mDeviceMode ) 
    { 
        case 0: lConfig.SetAccessMode( CyConfig::MODE_DRV    ); break; 
        case 1: lConfig.SetAccessMode( CyConfig::MODE_UDP    ); break; 
        case 2: lConfig.SetAccessMode( CyConfig::MODE_FILTER ); break; 
        default : CyAssert( false ); 
    } 
     
    lConfig.SetAnswerTimeOut( mAnswerTimeout ); 
    lConfig.SetFirstPacketTimeOut( mFirstPacketTimeout ); 
    lConfig.SetPacketTimeOut( mPacketTimeout ); 
    lConfig.SetRequestTimeOut( mRequestTimeout ); 
    lConfig.SetPacketSize( mPacketSize ); 
    CString lAdapterID; 
    mAdapterIDControl.GetLBText( mAdapterIDControl.GetCurSel(), lAdapterID ); 
    lConfig.SetAdapterID( CyAdapterID( (LPCTSTR)lAdapterID ) ); 
 
    lConfig.SetMulticast( mMulticastEnabled != 0, mMulticastMaster != 0 ); 
    lConfig.SetMulticastAddress( (LPCTSTR) mMulticastAddress ); 
 
    // Connect to the device 
    if ( lApp->HasCamera() ) 
        lGrabber.UnlinkFrom( lApp->GetCamera() );    
    mDeviceStatus.SetWindowText( "Not connected..." ); 
    if ( lGrabber.Connect( lConfig, GetConnectFlag( *this, lConfig ) ) != CY_RESULT_OK ) 
    { 
        CyErrorInfo lError; 
        lGrabber.GetErrorInfo( &lError ); 
 
        ostringstream lMessage; 
        lMessage << "Could not connect to device because " << lError.mMessage; 
        EndWaitCursor(); 
        AfxMessageBox( lMessage.str().c_str() ); 
        return false; 
    } 
    mDeviceStatus.SetWindowText( "Connected to device..." ); 
 
    // Get the device info 
    unsigned char lDevID, lModID, lSubID, lDummy, lMAC1, lMAC2, lMAC3, lMAC4, lMAC5, lMAC6; 
    lGrabber.GetDevice().GetDeviceInfo( &lDevID, 
                                        &lModID, 
                                        &lSubID, 
                                        &lDummy, 
                                        &lMAC1, 
                                        &lMAC2, 
                                        &lMAC3, 
                                        &lMAC4, 
                                        &lMAC5, 
                                        &lMAC6, 
                                        &lDummy, 
                                        &lDummy, 
                                        &lDummy, 
                                        &lDummy, 
                                        &lDummy, 
                                        &lDummy ); 
 
    // Generate the default device info text 
    CString lTemp; 
    lTemp.Format( "Device %u, Module %u, Sub-Module %u", 
                  lDevID, 
                  lModID, 
                  lSubID ); 
 
    // Ask the device list if there is a description available. 
    // If so, use it instead of the default 
    string lDeviceDesc, lModuleDesc, lSubDesc; 
    if ( CyDeviceList::GetInformation( lDevID, 
                                       lModID, 
                                       lSubID, 
                                       lDeviceDesc, 
                                       lModuleDesc, 
                                       lSubDesc ) == CY_RESULT_OK ) 
    { 
        lTemp.Format( "%s, %s", lDeviceDesc.c_str(), lModuleDesc.c_str() ); 
        if ( !lSubDesc.empty() ) 
        { 
            lTemp += ", "; 
            lTemp += lSubDesc.c_str(); 
        } 
    } 
 
    // Set the information in the page 
    mDeviceInfo.SetWindowText( lTemp ); 
    mDeviceID = lDevID; 
    mModuleID = lModID; 
    mSubID    = lSubID; 
 
    // Set the MAC address 
    lTemp.Format( "%02X-%02X-%02X-%02X-%02X-%02X", lMAC1, lMAC2, lMAC3, lMAC4, lMAC5, lMAC6 ); 
    SetMACToControls( *this, lTemp ); 
    lConfig.SetMACAddress( (LPCTSTR) lTemp ); 
 
    CyCameraRegistry lReg; 
    if ( mCameraName == "" ) 
    { 
        // Verify how many camera matche that ID, if there is only 
        // one, write it in the camera name space 
        CyCameraRegistry::CameraID lID = CyCameraRegistry::GetCameraID( mModuleID, mSubID ); 
        string lName; 
        if ( ( lReg.CountCameras( lID ) == 1 ) && 
             ( lReg.FindCamera( lID) == CY_RESULT_OK ) && 
             ( lReg.GetName( lName ) == CY_RESULT_OK ) ) 
        { 
            mCameraName = lName.c_str(); 
        } 
        else 
        { 
            EndWaitCursor(); 
            MessageBox( "Invalid Camera Name" ); 
            return false; 
        } 
    } 
 
    // Create the camera 
    CyCameraInterface* lCamera; 
    if ( lReg.CreateCamera( (LPCTSTR) mCameraName, &lCamera, &lApp->GetGrabber() ) == CY_RESULT_OK ) 
    { 
        lApp->SetCamera( lCamera ); 
 
        // If there is a config file, then read it 
        CyXMLDocument lDocument( (LPCTSTR)lApp->mFileName ); 
        if ( lDocument.LoadDocument() == CY_RESULT_OK ) 
        { 
            // Load the camera settings 
            if ( lCamera->LoadFromXML( lDocument ) != CY_RESULT_OK ) 
            { 
                CyErrorInfo lError; 
                lCamera->GetErrorInfo( &lError ); 
 
                ostringstream lMessage; 
                lMessage << "Could not read camera information because " << lError.mMessage; 
                if ( lError.mResult != CY_RESULT_NOT_FOUND ) 
                    AfxMessageBox( lMessage.str().c_str() ); 
            } 
        } 
 
        CyResult lResult = lCamera->UpdateToCamera(); 
        if ( lResult != CY_RESULT_OK ) 
        { 
            CyErrorInfo lError; 
            lCamera->GetErrorInfo( &lError ); 
 
            ostringstream lMessage; 
            lMessage << "Could not update camera because " << lError.mMessage; 
            AfxMessageBox( lMessage.str().c_str() ); 
            return false; 
        } 
    } 
    else 
    { 
        CyErrorInfo lError; 
        lReg.GetErrorInfo( &lError ); 
 
        ostringstream lMessage; 
        lMessage << "Could not create camera because " << lError.mMessage; 
        AfxMessageBox( lMessage.str().c_str() ); 
        return false; 
    } 
 
 
    // Restart the displays and grabbing for the channels 
    // check the state of the displays and grabbing for all channels 
    for ( lID = 0; lID < lDevice.GetChannelCount(); ++lID ) 
    { 
        // Get the channel from the config 
        const CyChannel lChannel( lDevice.GetChannel( lID) ); 
 
        // restart the display 
        if ( lDisplaying[ lID ] ) 
        { 
            // TODO: lFlags 
 
            lApp->GetDisplay().StartDisplaying( lApp->GetBuffer( lChannel ), lID, lApp->mBayerFlag ); 
        } 
 
        // restart the grabbing 
        if ( lGrabbing[ lID ] ) 
            lGrabber.StartGrabbing( lChannel, lApp->GetBuffer( lChannel ) ); 
    } 
 
    // Change the title of the window to include the name of the device 
    CString lCaption( mDeviceName  ); 
    if ( !lCaption.IsEmpty() ) 
        lCaption += " - "; 
        lCaption += "Camera Interface Application"; 
    lApp->mPropertySheet.SetWindowText( lCaption ); 
     
    EnableControls( TRUE ); 
    SetModified( FALSE ); 
 
    // If required, go to the acquisition tab 
    if ( lApp->mAcquisitionAfterConnect ) 
        GetParent()->PostMessage(PSM_SETCURSEL, 1); 
 
    EndWaitCursor(); 
    return true; 
} 
 
void CyDeviceDlg::OnFileOpen()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lConfig = lApp->GetConfig(); 
 
    // verify that the current camera settings have beed save 
    if ( lApp->mConfigChanged ) 
    { 
        switch ( MessageBox( "Camera configuration has changed, but not saved.\n" 
                             "Continue without saving?", 
                             "Camera Interface Application", 
                             MB_YESNOCANCEL ) ) 
        { 
            case IDNO: 
                lApp->OnFileSave(); 
                break; 
 
            case IDCANCEL: 
                return; 
                break; 
        } 
    } 
 
    // at this point, the user clicked ok, so we don't save 
    lApp->mConfigChanged = false; 
 
	CFileDialog	ConfigSaveDialog( true, CONFIG_EXT, NULL, OFN_HIDEREADONLY, CONFIG_FILE_TYPE ); 
	if ( ConfigSaveDialog.DoModal() == IDOK ) 
    { 
		try 
		{ 
            // Load the file and determine the selected item 
			lApp->mFileName = ConfigSaveDialog.GetPathName(); 
            CyXMLDocument lDocument( (LPCTSTR) lApp->mFileName ); 
            if ( lDocument.LoadDocument( ) != CY_RESULT_OK ) 
            { 
                CyErrorInfo lError; 
                lDocument.GetErrorInfo( &lError ); 
                MessageBox( lError.mMessage ); 
                return; 
            } 
 
			lConfig.LoadFromXML( lDocument ); 
		    if ( lConfig.FindDeviceByName( "Default" ) != CY_RESULT_OK ) 
            { 
                lConfig.SetIndex( 0 ); 
            } 
 
            // Fill the device ID list box 
            mCurrentDevice.ResetContent(); 
            CString lStr; 
            for ( unsigned int i = 0; i < lConfig.GetCount(); ++i ) 
            { 
                lStr.Format( "%d", i ); 
                mCurrentDevice.AddString( lStr ); 
            } 
 
            // Reset the current device index 
            mCurrentDeviceIndex = (unsigned short) -1; 
 
            mCurrentDevice.SetCurSel( lConfig.GetIndex() ); 
            OnSelchangeCurrentDevice(); 
            EnableControls( lConfig.GetCount() > 0 ); 
		} 
		catch ( ... ) 
		{ 
			CString lStr = "Unexpected exception"; 
			MessageBox( lStr, "Error", MB_OK  ); 
		} 
	}  
} 
 
BOOL CyDeviceDlg::PreTranslateMessage(MSG* pMsg)  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
 
	// allow tooltip messages to be filtered 
	if (CPropertyPage::PreTranslateMessage(pMsg)) 
		return TRUE; 
 
	if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST) 
	{ 
		// finally, translate the message 
		return ::TranslateAccelerator(m_hWnd, lApp->mAccelerators, pMsg); 
	} 
	return FALSE; 
} 
 
void CyDeviceDlg::OnFileNew()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lConfig = lApp->GetConfig(); 
    CyDevice& lDevice = lApp->GetDevice(); 
 
    if ( lConfig.GetCount() > 0 ) 
    { 
        switch ( MessageBox( "All your current settings will be lost!\nSave?", 
                             NULL, 
                             MB_YESNOCANCEL ) ) 
        { 
            case IDYES: 
                lApp->OnFileSave(); 
                break; 
 
            case IDCANCEL: 
                return; 
                break; 
        } 
 
        // delete all the config 
        while ( lConfig.DeleteDevice() == CY_RESULT_OK ); 
 
        // Remove the file name 
        lApp->mFileName = ""; 
 
        // Apply the change and voilą! 
        mCurrentDevice.ResetContent(); 
        mDeviceName = ""; 
        lApp->mPropertySheet.SetWindowText( "Camera Interface Application" ); 
        mDeviceMode = 0; 
        mIPName = ""; 
        mCameraName = ""; 
        lApp->SetCamera( 0 ); 
        mDeviceIP.SetWindowText( "" ); 
        mDeviceMAC1 = ""; 
        mDeviceMAC2 = ""; 
        mDeviceMAC3 = ""; 
        mDeviceMAC4 = ""; 
        mDeviceMAC5 = ""; 
        mDeviceMAC6 = ""; 
        UpdateData( FALSE ); 
        mMulticastEnabled = FALSE; 
        mMulticastMaster  = FALSE; 
        mMulticastNoData  = FALSE; 
        mMulticastAddress = ""; 
        mDeviceStatus.SetWindowText( "Not connected..." ); 
 
        // Reset the current device index 
        mCurrentDeviceIndex = (unsigned short) -1; 
 
        // Disconnect the current device 
        if ( lDevice.IsConnected() ) 
            lDevice.Disconnect(); 
 
        EnableControls( false ); 
        mCurrentDevice.SetCurSel( -1 ); 
    } 
} 
 
void CyDeviceDlg::OnDeltaposDevicePacketSizeSpin(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR; 
     
    CString lValue; 
    mPacketSizeControl.GetWindowText( lValue ); 
    unsigned long lSize = atol( (LPCTSTR) lValue ); 
 
    if ( lSize % 32 ) 
        lSize = ( lSize %32 ) * 32; 
 
    if ( pNMUpDown->iDelta > 0 ) 
    { 
        lSize += 32; 
    } 
    else 
    { 
        lSize -= 32; 
    } 
 
    if ( lSize < 32 ) 
        lSize = 32; 
    if ( lSize > 16224 ) 
        lSize = 16224; 
 
    lValue.Format( "%u", lSize ); 
    mPacketSizeControl.SetWindowText( lValue ); 
 
	*pResult = 0; 
} 
 
void CyDeviceDlg::OnDropFiles(HDROP hDropInfo)  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
 
    // verify that the current camera settings have beed save 
    if ( lApp->mConfigChanged ) 
    { 
        switch ( MessageBox( "Camera configuration has changed, but not saved.\n" 
                             "Continue without saving?", 
                             "Camera Interface Application", 
                             MB_YESNOCANCEL ) ) 
        { 
            case IDNO: 
                lApp->OnFileSave(); 
                break; 
 
            case IDCANCEL: 
                return; 
                break; 
        } 
    } 
 
    // at this point, the user clicked ok, so we don't save 
    lApp->mConfigChanged = false; 
 
	UINT lCount = DragQueryFile( hDropInfo, 0xFFFFFFFF, NULL, 0 ); 
    if ( lCount > 0 ) 
    { 
        // Get the file name 
        char lTemp[4096]; 
        DragQueryFile( hDropInfo, 0, lTemp, sizeof( lTemp ) ); 
 
        try 
        { 
            CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
            CyConfig& lConfig = lApp->GetConfig(); 
 
            CyXMLDocument lDocument( lTemp ); 
            if ( lDocument.LoadDocument( ) != CY_RESULT_OK ) 
            { 
                CyErrorInfo lError; 
                lDocument.GetErrorInfo( &lError ); 
                MessageBox( lError.mMessage ); 
                return; 
            } 
 
			lConfig.LoadFromXML( lDocument ); 
		    if ( lConfig.FindDeviceByName( "Default" ) != CY_RESULT_OK ) 
            { 
                lConfig.SetIndex( 0 ); 
            } 
 
            // Fill the device ID list box 
            mCurrentDevice.ResetContent(); 
            CString lStr; 
            for ( unsigned int i = 0; i < lConfig.GetCount(); ++i ) 
            { 
                lStr.Format( "%d", i ); 
                mCurrentDevice.AddString( lStr ); 
            } 
 
            // Reset the current device index 
            mCurrentDeviceIndex = (unsigned short) -1; 
 
            lApp->mFileName = lTemp; 
            mCurrentDevice.SetCurSel( lConfig.GetIndex() ); 
            OnSelchangeCurrentDevice(); 
            EnableControls( lConfig.GetCount() > 0 ); 
            		} 
		catch ( ... ) 
		{ 
			CString lStr = "Unexpected exception"; 
			MessageBox( lStr, "Error", MB_OK  ); 
		} 
    } 
	 
	CPropertyPage::OnDropFiles(hDropInfo); 
} 
 
void CyDeviceDlg::OnSelchangeDeviceMode()  
{ 
	SetModified(); 
 
    CString lValue; 
    mPacketSizeControl.GetWindowText( lValue ); 
    unsigned long lSize = atol( (LPCTSTR) lValue ); 
 
    if ( lSize > 16224 ) 
        lSize = 16224; 
 
    lValue.Format( "%u", lSize ); 
    mPacketSizeControl.SetWindowText( lValue ); 
 
    // re-adjust the adapter IDs 
    UpdateAdapterIDs(); 
} 
 
void CyDeviceDlg::OnDeviceReconnect()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyGrabber& lGrabber = lApp->GetGrabber(); 
    CyConfig& lConfig = lApp->GetConfig(); 
    CyDevice& lDevice = lApp->GetDevice(); 
    unsigned int lID; 
 
    BeginWaitCursor(); 
 
    // State of grabbing and displaying for channels 
    std::map< CyChannelID, bool > 
        lDisplaying; 
    std::map< CyChannelID, bool > 
        lGrabbing; 
 
    // check the state of the displays and grabbing for all channels 
    for ( lID = 0; lID < lDevice.GetChannelCount(); ++lID ) 
    { 
        // Get the channel from the config 
        const CyChannel lChannel( lDevice.GetChannel( lID ) ); 
 
        // Check the grabbing and stop it for the current channel 
        lGrabbing[ lID ] = lGrabber.IsStarted( lChannel ); 
        if ( lGrabbing[ lID ] ) 
            lGrabber.StopGrabbing( lChannel ); 
 
        // Check the display and stop it for the current channel 
        lDisplaying[ lID ] = lApp->GetDisplay().IsDisplaying( lID ); 
        if ( lDisplaying[ lID ] ) 
            lApp->GetDisplay().StopDisplaying( lID ); 
    } 
 
 
    if ( lGrabber.IsConnected() ) 
    { 
        lGrabber.Disconnect(); 
        ::Sleep( 500 ); 
    } 
 
    // Connect to the device 
    if ( lApp->HasCamera() ) 
        lGrabber.UnlinkFrom( lApp->GetCamera() );    
    mDeviceStatus.SetWindowText( "Not connected..." ); 
    if ( lGrabber.Connect( lConfig, GetConnectFlag( *this, lConfig ) ) != CY_RESULT_OK ) 
    { 
        CyErrorInfo lError; 
        lGrabber.GetErrorInfo( &lError ); 
 
        ostringstream lMessage; 
        lMessage << "Could not connect to device because " << lError.mMessage; 
        EndWaitCursor(); 
        AfxMessageBox( lMessage.str().c_str() ); 
        return; 
    } 
    mDeviceStatus.SetWindowText( "Connected to device..." ); 
 
    // Reset the camera settings 
    CyResult lResult = lApp->GetCamera().UpdateToCamera(); 
    if ( lResult != CY_RESULT_OK ) 
    { 
        CyErrorInfo lError; 
        lApp->GetCamera().GetErrorInfo( &lError ); 
 
        ostringstream lMessage; 
        lMessage << "Could not update camera because " << lError.mMessage; 
        AfxMessageBox( lMessage.str().c_str() ); 
        return; 
    } 
 
    if ( lApp->HasCamera() ) 
        lGrabber.LinkTo( lApp->GetCamera() );    
 
    // Get the device info 
    unsigned char lDevID, lModID, lSubID; 
    lGrabber.GetDevice().GetId( &lDevID, &lModID, &lSubID ); 
 
    // Generate the default device info text 
    CString lTemp; 
    lTemp.Format( "Device %u, Module %u, Sub-Module %u", 
                  lDevID, 
                  lModID, 
                  lSubID ); 
 
    // Ask the device list if there is a description available. 
    // If so, use it instead of the default 
    string lDeviceDesc, lModuleDesc, lSubDesc; 
    if ( CyDeviceList::GetInformation( lDevID, 
                                       lModID, 
                                       lSubID, 
                                       lDeviceDesc, 
                                       lModuleDesc, 
                                       lSubDesc ) == CY_RESULT_OK ) 
    { 
        lTemp.Format( "%s, %s", lDeviceDesc.c_str(), lModuleDesc.c_str() ); 
        if ( !lSubDesc.empty() ) 
        { 
            lTemp += ", "; 
            lTemp += lSubDesc.c_str(); 
        } 
    } 
 
    // Set the information in the page 
    mDeviceInfo.SetWindowText( lTemp ); 
    mDeviceID = lDevID; 
    mModuleID = lModID; 
    mSubID    = lSubID; 
 
    // Restart the displays and grabbing for the channels 
    // check the state of the displays and grabbing for all channels 
    for ( lID = 0; lID < lDevice.GetChannelCount(); ++lID ) 
    { 
        // Get the channel from the config 
        const CyChannel lChannel( lDevice.GetChannel( lID ) ); 
 
        // restart the grabbing 
        if ( lGrabbing[ lID ] ) 
            lGrabber.StartGrabbing( lChannel, lApp->GetBuffer( lChannel ) ); 
 
        // restart the display 
        if ( lDisplaying[ lID ] ) 
        { 
            // TODO: lFlags 
 
            lApp->GetDisplay().StartDisplaying( lApp->GetBuffer( lChannel ), lID, lApp->mBayerFlag ); 
        } 
    } 
 
    EndWaitCursor(); 
} 
 
void CyDeviceDlg::OnDeviceMulticast()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyConfig& lConfig = lApp->GetConfig(); 
    CyMulticastingDialog lDlg; 
 
    // Set the dialog information 
    lDlg.mEnabled = mMulticastEnabled != 0; 
    lDlg.mMaster  = mMulticastMaster != 0; 
    lDlg.mNoData  = mMulticastNoData != 0; 
 
    // Get the multicast address 
    if ( lDlg.mEnabled ) 
    { 
        unsigned int lIP1, lIP2, lIP3, lIP4; 
        if ( sscanf( (LPCTSTR) mMulticastAddress, 
                     "[%u.%u.%u.%u]", 
                     &lIP1, 
                     &lIP2, 
                     &lIP3, 
                     &lIP4 ) != 4 ) 
        { 
            MessageBox( "Invalid multicasting address in CyConfig" ); 
            return; 
        } 
 
        lDlg.mIP1 = lIP1; 
        lDlg.mIP2 = lIP2; 
        lDlg.mIP3 = lIP3; 
        lDlg.mIP4 = lIP4; 
    } 
 
    // Display the dialog and set the values on the config upon IDOK 
    if ( lDlg.DoModal() == IDOK ) 
    { 
        mMulticastEnabled = lDlg.mEnabled != 0; 
        mMulticastMaster  = lDlg.mMaster != 0; 
        mMulticastNoData  = lDlg.mNoData != 0; 
        mMulticastAddress.Format( "[%u.%u.%u.%u]", 
                                  lDlg.mIP1, 
                                  lDlg.mIP2, 
                                  lDlg.mIP3, 
                                  lDlg.mIP4 ); 
        SetModified(); 
    } 
} 
 
void CyDeviceDlg::WinHelp(DWORD dwData, UINT nCmd)  
{ 
	CPropertyPage::WinHelp( CyDeviceDlg::IDD, HELP_CONTEXT ); 
} 
 
void CyDeviceDlg::OnDeviceTimeout()  
{ 
    CyTimeoutDialog	lDialog; 
    CString lTemp; 
 
    lDialog.mImageSize = 0; 
 
    mPacketSizeControl.GetWindowText( lTemp ); 
    lDialog.mPacketSize = atol( lTemp ); 
 
    lDialog.mLinkType = mDeviceModeControl.GetCurSel(); 
 
    GetDlgItemText( IDC_DEVICE_REQUEST_TIMEOUT,lTemp ); 
    lDialog.mMaxTime = atol( lTemp ); 
 
    if ( lDialog.DoModal() == IDOK ) 
    { 
        lTemp.Format( "%lu", lDialog.mFirstPacketTimeout ); 
        SetDlgItemText( IDC_DEVICE_FIRST_PACKET_TIMEOUT, lTemp ); 
 
        lTemp.Format( "%lu", lDialog.mPacketTimeout ); 
        SetDlgItemText( IDC_DEVICE_PACKET_TIMEOUT, lTemp ); 
 
        lTemp.Format( "%lu", lDialog.mRequestTimeout ); 
        SetDlgItemText( IDC_DEVICE_REQUEST_TIMEOUT, lTemp ); 
    } 
} 
 
void CyDeviceDlg::OnFileSave()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
 
    lApp->OnFileSave(); 
} 
 
void CyDeviceDlg::OnFileSaveas()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
 
    lApp->OnFileSaveas(); 
} 
 
void CyDeviceDlg::OnSystemSerialPort()  
{ 
    CyApp* lApp = reinterpret_cast( AfxGetApp() ); 
    CyDevice& lDevice = lApp->GetDevice(); 
    CyAssert( lDevice.IsConnected() ); 
 
    if ( lDevice.IsSystemSerialPortEnabled() ) 
    { 
        if ( lDevice.DisableSystemSerialPort() == CY_RESULT_OK ) 
        { 
            SetDlgItemText( IDC_SYSTEM_SERIAL, "Enable Serial Port Link..." ); 
        } 
        else 
        { 
            CyErrorInfo lError; 
            lDevice.GetErrorInfo( &lError ); 
 
            ostringstream lMessage; 
            lMessage << "Could not stop system serial port because " << lError.mMessage; 
            AfxMessageBox( lMessage.str().c_str() ); 
        } 
    } 
    else 
    { 
        // Configure the serial port 
        CySerialConfigDialog lDialog; 
        if ( lDialog.DoModal() == IDOK ) 
        { 
            // Configure the iPORT serial port 
            CString lConfig; 
            lConfig.Format( "%u%c%u-%u", lDialog.GetBitSize(), lDialog.GetParity(), lDialog.GetStopBits(), lDialog.GetBaudRate() ); 
 
            // Enable the serial port 
            if ( lDevice.EnableSystemSerialPort( CyDevice::PORT_SERIAL_0, lDialog.mSystemPort + 1, lConfig ) == CY_RESULT_OK ) 
            { 
                SetDlgItemText( IDC_SYSTEM_SERIAL, "Disable Serial Port Link" ); 
            } 
            else 
            { 
                CyErrorInfo lError; 
                lDevice.GetErrorInfo( &lError ); 
 
                ostringstream lMessage; 
                lMessage << "Could not start system serial port because " << lError.mMessage; 
                AfxMessageBox( lMessage.str().c_str() ); 
            } 
        } 
    } 
} 
 
void CyDeviceDlg::UpdateAdapterIDs()  
{ 
	// Get the current device mode 
    CyConfig::AccessMode lMode; 
    switch( mDeviceModeControl.GetCurSel() ) 
    { 
        case 0 : lMode = CyConfig::MODE_DRV; break; 
        case 1 : lMode = CyConfig::MODE_UDP; break; 
        case 2 : lMode = CyConfig::MODE_FILTER; break; 
        default : CyAssert( false ); 
    } 
 
    // Save the current identifier for reselecting (if possible) 
    int lSelect = 0; // reselect the first item by default 
    CString lCurrentID; 
    if ( mAdapterIDControl.GetCurSel() != -1 ) 
        mAdapterIDControl.GetLBText( mAdapterIDControl.GetCurSel(), lCurrentID ); 
 
    // Fill the control with the adapter's found on the current mode 
    mAdapterIDControl.ResetContent(); 
    CyAdapterID lID; 
    for( unsigned long lIndex = 0; CyAdapterID::GetIdentifier( lMode, lIndex, lID ); ++lIndex ) 
    { 
        // add the current adapter ID to the control 
        int lItem = mAdapterIDControl.AddString( lID.GetIdentifier().c_str() ); 
 
        if ( lID.GetIdentifier() == (LPCTSTR) lCurrentID ) 
            lSelect = lItem; 
    } 
 
    // reset the current selection 
    mAdapterIDControl.SetCurSel( lSelect ); 
} 
 
void CyDeviceDlg::OnSelchangeDeviceAdapterId()  
{ 
    SetModified(); 
} 
 
void CyDeviceDlg::OnChangeDeviceMac()  
{ 
    SetModified(); 
 
    GetDlgItem( IDC_DEVICE_FORCE_IP )->EnableWindow( CanForceIPOnConnect( *this ) ); 
} 
 
void CyDeviceDlg::OnDeviceForceIp()  
{ 
    mForceIPOnConnect = reinterpret_cast( GetDlgItem( IDC_DEVICE_FORCE_IP ) )->GetCheck(); 
}