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:
Baudrate
The speed of serial connection (8-n-1 are the other serial parameters).
rTimeout
Corresponds to the reading timeout. Default value should be equal to 40000. The unit of measure is microseconds.
wTimeout
The writing timeout. Default value should be equal to 40000. The unit of measure corresponds to microseconds.

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).

The fid will be used in the Read Holding Registers and Preset Multiple Registers.
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
-1Tcgetattr() function failed
-2Tcsetattr() function failed
-3Tcflush() 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:
fid
the number of file id that corresponds to the serial port.
dest_node
The slave identification number.
addr
The first register to read from.
no_of_registers
The number of consecutive register(s) to read.
holdingRegs
The output array in which the just read registers will be stored.

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
-1Wrong number of bytes received
-2Wrong CRC
-3Read 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:
fid
the number of file id that corresponds to the serial port.
node
The slave identification number.
addr
The first register to write to.
regs
The number of consecutive register(s) to write.
holdingRegs
The source array of registers that will be stored into the device.

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
-1Wrong reply received
-3Read 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:
fid
the number of file id that corresponds to the serial port.

When no errors occur, the closeCom function will return zero. -1 otherwise.