1

I try to search my serial port of my GPS on my tablet (Windows CE).

I know that this is on "COM 3" but I want the program to find this by itself. I mean run in a loop (for) on all ports and search for this.

My question is which "if" I need to write to tell the program "this is my GPS port".

Thank you all.

Smi
  • 13,850
  • 9
  • 56
  • 64
idan
  • 13
  • 5

1 Answers1

1

Gps as i know works with a physical or virtual serial com port (ie com via usb). Since only one application can open a com port at a time there should be no program using gps while searching for the gps-port.

You already gave the answer "loop (for) on all ports and serche for".

Note the example below is an untested scetch how it could work. Feel free to update this wiki page to fix possible errors and add missing functionality.

 public string FindGpsPort()
 {
 foreach(string portname in System.IO.Ports.SerialPort.GetPortNames())
 {
      // using to make shure that the testport is closed after test
      using (SerialPort testport = new SerialPort(){PortName = portname})
      {
         // maybe neccessary to set baudrate, parity, ... of com port
         testport.Open(); 
         // to do if error or exception this is not the 
         // gps port or some software already uses the gps-port

         // to do: read some data from port and verify if it is GPS-Data
         // if valid return portname ; 
      }
 }
 // All com ports tried but not found. throw exception or return error code
 return null;
 }
k3b
  • 14,517
  • 7
  • 53
  • 85