• Libraries
  • Shop
  • Doc
  • Free Modbus
  • Support
  • Login
  • Introduction
  • Chapters
  • Modules
  • Classes

How to integrate the Protocol in your Application

Serial Protocols

Let's assume we want to talk to a Modbus slave device with slave address 1.

The registers for reading are in the reference range 4:00100 to 4:00119 and the registers for writing are in the range 4:00200 to 4:00219. The discretes for reading are in the reference range 0:00010 to 0:00019 and the discretes for writing are in the range 0:00020 to 0:00029.

1. Import the library package

import com.focus_sw.fieldtalk.*;

2. Device data profile definition

Define the data sets which reflects the slave's data profile by type and size:

short[] readRegSet = new short[20]
short[] writeRegSet = new short[20]
boolean[] readBitSet = new boolean[20]
boolean[] writeBitSet = new boolean[20]

If you are using floats instead of 16-bit shorts define:

float[] readRegSet = new float[10]
float[] writeRegSet = new float[10]

Note that because a float occupies two 16-bit registers the array size is half the size it would be for 16-bit shorts!

If you are using 32-bit ints instead of 16-bit shorts define:

int[] readRegSet = new int[10]
int[] writeRegSet = new int[10]

Note that because an int occupies two 16-bit registers the array size is half the size it would be for 16-bit shorts!

3. Declare and instantiate a protocol object

MbusRtuMasterProtocol mbus = new MbusRtuMasterProtocol ();

4. Open the protocol port

mbus.openPort("COM1", 19200, MbusRtuMasterProtocol.DATABITS_8,
                             MbusRtuMasterProtocol.STOPBITS_1,
                             MbusRtuMasterProtocol.PARITY_EVEN);

5. Perform the data transfer functions

  • To read register values:
    mbus.readMultipleRegisters(1, 100, readRegSet);
    
  • To write a single register value:
    mbus.writeSingleRegister(1, 200, writeRegSet);
    
  • To write a single float value:
    mbus.writeSingleRegister(1, 210, 3.141F);
    
  • To write mutliple register values:
    mbus.writeMultipleRegisters(1, 200, writeRegSet);
    
  • To read boolean (discrete) values:
    mbus.readCoils(1, 10, readBitSet);
    
  • To write a single boolean (discrete) value:
    mbus.writeCoil(1, 20, true);
    
  • To write multiple boolean (discrete) values:
    mbus.forceMultipleCoils(1, 20, writeBitSet);
    

6. Close the protocol port if not needed any more

mbus.closePort();

7. Error Handling

Serial protocol errors like slave device failures, transmission failures, checksum errors and time-outs throw appropriate exceptions, which are all derived from the IOException and BusProtocolException class. The following code snippet can handle these errors:

try
{
   mbus.readMultipleRegisters(1, 100, dataSetArray);
}
catch (BusProtocolException e)
{
   Has to be caught before IOException!
   Protocol error management, something is wrong
   with the slave device or the bus.
}
catch (IOException e)
{
   If this occurrs after we checked for a BusProtocolException
   then it signals that the local I/O subsytem failed, which is
   fatal.

}

An automatic retry mechanism is available and can be enabled with mbus.setRetryCnt(3) before opening the protocol port.

TCP/IP Protocols

Let's assume we want to talk to a Modbus slave device with unit address 1 and IP address 10.0.0.11.

The registers for reading are in the reference range 4:00100 to 4:00119 and the registers for writing are in the range 4:00200 to 4:00219. The discretes for reading are in the reference range 0:00010 to 0:00019 and the discretes for writing are in the range 0:00020 to 0:00029.

1. Import the library package

import com.focus_sw.fieldtalk.*;

2. Device data profile definition

Define the data sets which reflects the slave's data profile by type and size:

short[] readRegSet = new short[20]
short[] writeRegSet = new short[20]
boolean[] readBitSet = new boolean[20]
boolean[] writeBitSet = new boolean[20]

If you are using floats instead of 16-bit shorts define:

float[] readRegSet = new float[10]
float[] writeRegSet = new float[10]

Note that because a float occupies two 16-bit registers the array size is half the size it would be for 16-bit shorts!

If you are using 32-bit ints instead of 16-bit shorts define:

int[] readRegSet = new int[10]
int[] writeRegSet = new int[10]

Note that because an int occupies two 16-bit registers the array size is half the size it would be for 16-bit shorts!

3. Declare and instantiate a protocol object

MbusTcpMasterProtocol mbus = new MbusTcpMasterProtocol ();

4. Open the protocol port

mbus.openConnection("10.0.0.11);

5. Perform the data transfer functions

  • To read register values:
    mbus.readMultipleRegisters(1, 100, readRegSet);
    
  • To write a single register value:
    mbus.writeSingleRegister(1, 200, writeRegSet);
    
  • To write a single float value:
    mbus.writeSingleRegister(1, 210, 3.141F);
    
  • To write mutliple register values:
    mbus.writeMultipleRegisters(1, 200, writeRegSet);
    
  • To read boolean (discrete) values:
    mbus.readCoils(1, 10, readBitSet);
    
  • To write a single boolean (discrete) value:
    mbus.writeCoil(1, 20, true);
    
  • To write multiple boolean (discrete) values:
    mbus.forceMultipleCoils(1, 20, writeBitSet);
    

6. Close the connection if not needed any more

mbus.closeConnection();

7. Error Handling

TCP/IP protocol errors like slave failures, TCP/IP connection failures and time-outs throw appropriate exceptions, which are all derived from the IOException and BusProtocolException class. The following code snippet can handle these errors:

try
{
   mbus.readMultipleRegisters(1, 100, dataSetArray);
}
catch (BusProtocolException e)
{
   Protocol error management, something is wrong
   with the slave device but the connection is still alive.
}
catch (IOException e)
{
   If this occurrs after we checked for a BusProtocolException
   then it signals that the the TCP/IP connection was lost or closed
   by the remote end. Before using further data and control functions
   the connection has to be re-opened succesfully.
}

Examples

  • Serial Example
  • Multithreaded Example
  • MODBUS/TCP Example
  • Applet Example with Cyclic Polling

FieldTalk Modbus Master Library, Java Edition
Library version 2.6.5
Speak to the Experts
Modbus Organization Member logo

We are member of the Modbus Organization, Inc.

Buy with Confidence
30-day money back guarantee All our FieldTalk web sales are backed by a 30-day Money Back Guarantee.
We Accept
Bank VISA MasterCard PayPal
Customer Info
  • Info & Contact
  • Customer Login
  • Terms of Service
  • Terms of Sale
  • Privacy Policy
© 2005-2025 proconX Pty Ltd. All rights reserved. proconX and FieldTalk are trademarks of proconX Pty Ltd.
All other trademarks and registered trademarks appearing on www.modbusdriver.com are the property of their respective owners.