steveohara / j2mod

Enhanced Modbus library implemented in the Java programming language
Apache License 2.0
267 stars 111 forks source link

TCP slave, error on reading #39

Closed CJure closed 7 years ago

CJure commented 7 years ago

Hi,

I was testing j2mod TCP slave for my project. I used example code:

public static void main(String[] args) 
    {
        ModbusSlave slave;
        try
        {
            // Create your register set
            ProcessImage image = makeMyProcessImage();

            // Create a slave to listen on port 502 and create a pool of 5 listener threads
            // This will create a new slave or return you the same slave already assigned to this port
            slave = ModbusSlaveFactory.createTCPSlave(502, 5);

            // Add the register set to the slave for unit ID 1
            // Each slave can have multiple process images but they must have a unique Unit ID within the slave
            slave.addProcessImage(1, image);

            // Start the slave listening on the port - this will throw an error if the socket is already in use
//            ModbusSlaveFactory.close(); 
            slave.open();
            while(1 == 1)
            {
                Thread.sleep(1000);
                System.out.println(new Date().toString());
            }
            // Close the slave - closes the socket and stops listening
        }
        catch(Exception ex)
        {
            System.out.println("exception on main: " + ex);
        }
    }

    private static ProcessImage makeMyProcessImage() 
    {
        SimpleProcessImage spi = null;
        spi = new SimpleProcessImage();
        spi.addDigitalOut(new SimpleDigitalOut(true));
        spi.addDigitalOut(new SimpleDigitalOut(false));
        spi.addDigitalIn(new SimpleDigitalIn(false));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addDigitalIn(new SimpleDigitalIn(false));
        spi.addDigitalIn(new SimpleDigitalIn(true));
        spi.addRegister(new SimpleRegister(251));
        spi.addInputRegister(new SimpleInputRegister(45));
        return spi;
    }

For master I used ModMaster 0.4 program. On first read everything would be ok and I would get the value, that I had set in my Java program. But on second read I would get Bad file descriptor insider ModMaster and I would had to open and close connection to get another value.

After a while I tried it again, and now I can read same registers multiple times, but now I get unknown error if I change type of registers that I want to read or if I want to read bigger number of coils or registers then I had set in my Java program.

It's not ideal that you can read only coils / registers that had been set inside Java program, but it's quite a problem that you have to close and open connection if you want to read some other registers.

Does anyone any idea if this is library's limitation or should I change my code?

CJure commented 7 years ago

I tried with another client and it looks that it was client problem.