[英]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.