J2Mod一个Java编写的Modbus通信库,可以用于实现Modbus RTU服务器。以下是一个简单示例演示如何使用J2Mod创建一个Modbus RTU服务器:

  1. 添加J2Mod依赖: 首先,确保项目包含J2Mod库。你可以将J2Mod库添加项目中,方法取决于你使用构建工具比如Maven或Gradle)。

如果使用Maven可以pom.xml文件添加以下依赖项:

<dependency>
    <groupId>com.ghgande.j2mod</groupId>
    &lt;artifactId>j2mod</artifactId>
    <version>3.1.3</version> <!-- 使用最新版本 -->
</dependency>

如果使用Gradle,可以在build.gradle文件添加以下依赖项:

implementation 'com.ghgande.j2mod:j2mod:3.1.3' // 使用最新版

创建Modbus RTU服务

下面是一个简单的Modbus RTU服务示例监听在COM3串口上,地址为1:

import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.procimg.SimpleDigitalIn;
import com.ghgande.j2mod.modbus.procimg.SimpleDigitalOut;
import com.ghgande.j2mod.modbus.procimg.SimpleProcessImage;
import com.ghgande.j2mod.modbus.serial.SerialParameters;
import com.ghgande.j2mod.modbus.serial.SerialPort;
import com.ghgande.j2mod.modbus.serial.SerialUtils;
import com.ghgande.j2mod.modbus.serial.SerialPortException;
import com.ghgande.j2mod.modbus.serial.SerialPortFactory;
import com.ghgande.j2mod.modbus.ModbusCoupler;

public class ModbusRTUServerExample {

    public static void main(String[] args) {
        try {
            // Create a process image with a single coil at address 0
            SimpleProcessImage spi = new SimpleProcessImage();
            spi.addDigitalOut(new SimpleDigitalOut(true)); // Coil at address 0
            spi.addDigitalIn(new SimpleDigitalIn(false)); // Input at address 1

            // Set up the serial parameters
            SerialParameters serialParameters = new SerialParameters();
            serialParameters.setCommPortId("COM3");
            serialParameters.setBaudRate(SerialPort.BAUD_9600);
            serialParameters.setDatabits(8);
            serialParameters.setParity(SerialPort.PARITY_NONE);
            serialParameters.setStopbits(1);
            serialParameters.setEncoding(Modbus.SERIAL_ENCODING_RTU);
            serialParameters.setEcho(false);

            // Create the Modbus RTU serial port
            SerialPort serialPort = SerialPortFactory.create(serialParameters);
            serialPort.open();

            // Set the serial port for ModbusCoupler
            ModbusCoupler.getReference().setMaster(false);
            ModbusCoupler.getReference().setUnitID(1);
            ModbusCoupler.getReference().setProcessImage(spi);
            ModbusCoupler.getReference().setSerialPort(serialPort);

            // Start the Modbus RTU server
            ModbusCoupler.getReference().start();

            System.out.println("Modbus RTU server is running...");

            // Wait forever (you can add your own logic here)
            while (true) {
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. 注意,此示例中使用的串口是”COM3″,你可能需要根据你的系统配置更改串口确保你有足够的权限访问串口

原文地址:https://blog.csdn.net/a7491772/article/details/134759348

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_49048.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注