📄 index.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>PyGarmin - A Python Interface to Garmin GPS Equipment</title><style type="text/css"> body { margin-left: 12pt; margin-right: 12pt; background-color: #E0E0FF; } p { margin-left: 18pt; margin-right: 12pt } h1 h2 h3 h4 { font-family: arial,helvetica,sans-serif } div.code {text-align: center} .comment {font-style: italic; color: #804040}</style> </head> <body> <h1>PyGarmin - A Python Interface to Garmin GPS Equipment</h1> <p>Here's the important link:</p> <ul> <li><a href="http://sourceforge.net/project/?group_id=5570">PyGarmin Project Page</a> at SourceForge</li> </ul> <h2>Introduction</h2> <p>PyGarmin is a set of <a href="http://www.python.org">Python</a> classes which implement the protocol used by <a href="http://www.garmin.com">Garmin</a> GPS receivers to talk to each other and to other machines. It is based on the official <a href="http://www.garmin.com/support/commProtocol.html">protocol specification</a>. The project was started by <a href="http://www.uk.research.att.com/~qsf">Quentin Stafford-Fraser</a> but several others have helped to make it what it is today.</p> <p>PyGarmin is not a complete application. Some simple applications are now included, one of which is called pygarmin, but it is primarily just a toolkit to help you write applications. (I'm assuming you know how to program in <a href="http://www.python.org/">Python</a>. If you don't, check it out. It won't take you long to learn, and is well worth it.) This is a project which is in development. No support. No guarantees. And so forth.</p> <p>Having said all of that, this has been used to transfer information to and from several different Garmin receivers, mostly under Linux, though there is some Windows support now as well. If you use PyGarmin, it will probably be much quicker than writing your own software from scratch. If it works on your GPS, let us know. If it doesn't, let us know. The more info you can give us about what went wrong, the more likely we are to fix it.</p> <p>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!</p> <h2>Basics</h2> <p>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, which is 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.</p> <p>This makes matters more complicated, but at least these things are well documented by Garmin. The <a href="http://www.garmin.com/support/commProtocol.html">specification</a> 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.</p> <p>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 the spec if you want to know more about the classes.</p> <p>The class <tt>garmin.Garmin</tt> 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. 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.</p> <p>OK. Here's a simple Python program. You may need to set suitable permissions on the serial port (e.g /dev/ttyS0) before running it.</p> <div class="code"> <table width="90%" border="0" bgcolor="#ffeeff" cellpadding="10" cellspacing="0"> <tr> <td><span class="comment">#! /usr/local/bin/python</span> <br /> <span class="comment"># Load the module</span> <br /> <tt>import garmin</tt> <br /> <br /> <span class="comment"># Create a 'physical layer' connection using serial port</span> <br /> <tt>phys = garmin.UnixSerialLink("/dev/ttyS0")</tt> <br /> <br /> <span class="comment"># Create a Garmin object using this connection</span> <br /> <tt>gps = garmin.Garmin(phys)</tt> <br /> <br /> <span class="comment"># Get the waypoints from the GPS</span> <br /> <span class="comment"># (This may take a little while)</span> <br /> <tt>waypoints = gps.getWaypoints()</tt> <br /> <br /> <span class="comment"># Print the waypoints</span> <br /> <tt>for w in waypoints:</tt> <br /> <tt>牋牋print w.ident, <br /> 牋牋lat = garmin.degrees(w.slat) <br /> 牋牋lon = garmin.degrees(w.slon) <br /> 牋牋print lat, lon, w.cmnt</tt> <br /> </td> </tr> </table> </div> <p>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 <tt>garmin.degrees()</tt> converts these to degrees.</p> <h2>More details</h2> <p>There are 3 levels of protocol documented:</p> <div class="c2"> <table border="0" cellpadding="6" cellspacing="2"> <tr> <td bgcolor="#ffffe0" align="center"><b>Application layer</b></td> <td>(highest level)</td> </tr> <tr> <td bgcolor="#e0ffff" align="center"><b>Link layer</b></td> <td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -