0

我正在使用连接到运行 gpsd 服务的树莓派的 gps。我正在尝试使用 tcp 连接到该服务,但我无法让它工作。我也没有找到任何关于它的文档。

这是我现在拥有的代码:

  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();
  }

有人可以告诉我它是如何完成的,或者给我一个文档或 ac# 示例的链接。

4

1 回答 1

1

查看来自此来源的关于 C 示例的部分。

核心 C 客户端是一个套接字接收器

如果您需要功能齐全的客户端,我建议在我最初的评论中使用解决方法

您可以在 Raspberry Pi 上使用标准客户端,并在 Raspberry Pi 上构建一个新的网络服务,您可以从 c# 以更标准的方式连接到该服务”

C# 监听器

否则,您可以尝试只创建一个基本的 C# 侦听器:按照 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);
    }
}

整个详细的实现将超出这里的范围。

旁注

请记住,您需要在循环时睡觉。

可能更简单的解决方案

但是,乍一看,利用本机客户端绑定似乎是更简单的方法。

例如,gps_read()描述为

阻止从守护进程读取数据。

这里的想法是从 C# 调用C++ 包装器并编写客户端的单元操作,如其他答案中所述

于 2016-08-02T15:28:56.963 回答