0

In order to learn how to use Modbus, I've been trying to set up two computers I have to communicate between each other. I have them connected via an Ethernet cable, and have an instance of Visual studio running C# on each of them. I've attached both codes below, which are snippets from the NMODBUS sample program on github. Master:

        using (TcpClient client = new TcpClient("127.0.0.1", 502))
        {
            var factory = new ModbusFactory();
            IModbusMaster master = factory.CreateMaster(client);

            // read five input values
            ushort startAddress = 100;
            ushort numInputs = 5;
            bool[] inputs = master.ReadInputs(0, startAddress, numInputs);

            for (int i = 0; i < numInputs; i++)
            {
                Console.WriteLine($"Input {(startAddress + i)}={(inputs[i] ? 1 : 0)}");
            }

Slave: //

        // create and start the TCP slave
        int port = 502;
        IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
        TcpListener slaveTcpListener = new TcpListener(address, port);
        slaveTcpListener.Start();

        IModbusFactory factory = new ModbusFactory();

        IModbusSlaveNetwork network = factory.CreateSlaveNetwork(slaveTcpListener);

        IModbusSlave slave1 = factory.CreateSlave(1);
        IModbusSlave slave2 = factory.CreateSlave(2);

        network.AddSlave(slave1);
        network.AddSlave(slave2);

        network.ListenAsync().GetAwaiter().GetResult();

        // prevent the main thread from exiting
        Thread.Sleep(Timeout.Infinite);

Github: https://github.com/NModbus/NModbus

The slave code runs as expected, but the master code compiles, but then throws this error: System.Net.Sockets.SocketException: 'No connection could be made because the target machine actively refused it 127.0.0.1:502'

I'm confident that the code snippets are correct, and am currently convinced that the problem is that these two computers aren't actually talking with one another. What do I have to change to get the communication working?

  • 127.0.0.1 is the IP address of the computer you are running the code on, it will never be another computer. Have you set up static IP addresses for the 2 computers, or do you have a DHCP server to allocate IP addresses? – Neil Jun 13 '22 at 20:42
  • Windows does not allow the loopback address to be used for the client connection. Instead use the machine name or the IP of machine. – jdweng Jun 13 '22 at 21:10
  • Ok, now that I understand the importance of the 127.0.0.1 IP address, I understand that it will never work for what I was trying to do. I've gotten modbus to talk to itself on the same computer over 127.0.0.1, but I still want to talk to two seperate computers over ethernet. Is it possible to do this with the same code by changing the IP address? and if so, to what? I tried setting both programs to use the slave's IP address, but that didn't work. – Cassius.Peter Jun 14 '22 at 19:59
  • Set the slave to listen on [`IPAddress.Any`](https://stackoverflow.com/questions/7238096/tcplistener-how-to-listen-on-specific-port-on-all-interfaces) and the master should use the slaves IP (because that is what you want it to connect to). Note that firewalls may be an issue and you may need admin rights to listen on port 502 (ports below 1024 are restricted on some OS's). – Brits Jun 15 '22 at 03:05

1 Answers1

0

You are trying to run this on one computer and using the local address 127.0.0.1 so this is not a firewall problem. I recommend using the "hercules terminal" tool. You can use it to create a fake TCP client on port 506 and check if the server will then connect and send any query.

You can run both programs on one computer and then the address 127.0.0.1 is ok. Remember, however, that only one application can listen on a given port (in this case only one ModBus client application can be running on the computer).

I do not know how the ModBus library you are using is implemented, but sometimes it helps in such cases as you run Visual Studio as an administrator.

sewep
  • 9
  • 2