【2023】java通过modbus4j实现modus TCP通讯

Modbus通信协议:

  • 主要分为三个子协议
    • RTU
    • ASCII
    • TCP

Modbus RTU:——传输的是字节数组(bit[])
通信:读写
输出:可以读写
输入:只能读

  • 存储区:输出线圈、输入线圈、输出寄存器、输入寄存器

    • 线圈:代表一个布尔量、最小单位是一个布尔(1或者0),
    • 寄存器:一个寄存器代表16个最小单位,主要用于存储数据
    • 存储区代号:
      • 输出线圈: 0(代号)
        • 00001-09999(标准存储区地址范围)
        • 000001-065536(扩展存储区地址范围)
      • 输入线圈:1
        • 10001-19999
      • 输出寄存器:4
        • 40001-49999
      • 输入寄存器:3
        • 30001-39999
  • 存储区范围:5位和6位

    • 5位:标准地址-Y XXXX
    • 6位:扩展地址-Y XXXXX

功能代码
【2023】java通过modbus4j实现modus TCP通讯_第1张图片

验证4个常用功能码,仿真软件上面有F=01,F=02,F=03和F=04来显示

0x01:读线圈
0x02:读离散量输入
0x03:读保持寄存器
0x04:读输入寄存器

测试使用Modbus Slave(下载) 模拟
【2023】java通过modbus4j实现modus TCP通讯_第2张图片

saveid:看资料"从站在modbus总线上可以有多个",仿真软件就能模拟一个从站,就是ID=1,当然可以修改成ID=2
功能码:4个功能码,对应写4个方法,,仿真软件上的F=1,或者F=2,3,4
addr:一开始看代码4个方法addr都是从0开始,是否重复?答案是:4个功能码表示4个区域或者设备,addr表示各自区域的地址编号。

  • Connection设置:设置连接参数

【2023】java通过modbus4j实现modus TCP通讯_第3张图片

  • 地址设置: F8 ,可以通过Setup或者右击点位的
  • 【2023】java通过modbus4j实现modus TCP通讯_第4张图片

【2023】java通过modbus4j实现modus TCP通讯_第5张图片

  • 设置不同存储区
    【2023】java通过modbus4j实现modus TCP通讯_第6张图片
  • 读取从站【2023】java通过modbus4j实现modus TCP通讯_第7张图片

代码实现

1、依赖下载

阿里云不能直接下载

    <repositories>
	    <repository>
	        <releases>
	            <enabled>falseenabled>
	        releases>
	        <snapshots>
	            <enabled>trueenabled>
	        snapshots>
	        <id>ias-snapshotsid>
	        <name>Infinite Automation Snapshot Repositoryname>
	        <url>https://maven.mangoautomation.net/repository/ias-snapshot/url>
	    repository>
	    <repository>
	        <releases>
	            <enabled>trueenabled>
	        releases>
	        <snapshots>
	            <enabled>falseenabled>
	        snapshots>
	        <id>ias-releasesid>
	        <name>Infinite Automation Release Repositoryname>
	        <url>https://maven.mangoautomation.net/repository/ias-release/url>
	    repository>
	repositories>
	
    <dependencies>
		 <dependency>
		    <groupId>junitgroupId>
		    <artifactId>junitartifactId>
		    <version>4.13-beta-3version>
		    <scope>testscope>
		dependency>
		<dependency>
		    <groupId>com.infiniteautomationgroupId>
		    <artifactId>modbus4jartifactId>
		    <version>3.0.3version>
		dependency>
		
		<dependency>
		    <groupId>org.apache.commonsgroupId>
		    <artifactId>commons-lang3artifactId>
		    <version>3.9version>
		dependency>
    dependencies>

2、测试读取

  • 读取04(寄存器)
public class TestModbus {
    public static void main(String[] args) throws UnknownHostException {
            // 设置主机TCP参数
            TcpParameters tcpParameters = new TcpParameters();

            // 设置TCP的ip地址
            InetAddress address = InetAddress.getByName("127.0.0.1");
            // TCP参数设置ip地址
            tcpParameters.setHost(address);

            // TCP设置长连接
            tcpParameters.setKeepAlive(true);
            // TCP设置端口,这里设置是默认端口502
            tcpParameters.setPort(Modbus.TCP_PORT);

            // 创建一个主机
            ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
            Modbus.setAutoIncrementTransactionId(true);

            int slaveId = 1;//从机地址
            int offset = 0;//寄存器读取开始地址
            int quantity = 10;//读取的寄存器数量

            try {
            if (!master.isConnected()){
                master.connect(); //开启连接
            }
                // 读取对应从机的数据,readInputRegisters读取的写寄存器,功能码04
            int[] registerValues4 = master.readInputRegisters(slaveId,offset,quantity);

            for (int value : registerValues4){
                System.out.println("Address:"+offset++ +",Value:"+value);
            }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    master.disconnect();
                } catch (ModbusIOException e) {
                    e.printStackTrace();
                }
            }
    }
}

3、读取工具类

public class Modbus4jReadUtils {

    static ModbusFactory modbusFactory;
    static {
        if (modbusFactory == null){
            modbusFactory = new ModbusFactory();
        }
    }
    /*** 
     * 初始化连接
     * @return ModbusMaster 
     */
    public static ModbusMaster getMaster() throws ModbusInitException{
        IpParameters params = new IpParameters();
        params.setHost("localhost");
        params.setPort(502);
        // modbusFactory.createRtuMaster(wapper); //RTU 协议
        // modbusFactory.createUdpMaster(params);//UDP 协议
        // modbusFactory.createAsciiMaster(wrapper);//ASCII 协议
//        ModbusMaster master = modbusFactory.crea
        ModbusMaster master = modbusFactory.createTcpMaster(params,false);
        master.init();
        return master;
    }

    /***
     *  读取[01 Coil Status 0x]类型 开关数据
     * @author zhengfuping
     * @param slaveId 从站编号
     * @param offset 偏移位置
     * @return Boolean
     */
    public static Boolean readCoilStatus(int slaveId,int offset) throws ModbusInitException, ErrorResponseException, ModbusTransportException {
        BaseLocator<Boolean> loc = BaseLocator.coilStatus(slaveId,offset);
        Boolean value = getMaster().getValue(loc);
        return value;
    }


    /**
     * 读取[02 Input Status 1x]类型 开关数据
     *
     * @param slaveId 从站编号
     * @param offset 偏移位
     * @return
     */
    public static Boolean readInputStatus(int slaveId, int offset)
            throws ModbusTransportException, ErrorResponseException, ModbusInitException {
        // 02 Input Status
        BaseLocator<Boolean> loc = BaseLocator.inputStatus(slaveId, offset);
        Boolean value = getMaster().getValue(loc);
        return value;
    }

    /**
     * 读取[03 Holding Register类型 2x]模拟量数据
     * @param slaveId
     * @param offset 位置
     * @param dataType 数据类型,来自com.serotonin.modbus4j.code.DataType
     */
    public static Number readHoldingRegister(int slaveId, int offset, int dataType)
            throws ModbusTransportException, ErrorResponseException, ModbusInitException {
        // 03 Holding Register类型数据读取
        BaseLocator<Number> loc = BaseLocator.holdingRegister(slaveId, offset, dataType);
        Number value = getMaster().getValue(loc);
        return value;
    }

    /**
     * 读取[04 Input Registers 3x]类型 模拟量数据
     */
    public static Number readInputRegisters(int slaveId, int offset, int dataType)
            throws ModbusTransportException, ErrorResponseException, ModbusInitException {
        // 04 Input Registers类型数据读取
        BaseLocator<Number> loc = BaseLocator.inputRegister(slaveId, offset, dataType);
        Number value = getMaster().getValue(loc);
        return value;
    }

//    批量读取
public static void batchRead(Integer count) throws ModbusInitException, ErrorResponseException, ModbusTransportException {
    BatchRead<Integer> batch = new BatchRead<Integer>();

    for (int i = 0; i <= count; i++) {
        batch.addLocator(i,BaseLocator.holdingRegister(1,i, DataType.TWO_BYTE_INT_SIGNED));
    }

    ModbusMaster master =getMaster();

    batch.setContiguousRequests(false);
    BatchResults<Integer> results = master.send(batch);
    for (int i = 0; i <= count; i++) {
        System.err.println(results.getValue(i));
    }
}

    public static void main(String[] args) {
        try {
            // 01测试
            Boolean v011 = readCoilStatus(1,1);
            Boolean v012 = readCoilStatus(1,2);
            Boolean v013 = readCoilStatus(1,3);

            System.out.println("v011:" + v011);
            System.out.println("v012:" + v012);
            System.out.println("v013:" + v013);
            // 02测试
            Boolean v021 = readInputStatus(1, 1);
            Boolean v022 = readInputStatus(1, 2);
            Boolean v023 = readInputStatus(1, 3);
            System.out.println("v021:" + v021);
            System.out.println("v022:" + v022);
            System.out.println("v023:" + v023);

            // 03测试
            Number v031 = readHoldingRegister(1, 1, DataType.TWO_BYTE_INT_SIGNED);//signed
            Number v032 = readHoldingRegister(1, 2, DataType.TWO_BYTE_INT_SIGNED);// 同上
            System.out.println("v031:" + v031);
            System.out.println("v032:" + v032);

            // 04测试
            Number v041 = readInputRegisters(1, 1, DataType.TWO_BYTE_INT_SIGNED);//
            Number v042 = readInputRegisters(1, 2, DataType.TWO_BYTE_INT_SIGNED);//
            System.out.println("v041:" + v041);
            System.out.println("v042:" + v042);

//            批量读取
            batchRead(9);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

读取结果:
【2023】java通过modbus4j实现modus TCP通讯_第8张图片

4、写入工具类

public class Modbus4jWriteUtils {
    static Log log = LogFactory.getLog(Modbus4jWriteUtils.class);
    /**
     * 工厂。
     */
    static ModbusFactory modbusFactory;
    static {
        if (modbusFactory == null) {
            modbusFactory = new ModbusFactory();
        }
    }

    /**
     * 获取tcpMaster
     *
     * @return
     * @throws ModbusInitException
     */
    public static ModbusMaster getMaster() throws ModbusInitException {
        IpParameters params = new IpParameters();
        params.setHost("localhost");
        params.setPort(502);

        ModbusMaster tcpMaster = modbusFactory.createTcpMaster(params, false);
        tcpMaster.init();

        return tcpMaster;
    }

    /**
     * 写 [01 Coil Status(0x)]写一个 function ID = 5
     * @param slaveId slave的ID
     * @param writeOffset 位置
     * @param writeValue 值
     */
    public static boolean writeCoil(int slaveId, int writeOffset, boolean writeValue)
            throws ModbusTransportException, ModbusInitException {
        // 获取master
        ModbusMaster tcpMaster = getMaster();
        // 创建请求
        WriteCoilRequest request = new WriteCoilRequest(slaveId, writeOffset, writeValue);
        // 发送请求并获取响应对象
        WriteCoilResponse response = (WriteCoilResponse) tcpMaster.send(request);
        if (response.isException()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 写[01 Coil Status(0x)] 写多个 function ID = 15
     * @param slaveId
     * @param startOffset  开始位置
     * @param bdata 写入的数据
     * @return 是否写入成功
     */
    public static boolean writeCoils(int slaveId, int startOffset, boolean[] bdata)
            throws ModbusTransportException, ModbusInitException {
        // 获取master
        ModbusMaster tcpMaster = getMaster();
        // 创建请求
        WriteCoilsRequest request = new WriteCoilsRequest(slaveId, startOffset, bdata);
        // 发送请求并获取响应对象
        WriteCoilsResponse response = (WriteCoilsResponse) tcpMaster.send(request);
        if (response.isException()) {
            return false;
        } else {
            return true;
        }

    }

    /***
     * 写[03 Holding Register(4x)] 写一个 function ID = 6
     * @param slaveId
     * @param writeOffset
     * @param writeValue
     */
    public static boolean writeRegister(int slaveId, int writeOffset, short writeValue)
            throws ModbusTransportException, ModbusInitException {
        // 获取master
        ModbusMaster tcpMaster = getMaster();
        // 创建请求对象
        WriteRegisterRequest request = new WriteRegisterRequest(slaveId, writeOffset, writeValue);
        WriteRegisterResponse response = (WriteRegisterResponse) tcpMaster.send(request);
        if (response.isException()) {
            log.error(response.getExceptionMessage());
            return false;
        } else {
            return true;
        }

    }

    /**
     *
     * 写入[03 Holding Register(4x)]写多个 function ID=16
     * @param slaveId
     * @param startOffset 起始位置偏移量值
     * @param sdata 写入的数据
     */
    public static boolean writeRegisters(int slaveId, int startOffset, short[] sdata)
            throws ModbusTransportException, ModbusInitException {
        // 获取master
        ModbusMaster tcpMaster = getMaster();
        // 创建请求对象
        WriteRegistersRequest request = new WriteRegistersRequest(slaveId, startOffset, sdata);
        // 发送请求并获取响应对象
        ModbusResponse response = tcpMaster.send(request);
        if (response.isException()) {
            log.error(response.getExceptionMessage());
            return false;
        } else {
            return true;
        }
    }

    /**
     * 写入数字类型的模拟量(如:写入Float类型的模拟量、Double类型模拟量、整数类型Short、Integer、Long)
     *
     * @param slaveId
     * @param offset
     * @param value 写入值,Number的子类,例如写入Float浮点类型,Double双精度类型,以及整型short,int,long
     * @param dataType com.serotonin.modbus4j.code.DataType
     */
    public static void writeHoldingRegister(int slaveId, int offset, Number value, int dataType)
            throws ModbusTransportException, ErrorResponseException, ModbusInitException {
        // 获取master
        ModbusMaster tcpMaster = getMaster();
        // 类型
        BaseLocator<Number> locator = BaseLocator.holdingRegister(slaveId, offset, dataType);
        tcpMaster.setValue(locator, value);
    }

    public static void main(String[] args) {
        try {
//             测试01  单个写入(0x)
			boolean t01 = writeCoil(1, 0, true);
			System.out.println("T01:" + t01);

//             测试02  批量写入(0x)
			boolean t02 = writeCoils(1, 0, new boolean[] { true, false, true });
			System.out.println("T02:" + t02);

//             测试03  单个写入(4x)
			short v = -3;
			boolean t03 = writeRegister(1, 0, v);
			System.out.println("T03:" + t03);
//             测试04  批量写入(4x)
			boolean t04 = writeRegisters(1, 0, new short[] { -309, 390, 91 });
			System.out.println("t04:" + t04);
            //写模拟量
            writeHoldingRegister(1,8, 100, DataType.TWO_BYTE_INT_SIGNED);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(IO通讯,java,tcp/ip,开发语言)