I'm working on a python app that's reading from a gps usb dongle. This far everything has been running in ubuntu/debian based systems where I communicated with the gps in a rather blunt way of scanning all of /dev/ttyUSB0-开发者_如何学JAVA9 with pySerial for something speaking NMEA sentences on 38400 baud. Now I have been asked to get this app working cross platform and I'm a bit confused on which would be the best way of finding the gps dongle.
I have considered something along the lines of:
if os.name == "posix":
self.conn = serial.Serial("/dev/ttyUSB%i" % usb)
elif os.name == "nt":
...
But I would rather have a single solution that works cross platform. Does anyone know of such a solution?
You could use the comports
function from the scanwin32.py module provided in the pySerial documentation to figure out which COM ports are available, and then, using the returned informations about the open ports, find which one is your GPS dongle.
Edit: The documentation also provides a scan.py module which contains only a very simple function that probes each 256 ports to find which ones are open, maybe it would be sufficient for what you need.
I would guess that in the long run you may have more use of a cross platform anyway so go for why not stay with it?
精彩评论