Monthly Archives: April 2010

Programming Ugly Vs. Pretty

I recently complained I didn’t enjoy C# because it didn’t measure up to Python when it comes to being clear and concise. In fact, this is what got me to wade in so deep on my last Python project. I was enjoying the break from curly braces.

Yesterday, I read this article here – Why I Stopped Coding, and although I don’t have 1% of the author’s programming experience, I can relate to it. In fact, I interrupted my googlodyssee about the serial port enigma long enough to read this article, and it really hit the spot. It once again made me want to become a Python guru because I think eventually I’ll be able to do all I need and want to do using Python.

Advertisements

Leave a comment

Filed under Uncategorized

How to Talk to Garmin GPSMap 60CSx using Python

I haven’t blogged in a while. But I haven’t been idle at all.

Part 1) A month ago, I was working on a project where I had to collect GPS coordinates of a 25-acre piece of land. Real survey information was not needed but I had to show the outline of my project site on a variety of maps. I thought to myself – “Wouldn’t it be nice to be able to take the laptop running ArcGIS to the field, hook it up to my handheld GPS, and plot your GPS location in realtime?” – So what were my options ?

1) Google Earth offers such capability using a very straightforward process, especially if you’re using a Garmin GPS. With a few clicks you’ll be op and running. Directions are here. Unfortunately, this only works well while you’re connected to the web, and my laptop has no wireless service to allow it to work in the middle of nowhere.

2) Since most of my GIS work has been with ESRI (I’ve had my share of annoying freezes and crashes lately!), I was hoping for an ArcMap solution. In fact, there is a .NET project by Edan Cain for ArcGIS Explorer. I spent some time installing & uninstalling Explorer and subsequently the entire ESRI suite, only to run into the same non-descript error when I tried to run the script. I had similar bad luck with another similar tool for ArcMap.

3) GPS Trackmaker (it’s free!) worked well when it comes to displaying your current position as provided by the GPS. But getting it to accept my existing shapefiles as basemaps was too much of a headache to warrant further efforts. If you know the secret of how to get this to work, please let me know.

Part 2) – By now, I was frustrated enough with the “big packages”. Why not try to coomunicated with the GPS using Python, and then be able to work with the data, if not in realtime, at least at your discretion. Surely, this would just be a matter of locating a few modules and writing a few lines of code. In fact, there are plenty of examples on the web, and I also found one in Mapping Hacks on my shelf.

When downloading waypoints to Garmin’s Mapsource software or third-party utilities, I’ve always used the USB cable. But for use in Python, I quickly found people were saying reading from USB was a lot more complicated than from the serial port. There is a USB module – pyusb. But I had no luck with it. Most people seemed to be having success with serial.py, a Python extension for the PC’s serial port. So I ordered a serial cable which hadn’t originally been included with my GPS. When

However, something as simple as:

>>> import serial
>>> ser = serial.Serial(0)  #open connectionto COM1

didn’t work. When I checked what I had actually installed, I found there was no serial modules but one called serialwin32 in a folder serial. Using the following worked.

from serial import serialwin32

Then I tried:

s = ser.readline()

and nothing happened. I am not sure I have ever used my serial port before. My PC is only 2 years old. So I had to make sure I knew which port was COM1. I finally had to check BIOS settings to make sure they were ok.

I abandoned my work using serial.py, and looked at gpsd, a Linux daemon that can be run with cygwin under Windows. I installed this version and Cygwin, and tried to use the Python wrapper (gps.py) that comes with  it, but soon got bogged down in strange errors and missing modules.

I had similar problems using Pygarmin, which sounded like a dream come true. It was designed for use with Garmin GPS technology and included a set of classes for the NMEA specificiation. But it appears this module has not seen any development in some time, and it didn’t work for me. Same with pygps. I spent time installing and got sidetracked searching for missing modules related to GTK. My last ditch effort was to use a Python wrapper for GPSBabel. But no luck here either.

Instead, I tried the initial serial.py script one more time, and then decided to see if I could check on the status of COM1 while the GPS was plugged in. A handy toll for that is Comtest. While the script did nothing, Comtest showed me some output that looked like this:

@100430190019_____________________@100430190019___________________

That’s YYMMDDGMT ! Somebody was talking to COM1 !!

Then, it occurred to me that I should replace ser.readline() with ser.read(). And wham-bam, I was in business.

from serial import serialwin32
ser = serialwin32.Serial(0) 
print ser.portstr 
s = ser.read(1000)  
print s
ser.close()
  

The GPS was set for TEXT output mode at 9600 baud, and I was getting strings of text like this line:

@100430190020N3341662W09207282G012+00209E0044N0025D0048

Date Time                 Latitude    Longitude     Gobbledeegook

Granted, I spent a lot of time to get something this simple to work. Some of it because I didn’t know much about serial ports, some of it because I got sidetracked trying out different scenarios. But I learned a lot more than what was needed to get a solution, and I have made a long list of things. This is what I love about teaching myself programming skills. You started at A, wanting to go to B, and you get there via Z. Needless to say, I need to spend some more time trying to get pygarmin.py and nmea.py to work.

4 Comments

Filed under Uncategorized

Back to Python, C# Frustrations

So I’m back to Python… work with C# is on hold. I understand it but it’s no fun. It’s ugly.  When I play with Python code, it makes sense right away. It’ clear and concise. C# is cluttered and bloated. Yeah, it’s neat to be able to whip up a bunch of professional looking Windows forms in a few minutes… but just to say hello world, takes a page of code, it feels like. It’s just not the kind of programming, I think I will enjoy. It’s good to understand some of the code I see for use with ArcGIS, but right now, I don’t see myself writing much C# from scratch any time soon.

Every time I see a line like this, it looks so redundant:

Newclass Newobject = new Newclass();

I know what it means, and I get it that Python is dynamically typed while C# is a statically typed language. But just looking at the code, all I can think that there is twice as much code there as ought to be necessary. So… I’ll be spending more type getting better with Python.

Leave a comment

Filed under Uncategorized