Sun. Dec 5th, 2021
Introduction to modbus RTU library for Android devices

The purpose of this library is to let customers to leverage the ability of modbus RTU protocol by RS485 on Android devices, particularly on the Ltouch multi-touch panels (image on the fitting) and growth boards.

In order to talk along with your devices and devices from Android by RS485 and modbus, you’ll be able to hyperlink the modbus RTU library in your tasks and name the strategies, resembling operate code 3 Read Holding Registers or operate 16 Preset Multiple Registers, essentially the most extensively used modbus capabilities.

This doc helps you to perceive how to design a mission that makes use of the native modbus library for Android. Many approaches exist in the way in which a mission could possibly be structured; we recommend considered one of that’s full and environment friendly although.

Starting from January 2014, the most recent library (v.0.3) helps each Android 2.3 Gingerbread and Android 4.0.4 Ice Cream Sandwich.

  • Ltouch F contact panel

    The Ltouch board is a high-performance system based mostly on the Samsung ARM Cortex-A8 microcontroller particularly designed for industrial and residential automation. Supports Android 2.3 and 4.0.4.

    Details Store

The most frequent scenario that’s encountered in industrial and house automations functions is the next: a number of slave devices (resembling PLCs, Power inverters, distant I/O modules) related in a bus line and a contact panel that acts as a grasp displaying devices’ states accepting instructions from customers. Basically, that is on the coronary heart of Human Machine Interface (HMI).

The HMI interface is designed utilizing the Android framework. For these of you that aren’t conversant in creating Android tasks, we recommend to check out this transient tutorial on how to create your first app with Android. Many different tutorials and books can be found on the web.

The high-level structure of a hypothetical Android mission will be summarized within the image under. The rounded bins symbolize respectively:

  • The primary exercise (that runs on the MainUIthread)
  • The thread that handle all of the modbus requests (pink)
  • The native modbus library (yellow)
  • A FIFO queue knowledge construction that’s used for the modbus Writes.
  • android modbus rtu library

    The first query could come up is the next: why utilizing a FIFO queue and significant sections? Because we supposed that the mission has to handle common graphical updates, due to this fact the stream of learn requests has to be appropriately overlapped with writes. Indeed, the FIFO queue garantees the requests will likely be processed with the identical order as they arrive.

    In this tutorial, modbus reads and writes are thought-about as synonyms of modbus operate 3 and 16 respectively.

    The mainUIthread symbolize the principle thread Activity(“Window”) that the customers see. It accommodates all of the code obligatory to handle the UI, like buttons, progress bars, textual content bins, and so on. We used just one Activity, however clearly, actual world functions use multiple exercise for accomplish their jobs. Indeed, it begins and stops the thread that manages the modbus reads/writes. The java code could be:

