com.eurotech.framework.protocol.modbus
Class ModbusProtocolDevice

java.lang.Object
  extended by com.eurotech.framework.protocol.modbus.ModbusProtocolDevice
All Implemented Interfaces:
ModbusProtocolDeviceService

public class ModbusProtocolDevice
extends Object
implements ModbusProtocolDeviceService

The Modbus protocol implements a subset of the Modbus standard command set. It also provides for the extension of some data typing to allow register pairings to hold 32 bit data (see the configureDataMap for more detail).

The protocol supports RTU and ASCII mode operation.


Field Summary
static String PROTOCOL_CONNECTION_TYPE_ETHER_TCP
           
static String PROTOCOL_CONNECTION_TYPE_SERIAL
           
static String SERIAL_232
           
static String SERIAL_485
           
 
Fields inherited from interface com.eurotech.framework.protocol.modbus.ModbusProtocolDeviceService
SERVICE_NAME
 
Constructor Summary
ModbusProtocolDevice()
           
 
Method Summary
protected  void activate(org.osgi.service.component.ComponentContext componentContext)
           
 void configureConnection(Properties connectionConfig)
          two connection types are available, PROTOCOL_CONNECTION_TYPE_SERIAL (SERIAL MODE 232 or 485) and PROTOCOL_CONNECTION_TYPE_ETHER_TCP.
 void configureProtocol(Properties protocolConfig)
          modbus protocol configuration.
 void connect()
          attempt to connect to the field device using the provided configuration.
protected  void deactivate(org.osgi.service.component.ComponentContext componentContext)
           
 void disconnect()
          attempt to disconnect from the field device.
 ModbusCommEvent getCommEventCounter()
          Modbus function 11 (0x0B)
Get a status word and an event count from the device.
Return values in a ModbusCommEvent.
 ModbusCommEvent getCommEventLog()
          Modbus function 12 (0x0C)
Get a status word, an event count, a message count and a list of event bytes from the device.
Return values in a ModbusCommEvent.
 int getConnectStatus()
          for expedience, can test the status of the connection prior to attempting a command.
 NmeaPosition getNmeaPosition()
          Return NMEA Position of the device
 org.osgi.util.position.Position getPosition()
          Return OSGI Position of the device
 String getProtocolName()
          get the name "modbus" for this protocol
 String getUnitAddress()
          Modbus unit addresses must always be in the range of 1-255
 boolean[] readCoils(int dataAddress, int count)
          Modbus function 01
Read 1 to 2000 contiguous status of coils from the attached field device.
 boolean[] readDiscreteInputs(int dataAddress, int count)
          Modbus function 02
Read 1 to 2000 contiguous status of discrete inputs from the attached field device.
 boolean[] readExceptionStatus()
          Modbus function 07
read the content of 8 Exception Status outputs in the field device.
 int[] readHoldingRegisters(int dataAddress, int count)
          Modbus function 03
Read contents of 1 to 125 contiguous block of holding registers from the attached field device.
 int[] readInputRegisters(int dataAddress, int count)
          Modbus function 04
Read contents of 1 to 125 contiguous block of input registers from the attached field device.
 void setConnectionFactory(org.osgi.service.io.ConnectionFactory connectionFactory)
           
 void setUsbService(UsbService usbService)
           
 String toString()
          get the unit name registered for this instance
 void unsetConnectionFactory(org.osgi.service.io.ConnectionFactory connectionFactory)
           
 void unsetUsbService(UsbService usbService)
           
 void writeMultipleCoils(int dataAddress, boolean[] data)
          Modbus function 15 (0x0F)
write multiple coils in a sequence of coils to either ON or OFF in the attached field device.
 void writeMultipleRegister(int dataAddress, int[] data)
          Modbus function 16 (0x10)
write a block of contiguous registers (1 to 123) in the attached field device.
 void writeSingleCoil(int dataAddress, boolean data)
          Modbus function 05
write a single output to either ON or OFF in the attached field device.
 void writeSingleRegister(int dataAddress, int data)
          Modbus function 06
