-1

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

  • Start by using cmd.exe and > PING IP. Try both computer name and IP address. Sometimes this error occurs when two PCs are configured with same IP address. – jdweng Jul 22 '19 at 11:43
  • @jdweng I've tried both IPs. Machine as well as device's IP. pinging correctly. used telenet too, but got nowhere, everything's fine in it. Am I doing anything wrong while providing IP addresses? – Samrat Matte Jul 22 '19 at 11:46
  • The listener/server need to start first using : var listener = new TcpListener(IPAddress.Any, 502); Then the client should connect to server using either the server name or IP. The server should only have listener code and the client only have client code. – jdweng Jul 22 '19 at 11:53
  • I think you shouldn't create that `TcpListener` yourself. `ModbusTcpSlave` creates a `TcpListener` under the hood ([source](https://github.com/richardlawley/nmodbus/blob/master/src/Modbus/Device/ModbusTcpSlave.cs)). So your first listener is probably blocking the slave from using the same ports. Also, your console application finishes instantly. Add a `Console.Readline()` at the end of your Main method to make sure it keeps listening. – Jesse de Wit Jul 22 '19 at 13:56
  • @JessedeWit Ok I'll try that. – Samrat Matte Jul 23 '19 at 08:27

1 Answers1

0

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();
Dharman
  • 30,962
  • 25
  • 85
  • 135
midohioboarder
  • 438
  • 5
  • 14