steveohara / j2mod

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

Is ObservableRegister broken? #82

Closed pan-henryk closed 6 years ago

pan-henryk commented 6 years ago

I am trying to detect changes in my slave image in a way similar to described here: https://github.com/steveohara/j2mod/issues/64

My code looks like this:

`
SimpleProcessImage spi = new SimpleProcessImage(unitId);

               ObservableRegister or = new ObservableRegister();

                or.setValue(bb.getShort());

                or.addObserver(this);

                spi.setInputRegister(address,or);

`

I can read this register from master (so general setup is ok), but if I try to write to it, update method of observer is never called. At the message level slave receives write request and responds to it. Is there a chance that slave refactoring broke it?

I am using Modbus TCP.

steveohara commented 6 years ago

Could well be. Unfortunately we don't use this pattern at 4NG so it doesn't get a lot of love from us. If you could put together a unit test, I'll take a look at figuring out the problem.

pan-henryk commented 6 years ago

After creating a simple test I was able to find the problem - it is a misleading naming inconsistency (probably be the same issue in my code). In ProcessImage InputRegisters (F04) are added using addRegister function, while HoldingRegisters (F03) are added using addInputRegister function.

Here is the test: https://github.com/steveohara/j2mod/pull/85

steveohara commented 6 years ago

Test case added