串口操作类以及自动连接指定WiFi禁用启用WiFi和自动获取串口号

    xiaoxiao2021-11-29  21

    #pragma once #include <wlanapi.h> #include <DhcpCSdk.h> #include <WS2tcpip.h> #pragma comment(lib, "wlanapi.lib") #pragma comment(lib, "IPHlpApi.lib") #pragma comment(lib, "Ws2_32.lib") class CDongleCom { public: CDongleCom(); ~CDongleCom(); public: static int GetCurrentCom();  //自动获取串口号 static BOOL DisableWifi(BOOL bState = TRUE);//禁用或启用WiFi网卡 static BOOL ConnectWifi(CString sWifiName = _T("TP-LINK_ZXHK"));//自动连接指定WiFi BOOL InitCom(int iPort = 1, int iBaudRate = 115200, BYTE ByteSize = 8, BYTE StopBits = ONESTOPBIT, BYTE Parity = NOPARITY); void CloseCom(); BOOL HeatingFlash(double fOnTime=5, double fOffTime=5, int iCircle=5); void FlashOff(); BOOL PowerControl(double fOnTime, double fOffTime, int iCircle); BOOL PowerOff(); BOOL HeatingOn(); BOOL HeatingOff(); BOOL GetVersion(); BOOL GetComState(); protected: BOOL SendData(unsigned char *pData, int iDataLen); BOOL ReceiveData(unsigned char *pBuff, DWORD &dwLen); private: HANDLE m_hCom; BOOL m_bInited;

    };

    //CPP

    #include "stdafx.h" #include "DongleCom.h" CWinThread *g_pThread = NULL; UINT FlashThread(LPVOID lp); int g_nOnTime, g_nOffTime, g_nCircle; BOOL g_bRunFlash; CDongleCom::CDongleCom() { m_hCom = NULL; m_bInited = FALSE; } CDongleCom::~CDongleCom() { } BOOL CDongleCom::GetComState() { return m_bInited; } //初始化串口 BOOL CDongleCom::InitCom(int iPort, int iBaudRate, BYTE ByteSize, BYTE StopBits, BYTE Parity) { if (iPort<0) { return FALSE; } CloseCom(); CString chCom; chCom.Format(_T("\\\\.\\COM%d"), iPort); m_hCom = CreateFile(chCom, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (!m_hCom) { GetLastError(); AfxMessageBox(_T("COM initialization failure!")); return FALSE; } SetupComm(m_hCom, 1024, 1024); DCB dcb; GetCommState(m_hCom, &dcb); dcb.BaudRate = iBaudRate; dcb.ByteSize = ByteSize; dcb.StopBits = StopBits; dcb.Parity = Parity; SetCommState(m_hCom, &dcb); //SetCommMask(); COMMTIMEOUTS comtimes; GetCommTimeouts(m_hCom, &comtimes); comtimes.ReadTotalTimeoutMultiplier = 1000; comtimes.ReadTotalTimeoutConstant = 100; SetCommTimeouts(m_hCom, &comtimes); m_bInited = TRUE; return TRUE; } //关闭串口 void CDongleCom::CloseCom() { if (m_hCom) { CloseHandle(m_hCom); m_hCom = NULL; m_bInited = FALSE; } } //发送数据 BOOL CDongleCom::SendData(unsigned char *pData, int iDataLen) { unsigned char chSendBuf[1024]; memset(chSendBuf, 0, 1024); for (int i = 0; i<iDataLen; i++) { chSendBuf[i] = pData[i]; } PurgeComm(m_hCom, PURGE_TXCLEAR | PURGE_RXCLEAR); DWORD dwRt, dwErr; COMSTAT comst; if (!WriteFile(m_hCom, chSendBuf, iDataLen, &dwRt, NULL)) { ClearCommError(m_hCom, &dwErr, &comst); return FALSE; } return TRUE; } //接受数据 BOOL CDongleCom::ReceiveData(unsigned char *pBuff, DWORD &dwLen) { unsigned char chRecvBuf[1024]; DWORD iRecvLen; memset(chRecvBuf, 0, 1024); if (!ReadFile(m_hCom, &chRecvBuf, dwLen, &iRecvLen, NULL)) { return FALSE; } memcpy(pBuff, chRecvBuf, iRecvLen); dwLen = iRecvLen; return TRUE; } //开启电源灯 BOOL CDongleCom::HeatingOn() { unsigned char chBuff[2]; memset(chBuff, 0x17, 2); if (!SendData(chBuff, 2)) { return FALSE; } //DWORD dwLen = 1; //memset(chBuff, 0, 2); //if (!ReceiveData(chBuff, dwLen)) //{ // return FALSE; //} //if (1 != dwLen) //{ // return FALSE; //} return TRUE; } //关闭电源灯 BOOL CDongleCom::HeatingOff() { unsigned char chBuff[2]; memset(chBuff, 0x16, 2); if (!SendData(chBuff, 2)) { return FALSE; } //DWORD dwLen = 1; //memset(chBuff, 0, sizeof(chBuff)); //if (!ReceiveData(chBuff, dwLen)) //{ // return FALSE; //} //if (1 != dwLen) //{ // return FALSE; //} return TRUE; } //开始软件循环控制 BOOL CDongleCom::HeatingFlash(double fOnTime, double fOffTime, int iCircle) { g_nOnTime = int(fOnTime * 1000); g_nOffTime = int(fOffTime * 1000); g_nCircle = iCircle; g_bRunFlash = TRUE; if (NULL == (g_pThread =AfxBeginThread(FlashThread, this))) { return FALSE; } return TRUE; } //软件循环线程 UINT FlashThread(LPVOID lp) { CDongleCom *pDongleCom = (CDongleCom*)lp; for (int i = 0; i < g_nCircle; i++) { if (!pDongleCom->HeatingOn() || !g_bRunFlash) { AfxMessageBox(_T("HeatingOn failure!")); return FALSE; } Sleep(g_nOnTime); if (!pDongleCom->HeatingOff() || !g_bRunFlash) { AfxMessageBox(_T("HeatingOff failure!")); return FALSE; } Sleep(g_nOffTime); if (!g_bRunFlash) { return FALSE; } } return 0; } //停止软件循环控制 void CDongleCom::FlashOff() { if (NULL != g_pThread) { TerminateThread(g_pThread->m_hThread, 1); g_pThread = NULL; } g_bRunFlash = FALSE; HeatingOff(); } //开始硬件循环控制 BOOL CDongleCom::PowerControl(double fOnTime, double fOffTime, int iCircle) { int iOnTime = int(fOnTime * 1000) / 500; int iOffTime = int(fOffTime * 1000) / 500; UCHAR chBuff[5]; memset(chBuff, 0, 5); chBuff[0] = 0x16; chBuff[1] = iOnTime; chBuff[2] = iOffTime; chBuff[3] = iCircle; chBuff[4] = 0x01; if (!SendData(chBuff, 5)) { return FALSE; } //DWORD dwLen = 1; //memset(chBuff, 0, 5); //if (!ReceiveData(chBuff, dwLen)) //{ // return FALSE; //} //if (1 != dwLen) //{ // AfxMessageBox(_T("Number of data access is not correct!")); // return FALSE; //} return TRUE; } //停止硬件循环控制 BOOL CDongleCom::PowerOff() { UCHAR chBuff[2]; memset(chBuff, 0x15, 2); if (!SendData(chBuff, 2)) { return FALSE; } //DWORD dwLen = 1; //memset(chBuff, 0, 2); //if (!ReceiveData(chBuff, dwLen)) //{ // return FALSE; //} //if (1 != dwLen) //{ // return FALSE; //} return TRUE; } //自动关闭 和 开启 WiFi BOOL CDongleCom::DisableWifi(BOOL bState) { HANDLE hWIFI; DWORD dwMaxClient = 2; DWORD dwCurVersion = 0; //PWLAN_INTERFACE_INFO pIfInfo = NULL; PWLAN_INTERFACE_INFO_LIST pIfList = NULL; DWORD dwRst = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hWIFI); if (dwRst != ERROR_SUCCESS) { return FALSE; } dwRst = WlanEnumInterfaces(hWIFI, NULL, &pIfList); if (dwRst != ERROR_SUCCESS) { return FALSE; } WLAN_PHY_RADIO_STATE state; state.dwPhyIndex = 0; if (bState) { state.dot11SoftwareRadioState = dot11_radio_state_off; } else { state.dot11SoftwareRadioState = dot11_radio_state_on; } PVOID pData = &state; dwRst = WlanSetInterface(hWIFI, &pIfList->InterfaceInfo[0].InterfaceGuid, wlan_intf_opcode_radio_state, sizeof(WLAN_PHY_RADIO_STATE), pData, NULL); if (dwRst != ERROR_SUCCESS) { return FALSE; } return TRUE; } //连接已有WiFi BOOL CDongleCom::ConnectWifi(CString sWifiName) { HANDLE hWIFI; DWORD dwMaxClient = 2; DWORD dwCurVersion = 0; PWLAN_INTERFACE_INFO_LIST pIfList = NULL; DWORD dwRst = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hWIFI); if (dwRst != ERROR_SUCCESS) { return FALSE; } dwRst = WlanEnumInterfaces(hWIFI, NULL, &pIfList); if (dwRst != ERROR_SUCCESS) { return FALSE; } PWLAN_AVAILABLE_NETWORK_LIST pWLAN_AVAILABLE_NETWORK_LIST = NULL; PWLAN_INTERFACE_INFO pIfInfo = NULL; dwRst = WlanGetAvailableNetworkList(hWIFI, &pIfList->InterfaceInfo[0].InterfaceGuid, WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES, NULL, &pWLAN_AVAILABLE_NETWORK_LIST); if (dwRst != ERROR_SUCCESS) { return FALSE; } WLAN_AVAILABLE_NETWORK wlanAN; WLAN_CONNECTION_PARAMETERS wlanConnPara; wlanConnPara.wlanConnectionMode = wlan_connection_mode_profile; //YES,WE CONNECT AP VIA THE PROFILE   wlanConnPara.pDot11Ssid = NULL;                                 // SET SSID NULL   wlanConnPara.dot11BssType = dot11_BSS_type_infrastructure;      //dot11_BSS_type_any,I do not need it this time.           wlanConnPara.pDesiredBssidList = NULL;                          // the desired BSSID list is empty   wlanConnPara.dwFlags = WLAN_CONNECTION_HIDDEN_NETWORK; int iCount = pWLAN_AVAILABLE_NETWORK_LIST->dwNumberOfItems; CString sText; int i = 0; for (; i < iCount; i++) { wlanAN = pWLAN_AVAILABLE_NETWORK_LIST->Network[i]; sText = wlanAN.strProfileName; if (sWifiName == sText) { if (wlanAN.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) { break; } wlanConnPara.strProfile = wlanAN.strProfileName;                // set the profile name   dwRst = WlanConnect(hWIFI, &pIfList->InterfaceInfo[0].InterfaceGuid, &wlanConnPara, NULL); if (ERROR_SUCCESS != dwRst) { return FALSE; } break; } if (wlanAN.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) { if (sWifiName == sText) { break; } dwRst = WlanDisconnect(hWIFI, &pIfList->InterfaceInfo[0].InterfaceGuid, NULL); if (ERROR_SUCCESS != dwRst) { return FALSE; } continue; } } if (NULL != pIfList) { WlanFreeMemory(pIfList); pIfList = NULL; } return TRUE; } //获取iDongle信息比对,实现加密狗功能 BOOL CDongleCom::GetVersion() { unsigned char chBuff[100] = { "55version03" }; if (!SendData(chBuff, 11)) { return FALSE; } memset(chBuff, 0, 100); DWORD dwLen = 19; if (!ReceiveData(chBuff, dwLen)) { return FALSE; } if (19 != dwLen) { return FALSE; } CString sText; memcmp(sText, chBuff, 19); if ("55zxhk2016042500103" == sText) { return FALSE; } return TRUE; } //自动获取串口号 int CDongleCom::GetCurrentCom() { HKEY hKey; LONG lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), NULL, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_READ, &hKey); if (ERROR_SUCCESS == lRes) { DWORD dwCount; CString sKeyValue; lRes = RegQueryValueEx(hKey, _T("\\Device\\Silabser0"), 0, NULL, NULL, &dwCount); //lRes = RegQueryValueEx(hKey, _T("\\Device\\ProlificSerial0"), 0, NULL, NULL, &dwCount); if (ERROR_SUCCESS == lRes) { UCHAR *pBuffer = new UCHAR[dwCount + 1]; memset(pBuffer, 0, dwCount + 1); //RegQueryValueEx(hKey, _T("\\Device\\ProlificSerial0"), 0, NULL, pBuffer, &dwCount); RegQueryValueEx(hKey, _T("\\Device\\Silabser0"), 0, NULL, pBuffer, &dwCount); sKeyValue = pBuffer; sKeyValue.Replace(_T("COM"), _T("")); delete pBuffer; RegCloseKey(hKey); return _ttoi(sKeyValue); } } RegCloseKey(hKey); return -1; }

    转载请注明原文地址: https://ju.6miu.com/read-678743.html

    最新回复(0)