By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,034 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,034 IT Pros & Developers. It's quick & easy.

Garmin GPS and USB [PyGarUSB package]

P: 5
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
May 1 '07 #1
Share this Question
Share on Google+
10 Replies


bartonc
Expert 5K+
P: 6,596
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
May 2 '07 #2

bartonc
Expert 5K+
P: 6,596
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.
May 2 '07 #3

bartonc
Expert 5K+
P: 6,596
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>
May 3 '07 #4

bartonc
Expert 5K+
P: 6,596
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):
Expand|Select|Wrap|Line Numbers
  1.             pyBuffer += self.readBuffer[:nBytesReturned.value]
  2.             # Was this the last packet?
  3.             if nBytesReturned.value < ASYNC_DATA_SIZE:
  4.                 # Yes.
  5.                 ### ctypes.cast() seems to leak memory the way that it is used below ###
  6.                 ### The substitute code on the next 2 lines isn't elegant, but works ###
  7.                 ### For emergency use only ###   The leak seemed to be 4K per call   ###
  8.                 packetID = (UBYTE(pyBuffer[5]).value * 256) + UBYTE(pyBuffer[4]).value
  9.                 thePacket = CreatePacket(UBYTE(pyBuffer[0]).value, packetID,
  10.                                          tuple(pyBuffer[PACKET_STRUCT_SIZE:]))
  11. ##                print thePacket.mPacketId
  12.  
  13. ##                # this gets released #
  14. ##                theBuffer = CreateByteBuffer(pyBuffer)   # Copy py type to C type
  15. ##                # Create a custom pointer just for this instance
  16. ##                # .. so does this #
  17. ##                P_Packet_t = GetPackClassPointer(len(pyBuffer) - PACKET_STRUCT_SIZE)
  18.                 # empty the buffer
  19.                 del pyBuffer[:]
  20. ##                # I don't think that this checks!! #
  21. ##                ## thePacket = (Packet_t*) theBuffer; ##
  22. ##                thePacketPtr = cast(theBuffer, P_Packet_t)
  23. ##                thePacket = thePacketPtr.contents
  24. ##                print theBuffer, thePacketPtr.contents, thePacket
  25. ##                del thePacketPtr, P_Packet_t
  26.                 break
May 4 '07 #5

P: 5
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!
May 4 '07 #6

bartonc
Expert 5K+
P: 6,596
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.hasDeviceInfo variablle is new:
Expand|Select|Wrap|Line Numbers
  1. class GPS_Model:
  2.     """This model of a Garmin GPS provide startup logic (which is almost a direct translation
  3.        of the USBSDK). It is broken into two parts to allow for creating the model without an
  4.        actual connection: Even if EnumDevices() Open() will call __init__()
  5.        . _connect() will open a file handle and init
  6.        the device driver and the USB_Device class for use by ReadPacket() and WritePacket()
  7.     """
  8.     def __del__(self):
  9.         """In case Close() is not called.
  10.         """
  11.         self.Close()
  12.  
  13.     def __init__(self, guid):
  14.         """Calls functions from both SetupAPI dll to get the ball rolling.
  15.         """
  16.         self.guid = guid
  17.         self.devHandle = HANDLE(INVALID_HANDLE_VALUE)
  18.         self.hasDeviceInfo = False
  19.  
  20.         self.usb_device = None
  21.         self.PVTTimeoutTime = 0
  22.         self.PVTStage = 0
  23.  
  24.         # Get a HDEVINFO pointer into OS memory
  25.         theDevInfo = GetClassDevs(byref(guid), None, None,
  26.                                   DIGCF_PRESENT + DIGCF_DEVICEINTERFACE)
  27.  
  28.         # Create an Interface data object
  29.         theInterfaceData = SP_DEVICE_INTERFACE_DATA(sizeof(SP_DEVICE_INTERFACE_DATA))
  30.  
  31.         self.theDevInfo = theDevInfo
  32.         self.theInterfaceData = theInterfaceData
  33.  
  34.         # try to connect to the device and fill in the Device Interface Data #
  35. ##        success = self._connect(theDevInfo, guid, theInterfaceData)
  36.         success = EnumDevices(theDevInfo, None, byref(guid), 0, byref(theInterfaceData))
  37.         ## Will fail if there is no device on the buss ##
  38.         if success:
  39.             self.hasDeviceInfo = False
  40.             self._connect(theDevInfo, theInterfaceData)
and in Open():
Expand|Select|Wrap|Line Numbers
  1.     def Open(self):
  2.         """If the file handle is invalid, attempt to open it. Otherwise, send
  3.            a test command to see if Window still thinks that the handle is valid.
  4.            Return True if a conncetion exists.
  5.         """
  6.         if not self.hasDeviceInfo:
  7.             self.__init__(self.guid)
  8.         if self.devHandle.value == INVALID_HANDLE_VALUE:
  9.             try:
  10.                 return self._connect(self.theDevInfo, self.theInterfaceData)
  11.             except (WindowsError, PyGarUSBError):
  12.                 return False
  13.         else:
  14.             try:
  15.                 self.AbortOperation()
  16.                 return True
  17.             except WindowsError:
  18.                 return False
May 4 '07 #7

bartonc
Expert 5K+
P: 6,596
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2.     def AbortOperation(self):
  3.         """Besides aborting a command, its also handy for testing connectivity because
  4.            it doesn't expect any reply."""
  5.         if not self.usb_device.StopBgRead():
  6.             raise PyGarUSBError("Couldn't stop background task")
  7.         theCmdPacket = CreatePacket(*(L001.Pid_Command_Data + A010.Cmnd_Abort_Transfer))
  8.         self.WritePacket(theCmdPacket)
  9.         return True # No errors occured #
May 4 '07 #8

bartonc
Expert 5K+
P: 6,596
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)
May 4 '07 #9

P: 1
Hello Barton
Is your pyGarUSB still somewhere available to download ?
Regards
Joris Peumans
Apr 7 '08 #10

bartonc
Expert 5K+
P: 6,596
I'm sorry that that package has moved out of the public domain.
Apr 13 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.