Windows Mobile and Bluetooth GPS - Part II



In my recent post about programming my Cingular 8125 to talk to my Bluetooth GPS receiver, I failed to realize one minor detail.  Since I was pretty excited about getting longitude, latitude, and altitude to display on my phone I didn’t realize that it wasn’t updating.  No matter how long I let it run the values constantly stayed the same.  This was strange since I could see that the receiver and phone were paired and communicating.

After spending a couple of hours troubleshooting and debugging I realized that the values were being read correctly from the receiver, but weren’t being updated on the display.  Then I decided to test a quick debug hack that I normally run just to validate a value exists: MessageBox.show(longitude).  This displays a simple alert box and what suprised me was that each time I pressed “ok” the screen would redraw and display the appropriate value.  So I figured this was a simple fix and I would just need to invalidate or refresh the screen.  It turns out this was the wrong assumption.

So after another hour of pulling my hair out I decided there had to be something wrong with the thread that handled the serial port communication.  I figured this was a long shot since the values were actually coming in correctly, but for some strange reason I couldn’t get them to display (without the MessageBox hack).  My final guess was that the data was streaming so quickly that the UI didn’t have time to update with the new values.  So I updated the method responsible for reading the serial port data and placed a simple one second pause to prevent it from receiving data too quickly.  I lucked out on my final guess and everything now runs like a champ.  The one second delay did the trick and if you decide to use the code from this example then I would recommend updating your serialPort_DataReceived method with the following:

private void serialPort_DataReceived()
{
byte[] inputData = new byte[1];
string s = String.Empty;
while(serialPort.InBufferCount > 0)
{
inputData = serialPort.Input;
if(inputData[0] != ‘\r’ && inputData[0] != ‘\n’)
s += Encoding.ASCII.GetString(inputData, 0, inputData.Length);
else
{
parseNmeaSentence(s);
s = string.Empty;
}
}
Thread.Sleep(1000);
}

It’s amazing what simple lines of code can do!  I’ll be posting the app for download once I get a few more features in place.



Leave a Reply