steveohara / j2mod

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

How to add special function in ModbusMaster #113

Closed Julien-Faure closed 2 years ago

Julien-Faure commented 3 years ago

I would to add special function in ModbusMaster, can you help me please ?

Thank you in advance.

steveohara commented 3 years ago

What is the special function you want to add? You could of course just clone, add the code and create an MR if you're confident it will be good for the library

Julien-Faure commented 3 years ago

I wanted to know how to add custom function.

Read registers or write registers value are official functions.

For example :

I want to create a function with id 80.

This function could reboot a remote slave.

steveohara commented 2 years ago

ModbusMaster is an aggregate of other public classes that can do this if you use them directly. I would like to say that I can provide this as a feature of j2mod out of the box but I don't have time I'm afraid. Take a look at some of the examples under com.ghgande.j2mod.modbus.cmd for more concrete help

sfeilmeier commented 2 years ago

Me and @huseyinsaht currently face the same problem and I implemented a quick prototype that supports a specific Function Code 0x40. See this commit: https://github.com/sfeilmeier/j2mod/commit/449fabd56305639f59a630a2f2d9fa01e7439ede

Unfortunately the instantiation of the Request/Reponse objects happens in a static method inside ModbusRequest so we can only solve it by forking the library. It might be a good idea to refactor this method to allow providing a dynamic factory object that creates these objects at runtime.

@steveohara: Would this be an approach you would accept as a pull-request contribution or would you take another way?