简体   繁体   English

如何在同一网络中具有不同 IP 的设备和计算机上使用 tcplistener 和 tcpclient?

[英]How to use tcplistener and tcpclient on device and computer with different ip in same Network?

I'm trying to communicate with a modbus device in my network at ip 192.168.1.76.我正在尝试与网络中 ip 192.168.1.76 的 modbus 设备进行通信。 My host computer address is 192.168.1.132.我的主机地址是 192.168.1.132。 I'm not able to connect to or listen to device ip.我无法连接或收听设备 IP。

basically i'm using NModbus4 library.基本上我正在使用 NModbus4 库。 I've created a ModbusTCPSlave and attached the tcp listener to it.我创建了一个 ModbusTCPSlave 并将 tcp 侦听器附加到它。 then i assigned ModbusSlaveRequestReceived event to that slave.然后我将ModbusSlaveRequestReceived事件分配给该从站。 but it gives nothing in return when i try to change register values directly from Modscan software.但是当我尝试直接从 Modscan 软件更改寄存器值时,它没有任何回报。

Main()
{
    var masterEndpoint = new IPEndPoint(IPAddress.Parse("192.168.1.132"), 502);
    var listener = new TcpListener(IPAddress.Any, 502);
    listener.Start();

    var slave = ModbusTcpSlave.CreateTcp(255, new TcpListener(masterEndpoint), 10);
    slave.ModbusSlaveRequestReceived += Modbus_Request_Event;
    slave.Listen();
}
private static void Modbus_Request_Event(object sender, Modbus.Device.ModbusSlaveRequestEventArgs e)
{
    //disassemble packet from master
    byte fc = e.Message.FunctionCode;
    byte[] data = e.Message.MessageFrame;
    byte[] byteStartAddress = new byte[] { data[3], data[2] };
    byte[] byteNum = new byte[] { data[5], data[4] };
    Int16 StartAddress = BitConverter.ToInt16(byteStartAddress, 0);
    Int16 NumOfPoint = BitConverter.ToInt16(byteNum, 0);
    Console.WriteLine(fc.ToString() + "," + StartAddress.ToString() + "," + NumOfPoint.ToString());

}

I expect to get function code, start address and number of points in console application when any register value is changed我希望在更改任何寄存器值时在控制台应用程序中获得功能代码、起始地址和点数

I copied your code.我复制了你的代码。 Changed the IP address to my "server" and it worked.将 IP 地址更改为我的“服务器”并且它起作用了。 So, the issue you are having is either in the setup of your "server" or in the PLC program.因此,您遇到的问题要么是在“服务器”的设置中,要么是在 PLC 程序中。

I thought that I had to do some port forwarding on my router.我想我必须在我的路由器上做一些端口转发。 I did not.我没有。 It did not make a difference.它没有任何区别。

Server setup:服务器设置:

Your "server"'s IP address needs to be static.您的“服务器”的 IP 地址需要是静态的。 Whether your 'server' is your development system or not.无论您的“服务器”是否是您的开发系统。 And don't forget when you deploy... Server's IP address has to be static as well (not that it wouldn't be...just saying)并且不要忘记部署时...服务器的IP地址也必须是静态的(不是说它不会...只是说)

Add an inbound Firewall rule to allow connections to the port, in this case 502, otherwise you'll have to allow access every time you launch/start a test.添加入站防火墙规则以允许连接到端口,在本例中为 502,否则每次启动/开始测试时都必须允许访问。

PLC program PLC程序

I am using Click PLC's by Koyo.我正在使用 Koyo 的 Click PLC。 Not sure if this is the rule for all PLC's or not;不确定这是否是所有 PLC 的规则; but, we had to add a line of code to "write" the values we wanted to pick up off the TCP stream.但是,我们必须添加一行代码来“写入”我们想要从 TCP 流中获取的值。 Without the write, the PLC was not sending out a request to join the TcpListener.如果没有写入,PLC 不会发出加入 TcpListener 的请求。

Last: The code to start your listener only needs to be this:最后:启动监听器的代码只需如下:

        var listener = new TcpListener(IPAddress.Parse("192.168.1.244"), 502);
        listener.Start();

        var slave = ModbusTcpSlave.CreateTcp(255, listener, 10);
        slave.ModbusSlaveRequestReceived += Modbus_Request_Event;
        slave.Listen();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用TCPListener(或其他方法)绑定到网络上的设备 - How to use TCPListener (or other method) to bind to device on the network 为TcpListener和TcpClient绑定相同的端点 - Binding same EndPoint for TcpListener and TcpClient 在其他设备上运行时,TcpListener不接受TcpClient [C#,Unity] - TcpListener doesn't accept TcpClient when run on a different device [C#, Unity] 如何在具有相同端口的不同 IP 地址上运行 HttpListener 和 TcpListener? - How can you run an HttpListener and TcpListener on different IP addresses with the same port? 如何通过在本地网络上运行TcpListener自动检测IP - How to auto detect IP from running TcpListener on local network 如何处理TcpClient与TcpListener断开连接 - How to handle TcpClient disconnect from TcpListener 如何使用TcpListener和TcpClient创建双工连接? - How to create a duplex connection using TcpListener and TcpClient? 如何通过TcpClient和TcpListener发送对象? - How to send objects through TcpClient and TcpListener? C# TcpListener 和 TcpClient 在一个网络中的不同 PC 上 - C# TcpListener and TcpClient on separate PCs in one network 监听来自 2 个 TcpClient 实例对相同 IP 的响应,但端口不同 - Listen to responses from 2 TcpClient instances against the same IP, but different ports
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM