【开源视频联动物联网平台】j2mod 库写一个Modbus RTU 服务器

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

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

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

复制代码
<dependency>
    <groupId>com.ghgande.j2mod</groupId>
    <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",你可能需要根据你的系统配置更改串口。确保你有足够的权限访问该串口。
相关推荐
Q***K551 天前
C在物联网中的物联网平台
物联网
智能化咨询1 天前
(62页PPT)物联网智慧建筑综合解决方案(附下载方式)
物联网
阿拉斯攀登1 天前
安卓工控机 OTA 升级方案(SpringBoot+MQTT)
android·spring boot·物联网·iot
学术小白人1 天前
会议第一轮投稿!2026年物联网、数据科学与先进计算国际学术会议(IDSAC2026)
人工智能·物联网·数据分析·能源·制造·教育·rdlink研发家
0***141 天前
C在物联网中的Azure Sphere
物联网·microsoft·azure
TDengine (老段)2 天前
TDengine IDMP 重塑智慧水务运营(内附 Step by Step 步骤)
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
门思科技2 天前
工业物联网低成本改造:门思科技LoRaWAN全链路解决方案
科技·物联网
Z***25802 天前
HarmonyOS在物联网场景的应用
物联网·华为·harmonyos
我先去打把游戏先2 天前
ESP32学习笔记(基于IDF):基于OneNet的ESP32的OTA功能
笔记·物联网·学习·云计算·iphone·aws
t***L2662 天前
Python物联网案例
物联网