使用 c# 连接到压力传感器以获取值,在 c# 中不起作用
Connect to pressure sensor using c# to get the value ,doesn't work in c#
我有一个传感器,我使用网络连接到这个传感器 cable.I 应该向这个传感器发送命令以获取值。
The sensor ip is :192.168.2.44
my computer ip:192.168.2.111
我使用了一个名为 hercules
的程序来连接传感器:
在 TCP server
选项卡中,当我单击 listen button
时,我将端口定义为 3000
程序显示了这一点(如图所示)client connected
连接后,我可以向传感器发送命令以获取如图所示的值:
我找到了这段代码,但它不起作用,我无法通过它发送命令来获取值,主要问题是我的代码无法连接到端口,你能帮我一些忙吗?我是套接字编程的新手。
代码:
try
{
string hostname = "192.168.2.44";
int portno = 3000;
IPAddress ipa = Dns.GetHostAddresses(hostname)[0];
try
{
System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
sock.Connect(ipa, portno);
if (sock.Connected == true) // Port is in use and connection is successful
Console.WriteLine("Port is Closed");
sock.Close();
}
catch (System.Net.Sockets.SocketException ex)
{
if (ex.ErrorCode == 10061) // Port is unused and could not establish connection
Console.WriteLine("Port is open");
else
Console.WriteLine(ex.ErrorCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
异常:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.2.44:3000
我应该使用 c#
实现类似 hercules
的东西
在您的截图中,PC 是主设备(它打开一个监听服务器套接字),传感器是从设备。当您的代码假设时,该 PC 会尝试作为客户端连接到传感器。
最小的代码片段是这样的:
var listener = new TcpListener(IPAddress.Any, 3000);
listener.Start();
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
{
// build a request to send to sensor
var request = new byte[] { /*...*/ };
stream.Write(request, 0, request.Length);
// read a response from sensor;
// note, that respose colud be broken into several parts;
// you should determine, when reading is complete, according to the protocol for the sensor
while (!/* response is complete */)
{
// stream.Read calls here
}
}
另请注意,如果协议是文本的,那么您可以使用 StreamWriter
/StreamReader
构建请求和解析响应。
我有一个传感器,我使用网络连接到这个传感器 cable.I 应该向这个传感器发送命令以获取值。
The sensor ip is :192.168.2.44
my computer ip:192.168.2.111
我使用了一个名为 hercules
的程序来连接传感器:
在 TCP server
选项卡中,当我单击 listen button
时,我将端口定义为 3000
程序显示了这一点(如图所示)client connected
连接后,我可以向传感器发送命令以获取如图所示的值:
我找到了这段代码,但它不起作用,我无法通过它发送命令来获取值,主要问题是我的代码无法连接到端口,你能帮我一些忙吗?我是套接字编程的新手。
代码:
try
{
string hostname = "192.168.2.44";
int portno = 3000;
IPAddress ipa = Dns.GetHostAddresses(hostname)[0];
try
{
System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
sock.Connect(ipa, portno);
if (sock.Connected == true) // Port is in use and connection is successful
Console.WriteLine("Port is Closed");
sock.Close();
}
catch (System.Net.Sockets.SocketException ex)
{
if (ex.ErrorCode == 10061) // Port is unused and could not establish connection
Console.WriteLine("Port is open");
else
Console.WriteLine(ex.ErrorCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
异常:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.2.44:3000
我应该使用 c#
实现类似hercules
的东西
在您的截图中,PC 是主设备(它打开一个监听服务器套接字),传感器是从设备。当您的代码假设时,该 PC 会尝试作为客户端连接到传感器。
最小的代码片段是这样的:
var listener = new TcpListener(IPAddress.Any, 3000);
listener.Start();
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
{
// build a request to send to sensor
var request = new byte[] { /*...*/ };
stream.Write(request, 0, request.Length);
// read a response from sensor;
// note, that respose colud be broken into several parts;
// you should determine, when reading is complete, according to the protocol for the sensor
while (!/* response is complete */)
{
// stream.Read calls here
}
}
另请注意,如果协议是文本的,那么您可以使用 StreamWriter
/StreamReader
构建请求和解析响应。