简体   繁体   中英

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. My host computer address is 192.168.1.132. I'm not able to connect to or listen to device ip.

basically i'm using NModbus4 library. I've created a ModbusTCPSlave and attached the tcp listener to it. then i assigned ModbusSlaveRequestReceived event to that slave. but it gives nothing in return when i try to change register values directly from Modscan software.

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. So, the issue you are having is either in the setup of your "server" or in the PLC program.

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. 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)

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.

PLC program

I am using Click PLC's by Koyo. Not sure if this is the rule for all PLC's or not; but, we had to add a line of code to "write" the values we wanted to pick up off the TCP stream. Without the write, the PLC was not sending out a request to join the 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM