This guide describes the methods available in the Android modbus RTU library for the Ltouch development boards.
In order to open the serial port, call the openCom
method that has the signature:
public native static int openCom(long baudrate, long rtimeout, long wtimeout);This function needs the following three parameters:
As an example, the following statements open the serial port for modbus communication with a baudrate equal to 38400, sets the reading and writing timeouts to 40000 and finally returns the file id (fid).
long baudrate = 38400; long timeout = 40000; int fid = ModbusLib.openCom(baudrate,timeout,timeout);By default, the port that will be opened in the Ltouch development board for rs485 communication is
/dev/s3c2410_serial1
.
When no errors occurs, the function will return the number of the file id (fid) that is usually greater than zero. The following table contains the error numbers (all negative) the function may return:
Err # | Description |
-1 | Tcgetattr() function failed |
-2 | Tcsetattr() function failed |
-3 | Tcflush() function failed |
Once the serial port is opened and the corresponding fid is greater than zero, it is possible to call the ReadHoldingRegisters
method to read consecutive registers of the device(s). The signature of the function is:
public native static long ReadHoldingRegisters(int fid, int id, int addr, int no_of_registers,int []holdingRegs);This function accepts the following parameters:
The example that follows open the serial port and read five registers from slave device with id 1. The results will be stored into the holdingRegs array.
long baudrate = 38400; long timeout = 40000; int node_id = 1; int starting_address = 0; int no_of_registers = 5; int[] holdingRegs = new int[5]; int fid = ModbusLib.openCom(baudrate,timeout,timeout); int bytesread = ModbusLib.ReadHoldingRegisters(fid,node_id,starting_address,no_of_registers, holdingRegs);
When no errors occur, the ReadHoldingRegisters function will return (apart from the registers) the number of bytes received from the function 3 reply. Negative number corresponds to an erroneous situation:
Err # | Description |
-1 | Wrong number of bytes received |
-2 | Wrong CRC |
-3 | Read timeout |
To write a batch of registers to slave devices, you can use the PresetMultipleRegisters
method, prior to having a correct fid. The signature of the function is:
public native static long PresetMultipleRegisters(int fid, int node, int addr, int regs, int []holdingRegs);This function accepts the following parameter:
The statements that follow open the serial port and write five registers to the slave device with id 1. The function will return the number of bytes read in the modbus reply or an error code.
long baudrate = 38400; long timeout = 40000; int node_id = 1; int starting_address = 0; int no_of_registers = 5; int[] holdingRegs = new int[5]; for (int i=0; i < no_of_registers; i++){ holdingRegs[i]=i; } int fid = ModbusLib.openCom(baudrate,timeout,timeout); int bytesread = ModbusLib.PresetMultipleRegisters(fid, node_id, starting_address, no_of_registers, holdingRegs);
When no errors occur, the PresetMultipleRegisters function will return (apart from the registers) the number of bytes received from the function 16 reply. Negative number corresponds to erroneous situations:
Err # | Description |
-1 | Wrong reply received |
-3 | Read timeout |
The closeCom
method must be used to release the resources. In particular, it close the serial port. The signature is the following:
public native static int closeCom(int fid);It accepts the following parameter:
When no errors occur, the closeCom function will return zero. -1 otherwise.