使用 C# 连接到 gps deamon 服务器
Connect to a gps deamon server using C#
我正在使用连接到 raspberry pi 运行 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();
}
有人可以告诉我它是如何完成的,或者给我一个 link 文档或 c# 示例。
从这个 source 查看有关 C 示例的部分。
核心C客户端是套接字receiver。
如果您需要功能齐全的客户端,我在最初的评论中建议了解决方法
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# 侦听器
否则您可以尝试创建一个基本的 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()
被描述为
Blocking read for data from the daemon.
这里的想法是调用一个C++ wrapper from C# and to write the client's unit operations as described in this other
我正在使用连接到 raspberry pi 运行 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();
}
有人可以告诉我它是如何完成的,或者给我一个 link 文档或 c# 示例。
从这个 source 查看有关 C 示例的部分。
核心C客户端是套接字receiver。
如果您需要功能齐全的客户端,我在最初的评论中建议了解决方法
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# 侦听器
否则您可以尝试创建一个基本的 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()
被描述为
Blocking read for data from the daemon.
这里的想法是调用一个C++ wrapper from C# and to write the client's unit operations as described in this other