B4J Library Asynchronous modbus TCP master

Hello everyone,

Here a modbus TCP master library, fully programmed in b4j.
This library is asynchronous, by use of the asyncstreams and Callback method.
The response is an event, a separate sub, different then your sub where you did the request.
This has the advantage that an unresponsive server will not slow down your application.

The zip file here is a command line example, with the library.
You can compile this library as internal library as desired, but like this, you can look inside the code of the lib.

Supported functions:
-Read holding registers
-Read input registers
-Read coils
-Read discrete inputs
-Write single register
-Write multiple registers
-Write single coil
-Write multiple coils

Please note that I am not a professional programmer, so comments or feedback is always appreciated!

Kind regards,
Coldrestart.


** UPDATE on 25/07/2020**
Changes made in the library in the GUI version;
-Fixed typo in function name.
-TCP port and IP address are now in the Connect function instead of the initialization of the object.

**UPDATE on 18/08/2020**
Added: type selection, only for the representation, in signed, unsigned, hex and binary.

**UPDATE on 22/09/2020**
Changes made in the CLI version:
-The port and IP adress are passed when using the function "ConnectServer", instead of the initialization function.
Changes made for the CLI and GUI version: Conversion sub "ToBinaryString" was returning 9 bits instead of 8.
This issue was reported by the user "Madru" of the forum, thanks to him for reporting this issue.


GUI_MB_TCP_MASTER.png
 

Attachments

  • ModbusMaster_2020_09_22.zip
    9.4 KB · Views: 197
  • ModbusTCPMasterGUI_2020_09_22.zip
    13.3 KB · Views: 185
Last edited:

yaniv hanya

Active Member
Licensed User
how can we use it in b4a? we need exactly the same project for resperyPI device (android) to connect to monbus. how can we wrap/ translate it to b4a?
 

Coldrestart

Member
Licensed User
Yes, you can use this also in b4a, but you need to modify it.
I do not have much experiance in b4a, but you need to take in account that you are using asyncstreams.
So I think you need to initialize and access the lib in a service, and with the callsubdelayed functions, communicate with you UI. (Main thread)
Also, a timeout timer keeps track for an unresponsive slave, I also think that when the app is closed by the OS, this detection system is not working properly anymore.
 

Coldrestart

Member
Licensed User
Update 25/07/2020 - GUI example added, TCP port and IP address are now in the Connect function instead of the initialization of the object -> change made only in the GUI version.
 

Coldrestart

Member
Licensed User
Feedback from a user on the forum, that is good to know:
The starting address of a register is the PDU register, is zero based, while the official adressing is not zero based.
So it is possible if in a manual of a slave device, you need to read holding register 5001, in the lib, you need to send start register 5000.
If you don't like this, you can add a "+1" in the lib for the startaddress.

For more information about the modbus protocol, I refer to the official modbus website: https://modbus.org
There you can find the .pdf documents that describe the modbus protocol.


1596736833940.png
 

Coldrestart

Member
Licensed User
**UPDATE on 22/09/2020**
Changes made in the CLI version:
-The port and IP adress are passed when using the function "ConnectServer", instead of the initialization function.

Changes made for the CLI and GUI version: Conversion sub "ToBinaryString" was returning 9 bits instead of 8.
This issue was reported by the user "Madru" of the forum, thanks to him for reporting this issue.
 
Top