Coder Social home page Coder Social logo

TCP slave is stuck about jlibmodbus HOT 8 OPEN

kochedykov avatar kochedykov commented on August 15, 2024
TCP slave is stuck

from jlibmodbus.

Comments (8)

browncrane avatar browncrane commented on August 15, 2024

onReadHoldingRegisterRange: address 0, quantity 10
It seems like 10 registers are required. However,

            hr = new ModbusHoldingRegisters(10);
            hr.set(0, 12345);
            hr.set(1, 111);

you only set two. It's a usage problem.

from jlibmodbus.

daremi avatar daremi commented on August 15, 2024

I have removed the rest eight registers from the code above to make it shorter.

from jlibmodbus.

browncrane avatar browncrane commented on August 15, 2024

see the example

below line 94

            if (slave.isListening()) {
                Runtime.getRuntime().addShutdownHook(new Thread() {
                    @Override
                    public void run() {
                        synchronized (slave) {
                            slave.notifyAll();
                        }
                    }
                });

                synchronized (slave) {
                    slave.wait();
                }

                /*
                 * using master-branch it should be #slave.close();
                 */
                slave.shutdown();
            }

In your code, the slave would be destroy after the first request.

from jlibmodbus.

daremi avatar daremi commented on August 15, 2024

Thanks for ideas. Added code:

            System.out.println("before slave.isListening()");
            if (slave.isListening()) {
                Runtime.getRuntime().addShutdownHook(new Thread() {
                    @Override
                    public void run() {
                        synchronized (slave) {
                            slave.notifyAll();
                        }
                    }
                });

                synchronized (slave) {
                    slave.wait();
                }

                /*
                 * using master-branch it should be #slave.close();
                 */
                slave.shutdown();
            }
            System.out.println("after slave.isListening()");

and here are the logs (two requests from external software):

before slave.isListening()
Client connected /10.171.99.99
INFO: Frame recv: 00000000000601030000000A
onReadHoldingRegisterRange: address 0, quantity 10
INFO: Frame sent: 0000000000170103143039006F00DE014D01BC022B029A0309037803E7
WARNING: java.net.SocketTimeoutException: Read timed out
Client disconnected /10.171.99.99
Client connected /10.171.99.99
WARNING: java.net.SocketTimeoutException: Read timed out
Client disconnected /10.171.99.99

After that external software is freezing. In log there is no "after slave...", so this thread is waiting on wait() on tomcat side. I could run it as separate thread, but if synchronous not working, async will not either...
What is interesting, on debuging line by line, (without if() block) external software works fine until Resume thread.

from jlibmodbus.

ippodamia avatar ippodamia commented on August 15, 2024

I tested the slave shutdown code from the SimpleSlaveTCP.java example (line 99-117). My Slave is connected with a Master. I noticed that the code blocks in the wait method and never continuous with the shutdown of the slave. The communication with the Master is not aborted.

I then used the code from the ModbusTest.java and the ExampleTCP.java i.e.:

 try {
       slave.shutdown();
  } 
  catch (ModbusIOException e) {
        e.printStackTrace();
 }

This does not block but it still does not interrupt the communication with the Master.

Only when I also set a new empty DataHolder to the slave, is the communication with the Master really interrupted:

 try {
       this.slave.setDataHolder(new DataHolder());
       slave.shutdown();
  } 
  catch (ModbusIOException e) {
        e.printStackTrace();
  }

Should perhaps the reset of the DataHolder be part of the slave shutdown method?

Best Regards

from jlibmodbus.

browncrane avatar browncrane commented on August 15, 2024

Hi @ippodamia, What do you mean by?

it still does not interrupt the communication with the Master.

What type of Slave you are using?

from jlibmodbus.

ippodamia avatar ippodamia commented on August 15, 2024

Hello browncrane.
I am using a ModbusSlaveTCP slave. After the slave.shutdown(); method has been called the slave is not null and slave.isListening() returns false. I can see that my Master is still connected to the Slave (makes read requests that are getting answered).
I would expect that after the ModbusSlaveTCP.shutdownImpl() has been executed the ServerSocket should be closed and my Master should loose its connection.

Since the Socket has not been closed, the DataHolder of the Slave does not know that the Slave has been shutdown. I could check in my DataHolder methods (f.e. readHoldingRegisterRange()) if the slave is still listening, in order to detect that the Slave has been shutdown.

from jlibmodbus.

KWSimon avatar KWSimon commented on August 15, 2024

Hi @browncrane and @ippodamia,

I ran into the same issue as @ippodamia. After calling the slave.shutdown() method, Modbus TCP requests received by my application keep being processed.

From my understanding of the code I looked into, the issue come from the following :

The method RequestHandlerTCP.closeConnection() is not called by the class ModbusSlaveTCP (as it is by ModbusSlaveSerial).
As a consequence setListening(false) is not called neither and the method RequestHandler.isListening() keep return true.
So the condition to end the "infinite" loop processing the requests in RequestHandlerTCP.run() is never matched and the connection never closed.

I haven't found any workaround since no reference on the RequestHandlerTCP is keeped in ModbusSalveTCP that would allow to call the closeConnection() method.

com.intelligt.modbus.jlibmodbus.slave.ModbusSlaveTCP.run()

 @Override
    public void run() {
      [...]
                    threadPool.execute(new RequestHandlerTCP(this, s));
      [...]
    }

I hope this will help.

Simon

ps : I'm using the version 1.2.9.7

from jlibmodbus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.