带有 Spring Boot 项目的 Modbus 脉冲线圈 [英] Modbus Pulse Coils with Spring Boot project

查看:262
本文介绍了带有 Spring Boot 项目的 Modbus 脉冲线圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须在 Spring Boot 上使用 Modbus 协议 实现对 数字 IO 的调用> 项目由 Maven 构建.

Have to implement call to Digital IO with Modbus protocol on Spring Boot project build by Maven.

它应该是在身体上有一定持续时间(如 5 秒等)的脉冲呼叫

It should be a pulsed call with some duration at the body (like 5 seconds, etc.)

这里是规范的片段:

关于响应和错误的更多信息:

And more about Response and Errors:

似乎在这个调用应用程序应该表现得像一个主人.在这种情况下,Web Relay 应该像奴隶一样.

Seems at this call application should behave like a master. And Web Relay should be like a slave in this case.

我认为采用一些库来与 Modbus 集成比从零开始编写要好得多.

I suppose that taking some library for integrating with Modbus will be much more preferable than writing it from zero.

哪种方法更适合在 Spring Boot 中采用此功能?

这里是网络中继手册.

更新:

尝试了 KevinHerron 的回答

这是代码部分:

@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();
}

在真实设备上尝试了下面的代码片段,输出如下:

Tried below snippet with a real device, the output is following:

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$2(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()

master.sendRequest(request, 0xFF).get()

它开始工作了.

推荐答案

使用支持 WriteMultipleRegisters 函数的现有库应该很容易实现这一点.

This should be easy to accomplish using an existing library that supports the WriteMultipleRegisters function.

尝试 https://github.com/digitalpetri/modbus

如何根据文档截图构建 WriteMultipleRegisters 请求的示例:

An example of how to construct a WriteMultipleRegisters request based on that screenshot of documentation:

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();
  }

}

这篇关于带有 Spring Boot 项目的 Modbus 脉冲线圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