Modbus protocol Modbus is used for communication between devices. It is also rarely used in normal App development
1. Import the Modbus4Android library
- Gayhub: github.com/zgkxzx/Modb…
Importing the project using a Jar package can be, or can be directly down to the local, importing the project, I use is the second method
Modbus Master/ client based on TCP/IP
2.1. Initialize ModbusMaster
ModbusReq.getInstance().setParam(ModbusParam()
.setHost(salveIP)// The IP address of the slave
.setPort(salvePort)// The slave port
.setEncapsulated(false)//
.setKeepAlive(true)
.setTimeout(2000)
.setRetries(0)).init(object : OnRequestBack<String> {
override fun onSuccess(t: String?).{}override fun onFailed(msg: String?).{}})Copy the code
2.2 Read/write hold register
/** * Function Code 3 * Read Holding Registers ** @param onRequestBack callback * @param slaveId slave ID Slave ID * @param start start address Start position of read hold register * @param len length Length of read data */Copy the code
ModbusReq.getInstance().readHoldingRegisters(object : OnRequestBack<ShortArray> {
@SuppressLint("SetTextI18n")
override fun onSuccess(t: ShortArray?). {// This is the read data
}
override fun onFailed(msg: String?). {
}
}, slaveId, shart, len)
Copy the code
Write hold register
/**
* Function Code 16
* Write Registers
*
* @param onRequestBack callback
* @param slaveId slave id
* @param start start address
* @param values values
*/
Copy the code
ModbusReq.getInstance().writeRegisters(object : OnRequestBack<String> {
override fun onSuccess(s: String){}override fun onFailed(msg: String) {
}
}, slaveId, start, shortArray)
Copy the code
2.3. Read and write coils
** * Function Code 1 * Read Coil Register ** @param onRequestBack callback * @param slaveId slave id * @param start start address * @param len length */Copy the code
ModbusReq.getInstance().readCoil(object :OnRequestBack<BooleanArray>{
override fun onSuccess(t: BooleanArray?).{}override fun onFailed(msg: String?).{}},1.1.10)
Copy the code
Write the coil
ModbusReq.getInstance().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())
Copy the code
The usage of each function is basically the same, except that the method name is different from the function code. When reading, it passes in the id of the slave station and the starting position, as well as the length to read. When writing, it also passes in the address of the slave station, the starting position, and an array, and writes the values of the array from the starting position
2.4 recycling Master
ModbusReq.getInstance().destory()
Copy the code
Modbus Salve/ server based on TCP/IP
Start the service
var modbusSlave=TcpSlave(502.false)// Default port 502, compression is not enabled
modbusSlave.addProcessImage(BasicProcessImage(1))// Initialize the image of the datastore.
modbusSlave.start()// Close the service stop method
Copy the code
4, summarize
ModbusAndroid terminal is quite simple to use, encapsulation is very good, if useful to brothers, one key three connect!