rternier

I'm working with GPS devices and I need to autoconfigure which port to use when one is plugged in. I'm doing this in seperate factories loaded up through reflection so I can customize this per GPS unit.

I was using a parser to interact with the Device, but If the device doesn't have a GPS fix, sometimes I don't get any data returned.

I know there's a way to connect to a com port, open it up, take the first 5-7 characters returned from it and check against it (basically fill a buffer).

How can I go about doing that

Right now I'm doing this:
----

foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
{
System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort(s);

//port.Open();
//char[] c = new charDevil;
//port.Read(c, 0, 5);
//port.Close();


_timeOut = 1000;
_port = Int32.Parse(s.Replace("COM", ""));
IGPSAdaptor iAdaptor = this;
GPS.GPSInformation gi = iAdaptor.getGPSInformation();

//GPS.GPSInformation gi = GPS.Manager.getGPSInformation(_adaptor.DeviceName);

if (gi != null)
{
_timeOut = pTimeOut;
return true;
}
}

-------------------

The device in question should return this when you open up a buffer to that com port:
$GPGGA........

I need to check for those first characters when I open the port... but I'm having no luck.
Any help would be appreciated.

Thank you,



Re: .NET Base Class Library Read Data from a Com Port (Check to see which com port I need to use)

Carsten Kanstrup

When you open a port, various parameters like ReadBufferSize, BaudRate etc. are communicated to the driver. Any characters received by the UART before that time are lost (how do you expect to receive correct before the BaudRate is set ). You need to open the port and then poll the GPS device to get the answer.

PS. Remember that the Read method does NOT necessary return the requested number of bytes (may return only one byte even though you have asked for five).





Re: .NET Base Class Library Read Data from a Com Port (Check to see which com port I need to use)

rternier

I have tried this:
System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort(s, 4800);
port.ReadBufferSize = 1024;
port.Open();
string data = "";
try
{
port.ReadTimeout = 50;
data = port.ReadLine();
if (data.Substring(0, 5).ToUpper() == "$GPGG" || data.Contains("$GPGG"))
{
_port = Int32.Parse(s.Replace("COM", ""));
return true;
}
}
catch (Exception ex)
{
//mos likely timeout
}
finally
{
port.Close();
}
And though it's ugly code - I hate using a Catch block like this, it does return the one line I need. However, I'm running into wha tyou said. Every so often the GPS Device returns this:

00,,28,00,000,,26,00,000,,29,00,000,*78
$GPRMC,160139.922,V,,,,,,,051107,,*28
$GPGGA,160140.918,,,,,0,00,,,M,0.0,M,,0000*54
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPRMC,160140.918,V,,,,,,,051107,,*2F
$GPGGA,160141.916,,,,,0,00,,,M,0.0,M,,0000*5B

So I can't use the 1st line I need to use the 2nd.

What I'd love to do would be:

-Open Port
-Read x amount of characters
-Close port

If it times out, I'll just catch it and move onto the next port.

I'm a web developer, and I need to write this client to work with the website I've created... never thought I'd actually be working with serial ports Stick out tongue







Re: .NET Base Class Library Read Data from a Com Port (Check to see which com port I need to use)

rternier

I just ended up doing this:

port.ReadTimeout = 50;
data = port.ReadLine();
data += port.ReadLine();
data += port.ReadLine();
data += port.ReadLine();

if (data.Contains("$GPGG"))
{
Which works fine.





Re: .NET Base Class Library Read Data from a Com Port (Check to see which com port I need to use)

Carsten Kanstrup

I still see no synchronization between opening the port and transmission from your GPS. You just open the gate at a random time and are surprised that the first line is destroyed - I am not! Why not just throw the first line away unless it starts with "$"

PS. SerialPort has a minimum receive buffer of 4096 bytes and simply ignores values less than that, so the statement: "port.ReadBufferSize = 1024;" don't do anything.





Re: .NET Base Class Library Read Data from a Com Port (Check to see which com port I need to use)

rternier

Honestly I have hardly any idea what I'm doing with Serial Devices, I've never worked with them before.

For Synchronization, how do I go about doing that I only need to retrieve what the Device is sending me, nothing more. The device came with .NET parsers for the GPS device, however they have a longer timeout - I need this to be instantaneous.

I've searched a lot on the internet and found nothing useful - which is why I came here.

All I want to do is read X amount of data (say 400 characters) from the device (Sort of fill a buffer then send it out...) and then look at it. Any help would be appreciated.





Re: .NET Base Class Library Read Data from a Com Port (Check to see which com port I need to use)

Carsten Kanstrup

The first thing to do is to find out how your GPS device operates. I know that some GPS devices use a one-way communication so that they just sends a new message each time a new position is calculated. In this case, you can do nothing but wait for a telegram and then throw the first line away it it does not start with "$" (you start receiving in a middle of a telegram). If it is possible to poll the device, you can send a poll command and then get the reply. This will give you a faster response.

PS. If you call ReadLine from the UI thread, you will block this thread until you have received a line or the receiver times out. This is not the best programming practice. It may be better to use the DataReceived event and call ReadLine in the eventhandler. In this way, all reception and waiting is done on a thread pool thread so that you will not block the UI thread.

If you don't mind VB examples, you can read all about the serial port on this link: http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm





Re: .NET Base Class Library Read Data from a Com Port (Check to see which com port I need to use)

rternier

Carsten,

Sorry it took so long to reply, i'm back on this project now. The GPS device i'm working with is a regular device that just streams information to the computer.

It plugs in with a USB Cable, which then uses drivers to convert it to a serial port.

On my Vista machine (and any vista machine) this works great. however when I run my app on any XP box, it crashes.

I get the errors "COM port does not exist", though I have it showing in Device Manager that it's there... HyperTerminal can even connect to that port. I need to go through each com-port on the computer, and find the one that is the GPS device.

I looked at the "DataReceived" event, but I have no idea how to use it, and there's really no posts on the internet on it either (that I could fine).

-------------------------------
The code i'm currently using:

foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
{
try
{
port = new System.IO.Ports.SerialPort(s, 4800, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
message.Append("Trying Port: " + s + ". IsOpen " + port.IsOpen);

port.Open();


port.ReadTimeout = 1000;
//Read 3 lines.
data = port.ReadLine();
data += port.ReadLine();
data += port.ReadLine();
message.Append("***Port: " + s + " seems to be working. Data: " + data + ".\r\n");
if (data.Contains("$GP"))
{
_port = Int32.Parse(s.Replace("COM", ""));
pPort = _port;
_inUse = false;
return true;
}
}
catch (Exception ex)
{
errors.Append("\nError occured while connecting to COM PORT " + s +".\n" + ex.Message);
}








Re: .NET Base Class Library Read Data from a Com Port (Check to see which com port I need to use)

Carsten Kanstrup

There seems to be serious problems with SerialPort in .Net 3.5 - see this warning: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2635314&SiteID=1 . As can be seen, another person has also problems running a SerialPort application generated with .Net 3.5 on Vista on an XP computer and it may even damage the Windows installation.

Try to use .Net 2.0 and see if you still get the same problems.