串口驱动卸载/加载与 GSM 模块电源控制

直接上代码:


// 串口驱动卸载/加载 COM13
BOOL UnloadDeviceDriver(CString csDeviceName)
{
  BOOL bRet = FALSE;
  HANDLE hFind = INVALID_HANDLE_VALUE;
  if(csDeviceName.IsEmpty())
  {
    return FALSE;
  }
  DevicedeviceType deviceType;
  if(CString(csDeviceName.Right(1)) == _T(":"))
  {
    deviceType = DeviceSearchByLegacyName;
  }
  else
  {
    deviceType = DeviceSearchByDeviceName;
  }
  DEVMGR_DEVICE_INFORMATION devInfo = {0};
  devInfo.dwSize = sizeof(devInfo);
  hFind = FindFirstDevice(deviceType,(LPCVOID)csDeviceName.GetBuffer(),&devInfo);
  if(hFind == INVALID_HANDLE_VALUE)
  {
    return FALSE;
  }
  bRet = DeactivateDevice(devInfo.hDevice);
  FindClose(hFind);
  return bRet;
}
BOOL LoadDeviceDriver(CString csDeviceName)
{
  DWORD dwParam = 89;
  // ActivateDeviceEx(L"\\Drivers\\BuiltIn\\Serial3",NULL,0,&dwParam);
   HANDLE hHd = ActivateDeviceEx(str.GetBuffer(),NULL,0,&dwParam);
  if(NULL == hHd)
  {
    DWORD dwError = GetLastError();
    printf("LoadDeviceDriver Error : %d",dwError);
  }
}
// 电源控制
#define   VIDEOFUNDEVNAME   L"fun1:" 
#define SOFTI2C_IOCTL_TESTPIN 20
#define GSM_POWER_CONTROL_BY_MCU  0
BOOL GSM_Power_On(void)
{
  BOOL bRet;
  BYTE ucPara[3];
  HANDLE hFun1 = INVALID_HANDLE_VALUE;
  ucPara[0] = 1;
  ucPara[1] = 0;
  ucPara[2] = 14; 
  hFun1 = CreateFile(VIDEOFUNDEVNAME, GENERIC_WRITE|GENERIC_READ, 0, NULL, 0, 0, NULL);
  if(INVALID_HANDLE_VALUE == hFun1)
  {
    return FALSE;
  }
  bRet = DeviceIoControl(hFun1, SOFTI2C_IOCTL_TESTPIN, ucPara, 3, NULL, 0, NULL, NULL); 
  if(!bRet)
  {
    RETAILMSG(1,(L"[Power_On]Device IO control failed: %d\r\n",GetLastError()));
  }
  CloseHandle(hFun1);
  hFun1 = INVALID_HANDLE_VALUE;
  RETAILMSG(1,(L"\r\n open Gprs Power \r\n"));
  return bRet;
}
BOOL GSM_Power_Off(void)
{
  BOOL bRet;
  BYTE ucPara[3];
  HANDLE hFun1 = INVALID_HANDLE_VALUE;
  ucPara[0] = 0;
  ucPara[1] = 0;
  ucPara[2] = 14;
  hFun1 = CreateFile(VIDEOFUNDEVNAME, GENERIC_WRITE|GENERIC_READ, 0, NULL, 0, 0, NULL);
  if(INVALID_HANDLE_VALUE == hFun1)
  {
    return FALSE;
  }
  bRet = DeviceIoControl(hFun1, SOFTI2C_IOCTL_TESTPIN, ucPara, 3, NULL, 0, NULL, NULL); 
  if(!bRet)
  {
    RETAILMSG(1,(L"[Power_Off]Device IO control failed: %d\r\n",GetLastError()));
  }
  CloseHandle(hFun1);
  hFun1 = INVALID_HANDLE_VALUE;
  RETAILMSG(1,(L"\r\n close Gprs Power \r\n"));
  return bRet;
}


你可能感兴趣的:(串口驱动卸载/加载与 GSM 模块电源控制)