write a single holding register in the attached field device.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PROTOCOL_CONNECTION_TYPE_SERIAL

public static final String PROTOCOL_CONNECTION_TYPE_SERIAL
See Also:
Constant Field Values

SERIAL_232

public static final String SERIAL_232
See Also:
Constant Field Values

SERIAL_485

public static final String SERIAL_485
See Also:
Constant Field Values

PROTOCOL_CONNECTION_TYPE_ETHER_TCP

public static final String PROTOCOL_CONNECTION_TYPE_ETHER_TCP
See Also:
Constant Field Values
Constructor Detail

ModbusProtocolDevice

public ModbusProtocolDevice()
Method Detail

setConnectionFactory

public void setConnectionFactory(org.osgi.service.io.ConnectionFactory connectionFactory)

unsetConnectionFactory

public void unsetConnectionFactory(org.osgi.service.io.ConnectionFactory connectionFactory)

setUsbService

public void setUsbService(UsbService usbService)

unsetUsbService

public void unsetUsbService(UsbService usbService)

activate

protected void activate(org.osgi.service.component.ComponentContext componentContext)

deactivate

protected void deactivate(org.osgi.service.component.ComponentContext componentContext)

configureProtocol

public void configureProtocol(Properties protocolConfig)
                       throws ModbusProtocolException
modbus protocol configuration. Required properties for the modbus protocol configuration are:
KeyDescription
unitNameName to be returned by toString
unitAddressModbus address of the unit (1-255)
txModeMust be set to "RTU" or "ASCII"
respTimeoutmaximum time, in milliseconds, to wait for a response to a command

