// KwLoRaToolDlg.cpp: 实现文件 // #include "pch.h" #include "framework.h" #include "KwLoRaTool.h" #include "KwLoRaToolDlg.h" #include "afxdialogex.h" #include #include #include #pragma comment(lib,"SetupAPI.lib") #include "../KLink1/KLink.h" #pragma comment(lib,"KLink1.lib") KLink1 myKlink1; #ifdef _DEBUG #define new DEBUG_NEW #endif const CKwLoRaToolDlg::stWLConfig defaultWLConfig = { .RF_T_Freq = 430620000, // uint32_t Hz .RF_R_Freq = 430620000, // uint32_t //Hz .nChnSpacing = 530, // uint16_t ChannelSpacing; //kHz .nCycleTime = 80, // CycleTime // .ModemType = 1, // //0: FSK, 1: LoRa .workMode = 3, // 0, None 1, Uni, 2 Thr, 3 Multi1, 4 Multi2, 5 MulMa .nChannel = 0, .bMaster = 0, .nRadioAddr = 1 , .bEnableMulti = 0, .Tx_Power = 20, // uchar Tx_Power; .LoraBandWidth = 1, // uchar LoraBandWidth; // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] .LoRaFactor = 5, // uchar LoRaFactor; // [SF5 .. SF 12] .LoRaCodingRate = 1, // uchar LoRaCodingRate; // [1 : 4/5, 2: 4/6, 3: 4/7, 4: 4/8 .NetWorkAddr = 0x00, // uint8_t NetWorkAddr; .DeviceAddr = 0x0102, // uint16_t DeviceAddr; .bEnableAddr = 0, // uchar bEnableAddr; .bEnableEncrypt = 0, // uchar bEnableEncrypt; .bEnableRelay = 0, // uchar bEnableRelay; .LoRaPreambleLen = 4, // uchar LoRaPreamble_Len; // 2 - 12 .bAutoPower = 1, .bAutoReSend = 1, // 自动重发 }; // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialogEx { public: CAboutDlg(); // 对话框数据 #ifdef AFX_DESIGN_TIME enum { IDD = IDD_ABOUTBOX }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CKwLoRaToolDlg 对话框 CKwLoRaToolDlg::CKwLoRaToolDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_KWLORATOOL_DIALOG, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CKwLoRaToolDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_MFCBUTTON1, m_button1); DDX_Control(pDX, IDC_BUTTON_CONN, m_Button_Conn); DDX_Control(pDX, IDC_BUTTON_EN_CMD, m_MFCButton_EN_CMD); DDX_Control(pDX, IDC_BUTTON_SCAN_COM, m_MFCButton_SCAN_COM); DDX_Control(pDX, IDC_BUTTON_EXPORT_CONFIG, m_MFCButton_EXPORT_CONFIG); DDX_Control(pDX, IDC_BUTTON_CLEAR_LOG, m_MFCButton_CLEAR_LOG); DDX_Control(pDX, IDC_BUTTON_SEND_NID, m_MFCButton_SEND_NID); DDX_Control(pDX, IDC_BUTTON_SEND_ADDR, m_MFCButton_SEND_ADDR); DDX_Control(pDX, IDC_BUTTON_SEND_EN_ADDR, m_MFCButton_SEND_EN_ADDR); DDX_Control(pDX, IDC_BUTTON_SEND_ENCYPT, m_MFCButton_SEND_ENCYPT); DDX_Control(pDX, IDC_BUTTON_SEND_RELAY, m_MFCButton_SEND_RELAY); DDX_Control(pDX, IDC_BUTTON_SEND_T_FREQ, m_MFCButton_SEND_T_FREQ); DDX_Control(pDX, IDC_BUTTON_SEND_R_FREQ, m_MFCButton_SEND_R_FREQ); DDX_Control(pDX, IDC_BUTTON_SEND_RFPOWER, m_MFCButton_SEND_RF_POWER); DDX_Control(pDX, IDC_BUTTON_SEND_RFBW, m_MFCButton_SEND_RF_BW); DDX_Control(pDX, IDC_BUTTON_SEND_FACTOR, m_MFCButton_SEND_LORA_FACTOR); DDX_Control(pDX, IDC_BUTTON_SEND_FEC_RATE, m_MFCButton_SEND_LORA_FEC_RATE); DDX_Control(pDX, IDC_BUTTON_SEND_COM_BAUDRATE, m_MFCButton_SEND_COM_BAUDRATE); DDX_Control(pDX, IDC_BUTTON_SEND_COM_FORMAT, m_MFCButton_SEND_COM_FORMAT); DDX_Control(pDX, IDC_BUTTON_SEND_DEFAULT, m_MFCButton_SEND_DEFAULT); DDX_Control(pDX, IDC_BUTTON_SEND_RESET, m_MFCButton_SEND_RESET); DDX_Control(pDX, IDC_BUTTON_READ, m_MFCButton_RAED); DDX_Control(pDX, IDC_BUTTON_WRITE, m_MFCButton_WRITE); DDX_Control(pDX, IDC_COMBO_COM_SEL, m_combo_com_sel); } BEGIN_MESSAGE_MAP(CKwLoRaToolDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_DEVICECHANGE() ON_BN_CLICKED(IDC_BUTTON_CONN, &CKwLoRaToolDlg::OnBnClickedButtonConn) ON_BN_CLICKED(IDC_BUTTON_EN_CMD, &CKwLoRaToolDlg::OnBnClickedButtonEnCmd) ON_BN_CLICKED(IDC_BUTTON_SCAN_COM, &CKwLoRaToolDlg::OnBnClickedButtonScanCom) ON_BN_CLICKED(IDC_BUTTON_EXPORT_CONFIG, &CKwLoRaToolDlg::OnBnClickedButtonExportConfig) ON_BN_CLICKED(IDC_BUTTON_CLEAR_LOG, &CKwLoRaToolDlg::OnBnClickedButtonClearLog) ON_BN_CLICKED(IDC_BUTTON_SEND_NID, &CKwLoRaToolDlg::OnBnClickedButtonSendNid) ON_BN_CLICKED(IDC_BUTTON_SEND_ADDR, &CKwLoRaToolDlg::OnBnClickedButtonSendAddr) ON_BN_CLICKED(IDC_BUTTON_SEND_EN_ADDR, &CKwLoRaToolDlg::OnBnClickedButtonSendEnAddr) ON_BN_CLICKED(IDC_BUTTON_SEND_ENCYPT, &CKwLoRaToolDlg::OnBnClickedButtonSendEncypt) ON_BN_CLICKED(IDC_BUTTON_SEND_RELAY, &CKwLoRaToolDlg::OnBnClickedButtonSendRelay) ON_BN_CLICKED(IDC_BUTTON_SEND_T_FREQ, &CKwLoRaToolDlg::OnBnClickedButtonSendTFreq) ON_BN_CLICKED(IDC_BUTTON_SEND_R_FREQ, &CKwLoRaToolDlg::OnBnClickedButtonSendRFreq) ON_BN_CLICKED(IDC_BUTTON_SEND_RFPOWER, &CKwLoRaToolDlg::OnBnClickedButtonSendRfpower) ON_BN_CLICKED(IDC_BUTTON_SEND_RFBW, &CKwLoRaToolDlg::OnBnClickedButtonSendRfbw) ON_BN_CLICKED(IDC_BUTTON_SEND_FACTOR, &CKwLoRaToolDlg::OnBnClickedButtonSendFactor) ON_BN_CLICKED(IDC_BUTTON_SEND_FEC_RATE, &CKwLoRaToolDlg::OnBnClickedButtonSendFecRate) ON_BN_CLICKED(IDC_BUTTON_SEND_COM_BAUDRATE, &CKwLoRaToolDlg::OnBnClickedButtonSendComBaudrate) ON_BN_CLICKED(IDC_BUTTON_SEND_COM_FORMAT, &CKwLoRaToolDlg::OnBnClickedButtonSendComFormat) ON_BN_CLICKED(IDC_BUTTON_SEND_DEFAULT, &CKwLoRaToolDlg::OnBnClickedButtonSendDefault) ON_BN_CLICKED(IDC_BUTTON_SEND_RESET, &CKwLoRaToolDlg::OnBnClickedButtonSendReset) ON_BN_CLICKED(IDC_BUTTON_READ, &CKwLoRaToolDlg::OnBnClickedButtonRead) ON_BN_CLICKED(IDC_BUTTON_WRITE, &CKwLoRaToolDlg::OnBnClickedButtonWrite) ON_WM_TIMER() END_MESSAGE_MAP() // 串口接收数据, 响应方式 // 1, 阻塞等待 // 2, 非阻塞,轮询 // 3, 回调函数 // 4, windows消息 // // CKwLoRaToolDlg 消息处理程序 unsigned int BtnIdList[] = { IDC_BUTTON_CONN,IDC_BUTTON_EN_CMD,IDC_BUTTON_SCAN_COM,IDC_BUTTON_EXPORT_CONFIG, IDC_BUTTON_CLEAR_LOG, IDC_BUTTON_SEND_NID,IDC_BUTTON_SEND_ADDR,IDC_BUTTON_SEND_EN_ADDR,IDC_BUTTON_SEND_ENCYPT,IDC_BUTTON_SEND_RELAY, IDC_BUTTON_SEND_T_FREQ,IDC_BUTTON_SEND_R_FREQ,IDC_BUTTON_SEND_RFPOWER,IDC_BUTTON_SEND_RFBW,IDC_BUTTON_SEND_FACTOR,IDC_BUTTON_SEND_FEC_RATE, IDC_BUTTON_SEND_COM_BAUDRATE,IDC_BUTTON_SEND_COM_FORMAT, IDC_BUTTON_SEND_DEFAULT,IDC_BUTTON_SEND_RESET,IDC_BUTTON_READ,IDC_BUTTON_WRITE }; BOOL CKwLoRaToolDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != nullptr) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 m_button1.m_bTransparent = FALSE; m_button1.m_bDontUseWinXPTheme = TRUE; m_button1.SetFaceColor(RGB(128, 128, 255)); for (int i = 0; i < sizeof(BtnIdList) / sizeof(unsigned int); i++){ ((CMFCButton*)GetDlgItem(BtnIdList[i]))->m_bTransparent = FALSE; ((CMFCButton*)GetDlgItem(BtnIdList[i]))->m_bDontUseWinXPTheme = TRUE; ((CMFCButton*)GetDlgItem(BtnIdList[i]))->SetFaceColor(RGB(40, 40, 255)); ((CMFCButton*)GetDlgItem(BtnIdList[i]))->SetTextColor(RGB(255, 255, 255)); } myLogger1.AttachWnd(GetDlgItem(IDC_EDIT_LOG)->m_hWnd); myLogger1.bShowLog[0] = 1; myLogger1.bSaveLog[0] = 1; myLogger1.SetLogPathName(_T("D:/logs/FirmWareTool"), _T("RunLog")); myLogger1.bShowThreadId = 0; myLogger1.bShowChannel = 0; SysLog(_T("Program Start \r\n")); SetTimer(0, 100, NULL);//delayinit SetTimer(2, 100, NULL);//display // LoadConfig(); myWLConfig = defaultWLConfig; ShowParams(); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CKwLoRaToolDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CKwLoRaToolDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CKwLoRaToolDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } int CKwLoRaToolDlg::DelayInit() { // TODO: 在此处添加实现代码. return 0; } //得到COMx的名字 //namebuf:用于存放名字的缓冲区 //bufsize:缓冲区大小 //comx:要查找的COM编号.例如:COM1,COM2,COM3.... //返回值:0,成功找到了. // 1,失败. int get_com_name(CString comx, CString& namebuf) { HDEVINFO hdinfo; int res = 0; SP_DEVINFO_DATA hddevinfo = { sizeof(SP_DEVINFO_DATA) }; hdinfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT);//获取PORTS类别的已安装设备信息 if (hdinfo != INVALID_HANDLE_VALUE)//获取成功 { for (int i = 0; SetupDiEnumDeviceInfo(hdinfo, i, &hddevinfo); i++)//轮询所有已安装设备 { SetupDiGetDeviceRegistryProperty(hdinfo, &hddevinfo, SPDRP_FRIENDLYNAME, 0, (PBYTE)(namebuf.GetBuffer(2048)), 2048, 0);//获得单个装置的详细资料 namebuf.ReleaseBuffer(); if (namebuf.Find(comx) != -1) { res = 1; break; } //char *t; //t = strstr(namebuf, comx); //if (t) //{ // t--; // *t = '\0';//添加结束符,作用就是把"(COMX)"这段字符去掉 // res = 0; // break;//成功找到了COMx的名字 //} } } return res; } int CKwLoRaToolDlg::ShowParams() { // TODO: 在此处添加实现代码. UpdateComList(); CString s1; s1.Format(_T("%02X"), myWLConfig.NetWorkAddr); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.nRadioAddr); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.nChannel); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.DeviceAddr); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.bEnableAddr); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.bEnableEncrypt); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.bEnableRelay); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.NetWorkAddr); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.NetWorkAddr); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.Tx_Power); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.LoraBandWidth); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.LoRaFactor); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.LoRaCodingRate); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.LoRaPreambleLen); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.bAutoPower); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.bAutoReSend); SetDlgItemText(IDC_EDIT_NET_ID, s1); s1.Format(_T("%02X"), myWLConfig.NetWorkAddr); return 0; } int CKwLoRaToolDlg::UpdateComList() { // TODO: 在此处添加实现代码. CString s1, s2; m_combo_com_sel.Clear(); m_combo_com_sel.ResetContent(); for (int i = 1; i <= 16; i++) { s1.Format(_T("COM%d"), i); if (get_com_name(s1 + _T(")"), s2)) { s1 = s2; } m_combo_com_sel.AddString(s1); } m_combo_com_sel.SetCurSel(0); ((CComboBox*)GetDlgItem(IDC_COMBO_CONN_BAUDRATE))->SetCurSel(3); ((CComboBox*)GetDlgItem(IDC_COMBO_CONN_PARITY))->SetCurSel(0); ((CComboBox*)GetDlgItem(IDC_COMBO_CONN_DATABIT))->SetCurSel(1); ((CComboBox*)GetDlgItem(IDC_COMBO_CONN_STOPBIT))->SetCurSel(0); return 0; HKEY hKey; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Hardware\\DeviceMap\\SerialComm"), NULL, KEY_READ, &hKey) == ERROR_SUCCESS) { TCHAR szPortName[256], szComName[256]; DWORD dwLong, dwSize; int nCount = 0; m_combo_com_sel.ResetContent(); while (true) { dwLong = dwSize = 256; if (RegEnumValue(hKey, nCount, szPortName, &dwLong, NULL, NULL, (PUCHAR)szComName, &dwSize) == ERROR_NO_MORE_ITEMS) break; m_combo_com_sel.InsertString(nCount, szComName); nCount++; } RegCloseKey(hKey); m_combo_com_sel.SetCurSel(0); } return 0; } ///* BOOL CKwLoRaToolDlg::OnDeviceChange(UINT nEventType, DWORD_PTR dwData) { if (nEventType == DBT_DEVNODES_CHANGED) UpdateComList(); return TRUE; } // */ int CKwLoRaToolDlg::GetParams() { // TODO: 在此处添加实现代码. return 0; } void CKwLoRaToolDlg::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if (nIDEvent == 0) { KillTimer(0); DelayInit(); } else if (nIDEvent == 1) { //ProcessPLC(); } else if (nIDEvent == 2) { // ProcessInput(); //DrawPic1(); myLogger1.UpdateLogDisplay(0); } else if (nIDEvent == 3) { } else { } CDialogEx::OnTimer(nIDEvent); } void CKwLoRaToolDlg::OnBnClickedButtonConn() { // TODO: 在此添加控件通知处理程序代码 myKlink1.Connect(); } void CKwLoRaToolDlg::OnBnClickedButtonEnCmd() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonScanCom() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonExportConfig() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendNid() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendAddr() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendEnAddr() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendEncypt() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendRelay() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendTFreq() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendRFreq() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendRfpower() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendRfbw() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendFactor() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendFecRate() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendComBaudrate() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendComFormat() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendDefault() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonSendReset() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonRead() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonWrite() { // TODO: 在此添加控件通知处理程序代码 } void CKwLoRaToolDlg::OnBnClickedButtonClearLog() { // TODO: 在此添加控件通知处理程序代码 }