vip381756915 2019-09-09 18:04 采纳率: 100%
浏览 2622
已采纳

java对接modbus报错timeout

本人在网上找了很多类似的例子,但是发送都报错。求问哪位大神遇到过这样的问题,或有更好的例子。跪求啊

使用的包

    <dependency>
            <groupId>com.infiniteautomation</groupId>
            <artifactId>modbus4j</artifactId>
            <version>3.0.3</version>
        </dependency>

代码


package com.shop.utils.modbus;

import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.msg.*;
import com.serotonin.modbus4j.sero.util.queue.ByteQueue;

public class ReadAWriteUtils {

    /**
     * 批量写数据到保持寄存器
     * @param ip 从站IP
     * @param port modbus端口
     * @param slaveId 从站ID
     * @param start 起始地址偏移量
     * @param values 待写数据
     */
    public static void modbusWTCP(String ip, int port, int slaveId, int start, short[] values) {
        ModbusFactory modbusFactory = new ModbusFactory();
        // 设备ModbusTCP的Ip与端口,如果不设定端口则默认为502
        IpParameters params = new IpParameters();
        params.setHost(ip);
        // 设置端口,默认502
        if (502 != port) {
            params.setPort(port);
        }
        ModbusMaster tcpMaster = null;
        // 参数1:IP和端口信息 参数2:保持连接激活
        tcpMaster = modbusFactory.createTcpMaster(params, true);
        try {
            tcpMaster.init();
            System.out.println("=======初始化成功========");
        } catch (ModbusInitException e) {
            System.out.println("初始化异常");
        }
        try {
            WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values);
            WriteRegistersResponse response = (WriteRegistersResponse) tcpMaster.send(request);
            if (response.isException()){
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            }else{
                System.out.println("Success");
            }
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
    }


    /**
     * 读保持寄存器上的内容
     * @param ip 从站IP
     * @param port modbus端口
     * @param start 起始地址偏移量
     * @param readLenth 待读寄存器个数
     * @return
     */
    public static ByteQueue modbusTCP(String ip, int port, int start,int readLenth) {
        ModbusFactory modbusFactory = new ModbusFactory();
        // 设备ModbusTCP的Ip与端口,如果不设定端口则默认为502
        IpParameters params = new IpParameters();
        params.setHost(ip);
        //设置端口,默认502
        if(502!=port){
            params.setPort(port);
        }
        ModbusMaster tcpMaster = null;
        tcpMaster = modbusFactory.createTcpMaster(params, true);
        try {
            tcpMaster.init();
            System.out.println("========初始化成功=======");
        } catch (ModbusInitException e) {
            e.printStackTrace();
            return null;
        }
        ModbusRequest modbusRequest=null;
        try {
            //功能码03   读取保持寄存器的值
            modbusRequest = new ReadInputRegistersRequest(1, start, readLenth);
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
        ModbusResponse modbusResponse=null;
        try {
            modbusResponse = tcpMaster.send(modbusRequest);
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
        ByteQueue byteQueue= new ByteQueue(1024);
        modbusResponse.write(byteQueue);
        System.out.println("功能码:"+modbusRequest.getFunctionCode());
        System.out.println("从站地址:"+modbusRequest.getSlaveId());
        System.out.println("收到的响应信息大小:"+byteQueue.size());
        System.out.println("收到的响应信息值:"+byteQueue);
        return byteQueue;
    }

    public static void main(String[] args){

        modbusTCP("192.168.11.51",502,0x00,2);
    }
}

报错信息:

========初始化成功=======
17:58:06.566 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - Encap Request: 00 00 00 00 00 06 01 04 00 00 00 02 
17:58:06.571 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - Sending on port: 502
17:58:08.097 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - Exception: request=XaMessage [transactionId=0, message=ReadInputRegistersRequest [slaveId=1, getFunctionCode()=4, toString()=ReadNumericRequest [startOffset=0, numberOfRegisters=2]]] request=XaMessage [transactionId=0, message=ReadInputRegistersRequest [slaveId=1, getFunctionCode()=4, toString()=ReadNumericRequest [startOffset=0, numberOfRegisters=2]]]
17:58:08.097 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - KeepAlive - reconnect!
17:58:08.097 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - Modbus4J: Keep-alive connection may have been reset. Attempting to re-open.
com.serotonin.modbus4j.exception.ModbusTransportException: com.serotonin.modbus4j.sero.messaging.TimeoutException: request=XaMessage [transactionId=0, message=ReadInputRegistersRequest [slaveId=1, getFunctionCode()=4, toString()=ReadNumericRequest [startOffset=0, numberOfRegisters=2]]]
    at com.serotonin.modbus4j.ip.tcp.TcpMaster.sendImpl(TcpMaster.java:168)
    at com.serotonin.modbus4j.ModbusMaster.send(ModbusMaster.java:105)
    at com.shop.utils.modbus.ReadAWriteUtils.modbusTCP(ReadAWriteUtils.java:88)
    at com.shop.utils.modbus.ReadAWriteUtils.main(ReadAWriteUtils.java:103)
Caused by: com.serotonin.modbus4j.sero.messaging.TimeoutException: request=XaMessage [transactionId=0, message=ReadInputRegistersRequest [slaveId=1, getFunctionCode()=4, toString()=ReadNumericRequest [startOffset=0, numberOfRegisters=2]]]
    at com.serotonin.modbus4j.sero.messaging.MessageControl.send(MessageControl.java:140)
    at com.serotonin.modbus4j.sero.messaging.MessageControl.send(MessageControl.java:105)
    at com.serotonin.modbus4j.ip.tcp.TcpMaster.sendImpl(TcpMaster.java:154)
    ... 3 more
Exception in thread "main" java.lang.NullPointerException
17:58:09.600 [main] DEBUG com.serotonin.modbus4j.ip.tcp.TcpMaster - Exception: request=XaMessage [transactionId=0, message=ReadInputRegistersRequest [slaveId=1, getFunctionCode()=4, toString()=ReadNumericRequest [startOffset=0, numberOfRegisters=2]]] request=XaMessage [transactionId=0, message=ReadInputRegistersRequest [slaveId=1, getFunctionCode()=4, toString()=ReadNumericRequest [startOffset=0, numberOfRegisters=2]]]
    at com.shop.utils.modbus.ReadAWriteUtils.modbusTCP(ReadAWriteUtils.java:93)
    at com.shop.utils.modbus.ReadAWriteUtils.main(ReadAWriteUtils.java:103)
Disconnected from the target VM, address: '127.0.0.1:49432', transport: 'socket'

Process finished with exit code 1
  • 写回答

2条回答 默认 最新

  • 珍妮玛•黛金 博客专家认证 2019-09-10 10:10
    关注

    https://download.csdn.net/download/u013254183/11706761
    这是我之前项目当中使用的modbustcp工具类,比较稳定,你可以看看,里面有modbus slave调试工具

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件