Specified by:
configureProtocol in interface ModbusProtocolDeviceService
Parameters:
protocolConfig - (key/value pairing directly from configuration file)
  • unitName : Name to be returned by toString
  • unitAddress : Modbus address of the unit (1-255)
  • txMode : Must be set to "RTU" or "ASCII"
  • respTimeout : maximum time, in milliseconds, to wait for a response to a command
    Throws:
    ModbusProtocolException

  • configureConnection

    public void configureConnection(Properties connectionConfig)
                             throws ModbusProtocolException
    two connection types are available, PROTOCOL_CONNECTION_TYPE_SERIAL (SERIAL MODE 232 or 485) and PROTOCOL_CONNECTION_TYPE_ETHER_TCP. The required properties for the connection will vary with each connection.

    PROTOCOL_CONNECTION_TYPE_SERIAL

    see CommConnection package for more detail.
    Key Description
    connectionType "SERIAL" (from PROTOCOL_CONNECTION_TYPE_SERIAL). This parameter indicates the connection type for the configuration. See CommConnection for more details on serial port configuration.
    port the actual device port, such as "/dev/ttyUSB0" in linux
    serialMode SERIAL_232 or SERIAL_485
    baudRate baud rate to be configured for the port
    stopBits number of stop bits to be configured for the port
    parity parity mode to be configured for the port
    bitsPerWord only RTU mode supported, bitsPerWord must be 8

    PROTOCOL_CONNECTION_TYPE_ETHER_TCP

    The Ethernet mode merely opens a socket and sends the full RTU mode Modbus packet over that socket connection and expects to receive a full RTU mode Modbus response, including the CRC bytes.
    Key Description
    connectionType "ETHERTCP" (from PROTOCOL_CONNECTION_TYPE_ETHER_TCP). This parameter indicates the connection type for the configurator.
    ipAddress the 4 octet IP address of the field device (xxx.xxx.xxx.xxx)
    port port on the field device to connect to

    Specified by:
    configureConnection in interface ModbusProtocolDeviceService
    Parameters:
    connectionConfig - (key/value pairing directly from configuration file)
  • connectionType : PROTOCOL_CONNECTION_TYPE_SERIAL("SERIAL") or PROTOCOL_CONNECTION_TYPE_ETHER_TCP("ETHERTCP")

    for SERIAL mode :
  • port : Name of the port ("/dev/ttyUSB0")
  • baudRate : baudrate
  • stopBits : number of stopbits
  • parity : parity mode (0=none, 1=odd, 2=even)
  • bitsPerWord : number of bits per word
  • serialMode : Serial Mode : SERIAL_232("RS232") or SERIAL_485("RS485").
    if SERIAL_485
  • serialGPIOswitch : pin number as a filename in "/sys/class/gpio/"
  • serialGPIOrsmode : pin number as a filename in "/sys/class/gpio/"

    for ETHERNET mode :
  • port : TCP port to be used
  • ipAddress : the 4 octet IP address of the field device (xxx.xxx.xxx.xxx)
    Throws:
    ModbusProtocolException

  • toString

    public String toString()
    get the unit name registered for this instance

    Specified by:
    toString in interface ModbusProtocolDeviceService
    Overrides:
    toString in class Object
    Returns:
    unitName
    See Also:
    Object.toString()

    getProtocolName

    public String getProtocolName()
    get the name "modbus" for this protocol

    Specified by:
    getProtocolName in interface ModbusProtocolDeviceService
    Returns:
    "modbus"

    getUnitAddress

    public String getUnitAddress()
    Modbus unit addresses must always be in the range of 1-255

    Specified by:
    getUnitAddress in interface ModbusProtocolDeviceService
    Returns:
    unit address (1-255) as a string

    connect

    public void connect()
                 throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    attempt to connect to the field device using the provided configuration. Attempts to connect before configuring the connection or any issues with connecting to the field device will result in an exception being thrown. This includes things like a networking failure in the case of a protocol configured to access the field device of a network.

    Refer to getConnectStatus to determine if the connection is completed.

    All protocols must implement this method.

    Specified by:
    connect in interface ModbusProtocolDeviceService
    Throws:
    ModbusProtocolException

    disconnect

    public void disconnect()
    Description copied from interface: ModbusProtocolDeviceService
    attempt to disconnect from the field device. This should close any port used exclusively for this protocol to talk with its attached device. Attempting to close an already closed connection is not invalid.

    All protocols must implement this method.

    Specified by:
    disconnect in interface ModbusProtocolDeviceService
    See Also:
    ModbusProtocolDeviceService.getConnectStatus()

    getConnectStatus

    public int getConnectStatus()
    Description copied from interface: ModbusProtocolDeviceService
    for expedience, can test the status of the connection prior to attempting a command. A connection status of CONNECTED does not assure that a subsequent command will succeed.

    All protocols must implement this method.

    Specified by:
    getConnectStatus in interface ModbusProtocolDeviceService
    Returns:
    current connection status as defined in EsfConnectionStatus.

    getPosition

    public org.osgi.util.position.Position getPosition()
    Description copied from interface: ModbusProtocolDeviceService
    Return OSGI Position of the device

    Specified by:
    getPosition in interface ModbusProtocolDeviceService
    See Also:
    Position

    getNmeaPosition

    public NmeaPosition getNmeaPosition()
    Description copied from interface: ModbusProtocolDeviceService
    Return NMEA Position of the device

    Specified by:
    getNmeaPosition in interface ModbusProtocolDeviceService
    See Also:
    NmeaPosition

    readCoils

    public boolean[] readCoils(int dataAddress,
                               int count)
                        throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 01
    Read 1 to 2000 contiguous status of coils from the attached field device.

    Specified by:
    readCoils in interface ModbusProtocolDeviceService
    Parameters:
    dataAddress - starting address
    count - quantity of coils
    Returns:
    an array of booleans representing the requested data points. true for a given point if the point is set, false otherwise.
    Throws:
    ModbusProtocolException

    readDiscreteInputs

    public boolean[] readDiscreteInputs(int dataAddress,
                                        int count)
                                 throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 02
    Read 1 to 2000 contiguous status of discrete inputs from the attached field device.

    Specified by:
    readDiscreteInputs in interface ModbusProtocolDeviceService
    Parameters:
    dataAddress - starting address
    count - quantity of inputs
    Returns:
    an array of booleans representing the requested data points. true for a given point if the point is set, false otherwise.
    Throws:
    ModbusProtocolException

    writeSingleCoil

    public void writeSingleCoil(int dataAddress,
                                boolean data)
                         throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 05
    write a single output to either ON or OFF in the attached field device.

    Specified by:
    writeSingleCoil in interface ModbusProtocolDeviceService
    Parameters:
    dataAddress - Output address.
    data - Output value (boolean) to write.
    Throws:
    ModbusProtocolException

    writeMultipleCoils

    public void writeMultipleCoils(int dataAddress,
                                   boolean[] data)
                            throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 15 (0x0F)
    write multiple coils in a sequence of coils to either ON or OFF in the attached field device.

    Specified by:
    writeMultipleCoils in interface ModbusProtocolDeviceService
    Parameters:
    dataAddress - Starting Output address.
    data - Outputs value (array of boolean) to write.
    Throws:
    ModbusProtocolException

    readHoldingRegisters

    public int[] readHoldingRegisters(int dataAddress,
                                      int count)
                               throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 03
    Read contents of 1 to 125 contiguous block of holding registers from the attached field device.

    Specified by:
    readHoldingRegisters in interface ModbusProtocolDeviceService
    Parameters:
    dataAddress - starting address
    count - quantity of registers (maximum 0x7D)
    Returns:
    an array of int representing the requested data points (data registers on 2 bytes).
    Throws:
    ModbusProtocolException

    readInputRegisters

    public int[] readInputRegisters(int dataAddress,
                                    int count)
                             throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 04
    Read contents of 1 to 125 contiguous block of input registers from the attached field device.

    Specified by:
    readInputRegisters in interface ModbusProtocolDeviceService
    Parameters:
    dataAddress - starting address
    count - quantity of registers (maximum 0x7D)
    Returns:
    an array of int representing the requested data points (data registers on 2 bytes).
    Throws:
    ModbusProtocolException

    writeSingleRegister

    public void writeSingleRegister(int dataAddress,
                                    int data)
                             throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 06
    write a single holding register in the attached field device.

    Specified by:
    writeSingleRegister in interface ModbusProtocolDeviceService
    Parameters:
    dataAddress - Output address.
    data - Output value (2 bytes) to write.
    Throws:
    ModbusProtocolException

    writeMultipleRegister

    public void writeMultipleRegister(int dataAddress,
                                      int[] data)
                               throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 16 (0x10)
    write a block of contiguous registers (1 to 123) in the attached field device.

    Specified by:
    writeMultipleRegister in interface ModbusProtocolDeviceService
    Parameters:
    dataAddress - Output address.
    data - Registers value (array of int converted in 2 bytes values) to write.
    Throws:
    ModbusProtocolException

    readExceptionStatus

    public boolean[] readExceptionStatus()
                                  throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 07
    read the content of 8 Exception Status outputs in the field device.

    Specified by:
    readExceptionStatus in interface ModbusProtocolDeviceService
    Throws:
    ModbusProtocolException

    getCommEventCounter

    public ModbusCommEvent getCommEventCounter()
                                        throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 11 (0x0B)
    Get a status word and an event count from the device.
    Return values in a ModbusCommEvent.

    Specified by:
    getCommEventCounter in interface ModbusProtocolDeviceService
    Throws:
    ModbusProtocolException
    See Also:


    getCommEventLog

    public ModbusCommEvent getCommEventLog()
                                    throws ModbusProtocolException
    Description copied from interface: ModbusProtocolDeviceService
    Modbus function 12 (0x0C)
    Get a status word, an event count, a message count and a list of event bytes from the device.
    Return values in a ModbusCommEvent.

    Specified by:
    getCommEventLog in interface ModbusProtocolDeviceService
    Throws:
    ModbusProtocolException
    See Also:



    Copyright © 2013. All Rights Reserved.