带有 Spring 引导项目的 Modbus 脉冲线圈
Modbus Pulse Coils with Spring Boot project
必须在 上使用 Modbus 协议 实现对 数字 IO 的调用]Spring Boot 由 Maven.[=18 构建的项目=]
它应该是一个在 body 持续一段时间的脉冲呼叫(例如 5 秒等)
这是规范中的一个片段:
关于响应和错误的更多信息:
似乎在这个调用应用程序应该表现得像一个主人。在这种情况下,Web Relay 应该就像一个奴隶。
我认为使用一些库来与 Modbus 集成比从零开始编写要好得多。
哪种方法更适合通过 Spring 引导采用此功能?
更新:
尝试过
代码部分如下:
@Override
public void callDigitalIO(String ipAddress, String relay) {
log.debug("call Digital IO by MODBUS");
checkNotEmpty(ipAddress, relay);
ModbusTcpMasterConfig config = new ModbusTcpMasterConfig.Builder(ipAddress).build();
ModbusTcpMaster master = new ModbusTcpMaster(config);
master.connect();
ByteBuf buffer = Unpooled.buffer();
buffer.writeInt(0x00003F00);
buffer.writeInt(0x000040A0);
int relayNum = Integer.parseInt(relay);
WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(
relayNum,
0x02 * relayNum,
buffer
);
log.debug("MODBUS_REQUEST: {}", request);
master.sendRequest(request, 0xFF)
.whenCompleteAsync((response, ex) -> {
if (response != null) {
log.info("MODBUS_RESPONSE: {}", response);
} else {
log.error("EXECUTION_FAILED: {}", ex.getMessage(), ex);
}
});
master.disconnect();
}
在真实设备上尝试了下面的代码片段,输出如下:
2021-02-25 22-07-03.955 [carpark-ex-4] DEBUG c.s.s.dio.ModbusDigitalIoServiceImpl - MODBUS_REQUEST: WriteMultipleRegistersRequest(UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 8, cap: 256))
2021-02-25 22-07-03.970 [ForkJoinPool.commonPool-worker-5] ERROR c.s.s.dio.ModbusDigitalIoServiceImpl - EXECUTION_FAILED: not connected
java.lang.Exception: not connected
at com.digitalpetri.netty.fsm.ChannelFsmFactory.lambda$null(ChannelFsmFactory.java:115)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at java.base/java.lang.Thread.run(Thread.java:832)
只有当我这样做的时候:
master.sendRequest(request, 0xFF).get()
它开始起作用了。
使用支持 WriteMultipleRegisters 函数的现有库应该很容易完成。
尝试https://github.com/digitalpetri/modbus
如何根据文档的屏幕截图构建 WriteMultipleRegisters 请求的示例:
class Scratch {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ModbusTcpMasterConfig config = new ModbusTcpMasterConfig.Builder("ip_or_hostname").build();
ModbusTcpMaster master = new ModbusTcpMaster(config);
master.connect().get();
ByteBuf buffer = Unpooled.buffer();
buffer.writeInt(0x00003F00);
buffer.writeInt(0x000040A0);
WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(
0x16,
0x04,
buffer
);
master.sendRequest(request, 0xFF).get();
}
}
必须在 上使用 Modbus 协议 实现对 数字 IO 的调用]Spring Boot 由 Maven.[=18 构建的项目=]
它应该是一个在 body 持续一段时间的脉冲呼叫(例如 5 秒等)
这是规范中的一个片段:
关于响应和错误的更多信息:
似乎在这个调用应用程序应该表现得像一个主人。在这种情况下,Web Relay 应该就像一个奴隶。
我认为使用一些库来与 Modbus 集成比从零开始编写要好得多。
哪种方法更适合通过 Spring 引导采用此功能?
更新:
尝试过
代码部分如下:
@Override
public void callDigitalIO(String ipAddress, String relay) {
log.debug("call Digital IO by MODBUS");
checkNotEmpty(ipAddress, relay);
ModbusTcpMasterConfig config = new ModbusTcpMasterConfig.Builder(ipAddress).build();
ModbusTcpMaster master = new ModbusTcpMaster(config);
master.connect();
ByteBuf buffer = Unpooled.buffer();
buffer.writeInt(0x00003F00);
buffer.writeInt(0x000040A0);
int relayNum = Integer.parseInt(relay);
WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(
relayNum,
0x02 * relayNum,
buffer
);
log.debug("MODBUS_REQUEST: {}", request);
master.sendRequest(request, 0xFF)
.whenCompleteAsync((response, ex) -> {
if (response != null) {
log.info("MODBUS_RESPONSE: {}", response);
} else {
log.error("EXECUTION_FAILED: {}", ex.getMessage(), ex);
}
});
master.disconnect();
}
在真实设备上尝试了下面的代码片段,输出如下:
2021-02-25 22-07-03.955 [carpark-ex-4] DEBUG c.s.s.dio.ModbusDigitalIoServiceImpl - MODBUS_REQUEST: WriteMultipleRegistersRequest(UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 8, cap: 256))
2021-02-25 22-07-03.970 [ForkJoinPool.commonPool-worker-5] ERROR c.s.s.dio.ModbusDigitalIoServiceImpl - EXECUTION_FAILED: not connected
java.lang.Exception: not connected
at com.digitalpetri.netty.fsm.ChannelFsmFactory.lambda$null(ChannelFsmFactory.java:115)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at java.base/java.lang.Thread.run(Thread.java:832)
只有当我这样做的时候:
master.sendRequest(request, 0xFF).get()
它开始起作用了。
使用支持 WriteMultipleRegisters 函数的现有库应该很容易完成。
尝试https://github.com/digitalpetri/modbus
如何根据文档的屏幕截图构建 WriteMultipleRegisters 请求的示例:
class Scratch {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ModbusTcpMasterConfig config = new ModbusTcpMasterConfig.Builder("ip_or_hostname").build();
ModbusTcpMaster master = new ModbusTcpMaster(config);
master.connect().get();
ByteBuf buffer = Unpooled.buffer();
buffer.writeInt(0x00003F00);
buffer.writeInt(0x000040A0);
WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(
0x16,
0x04,
buffer
);
master.sendRequest(request, 0xFF).get();
}
}