Python Forum
NMEA read parse and write from/to serial port - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: NMEA read parse and write from/to serial port (/thread-4370.html)



NMEA read parse and write from/to serial port - ESEAMarine - Aug-11-2017

Hi,
I am trying to write a code that can receive a stream of NMEA sentences from my GPS via serial port, parse the sentence and write it as a different type of NMEA sentence to another serial port.

This is what I have so far:

import serial
import pynmea2
import time

#define COM-ports
port2 = "COM5"
ser2 = serial.Serial(port2, 4800, timeout=0)
port = "COM6"
ser = serial.Serial(port, 4800, timeout=0)

#read from COM-port, parse NMEA and send to new COM-port
streamreader = pynmea2.NMEAStreamReader()

while True:
    data = ser2.read()
    for msg in streamreader.next(data):
        msg = pynmea2.GGA('GP', 'GGA', (msg.lat, '1929.045', 'S', '02410.506', 'E', '1', '04', '2.6', '100.00', 'M', '-33.9', 'M', '', '0000'))
        
        while True:
            ser.write(str(msg))
            time.sleep(1)

#close COM-ports
ser.close()
ser2.close()
My problem is that the parsed sentence is written as a continuous string, whilst I would like it to be written as a list like this:
$GPGGA,4425.8867,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*64
$GPGGA,4425.8867,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*64
$GPGGA,4425.8867,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*64
$GPGGA,4425.8867,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*64

If I just write msg without the str I get the following error:
Traceback (most recent call last):
File "\My Documents\LiClipse Workspace\NMEA converter\project.py", line 20, in <module>
ser.write(msg)
File "\Downloads\pyserial-3.4\serial\serialwin32.py", line 308, in write
data = to_bytes(data)
File "\Downloads\pyserial-3.4\serial\serialutil.py", line 66, in to_bytes
return bytes(bytearray(seq))
TypeError: 'GGA' object is not iterable

I'm pretty new to Python Confused Can anyone please push me in the right direction here? Would be much appreciated.


RE: NMEA read parse and write from/to serial port - sparkz_alot - Aug-11-2017

(Aug-11-2017, 09:49 AM)ESEAMarine Wrote: Can anyone please push me in the right direction here?

Yes, in the future, post your questions under the proper forum heading. This will increase your chances for a speedier reply.