• Java实现Modbus Tcp协议读写模拟工具数据


    前言

    参考文章:https://www.cnblogs.com/ioufev/p/10831289.html

    该文中谈及常见的几种读取设备数据实现,说到modbus4j的通讯实现方式是同步的,实际应用中可能会读取大量的数据,需要异步进行,可以用modbus-master-tcp

    本文也是基于modbus-master-tcp依赖库进行开发,它底层是基于Netty写的,所以具备高性能、支持异步

    SpringBoot项目配置的核心依赖

    
    <dependency>
        <groupId>com.digitalpetri.modbusgroupId>
        <artifactId>modbus-master-tcpartifactId>
        <version>1.2.0version>
    dependency>
    
    
    <dependency>
        <groupId>com.digitalpetri.modbusgroupId>
        <artifactId>modbus-slave-tcpartifactId>
        <version>1.2.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    设备模拟工具选择Modbus Slave,网上很多下载资源

    一、读写模拟工具中数据

    在这里插入图片描述
    一开始我也把Server和Client搞混了,大家注意下面重点知识

    重点知识

    Modbus一主多从讲的是一次只有一个主机(Master)连接到网络,只有主设备(Master)可以启动通信并向从设备(Slave)发送请求,从设备不能主动发送;从设备(Slave)只能向主设备(Master)发送回复,且从设备就不能自己主动发送。

    一个Master(物理网平台),多个Slave(设备);平台是Client,设备是Server

    实现如下

    (1) 定义Controller层

    @RestController
    @RequestMapping("/modbus")
    public class ModbusController {
    
        @Autowired
        private ModbusTcpService modbusTcpService;
    
        // 连接slave设备
        @GetMapping(value = "/connect_slave")
        public ResponseMessage connectSlave(){
            return modbusTcpService.connectSlave();
        }
    
        // 读取保持寄存器
        @PostMapping(value = "/readHoldingRegisters")
        public ResponseMessage readHoldingRegisters(@RequestBody SlaveDto slaveDto) throws ExecutionException, InterruptedException {
            return modbusTcpService.readHoldingRegisters(slaveDto);
        }
        
    	// 写单个寄存器
        @PostMapping(value = "/writeSingleRegister")
        public ResponseMessage writeSingleRegister(@RequestBody WriteSlaveDto wsDto) {
            return modbusTcpService.writeSingleRegister(wsDto);
        }
    
        // 写多个寄存器
        @PostMapping(value = "/writeMultipleRegisters")
        public ResponseMessage writeMultipleRegisters(@RequestBody WriteSlaveDto wsDto){
            return modbusTcpService.writeMultipleRegisters(wsDto);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    其他相关类

    SlaveDto.java

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Component
    public class SlaveDto {
    
        private Integer slaveId;
    
        private Integer address;
    
        private Integer quantity;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    WriteSlaveDto.java

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class WriteSlaveDto extends SlaveDto {
    
        // 写单个数值
        private Integer value;
    
        // 写多个数值
        private int[] values;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ResponseMessage.java就是常见的返回msg、code和data的类,随便找一个符合就行

    (2) 定义Service层实现

    @Service
    @Slf4j
    public class ModbusTcpService{
    
    	@Autowired
        private ModbusMasterUtil modbusMasterUtil;
    
    	public ResponseMessage connectSlave() {
            try {
                modbusMasterUtil.createModbusConnector(ModbusClientConstants.IP, ModbusClientConstants.TCP_PORT);
                return ResponseMessage.ok();
            }catch (Exception e){
                log.error("connect slave fail, {}", e.getMessage());
                return ResponseMessage.error(e.getMessage());
            }
        }
    
        private ModbusTcpMaster checkConnectSlave() {
            return modbusMasterUtil.getModbusTcpMaster();
        }
    
        public ResponseMessage readHoldingRegisters(SlaveDto slaveDto) throws ExecutionException, InterruptedException {
            if (checkConnectSlave() == null) return ResponseMessage.error("not connect slave");
    
            CompletableFuture<int[]> registerFuture = modbusMasterUtil.readHoldingRegisters(slaveDto.getSlaveId(), slaveDto.getAddress(), slaveDto.getQuantity());
            int[] registerValues = registerFuture.get();
            if (registerFuture == null) return ResponseMessage.error("Read exception, please check json parameters");
            log.info("readHoldingRegisters info={}", Arrays.toString(registerValues));
            return ResponseMessage.ok(registerValues);
        }
    
        public ResponseMessage writeSingleRegister(WriteSlaveDto wsDto) {
            if (checkConnectSlave() == null) return ResponseMessage.error("not connect slave");
    
            modbusMasterUtil.writeSingleRegister(wsDto.getSlaveId(), wsDto.getAddress(), wsDto.getValue());
            return ResponseMessage.ok();
        }
    
        public ResponseMessage writeMultipleRegisters(WriteSlaveDto wsDto) {
            if (checkConnectSlave() == null) return ResponseMessage.error("not connect slave");
    
            if(wsDto.getValues().length != wsDto.getQuantity()) return ResponseMessage.error("quantity error");
    
            modbusMasterUtil.writeMultipleRegisters(wsDto.getSlaveId(), wsDto.getAddress(), wsDto.getQuantity(), wsDto.getValues());
            return ResponseMessage.ok();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    IP和Port就是一个常量类,写着Modbus默认的127.0.0.1和502

    Modbus Tcp的工具类为ModbusMasterUtil,这来自于网上的,还挺好用

    @Component
    @Slf4j
    public class ModbusMasterUtil {
    
        private ModbusTcpMaster modbusMaster = null;
    
        public ModbusTcpMaster getModbusTcpMaster() {
            return modbusMaster;
        }
     
        /**
         * 将两个int数拼接成为一个浮点数
         *
         * @param highValue 高16位数值
         * @param lowValue  低16位数值
         * @return 返回拼接好的浮点数
         * @author huangji
         */
        public static float concatenateFloat(int highValue, int lowValue) {
            int combinedValue = ((highValue << 16) | (lowValue & 0xFFFF));
            return Float.intBitsToFloat(combinedValue);
        }
     
        public static int[] floatToIntArray(float floatValue) {
            int combinedIntValue = Float.floatToIntBits(floatValue);
            int[] resultArray = new int[2];
            resultArray[0] = (combinedIntValue >> 16) & 0xFFFF;
            resultArray[1] = combinedIntValue & 0xFFFF;
            return resultArray;
        }
     
        /**
         * 将传入的boolean[]类型数组按位转换成byte[]类型数组
         *
         * @param booleans 传入的boolean数组
         * @return 返回转化后的 byte[]
         * @author huangji
         */
        public static byte[] booleanToByte(boolean[] booleans) {
            BitSet bitSet = new BitSet(booleans.length);
            for (int i = 0; i < booleans.length; i++) {
                bitSet.set(i, booleans[i]);
            }
            return bitSet.toByteArray();
        }
     
        /**
         * 将传入的int[]类型数组转换成为byte[]类型数组
         *
         * @param values 传入的int[]数组
         * @return 返回 byte[]类型的数组
         * @author huangji
         */
        public static byte[] intToByte(int[] values) {
            byte[] bytes = new byte[values.length * 2];
            for (int i = 0; i < bytes.length; i += 2) {
                bytes[i] = (byte) (values[i / 2] >> 8 & 0xFF);
                bytes[i + 1] = (byte) (values[i / 2] & 0xFF);
            }
            return bytes;
        }
     
        /**
         * 根据传入的ip地址,创建modbus连接器
         *
         * @param ipAddr ip地址
         * @return 创建连接器,并进行连接,之后返回此连接器
         * @author huangji
         */
        public CompletableFuture<ModbusTcpMaster> createModbusConnector(String ipAddr) {
            return createModbusConnector(ipAddr, ModbusClientConstants.TCP_PORT);
        }
     
        /**
         * 根据传入的ip地址,创建modbus连接器
         *
         * @param ipAddr ip地址
         * @param port   端口号
         * @return 创建连接器,并进行连接,之后返回此连接器
         * @author huangji
         */
        public CompletableFuture<ModbusTcpMaster> createModbusConnector(String ipAddr, int port) {
            return createModbusConnector(new ModbusNetworkAddress(ipAddr, port));
        }
     
        /**
         * 根据传入的ModbusNetworkAddress\引用,创建modbus连接器
         *
         * @param modbusNetworkAddress ModbusNetworkAddress类型的实体对象引用
         * @return 创建连接器,并进行连接,之后返回此连接器
         * @author huangji
         */
        public CompletableFuture<ModbusTcpMaster> createModbusConnector(ModbusNetworkAddress modbusNetworkAddress) {
            String ipAddr = modbusNetworkAddress.getIpAddr();
            int port = modbusNetworkAddress.getPort();
            if (modbusMaster == null) {
                ModbusTcpMasterConfig masterConfig = new ModbusTcpMasterConfig.Builder(ipAddr).setPort(port).setTimeout(Duration.parse(ModbusClientConstants.TIMEOUT_DURATION)).setPersistent(true).setLazy(false).build();
                modbusMaster = new ModbusTcpMaster(masterConfig);
            }
            return modbusMaster.connect();
        }
     
        public void setBooleanArray(short unsignedShortValue, int[] array, int index, int size) {
            for (int i = index; i < index + size; i++) {
                array[i] = (unsignedShortValue & (0x01 << (i - index))) != 0 ? 1 : 0;
            }
        }
     
        /**
         * 异步方法,读取modbus设备的线圈值,对应功能号01
         *
         * @param slaveId  设备id
         * @param address  要读取的寄存器地址
         * @param quantity 要读取的寄存器数量
         * @return 返回 CompletableFuture
         * @author huangji
         */
        public CompletableFuture<int[]> readCoils(int slaveId, int address, int quantity) {
            CompletableFuture<ReadCoilsResponse> futureResponse = modbusMaster.sendRequest(new ReadCoilsRequest(address, quantity),
                    slaveId);
            return futureResponse.handle((response, ex) -> {
                if (ex != null) {
                    ReferenceCountUtil.release(response);
                    return null;
                } else {
                    ByteBuf byteBuf = response.getCoilStatus();
                    int[] values = new int[quantity];
                    int minimum = Math.min(quantity, byteBuf.capacity() * 8);
                    for (int i = 0; i < minimum; i += 8) {
                        setBooleanArray(byteBuf.readUnsignedByte(), values, i, Math.min(minimum - i, 8));
                    }
                    ReferenceCountUtil.release(response);
                    return values;
                }
            });
        }
     
        /**
         * 异步方法,读取modbus设备的离散输入值,对应功能号02
         *
         * @param slaveId  设备id
         * @param address  要读取的寄存器地址
         * @param quantity 要读取的寄存器数量
         * @return 返回 CompletableFuture
         * @author huangji
         */
        public CompletableFuture<int[]> readDiscreteInputs(int slaveId, int address, int quantity) {
            CompletableFuture<ReadDiscreteInputsResponse> futureResponse = modbusMaster.sendRequest(new ReadDiscreteInputsRequest(address, quantity),
                    slaveId);
            return futureResponse.handle((response, ex) -> {
                if (ex != null) {
                    ReferenceCountUtil.release(response);
                    return null;
                } else {
                    ByteBuf byteBuf = response.getInputStatus();
                    int[] values = new int[quantity];
                    int minimum = Math.min(quantity, byteBuf.capacity() * 8);
                    for (int i = 0; i < minimum; i += 8) {
                        setBooleanArray(byteBuf.readUnsignedByte(), values, i, Math.min(minimum - i, 8));
                    }
                    ReferenceCountUtil.release(response);
                    return values;
                }
            });
        }
    
        /**
         * 异步方法,读取modbus设备的保持寄存器值,对应功能号03
         *
         * @param slaveId  设备id
         * @param address  要读取的寄存器地址
         * @param quantity 要读取的寄存器数量
         * @return 返回 CompletableFuture
         * @author huangji
         */
        public CompletableFuture<int[]> readHoldingRegisters(int slaveId, int address, int quantity) {
            CompletableFuture<ReadHoldingRegistersResponse> futureResponse = modbusMaster.sendRequest(new ReadHoldingRegistersRequest(address, quantity),
                    slaveId);
            return futureResponse.handle((response, ex) -> {
                if (ex != null) {
                    ReferenceCountUtil.release(response);
                    return null;
                } else {
                    ByteBuf byteBuf = response.getRegisters();
                    int[] values = new int[quantity];
                    for (int i = 0; i < byteBuf.capacity() / 2; i++) {
                        values[i] = byteBuf.readUnsignedShort();
                    }
                    ReferenceCountUtil.release(response);
                    return values;
                }
            });
        }
    
        /**
         * 异步方法,读取modbus设备的输入寄存器值,对应功能号04
         *
         * @param slaveId  设备id
         * @param address  要读取的寄存器地址
         * @param quantity 要读取的寄存器数量
         * @return 返回 CompletableFuture
         * @author huangji
         */
        public CompletableFuture<int[]> readInputRegisters(int slaveId, int address, int quantity) {
            CompletableFuture<ReadInputRegistersResponse> futureResponse = modbusMaster.sendRequest(new ReadInputRegistersRequest(address, quantity),
                    slaveId);
            return futureResponse.handle((response, ex) -> {
                if (ex != null) {
                    ReferenceCountUtil.release(response);
                    return null;
                } else {
                    ByteBuf byteBuf = response.getRegisters();
                    int[] values = new int[quantity];
                    for (int i = 0; i < byteBuf.capacity() / 2; i++) {
                        values[i] = byteBuf.readUnsignedShort();
                    }
                    ReferenceCountUtil.release(response);
                    return values;
                }
            });
        }
     
        /**
         * 异步方法,写入单个线圈的数值,对应功能号05
         *
         * @param slaveId 设备id
         * @param address 要读取的寄存器地址
         * @param value   要写入的boolean值
         * @return 返回 CompletableFuture
         * @author huangji
         */
        public CompletableFuture<Boolean> writeSingleCoil(int slaveId, int address, boolean value) {
            CompletableFuture<WriteSingleCoilResponse> futureResponse = modbusMaster.sendRequest(new WriteSingleCoilRequest(address, value),
                    slaveId);
            return futureResponse.handle((response, ex) -> {
                if (ex != null) {
                    ReferenceCountUtil.release(response);
                    return false;
                } else {
                    boolean responseValue = response.getValue() != 0;
                    ReferenceCountUtil.release(response);
                    return responseValue == value;
                }
            });
        }
     
        /**
         * 异步方法,写入单个寄存器的数值,对应功能号06
         *
         * @param slaveId 设备id
         * @param address 要读取的寄存器地址
         * @param value   要写入的值
         * @return 返回 CompletableFuture
         * @author huangji
         */
        public CompletableFuture<Boolean> writeSingleRegister(int slaveId, int address, int value) {
            CompletableFuture<WriteSingleRegisterResponse> futureResponse = modbusMaster.sendRequest(new WriteSingleRegisterRequest(address, value),
                    slaveId);
            return futureResponse.handle((response, ex) -> {
                if (ex != null) {
                    ReferenceCountUtil.release(response);
                    return false;
                } else {
                    int responseValue = response.getValue();
                    ReferenceCountUtil.release(response);
                    return responseValue == value;
                }
            });
        }
     
        /**
         * 异步方法,写入多个线圈的数值,对应功能号15
         *
         * @param slaveId  设备id
         * @param address  要写入的寄存器地址
         * @param quantity 要写入的寄存器个数
         * @param values   要写入的boolean[]
         * @return 返回 CompletableFuture
         * @author huangji
         */
        public CompletableFuture<Boolean> writeMultipleCoils(int slaveId, int address, int quantity, boolean[] values) {
            byte[] bytes = booleanToByte(values);
            CompletableFuture<WriteMultipleCoilsResponse> futureResponse = modbusMaster.sendRequest(new WriteMultipleCoilsRequest(address, quantity, bytes),
                    slaveId);
            return futureResponse.handle((response, ex) -> {
                if (ex != null) {
                    ReferenceCountUtil.release(response);
                    return false;
                } else {
                    int responseQuantity = response.getQuantity();
                    ReferenceCountUtil.release(response);
                    return values.length == responseQuantity;
                }
            });
        }
     
        /**
         * 异步方法,写入多个寄存器的数值,对应功能号16
         *
         * @param slaveId  设备id
         * @param address  要写入的寄存器地址
         * @param quantity 要写入的寄存器个数
         * @param values   要写入的int[]
         * @return 返回 CompletableFuture
         * @author huangji
         */
        public CompletableFuture<Boolean> writeMultipleRegisters(int slaveId, int address, int quantity, int[] values) {
            byte[] bytes = intToByte(values);
            CompletableFuture<WriteMultipleRegistersResponse> futureResponse = modbusMaster.sendRequest(new WriteMultipleRegistersRequest(address, quantity, bytes),
                    slaveId);
            return futureResponse.handle((response, ex) -> {
                if (ex != null) {
                    ReferenceCountUtil.release(response);
                    return false;
                } else {
                    int responseQuantity = response.getQuantity();
                    ReferenceCountUtil.release(response);
                    return values.length == responseQuantity;
                }
            });
        }
     
        /**
         * 关闭连接器并释放相关资源
         *
         * @author huangji
         */
        public void disposeModbusConnector() {
            if (modbusMaster != null) {
                modbusMaster.disconnect();
            }
            Modbus.releaseSharedResources();
        }
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335

    二、调试

    首先要在Modbus Slave工具点击Connect进行连接,随便输入一些数字,然后发送请求;

    注:可以先用connect_slave接口发送请求建立平台与设备的连接

    (1) 读数据

    在这里插入图片描述

    (2) 向寄存器写单个数据

    在这里插入图片描述

    (3) 向寄存器写多个数据

    在这里插入图片描述

  • 相关阅读:
    硬件知识:独立显卡和集成显卡的区别,你知道吗?
    建立基于Open vSwitch的GRE隧道
    JDBC连接Mysql(executeQuely)3/13
    一文全面了解火山语音无监督预训练技术的落地实践
    面试题:浏览器HTTP概念及对前端的影响
    如何骚操作通过科目一斩获高分,python带你了解~
    【HCIE考试喜报】2022年11月11日考试通过
    8.0MGR单主模式搭建_克隆(clone)插件方式
    C#进阶09——值类型和引用类型2
    My Code Style
  • 原文地址:https://blog.csdn.net/weixin_39615182/article/details/132975277