<$BlogRSDUrl$>

Wednesday, May 11, 2005

Serial.IO.Ports.SerialPort.WorksJustWow with my GPS Receiver



Recently I decided to dust[!] out my old GPSLibrary stuff, and give some new light to the lib. So I decided to scrap the current architecture and re-architect it with all the new modern frills available, so that it works for my smartphone as well.

First thing, I decided to scrap out was my own custom written Serial Port Wrapper, I had a tough time with those Virtual COM Ports. This time, I decided to use the Serial Port Class, which is newly added to the .NET 2.0 IO Libraries.

First skeptical issue was those Virtual COM Ports, but I was pleasantly delighted with this SerialPort class. This SerialPort class behaved and worked absolutely to my requirements without making fuss. And all I had written is a very few lines of code. In My previous COM + PInvoke a.k.a Interop wrapper thingy, I had to write loads of code for this, which is obviously un-optimized and had performance hits… Serial.IO.Ports is Just Wow.

Here are some snippets….


private void OpenPort()
{

Console.WriteLine("\nAvailable Ports : ");
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
Console.WriteLine(port);


private SerialPort port = new SerialPort("COM6", 4800, Parity.None, 8,StopBits.One);


Console.Write("\nConnected to - " + port.PortName);
Console.Write(" @ BaudRate " + port.BaudRate.ToString());
Console.WriteLine(" communincating with " + port.DataBits.ToString() + " Databits \n\n");
Console.WriteLine("Buffer Size : " + port.WriteBufferSize.ToString());
Console.WriteLine(">> Data from GPS Receiver : \n");

port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

port.Open();

}

void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Console.Write(port.ReadExisting());
}

And here is the output from my GPS Receiver (Pharos – GPS 360)


What you see in the picture is the list of available ports in my machine, My GPS Receiver is connected to COM 6, and the Data from GPS Receiver is in NMEA Protocol Format. To Know more about NMEA Protocol read my previous blog here @ http://loguk.blogspot.com/2004/11/more-on-nmea-0183-nmea-0183-is-ascii.html and to know more about GPS Read my previous blog here @ http://loguk.blogspot.com/2004/10/question-on-gps-recently-there-was.html

Also Writing data to a COM port is made a lot more easy… here is a simple snippet to write data to a COM Port

private void WriteData()
{
port.Open();
port.Write("Ah! GPS… Hack It…dude");
port.Write(new byte[] { 0x0A, 0x0B, 0x0C }, 0, 3);
port.Close();
}


Okay, you cannot use these code straight to the production code… I need to do Rico Mariani thingy’s :) and take care of thread safety et al… whatever… This Serialport Class is just WOW.

posted by Logu Krishnan : 10:12 PM

Comments:
Hello I really like your gps tracking device blog feel free to checkout my site gps tracking device it also has some gps tracking device on it. Thx. again for your time and effort.


 
Post a Comment

This page is powered by Blogger. Isn't yours?