PyGarmin is a set of Python classes which implement the protocol used by Garmin GPS receivers to talk to each other and to other machines. It is based on the official protocol specification. PyGarmin is not a complete application - it is just a toolkit to help you write applications. I'm assuming you know how to program in Python.
This is a project which is in development. Everything could change dramatically, and probably will. Some bits are not fully implemented yet. Much of it is not really tested. No support. No guarantees. And so forth.
Having said all of that, this does allow me to download information from my GPS12XL to my Linux box, and it ought to adjust automatically to a large number of different Garmin receivers. If you use PyGarmin, it will probably be much quicker than writing your own software from scratch. It has had limited testing on other models. If it works on your GPS, let me know. If it doesn't, let me know. The more info you can give me about what went wrong, the more likely I am to fix it.
If you want to cut straight to the code go to the SourceForge project page mentioned above. But I suggest you read on first. The code looks quites scary if you don't know what's happening!
Almost every model of Garmin receiver implements a slightly different protocol. They have many things in common, but there are minor differences. For example, some receivers can display icons, and they therefore transmit waypoints which have an extra 'symbol' field, not used in other models. Others don't use icons, but do store altitude. And so forth. You need to get the protocol right for your particular model.
This makes matters more complicated, but at least these things are well documented by Garmin. The specification includes a big table which details, for each product type, what protocol it uses for basic commands, what it uses for downloading waypoints, what it uses for downloading routes, and so forth.
I have created Python classes for each of the protocols listed in the spec, and for each of the data types. Well, most of them. The big table becomes, in Python, a mapping from the Garmin product ID to the set of relevant classes. This means that, while there are a large number of classes defined in the source, only a few of them will ever be used by any given receiver. The classes are all given names based on those used in the specification, so look at that if you want to know more about them.
The included class garmin.Garmin will connect to your GPS, read its product ID and software version, and then look up the appropriate classes in the table. It creates instances of the protocol classes and notes the datatype classes for each type of data used in the transmisisons. It also has some friendly methods like 'getWaypoints', which do what you would expect. But what you get back when you call this is a list of objects, each of which is an instance of a class derived from garmin.Waypoint, but the precise type of the objects will depend on the GPS you're talking to.
OK. Here's a simple Python program:
#! /usr/local/bin/python # Load the module import garmin # Create a 'physical layer' connection using serial port phys = garmin.UnixSerialLink("/dev/ttyS0") # Create a Garmin object using this connection gps = garmin.Garmin(phys) # Get the waypoints from the GPS # (This may take a little while) waypoints = gps.getWaypoints() # Print the waypoints for w in waypoints: print w.ident, lat = garmin.degrees(w.slat) lon = garmin.degrees(w.slon) print lat, lon, w.cmnt |
Simple, eh? This should work for almost any model, because all waypoints will have an identity, a latitude & longitude, and a comment field. The latitude and longitude are stored in 'semicircle' coordinates (basically degrees, but scaled to fill a signed long integer), and so the fields are called 'slat' and 'slon'. The function garmin.degrees() converts these to degrees.
There are 3 levels of protocol documented:
Application layer | (highest level) |
Link layer | |
Physical layer | (lowest level) |
The specification documents the various different versions of these under labels of Pxxx, Lxxx, Axxx etc, where xxx is a number, and this convention is followed here. There are also various data types, named Dxxx. Roughly speaking, the Physical protocols specify RS232, the Link protocols specify a packet structure for sending messages to and fro, and the Application protocols specify what can actually go in those packets.
For example, a Garmin GPS 38 will talk to your computer over physical layer P000 (RS232) using a packet structure defined by link layer L001. If you want to transfer waypoints to and from it, they will be sent using application layer A100 (a waypoint transfer protocol), and the actual waypoints transferred will be of type D100.
At the time of writing, the only documented physical layer is P000 which is roughly RS232 at 9600 baud, 8 data bits, no parity, 1 stop bit. In the software, we model this as a P000 class that has read and write methods, which can be used by the higher protocol levels. The UnixSerialPort class used in the sample code above is a subtype of P000.
If you want to understand more, read the source and the specification. I may get round to more docs, when there's more to document.
Things which may be fixed/finished. Volunteers welcome!
Thanks are due to James A. H. Skillen, who implemented many of the more recent bits of the protocol, and so made PyGarmin useful for people with newer GPS units than mine!
This software is released under the GNU General Public Licence. It comes with no warranties, explicit or implied, and you use it at your own risk. It may be Y2K compliant.
You can download the releases here, or you can get the really up-to-date versions via CVS from the SourceForge site mentioned at the start of this page.
Quentin Stafford-Fraser