Garmin GPS and USB [PyGarUSB package]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robingfx
    New Member
    • Apr 2007
    • 5

    Garmin GPS and USB [PyGarUSB package]

    Hello,

    I am starting this discussion in an effort to get my Garmin GPS to work with pyUSB, and a place to exchange information and code appropriate for this topic.

    According to bartonc, in another discussion, Garmin and pyUSB don't work together because of the way Garmin installs in Windows. Can anyone explain why? And is there a workaround for this?

    Really, the simplest way to get this to work is to buy another GPS, but I would like to know if this is a battle worth fighting first. I spent $100 on this Garmin so I hope I can make it work.

    Any help is greatly appreciated. I will post any code that I find worth sharing.

    /Robin
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by robingfx
    Hello,

    I am starting this discussion in an effort to get my Garmin GPS to work with pyUSB, and a place to exchange information and code appropriate for this topic.

    According to bartonc, in another discussion, Garmin and pyUSB don't work together because of the way Garmin installs in Windows. Can anyone explain why? And is there a workaround for this?

    Really, the simplest way to get this to work is to buy another GPS, but I would like to know if this is a battle worth fighting first. I spent $100 on this Garmin so I hope I can make it work.

    Any help is greatly appreciated. I will post any code that I find worth sharing.

    /Robin
    Hi Robin (you can call me Barton). I finished a very solid foundation for my PyGarminUSB package last night. Unforotunately, that machine is not hooked up to internet service at the moment. I'll get it hooked up and attach the zipped package (probably tonight).

    It' uses ctypes (2.5 compatable) and a module that used to be part of ctypes which was split off as of verson 1 of ctypes. I think it's called comtypes (for GUID creation). It's may or may not be crutial - you may want to research that.

    Keep in touch,
    Barton

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      Hi Robin (you can call me Barton). I finished a very solid foundation for my PyGarminUSB package last night. Unforotunately, that machine is not hooked up to internet service at the moment. I'll get it hooked up and attach the zipped package (probably tonight).

      It' uses ctypes (2.5 compatable) and a module that used to be part of ctypes which was split off as of verson 1 of ctypes. I think it's called comtypes (for GUID creation). It's may or may not be crutial - you may want to research that.

      Keep in touch,
      Barton
      You may also want to check out the USBSDK on the Garmin web site. Even if you don't have VS, there is a very useful PDF doc in that downlowd.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bartonc
        You may also want to check out the USBSDK on the Garmin web site. Even if you don't have VS, there is a very useful PDF doc in that downlowd.
        One good thing came from a night's delay: The package is that much closer to a final release. My hope in posting this now is that some colaboration may come from the early release and the package will benefit (as well as all future users). So please keep in touch on this one and post any mods/improvements/additions for inclusion in the final release. Thanks so much.

        SEE ATTACHMENT <Attachment removed due to out of date modules>
        Last edited by bartonc; May 18 '07, 05:47 AM. Reason: <Attachment removed due to out of date modules>

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by bartonc
          One good thing came from a night's delay: The package is that much closer to a final release. My hope in posting this now is that some colaboration may come from the early release and the package will benefit (as well as all future users). So please keep in touch on this one and post any mods/improvements/additions for inclusion in the final release. Thanks so much.

          SEE ATTACHMENT
          In particular, I was having difficulty with this section (removed from the PyGarUSB.py file):
          Code:
                      pyBuffer += self.readBuffer[:nBytesReturned.value]
                      # Was this the last packet?
                      if nBytesReturned.value < ASYNC_DATA_SIZE:
                          # Yes.
                          ### ctypes.cast() seems to leak memory the way that it is used below ###
                          ### The substitute code on the next 2 lines isn't elegant, but works ###
                          ### For emergency use only ###   The leak seemed to be 4K per call   ###
                          packetID = (UBYTE(pyBuffer[5]).value * 256) + UBYTE(pyBuffer[4]).value
                          thePacket = CreatePacket(UBYTE(pyBuffer[0]).value, packetID,
                                                   tuple(pyBuffer[PACKET_STRUCT_SIZE:]))
          ##                print thePacket.mPacketId
          
          ##                # this gets released #
          ##                theBuffer = CreateByteBuffer(pyBuffer)   # Copy py type to C type
          ##                # Create a custom pointer just for this instance
          ##                # .. so does this #
          ##                P_Packet_t = GetPackClassPointer(len(pyBuffer) - PACKET_STRUCT_SIZE)
                          # empty the buffer
                          del pyBuffer[:]
          ##                # I don't think that this checks!! #
          ##                ## thePacket = (Packet_t*) theBuffer; ##
          ##                thePacketPtr = cast(theBuffer, P_Packet_t)
          ##                thePacket = thePacketPtr.contents
          ##                print theBuffer, thePacketPtr.contents, thePacket
          ##                del thePacketPtr, P_Packet_t
                          break

          Comment

          • robingfx
            New Member
            • Apr 2007
            • 5

            #6
            Barton! I don't know how to thank you properly. It's so awesome that you've takend the time to make this work. Not that I have gotten it to work yet (some includes missing such as win32file), but I am still amazed. I hope I can get it to work soon and undertand it well enough to be of any help in this discussion. Enormously grateful. Thanks!

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by robingfx
              Barton! I don't know how to thank you properly. It's so awesome that you've takend the time to make this work. Not that I have gotten it to work yet (some includes missing such as win32file), but I am still amazed. I hope I can get it to work soon and undertand it well enough to be of any help in this discussion. Enormously grateful. Thanks!
              I'm glad to be giving badk to the Python community in this way and I'm looking forward to your participation.

              I made a change in the startup last night. It allows for the device to be unplugged when the GUI is started: The self.hasDeviceI nfo variablle is new:
              Code:
              class GPS_Model:
                  """This model of a Garmin GPS provide startup logic (which is almost a direct translation
                     of the USBSDK). It is broken into two parts to allow for creating the model without an
                     actual connection: Even if EnumDevices() Open() will call __init__()
                     . _connect() will open a file handle and init
                     the device driver and the USB_Device class for use by ReadPacket() and WritePacket()
                  """
                  def __del__(self):
                      """In case Close() is not called.
                      """
                      self.Close()
              
                  def __init__(self, guid):
                      """Calls functions from both SetupAPI dll to get the ball rolling.
                      """
                      self.guid = guid
                      self.devHandle = HANDLE(INVALID_HANDLE_VALUE)
                      self.hasDeviceInfo = False
              
                      self.usb_device = None
                      self.PVTTimeoutTime = 0
                      self.PVTStage = 0
              
                      # Get a HDEVINFO pointer into OS memory
                      theDevInfo = GetClassDevs(byref(guid), None, None,
                                                DIGCF_PRESENT + DIGCF_DEVICEINTERFACE)
              
                      # Create an Interface data object
                      theInterfaceData = SP_DEVICE_INTERFACE_DATA(sizeof(SP_DEVICE_INTERFACE_DATA))
              
                      self.theDevInfo = theDevInfo
                      self.theInterfaceData = theInterfaceData
              
                      # try to connect to the device and fill in the Device Interface Data #
              ##        success = self._connect(theDevInfo, guid, theInterfaceData)
                      success = EnumDevices(theDevInfo, None, byref(guid), 0, byref(theInterfaceData))
                      ## Will fail if there is no device on the buss ##
                      if success:
                          self.hasDeviceInfo = False
                          self._connect(theDevInfo, theInterfaceData)
              and in Open():
              Code:
                  def Open(self):
                      """If the file handle is invalid, attempt to open it. Otherwise, send
                         a test command to see if Window still thinks that the handle is valid.
                         Return True if a conncetion exists.
                      """
                      if not self.hasDeviceInfo:
                          self.__init__(self.guid)
                      if self.devHandle.value == INVALID_HANDLE_VALUE:
                          try:
                              return self._connect(self.theDevInfo, self.theInterfaceData)
                          except (WindowsError, PyGarUSBError):
                              return False
                      else:
                          try:
                              self.AbortOperation()
                              return True
                          except WindowsError:
                              return False

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by robingfx
                Barton! I don't know how to thank you properly. It's so awesome that you've takend the time to make this work. Not that I have gotten it to work yet (some includes missing such as win32file), but I am still amazed. I hope I can get it to work soon and undertand it well enough to be of any help in this discussion. Enormously grateful. Thanks!
                StopBackground seems like a sensible addition to Abort, but I haven't tested it yet.
                Code:
                    def AbortOperation(self):
                        """Besides aborting a command, its also handy for testing connectivity because
                           it doesn't expect any reply."""
                        if not self.usb_device.StopBgRead():
                            raise PyGarUSBError("Couldn't stop background task")
                        theCmdPacket = CreatePacket(*(L001.Pid_Command_Data + A010.Cmnd_Abort_Transfer))
                        self.WritePacket(theCmdPacket)
                        return True # No errors occured #

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by bartonc
                  One good thing came from a night's delay: The package is that much closer to a final release. My hope in posting this now is that some colaboration may come from the early release and the package will benefit (as well as all future users). So please keep in touch on this one and post any mods/improvements/additions for inclusion in the final release. Thanks so much.

                  SEE ATTACHMENT
                  I just had a look inside that folder. Sorry about the clutter created during testing.
                  The current files are:
                  Frame2.py (the View; parent of the Control)
                  wxGPS.py (the Control)
                  PyGarUSB.py (the Model)

                  Comment

                  • PeumansJoris
                    New Member
                    • Apr 2008
                    • 1

                    #10
                    Hello Barton
                    Is your pyGarUSB still somewhere available to download ?
                    Regards
                    Joris Peumans

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      I'm sorry that that package has moved out of the public domain.

                      Comment

                      Working...