SoFunction
Updated on 2025-03-01

Simple way to use Modbus protocol on Android

Click me to learn about the Modbus protocol

Modbus is used for communication between devices, and is also rarely used in normal App development.

1. Import Modbus4Android library

gayhub address:/zgkxzx/Modb

You can use the Jar package to introduce it, or you can directly down this project to the local area and introduce this project. I use the second method

2. Modbus Master/Client based on TCP/IP

2.1. Initialize ModbusMaster

        ().setParam(ModbusParam()
                .setHost(salveIP)//The IP address of the slave station                .setPort(salvePort)//The port of the slave station                .setEncapsulated(false)//
                .setKeepAlive(true)
                .setTimeout(2000)
                .setRetries(0))
                .init(object : OnRequestBack<String> {
                    override fun onSuccess(t: String?) {
                   
                    }

                    override fun onFailed(msg: String?) {
                      
                    }
                })

2.2. Read and write hold register

Read hold register
/**
  * Function Code 3
  * Read Holding Registers
  *
  * @param onRequestBack callback
  * @param slaveId slave id Slave id
  * @param start start address Read the start position of the hold register
  * @param len length The length of data read
  */
 ().readHoldingRegisters(object : OnRequestBack<ShortArray> {
            @SuppressLint("SetTextI18n")
            override fun onSuccess(t: ShortArray?) {//This is the data read            
            }

            override fun onFailed(msg: String?) {
                
            }
        }, slaveId, shart, len)

Write hold register

/**
 * Function Code 16
 * Write Registers
 *
 * @param onRequestBack callback
 * @param slaveId       slave id
 * @param start         start address
 * @param values        values
 */
 ().writeRegisters(object : OnRequestBack<String> {
            override fun onSuccess(s: String) {

            }

            override fun onFailed(msg: String) {
            
            }
        }, slaveId, start, shortArray)
        

2.3. Reading and writing coil

Read coil

Read coil
/**
 * Function Code 1
 * Read Coil Register
 *
 * @param onRequestBack callback
 * @param slaveId       slave id
 * @param start         start address
 * @param len           length
 */
().readCoil(object :OnRequestBack<BooleanArray>{
    override fun onSuccess(t: BooleanArray?) {
       
    }

    override fun onFailed(msg: String?) {
       
    }
},1,1,10)

Writing coil

().writeCoils(object :OnRequestBack<String>{
    override fun onFailed(msg: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onSuccess(t: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
},1,1, arrayOf(true,true,false,false).toBooleanArray())

The usage of each function is basically the same, that is, the method name and function code are different. When reading, they are passed in the slave station id and the starting position, and the length to be read. When writing, they are also passed in a slave station address, the starting position and an array, and the value of the array is written in sequence from the starting position.

2.4 Recycling Master

().destory()

3. Modbus Salve/Server based on TCP/IP

Start the service

var modbusSlave=TcpSlave(502,false)//Default port 502, compression is not enabled(BasicProcessImage(1))//Initialize the image of the data storage,()//Close the service stop method

4. Summary

This is all about this article about using the Modbus protocol on the Android side. For more related content on the Android side, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!