j2mod

repository·master·Indexed 43 days ago

https://github.com/steveohara/j2mod

A production-ready Java implementation of the Modbus protocol supporting Master and Slave configurations. It provides support for Modbus TCP, UDP, RTU over TCP, Serial RTU, and Serial ASCII transport layers. The library utilizes jSerialComm for serial communication and slf4j for logging, requiring JRE 1.8 (or JRE 1.6 for versions 2.70 and lower).

Tokens
1.2K
Snippets
5
Records
7
Agent score
6%

What's inside j2mod

  1. Overview of j2mod Modbus implementation

    master

    j2mod is a production-ready Modbus library (a fork of the original j2mod/jamod) that supports multiple Modbus protocols and configurations. It is designed to work in both Master and Slave roles.

    Supported Protocols:

    • Modbus TCP
    • Modbus UDP
    • RTU over TCP
    • Serial RTU
    • Serial ASCII

    Key Technical Details:

    • Serial Communication: Implemented via jSerialComm, providing native support for most platforms.
    • Logging: Uses slf4j as a logging facade to integrate with your existing application logging framework.
    • Runtime Requirements: Requires JRE 1.8 (or JRE 1.6 for versions 2.70 and lower).
  2. Include j2mod in a Maven project

    master

    To use j2mod in your project, add the following dependency to your pom.xml. Replace LATEST with the specific version you wish to use (stable releases are available on Maven Central).

    <dependency>
        <groupId>com.ghgande</groupId>
        <artifactId>j2mod</artifactId>
        <version>LATEST</version>
    </dependency>
  3. Known limitations and issues in j2mod

    master

    When using j2mod, be aware of the following current limitations:

    • RTU over TCP: There are currently no unit tests covering the RTU over TCP transport layer.
    • ModbusSlave Listeners: It is not currently possible to add an AbstractSerialTransportListener to a ModbusSlave. Consequently, you cannot be notified when the library switches between send and receive states in slave mode.
    • Encapsulation: The library is undergoing refactoring to better hide package components; users should be aware that some package-level components may not be intended for direct public use.
  4. Use UDPMasterTerminal for Modbus communication over UDP

    master

    The UDPMasterTerminal class is used to establish a Modbus master connection using the UDP protocol. It manages a DatagramSocket to send and receive Modbus messages.

    To use it, you can either initialize it with a specific InetAddress or use the default constructor and configure the address later. You must call activate() to initialize the socket and the ModbusUDPTransport before attempting to send or receive messages.

    // Example usage pattern
    InetAddress remoteAddr = InetAddress.getByName("192.168.1.100");
    UDPMasterTerminal terminal = new UDPMasterTerminal(remoteAddr);
    
    try {
        terminal.activate();
        // Use terminal with a Modbus driver/transport
        // ...
    } finally {
        terminal.deactivate();
    }
  5. Send and receive messages via UDPMasterTerminal

    master

    Once activated, you can use the following methods for raw message exchange:

    • sendMessage(byte[] msg): Sends the provided byte array as a DatagramPacket to the configured remote address and port.
    • receiveMessage(): Receives a message. It uses a buffer of 262 bytes (designed to accommodate the maximum Modbus message limit of 256 bytes plus a 6-byte header). It respects the configured timeout via socket.setSoTimeout(timeout).
    // Sending a message
    byte[] myMessage = new byte[] { /* ... Modbus PDU ... */ };
    terminal.sendMessage(myMessage);
    
    // Receiving a message
    byte[] response = terminal.receiveMessage();
  6. Manage UDPMasterTerminal lifecycle with activate() and deactivate()

    master

    To prepare the terminal for communication, call activate(). This method initializes the DatagramSocket, sets the receive/send buffer sizes to 1024 bytes, applies the configured timeout, and initializes the ModbusUDPTransport.

    To release resources and close the socket, call deactivate().

    // Activation
    terminal.activate();
    
    // Deactivation
    terminal.deactivate();
  7. Initialize UDPMasterTerminal

    master

    The UDPMasterTerminal provides two constructors:

    1. UDPMasterTerminal(InetAddress addr): Creates a UDP master connection pre-configured with the specified remote Internet address.
    2. UDPMasterTerminal(): Creates an uninitialized UDP master connection.
    // Constructor with address
    UDPMasterTerminal terminal = new UDPMasterTerminal(InetAddress.getByName("127.0.0.1"));
    
    // Uninitialized constructor
    UDPMasterTerminal terminal = new UDPMasterTerminal();