Modbus4j - Java version of Modbus protocol implementation

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Modbus4j - Java version of Modbus protocol implementation缩略图

GitHub: https://github.com/infiniteautomation/modbus4j

The high-performance and easy-to-use implementation of Modbus protocol written in Java by Infinite Automation Systems and Serotonin Software. Supports ASCII, RTU, TCP, and UDP transmission as slave or master, automatically requests partitioning and responds to data type parsing.

You can now use the latest version of the public Maven repository and add it to pom.xml

<repositories>
    <repository>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <id>ias-snapshots</id>
        <name>Infinite Automation Snapshot Repository</name>
        <url>https://maven.mangoautomation.net/repository/ias-snapshot/</url>
    </repository>
    <repository>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>ias-releases</id>
        <name>Infinite Automation Release Repository</name>
        <url>https://maven.mangoautomation.net/repository/ias-release/</url>
    </repository>
</repositories>

The dependency information is:

<dependency>
    <groupId>com.infiniteautomation</groupId>
    <artifactId>modbus4j</artifactId>
    <version>3.0.3</version>
</dependency>

Core class

  • Host Master and its subclasses: the entry point of the host, the starting and ending points of the data stream.
  • Data Port Class StreamTransport: Responsible for writing and reading data.
  • Modbus message class ModbusMessage and its subclasses: supports various methods defined by Modbus (FunctionalCode)
  • Sending and receiving data control class messageControl: supports timeout, retry, default 200ms, once.
    • Waiting Room for Sending and Receiving: Responsible for synchronizing sending and receiving logic.
    • Output Request Message Class: OutgoingRequestMessage and its subclasses.
    • Received Response message class: IncomingResponseMessage and its subclasses.
    • parsing class: Message Parser: responsible for parsing received messages.
  • Protocol Data Type Definition: DataType
  • Protocol Function Code Definition: FunctionalCode
  • Protocol Register Range: LoadRange

Data Flow

  1. Create correspondingModbusFactoryCreate correspondingMasterobjects through
  2. Encapsulate the instructions that need to be sent, such asReadHoldingRegistersRequestThis is a read register instruction that specifies the register address and length.
  3. MasterObject will do thisReadHoldingRegistersRequestconvert toOutgoingRequestMessageObject, then transfer toMessageControl.
  4. Through the driver layer,MessageControlTranslate thisOutgoingRequestMessageWrite corresponding communication hardware peripherals (serial port, network port, etc.) and wait for data to be returnedIncomingResponseMessage.
  5. If not, return null and remind timeout.
  6. If a valid return is received, thenMessageControlUtilizeMessageParserIncomingResponseMessageConvert to correspondingModbusResponseReturn to the upper level.

Unit Testing

defines a series of unit testing methods in modbus4j/src test/. By tracking the relevant methods, one can understand the actual operation process of the modbus protocol.

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.