public class Most importantActivity extends Activity{
	personal int fid 	     = 0;	//file handler to the serial port
	personal Thread workingthread = null;	//modbus thread
	personal int ReadsRefreshRate = 1000;
	//declare the queue with most 2000 components
	personal BlockingQueue queue_writes = new ArrayBlockingQueue(2000);
	protected void onResume() {
		if (fid == 0){
			//Serial port opening with baudrate 38400bps and 40000ns for learn/write timeout
            		fid = ModbusLib.openCom(38400,40000,40000);
		//begin the thread that handle the modbus RTU requests
		workingthread=new ReadsWrites(queue_writes, mHandler, fid, ReadsRefreshRate);

The onResume technique accounts for opening the serial port (on this case with baudrate set to 38400bps and timeouts 40000ns for each modbus reads and writes) and for beginning the thread that manages the modbus requests. When creating the modbus thread, it wants 4 parameters:

  • a blocking queue knowledge construction,
  • an handler for returning again the modbus replies to the UI thread,
  • the file id that refers to the serial port you need to work with,
  • the Reads Refresh Rate that’s the perfect refresh fee with which the reads will likely be executed.

The queue_writes is a blocking queue with a most size of 2000. We used a BlockingQueue knowledge construction as a result of the weather have to be added atomically, preserving additionally the order with which requests arrives. Because of that, the code that inserts the weather into the queue will likely be in a crucial part.

The objects which might be saved within the queue have to be of kind WriteRequest. The class WriteRequest merely defines the article “modbus function” and is coded as observe:

public class WriteRequest {
	personal int tackle;			//register tackle to begin writing
	personal int node_id;			//slave id gadget quantity
	personal int regs;			//variety of registers to write
	personal int[] holdingRegs;		//registers to write

	public int getAddress() {
		return tackle;

	public void setAddress(int tackle) {
		this.tackle = tackle;

	public int getNode_id() {
		return node_id;

	public void setNode_id(int node_id) {
		this.node_id = node_id;

	public int getRegs() {
		return regs;

	public void setRegs(int regs) {
		this.regs = regs;

	public int[] getHoldingRegs() {
		return holdingRegs;

	public void setHoldingRegs(int[] holdingRegs) {
		this.holdingRegs = holdingRegs;

	public WriteRequest(int node_id, int tackle, int regs, int[] holdingRegs) {
		//class constructor
		this.tackle = tackle;
		this.node_id = node_id;
		this.regs = regs;
		this.holdingRegs = holdingRegs;

A modbus write request will be outlined as

int hr = new int[6];		//the holding register
WriteRequest w1 = new WriteRequest(1,1,6,hr);

with hr as a sound reference to an integer array. As beforehand said, the code that provides write requests has to be in a crucial part to protect the order. This is completed through the use of a Java synchronized assertion specifying the identify of the variable to be accessed atomically (i.e., the queue), particularly:

synchronized (queue_writes) {

	WriteRequest w1 = new WriteRequest(1,1,6,hr);


The remaining assertion wakes up the modbus thread in case it was sleeping, due to this fact it will possibly course of the brand new requests which might be on the queue.

We are actually prepared to analyze how will be designed the principle thread that handle the modbus requests. We known as this class ReadsWrites and it extends Thread so as to be executed in parallel with the opposite threads within the Android OS. The class personal variables wrqueue, hd, fid and reads_rr are the identical as these handed to the article constructor along with another variables referring to modbus reads.

The core of the category stands within the crucial part. It works in a method that when the queue isn’t empty, it executes all of the writing requests; in any other case it cyclically reads registers from the slave(s) and pause itself for a restricted timespan. The thread wakes up when a brand new write request has to be executed.

Since in Android background threads can’t entry the graphical objects immediately, it’s necessary to handle the UI updates with messages and specifically utilizing the deal with that had been gathered on the time of thread creation. The messages are created and despatched by calling the receiveMessage and shipMessage strategies. The class’ code could be the next:

public class ReadsWrites extends Thread{

   personal BlockingQueue wrqueue = null;
   personal Handler hd                          = null;
   personal int max_retries                     = 1;	//defines what number of retries earlier than giving up
   personal int reads_rr                        = 1000; 	//Read refresh fee, default worth 
   personal int node_to_write     = 1;
   personal int starting_address  = 1;
   personal int no_of_registers   = 10;
   personal int offset            = no_of_registers;
   personal int fid               = 0;
   int retries                   = 0;
   public ReadsWrites(BlockingQueue q, Handler deal with, int serial_port, int rrefresh) {
      wrqueue  = q;
      hd       = deal with;
      fid      = serial_port;
      reads_rr = rrefresh;
   public void run() {
         int[] holdingRegs;
         //registers that shops the learn values
         holdingRegs       = new int[15];
         lengthy bytes_received1    = 0;

         strive {
            synchronized (wrqueue) {
               if (!wrqueue.isEmpty()){
                  //extract Modbus Presets Registers from queue
                  WriteRequest wr = wrqueue.ballot();
                  //wr will likely be not null as a result of isEmpty is fake
                  //use the preset parameters from wr object
                  node_to_write     = wr.getNode_id();
                  starting_address  = wr.getAddress();
                  no_of_registers   = wr.getRegs();
                  holdingRegs       = wr.getHoldingRegs();
                  if (bytes_received1 = 7){

                          Message msg = hd.receiveMessage();
                          msg.what = 1;
                          msg.arg1 = holdingRegs[0];
                          msg.arg2 = holdingRegs[1];
                  }whereas (retries 

In this demo mission, two values solely are despatched again to the principle UI thread. If you want to change greater than few values, please check out our weblog publish by which Android bundle is used to determine this concern.

The design we offered on this overview helps you to lay out the structure of your Android utility. It is sort of normal so will be adopted in lots of conditions and by {many professional} and residential customers of our Android contact panels and growth boards. We strongly recommend to beginning with it and increasing as your wants.

We examined this library with many devices resembling PLCs, AC Drives, Arduinos and actuators. It may occur that, largely in circumstances when the refresh frequency is excessive, not all of the requests will be executed appropriately. This concern is attributable to slave devices related on the bus which might be gradual in aswering. This concern is easy to determine: simply tune the reply delay of your PLC or actuator as little as potential, that is it.

This Java snippets we offered above don't handle the modbus errors which may occur. We omit these elements for the aim of readability. We strongly recommend to handle these conditions so as to create profitable and fault tolerant Android functions.

By admin