Sun. Oct 24th, 2021

Edited by Marta Todeschini and Diego Rondini.


This technical notice goals to clarify the best way to use the Modbus protocol in Android.
The first part describes the principle capabilities of the protocol: the aim of its use, the construction of the packages and the architectural context in which the protocol is usually carried out.
The a part of the technical notice is targeted on the outline of the totally different choices by which to make use of Modbus in Android.

Modbus protocol

Modbus is an utility layer messaging protocol for communication between units elegant on the shopper/server paradigm. It is used in the sector of automation and permits units to change messages. Modbus is usually used to transmit indicators from instrumentation and management units to a principal controller or a knowledge gathering. The specs for the newest model of the protocol, 1.1b3, are available in the official Modbus Organization web site.

The following determine reveals the shopper/server construction of Modbus, the place the shopper acts because the Master and the server is the Slave. Communication is predicated on transactions, which include a request issued by the shopper to the server and a response issued by the server to the shopper.


The communication is predicated on a easy Protocol Data Unit (PDU) outlined by Modbus, which is impartial of the underlying communication layers. The PDU consists by a perform code and the information. The mapping of Modbus on particular buses or community can introduce some extra fields on the Application Data Unit (ADU), as you possibly can see in the next determine.


The protocol specification defines three varieties of PDU’s:

  1. Request PDU: a code specifying a perform (Function Code, 1 byte) and performance particular information (Function Data, various variety of bytes)
  2. Response PDU: the perform code comparable to the request (Function Code, 1 byte) and response particular information (Response Data, various variety of bytes)
  3. Exception Response PDU: the perform code comparable to the request + 0x80 (128), (Error Code, 1 byte)
    and a code specifying the exception (Exception Code, 1 byte)

There are two variations of Modbus protocol:

  1. Modbus RTU and Modbus ASCII for serial strains communication
  2. Modbus TCP for Ethernet communication

Information is saved in the Slave gadget in 4 totally different tables: two for coils and two for registers. Each desk has 9999 attainable addresses. Coils are discrete values (1 bit every) and registers are numerical values (every 1 phrase = 16 bits = 2 bytes). The following desk accommodates the fundamental Modbus information sorts outlined by the specification.

Coil/Register Numbers Data Addresses Type Table Name
1-9999 0000 to 270E Read-Write Discrete Output Coils
10001-19999 0000 to 270E Read-Only Discrete Input Contacts
30001-39999 0000 to 270E Read-Only Analog Input Registers
40001-49999 0000 to 270E Read-Write Analog Output Holding Registers

Modbus RTU

Modbus RTU (Remote Terminal Unit) is used to attach a supervisory pc with a RTU in supervisor management and information acquisition programs.

This packet consists by 4 elements:

  1. Slave ID: the distinctive deal with from 1 to 247 (most variety of Slaves for every grasp)
  2. Function Code: the desk to entry and whether or not to learn or write
  3. Data
  4. Error test: sometimes CRC (Cyclic Redundancy Check) is used

Modbus TCP/IP

TCP is a connection-based protocol so a connection should be established earlier than transferring information.

Modbus TCP merely takes a Modbus RTU message, transmits it with a TCP/IP wrapper and sends it over community as an alternative of serial strains.

The packet consists by six elements:

  1. Transition ID: 2 bytes set by the shopper to uniquely determine every request
  2. Protocol ID: 2 bytes set by the shopper, all the time 0000
  3. Length: 2 bytes for variety of bytes in the message to observe
  4. Unit ID: 1 byte set by the shopper and echoed by the server
  5. Function Code: the desk to entry and whether or not to learn or write
  6. Data

Modbus libraries for Android

The are a number of Java libraries that implement the Modbus protocol, essentially the most used are:

  1. jamod
  2. jLibModbus
  3. j2mod
  4. Modbus4j

Let’s take a look on the first two.


jamod (Java Modbus Library) is a pure Java library that can be utilized to implement Modbus Master and Slave each in IP and serial transport flavours. The library is properly documented because it supplies each full API documentation and guidelines on the best way to use jamod.


The Master is the shopper, which establishes a reference to the Slave (i.e. the server) and makes use of this connection for sending a Request to the Slave, from which a Response will probably be obtained. There might be just one Master requiring information from a supply (information acquisition) or writing information to a sink (gadget management).
The related Java lessons to implement a Master are:

  • SerialConnection for the creation of a serial connection
  • ModbusSerialTransaction for the transaction which consists by Request and Response
  • SerialParameters the parameters for use

The Slave is a server which has a listener for receiving an incoming request from the Master that are served offering a corresponding response. The related Java lessons to implement a Slave are:

  • ModbusSerialListener the listener for a request from a Master
  • DigitalIn, DigitalOut, InputRegister, Register the Modbus information sorts
  • SerialParameters the parameters for use

j2mod is a fork of the jamod 1.2 codebase and has quite a few bug fixes and extra options with respect to the unique mission. This is probably going the only option for jamod customers seeking to migrate to an up-to-date library.


  1. Easy to make use of
  2. Fully documented
  3. Several utilization examples accessible


  1. Modbus RTU depends on Java Communications API, which has no official implementation for Android. The solely possibility is to make use of some port of RXTX for Android
  2. The library will not be up-to-date, the lastest official launch (1.2rc1) dates again to 2004


jLibModbus is an implementation of the Modbus protocol in Java language. It helps the commonest libraries for serial communication equivalent to RXTX, jssc, jSerialComm, purejavacomm and Java Communications API. jLibModbus covers all the principle options of the Modbus protocol, equivalent to Modbus RTU, Modbus TCP, Modbus ASCII, Modbus Master and Modbus Slave.

jLibModbus supply code is hosted in this github repository.


  1. Actively maintained
  2. Support for a number of totally different serial communication libraries (jssc, RXTX, purejavacomm, Java Communications API)
  3. (Quite) full implementation of the Modbus protocol.


  1. Code construction wants a while to be understood
  2. Documentation typically unclear or missing

Serial libraries for Modbus RTU in Android

An side which requires explicit consideration when utilizing Modbus RTU on Android is the selection of the serial communication library for 2 purpose:

  1. Java libraries, particularly when Java Communications API are concerned, attempt to summary away how the serial gadget is accessed by the platform, thus requiring using an exterior library for serial communication
  2. implementations for Linux of Java serial libraries won’t be simple to port to Android due to variations between the 2 platforms, particularly concerning the compilation of the native code accessing serial port units

Libraries like RXTX for instance have ports for Android, however are typically unmaintained and never appropriate with latest Android model.


jSerialComm is a library written in Java which supplies a approach to entry normal serial ports with out requiring exterior libraries or native code. The declared function of the library is to be a legitimate different to RXTX and Java Communications API. jSerialComm is platform-independent (mechanically makes use of appropriate native library elegant on present structure) and has the flexibility to open a number of ports concurrently.

See the Javadoc Library Reference for a whole overview of jSerialComm API.


  1. Multi-platform (in explicit helps Android)
  2. Lightweight and environment friendly
  3. Regularly up to date


  1. Cannot be used immediately in mixture with jamod, as it isn’t a Java Communications API implementation

By admin