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
- Create corresponding
ModbusFactoryCreate correspondingMasterobjects through - Encapsulate the instructions that need to be sent, such as
ReadHoldingRegistersRequestThis is a read register instruction that specifies the register address and length. MasterObject will do thisReadHoldingRegistersRequestconvert toOutgoingRequestMessageObject, then transfer toMessageControl.- Through the driver layer,
MessageControlTranslate thisOutgoingRequestMessageWrite corresponding communication hardware peripherals (serial port, network port, etc.) and wait for data to be returnedIncomingResponseMessage. - If not, return null and remind timeout.
- If a valid return is received, then
MessageControlUtilizeMessageParser将IncomingResponseMessageConvert 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.
Leave a Reply