0

I am working with a gps connected to a raspberry pi running a gpsd service. I am trying to connect to the service using tcp but I cannot get it to work. I also don't find any documentation about it.

This is the code I have right now:

  private static void Main(string[] args)
  {
        Console.WriteLine($"Trying to connect to {ServerAddress}...");
        var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        client.Connect(ServerAddress, 80);
        var result = new byte[256];
        client.Receive(result);
        Test();
  }

Can somebody tell me how it is done, or give me a link to documentation or a c# example.

kevingoos
  • 3,785
  • 4
  • 36
  • 63
  • 1
    Is this [reference](http://www.catb.org/gpsd/client-howto.html) useful? –  Aug 02 '16 at 13:13
  • I actually read the website you referenced, and it is very usefull when you are using python or c++. But it actually doesn't contain any info for when you are using c#. – kevingoos Aug 02 '16 at 13:15
  • Basically, do you want to translate those C or C++ Examples to C#? Just to better understand: why can't you invoke C++? Are you going to run C# (mono) on the Raspberry Pi? –  Aug 02 '16 at 13:25
  • What I am trying to do is actually built a client that connects to the gpsd service. My client is a bigger program that wants to use the gps location of the raspberry pi (remotely). This program is written in c#, and runs on different places. – kevingoos Aug 02 '16 at 13:27
  • So... you could use the standard client on the raspberry pi and build a new network service on the raspberry, to which you could connect in a more standard way from c#. Does it make sense? –  Aug 02 '16 at 13:36
  • Ok I understand what you mean, but I am also using qgis and that doesn't need the extra service. Is there maybe a way to create a program in c++ and import it into my c# project? That will elimenate the extra delay of the service – kevingoos Aug 02 '16 at 13:40

1 Answers1

1

Look at the section about C Examples from this source.

The core C client is a socket receiver.

If you need a fully functional client, I suggest the workaround in my initial comments

you could use the standard client on the Raspberry Pi and build a new network service on the Raspberry, to which you could connect in a more standard way from c#"

C# Listener

Otherwise you can try to create just a basic C# Listener: follow the msdn How-To.

public void createListener()
{
    // Create an instance of the TcpListener class.
    TcpListener tcpListener = null;
    IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
    try
    {
        // Set the listener on the local IP address 
        // and specify the port.
        tcpListener = new TcpListener(ipAddress, 13);
        tcpListener.Start();
        output = "Waiting for a connection...";
    }
    catch (Exception e)
    {
        output = "Error: " + e.ToString();
        MessageBox.Show(output);
    }
    while (true)
    {
        // Always use a Sleep call in a while(true) loop 
        // to avoid locking up your CPU.
        Thread.Sleep(10);
        // Create a TCP socket. 
        // If you ran this server on the desktop, you could use 
        // Socket socket = tcpListener.AcceptSocket() 
        // for greater flexibility.
        TcpClient tcpClient = tcpListener.AcceptTcpClient();
        // Read the data stream from the client. 
        byte[] bytes = new byte[256];
        NetworkStream stream = tcpClient.GetStream();
        stream.Read(bytes, 0, bytes.Length);
        SocketHelper helper = new SocketHelper();
        helper.processMsg(tcpClient, stream, bytes);
    }
}

The whole, detailed implementation would be out of scope here.

A side note

Keep in mind that you need to sleep while looping.

Likely simpler solution

However, leveraging the native client bindings seems the easier method, at first sight.

For example, the gps_read() is described as

Blocking read for data from the daemon.

The idea here is to call a C++ wrapper from C# and to write the client's unit operations as described in this other answer

Community
  • 1
  • 1