J2mod:I/O异常-读取失败

问题描述 投票:0回答:1

我想利用功能码= 03,这意味着读取保持寄存器,但是在连接硬件后我遇到了一些错误。

我使用了 j2mod-1.06 jar 文件和 rxtx。我正在使用 eclipse。在 eclipse 中,在覆盖会话期间,这部分显示为红色,这意味着它没有运行。

  catch (Exception exception) 
        {
            System.out.println("Not Run");
            exception.printStackTrace();

        }

        /* Read the first four holding registers */
        try 
        {
            slaveResponse = serialMaster.readMultipleRegisters(unitID, startingRegister, registerCount);
            for (int i = 0; i < slaveResponse.length; i++) 
            {
                System.out.println("reg" + i + " = " + slaveResponse[i]);
            }
        } 

这是它的代码

import com.ghgande.j2mod.modbus.facade.ModbusSerialMaster;
import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.ModbusException;
import com.ghgande.j2mod.modbus.procimg.Register;
import com.ghgande.j2mod.modbus.util.SerialParameters;

public class ModbusMaster
{

/**
 * @param args
 */
    public static void main(String[] args) 
    {
        /* The important instances of the classes mentioned before */
        ModbusSerialMaster serialMaster = null; // the connection

        /* Variables for storying the parameters */
        String portname = "COM3"; // the name of the serial port to be used
        int unitID = 1 ; // the unit identifier we will be talking to
        int startingRegister = 10001; // the reference, where to start reading from
        int registerCount = 0; // the count of the input registers to read
        Register[] slaveResponse = new Register[registerCount];

        try 
        {
            /* Setup the serial parameters */
            SerialParameters parameters = new SerialParameters();
            parameters.setPortName(portname);
            parameters.setBaudRate(9600);
            parameters.setDatabits(8);
            parameters.setParity("None");
            parameters.setStopbits(1);
            parameters.setEncoding(Modbus.SERIAL_ENCODING_RTU);
            parameters.setEcho(false);

            /* Open the connection */
            serialMaster = new ModbusSerialMaster(parameters);
            serialMaster.connect();

        } 

        catch (Exception exception) 
        {
            System.out.println("Not Run");
            exception.printStackTrace();

        }

        /* Read the first four holding registers */
        try 
        {
            slaveResponse = serialMaster.readMultipleRegisters(unitID, startingRegister, registerCount);
            for (int i = 0; i < slaveResponse.length; i++) 
            {
                System.out.println("reg" + i + " = " + slaveResponse[i]);
            }
        } 

        catch (ModbusException e) 
        {
            e.printStackTrace();
        }
        /* Close the connection */
        serialMaster.disconnect();

    }
}

这是我得到的错误。

com.ghgande.j2mod.modbus.ModbusIOException: I/O exception - failed to read
    at com.ghgande.j2mod.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:645)
    at com.ghgande.j2mod.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:189)
    at com.ghgande.j2mod.modbus.facade.ModbusSerialMaster.readMultipleRegisters(ModbusSerialMaster.java:269)
    at ModbusMaster.main(ModbusMaster.java:53)
java modbus rxtx
1个回答
0
投票

你能帮我解决这个问题吗? 代码: ModbusTCPMaster 主站 = new ModbusTCPMaster(ipAddress, 102); //替换为实际的PLC IP地址 master.setReconnecting(true);//设置Modbus端口号 尝试 { master.connect(); //连接PLC //读取保存记录中的数据 寄存器[] SlaveResponse = 新寄存器[0]; BitVector bitVector = master.readInputDiscretes(0x00, 001, 1); System.out.println(bitVector); master.disconnect(); // 断开连接 } catch (异常 e) { 抛出新的运行时异常(e); } 这里:master.readInputDiscretes(0x00, 001, 1); 问题:我不知道该方法的参数,并且运行结果“com.ghgande.j2mod.modbus.ModbusIOException:执行事务00 04 00 00 00 06 00 02 00 01 00 01失败(尝试了5次)一般异常 - 失败阅读 - 连接重置”

© www.soinside.com 2019 - 2024. All rights reserved.