Communicating with a GPS Device

You can use the methods of the IPosNav interface to communicate with a GPS device.

    To communicate with a GPS device

  1. Initialize COM and obtain a handle to an IPosNav interface.

    The following code example shows how to do this:

    HRESULT hResult = CoCreateInstance(CLSID_PosNav, NULL,
                                       CLSCTX_INPROC_SERVER, IID_IPosNav, 
                                       (LPLPVOID) &pPosNav);
    
  2. Call FindDevices to obtain a list of installed GPS devices, then select a device to use.

    The following code example shows how to do this:

    hres = pPosNav->FindDevices(&pDevice, &dwNumDev);
    if(FAILED (hres) || dwNumDev == 0)
      {
      pPosNav->Release();
      // Insert error handling here.
      }
    
    // Call your selection function to choose one device.
    PNDEVICE *pDeviceOfChoice = MySelectDevice(pDevice);
    
  3. Call OpenDevice to open the selected device for communication.

    The following code example shows how to do this:

    hres = pPosNav->OpenDevice(&hPNAPI, pDeviceOfChoice);
    
  4. Call DeleteDeviceList to delete the device list after the selected device is open.

    The following code example shows how to do this:

    pPosNav->DeleteDeviceList(pDevice);
    if(FAILED (hres))
      {
      g_pPosNav->Release();
      // Insert error handling here.
      }
    
  5. Call StartCall to retrieve data from the device.
  6. Call CloseDevice to close the device.

    The following code example shows how to do this:

    hres = pPosNav->CloseDevice(hPNAPI);
    // Release the interface.
    pPosNav->Release();