⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configdlg.cpp

📁 Bluetooth 2.0 虚拟成串口 设置代码对蓝牙虚拟串口的设备编写程序有很大的帮助
💻 CPP
字号:
// ConfigDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BprConfig.h"
#include "ConfigDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// definition of my global constants & variables

CFG_INF cur_config, new_config;

static void PORT_EventHandler(unsigned char event, PORT_EVENT_PARA *event_para);

/////////////////////////////////////////////////////////////////////////////
// CConfigDlg dialog


CConfigDlg::CConfigDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConfigDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConfigDlg)
	//}}AFX_DATA_INIT
}


void CConfigDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConfigDlg)
	DDX_Control(pDX, IDC_PRINT_FW_INFO, m_print_fw_info);
	DDX_Control(pDX, IDC_FW_VERSION, m_fw_version);
	DDX_Control(pDX, IDC_WAIT_FOR_ALL, m_wait_for_all);
	DDX_Control(pDX, IDC_AUTO_CONNECT, m_auto_connect);
	DDX_Control(pDX, IDC_ADAPTER_FLOWCTRL, m_adapter_flowctrl);
	DDX_Control(pDX, IDC_BD_ADDR, m_bd_addr);
	DDX_Control(pDX, IDC_AUTO_DETECT, m_auto_detect);
	DDX_Control(pDX, IDC_ROLE, m_role);
	DDX_Control(pDX, IDC_DEV_NAME, m_dev_name);
	DDX_Control(pDX, IDC_DEV_LOCATION, m_dev_location);
	DDX_Control(pDX, IDC_ADAPTER_BAUDRATE, m_adapter_baudrate);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CConfigDlg, CDialog)
	//{{AFX_MSG_MAP(CConfigDlg)
	ON_BN_CLICKED(IDC_AUTO_DETECT, OnAutoDetect)
	ON_CBN_SELENDOK(IDC_ROLE, OnSelendokRole)
	ON_BN_CLICKED(IDC_AUTO_CONNECT, OnAutoConnect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConfigDlg message handlers

BOOL CConfigDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();
	
    // TODO: Add extra initialization here

    // register PORT event handler 
    PORT_RegisterEventHandler((long)PORT_EventHandler, 0);

//	Sleep(1000);

    // first, read adapter's Bluetooth address
    PORT_ReadBD_Addr();

    if (WaitForSingleObject(hEvent, 2000) != WAIT_OBJECT_0) {
        return FALSE;
    }

    // next, read adapter's firmware version
    PORT_ReadFW_Version();

    if (WaitForSingleObject(hEvent, 2000) != WAIT_OBJECT_0) {
        return FALSE;
    }

    // finally, read the config information from the adapter
    PORT_ReadConfig();

    if (WaitForSingleObject(hEvent, 2000) != WAIT_OBJECT_0) {
        return FALSE;
    }

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

void CConfigDlg::OnSelendokRole() 
{
	// TODO: Add your control notification handler code here
	
    m_auto_connect.EnableWindow(TRUE);
    m_wait_for_all.EnableWindow(TRUE);
    m_auto_detect.EnableWindow(TRUE);
    m_dev_name.EnableWindow(TRUE);
    m_dev_location.EnableWindow(TRUE);
    m_print_fw_info.EnableWindow(TRUE);

    if (m_role.GetCurSel() == CLIENT_DEVICE) {
        m_auto_detect.SetCheck(FALSE);
        m_auto_detect.EnableWindow(FALSE);
        
        m_dev_name.SetWindowText("");
        m_dev_name.EnableWindow(FALSE);
        
        m_dev_location.SetWindowText("");
        m_dev_location.EnableWindow(FALSE);
        
        m_print_fw_info.SetCheck(FALSE);
        m_print_fw_info.EnableWindow(FALSE);

        if (!m_auto_connect.GetCheck()) {
            m_wait_for_all.EnableWindow(FALSE);
        }
    }
    else {
        m_auto_connect.SetCheck(FALSE);
        m_auto_connect.EnableWindow(FALSE);

        m_wait_for_all.SetCheck(FALSE);
        m_wait_for_all.EnableWindow(FALSE);

        if (m_auto_detect.GetCheck()) {
            m_dev_name.EnableWindow(FALSE);
        }
    }
}

void CConfigDlg::OnAutoConnect() 
{
	// TODO: Add your control notification handler code here
	
    if (m_auto_connect.GetCheck()) {
        m_wait_for_all.EnableWindow(TRUE);
    }
    else {
        m_wait_for_all.SetCheck(FALSE);
        m_wait_for_all.EnableWindow(FALSE);
    }
}

void CConfigDlg::OnAutoDetect() 
{
	// TODO: Add your control notification handler code here
	
    if (m_auto_detect.GetCheck()) {
        m_dev_name.EnableWindow(FALSE);
    }
    else {
        m_dev_name.EnableWindow(TRUE);
    }
}

void CConfigDlg::OnOK() 
{
    static unsigned char to_fw_baudrate[4] = 
        {BR_DIP_SW, BR_9600, BR_19200, BR_38400};

	// TODO: Add extra validation here

    memset((void *)&new_config, 0, sizeof(CFG_INF));

    // read config and check if it has been changed
    new_config.Role = m_role.GetCurSel();
    new_config.Baudrate = to_fw_baudrate[m_adapter_baudrate.GetCurSel()];
    new_config.FlowControl = m_adapter_flowctrl.GetCurSel();
    new_config.AutoConnect = m_auto_connect.GetCheck();
    new_config.WaitForAllConnected = m_wait_for_all.GetCheck();
    new_config.AutoDetect = m_auto_detect.GetCheck();
    m_dev_name.GetWindowText(new_config.DevName, sizeof(DEV_NAME));
    m_dev_location.GetWindowText(new_config.DevLocation, sizeof(DEV_LOCATION));
    new_config.PrintFW_Info = m_print_fw_info.GetCheck();

    if (memcmp((void *)&cur_config, (void *)&new_config, sizeof(CFG_INF))) {
        // changed; save it into the adapter's flash memory
        PORT_WriteConfig(new_config.Role, 
                         new_config.Baudrate, 
                         new_config.FlowControl,
                         new_config.AutoConnect,
                         new_config.WaitForAllConnected,
                         new_config.PrintFW_Info,
                         new_config.AutoDetect,
                         new_config.DevName, 
                         new_config.DevLocation);

        if (WaitForSingleObject(hEvent, 3000) != WAIT_OBJECT_0) {
            MessageBox("Can't write configuration data to the Adapter.", 
                       "Error", MB_ICONERROR | MB_OK);
        }
    }
	
	CDialog::OnOK();
}

static void 
PORT_EventHandler(unsigned char event, PORT_EVENT_PARA *event_para)
{
    char bd_addr[20];
    static unsigned char from_fw_baudrate[BR_115200 + 1] = 
        {0, 0, 1, 0, 2, 3, 0, 0};

	switch (event) {
    case REPORT_BD_ADDR:
        memcpy(local_bd_addr, event_para->ReportBD_Addr.BD_Addr, 6);
        SetEvent(hEvent);
        break;

    case REPORT_FW_VERSION:
        memcpy(fw_version, event_para->ReportFW_Version.FW_Version, 14);
        SetEvent(hEvent);
        break;

    case READ_CONFIG_RESULT:
        memset((void *)&cur_config, 0, sizeof(CFG_INF));

        // read the result
        cur_config.Role = event_para->ReadConfigResult.Role;
        cur_config.Baudrate = event_para->ReadConfigResult.Baudrate;
        cur_config.FlowControl = event_para->ReadConfigResult.FlowControl;
        cur_config.AutoConnect = event_para->ReadConfigResult.AutoConnect;
        cur_config.WaitForAllConnected = event_para->ReadConfigResult.WaitForAllConnected;
        cur_config.PrintFW_Info = event_para->ReadConfigResult.PrintFW_Info;
        cur_config.AutoDetect = event_para->ReadConfigResult.DeviceNameAutoDetect;
        strcpy(cur_config.DevName, event_para->ReadConfigResult.DeviceName);
        strcpy(cur_config.DevLocation, event_para->ReadConfigResult.DeviceLocation);

        // update the dialog
        sprintf(bd_addr, "%02X:%02X:%02X:%02X:%02X:%02X", 
                local_bd_addr[5], local_bd_addr[4], local_bd_addr[3], 
                local_bd_addr[2], local_bd_addr[1], local_bd_addr[0]);
        theApp.pConfigDlg->m_bd_addr.SetWindowText(bd_addr);
        theApp.pConfigDlg->m_fw_version.SetWindowText(fw_version);
        theApp.pConfigDlg->m_role.SetCurSel(cur_config.Role);
        theApp.pConfigDlg->m_adapter_baudrate.SetCurSel(from_fw_baudrate[cur_config.Baudrate]);
        theApp.pConfigDlg->m_adapter_flowctrl.SetCurSel(cur_config.FlowControl);
        theApp.pConfigDlg->m_auto_connect.SetCheck(cur_config.AutoConnect);
        theApp.pConfigDlg->m_wait_for_all.SetCheck(cur_config.WaitForAllConnected);
        theApp.pConfigDlg->m_auto_detect.SetCheck(cur_config.AutoDetect);
        theApp.pConfigDlg->m_dev_name.SetWindowText(cur_config.DevName);
        theApp.pConfigDlg->m_dev_location.SetWindowText(cur_config.DevLocation);
        theApp.pConfigDlg->m_print_fw_info.SetCheck(cur_config.PrintFW_Info);

        theApp.pConfigDlg->OnSelendokRole();
		break;

    case WRITE_CONFIG_RESULT:
        // check if the result is OK
        if (event_para->WriteConfigResult.Result) {
            memcpy((void *)&cur_config, (void *)&new_config, sizeof(CFG_INF));
            Sleep(1000);
            SetEvent(hEvent);
        }
        break;

	default:
		// just ignore other events
		break;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -