Serial GPS

[Sorry the damn forum eat my question.]

Hello people.

Since you can see on other post in this forum that I’ve created, I’m(and other prople too) having a lot of problems with Seeed GPS module sold by GHI. After a lot of discussion and nothing handled, I decided to seek for other options.

I got a Prolific based GPS USB-to-serial. Now I’m trying to use the USBHost module to get the NMEA data from this thing and try handle my problem with other module.

So, the bare code is:

void DeviceConnectedEvent(USBH_Device device)
            Debug.Print("Device connected");
            switch (device.TYPE)
                case USBH_DeviceType.Serial_Prolific:
                    serialUSB = new USBH_SerialUSB(device, 4800,
                        System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                    serialUSBThread = new Thread(SerialUSBThread);

void SerialUSBThread()
            byte[] data = new byte[200];
            while (true)
                serialUSB.Read(data, 0, data.Length);
                StreamReader r = new StreamReader(new MemoryStream(data));

Now, the connection is made, and the serialUSB start grab the NME sentences but something is wrong. The data that comes from NMEA strings seems to be cut. Part on one line part on other.



So, I have two questions:

  1. What is wrong with this simple code that the strings are coming wraped?
  2. Anyone has a NMEA parser sample so I don’t have to write myself one from scratch?

Thanks a lot. Really appreciate.

We might need a bit more to go on!

@ galvesribeiro - And the question is?

maybe it’s the start of a Fez adventure that we all fill in…

I’ll start…

Hello people.

I’m looking at creating a GPS enable…

@ galvesribeiro - looks normal to me - have a look on the codeshare for GPS examples

I’d have thought you might have to keep fetching data and building up strings until you hit an end of line character, otherwise your lines might break up just because you’ve grabbed them before they’ve finished sending?

@ RorschachUK - Yeah you need to build up the strings from the buffer and then parse them, the code in Codeshare do just that for you

@ Justin - Already checked. Nothing with USB-to-Serial GPS. Only with SerialPort class. This class has a ReadLine right from the data coming, so is easy to get the right lines. The USBH class don’t give me even the size of the data that just come in. And, I don’t know if is there a way to use SerialPort class with a GPS-to-Serial device pluged on USBH module. If yes, would love to know how! :slight_smile: And about the NMEA parser, don’t found any really usefull there also… Keep searching…

@ RorschachUK - How can I do it if I don’t have the size of the lines? Any sample with this my actual code?

Thanks to both for the replies.

Best regards…

[quote=“galvesribeiro”]@ RorschachUK - How can I do it if I don’t have the size of the lines? Any sample with this my actual code?
[/quote]You don’t need the size of the lines, you just hold off from doing anything until you reach a carriage return.

@ galvesribeiro - There is no readline in SerialPort class in .net micro you build up the string on datareceived

Have a look at this:

The problem is that the ReadLine() only reads till it finds the newline character and throws away the rest, which is the beginning of the next sentence.

Try this: (I couldn’t test it because I don’t have the right hardware, but you get the general idea)

private MemoryStream _stream = new MemoryStream();
        private const byte EOL_VALUE = (byte)'\n';
        void SerialUSBThread()
            byte[] data = new byte[1];
            while (true)
                // Here we read one byte at a time because there is up to a one second
                // gap between sentences and if we read an arbitrary length we will most
                // likely not get the end of the first sentance until the next sentance
                // has started coming through a second later.
                int readCount = serialUSB.Read(data, 0, data.Length);
                if (readCount != 1)
                    Debug.Print("Failed to read");

                if (data[0] == EOL_VALUE)
                    Debug.Print(new string(Encoding.UTF8.GetChars(_stream.ToArray())));// It's actually ASCII encoded, but we can use UTF8 since there is no ASCII encoding

                    // Get a new empty stream ready for the next sentance
                    _stream = new MemoryStream();

Since the question of parsing serial ASCII data comes up so many times. I’ve written an abstract baseclass for ASCII line processing.


Note: it is not yet tested on a real device, since I’m in the progress of cleaning up my desk here. So have no working setup for the moment. But you have one, so if you like, you can give it a try.