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

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调试工具

    评论
    解决 无用 2
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题