简体   繁体   中英

Connect to a gps deamon server using C#

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 ac# example.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM