Python Forum
NMEA read parse and write from/to serial port
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NMEA read parse and write from/to serial port
#1
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.
Reply
#2
(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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,008 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  MCU reboots after opening Serial port when ran from Raspberry PI zazas321 3 428 Mar-19-2024, 09:02 AM
Last Post: zazas321
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,429 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 611 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,903 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  parse/read from file seperated by dots giovanne 5 1,105 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  How do I read and write a binary file in Python? blackears 6 6,512 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Serial Port As Global Prasanjith 2 1,489 Mar-23-2023, 08:54 PM
Last Post: deanhystad
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 819 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,588 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020