Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java net.wimpi.modbus.ModbusSlaveException:尝试使用modbus协议连接装置时,错误代码=2_Java_Modbus_Modbus Tcp - Fatal编程技术网

Java net.wimpi.modbus.ModbusSlaveException:尝试使用modbus协议连接装置时,错误代码=2

Java net.wimpi.modbus.ModbusSlaveException:尝试使用modbus协议连接装置时,错误代码=2,java,modbus,modbus-tcp,Java,Modbus,Modbus Tcp,我有一个装置支持ModBus协议,使用“Jamod”尝试连接到装置并读取寄存器值,得到错误代码-2 单位配置: 该装置通过RS-485和以太网接口支持Modbus协议。在RS-485接口上,它在Modbus网络上具有可配置的Modbus地址;默认情况下,该值设置为99。该单元还将响应0的广播地址 默认情况下,RS-485接口的波特率为9600波特,8位偶数奇偶校验。它可配置为1200、2400、4800、9600、19200、38400、57600或15200波特 以太网接口使用RJ45连接器

我有一个装置支持ModBus协议,使用“Jamod”尝试连接到装置并读取寄存器值,得到错误代码-2

单位配置:


该装置通过RS-485和以太网接口支持Modbus协议。在RS-485接口上,它在Modbus网络上具有可配置的Modbus地址;默认情况下,该值设置为99。该单元还将响应0的广播地址

默认情况下,RS-485接口的波特率为9600波特,8位偶数奇偶校验。它可配置为1200、2400、4800、9600、19200、38400、57600或15200波特

以太网接口使用RJ45连接器。此接口支持端口502上的TCP/IP以太网连接。从属地址为0

该装置使用Modbus读取输入寄存器功能代码4返回数据。它还允许使用Modbus保持寄存器访问功能3和16读取和写入配置参数。还支持Modbus诊断功能代码8的子集


请给出连接到设备的说明并阅读,谢谢

*******************Sample Code***********************


import java.io.*;
import java.lang.*;
import java.net.InetAddress;
import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputRegistersRequest;
import net.wimpi.modbus.msg.ReadInputRegistersResponse;
import net.wimpi.modbus.net.TCPMasterConnection;

public class modbus_conn {

    public static void main(String args[]){
                try {                        
                        /* The important instances of the class*/
                        TCPMasterConnection con = null;  //the connection
                        ModbusTCPTransaction trans = null;  //the transaction
                        ReadInputRegistersRequest rreq = null;  //the read request
                        ReadInputRegistersResponse rres = null;  //the read response
                        /* Variables for storing the parameters */
                        InetAddress addr = null;  // the slave's address
                        int port = 502;  // the default port
                        //int coil = 1;  // one of the coils (D0 1 for this address) to switch ON/OFF

                        //Setup the parameters
                        addr = InetAddress.getByName("127.192.6.31");  // ** The address assigned to the module **

                        //Open the connection
                        con = new TCPMasterConnection(addr);
                        con.setPort(port);
                        con.connect();

                        //Prepare the READ request
                        int k = 30001;  // register address starting from 30001
                        rreq = new ReadInputRegistersRequest(k, 2);  // Reading 8 bytes

                        //Prepare the READ transaction
                        trans = new ModbusTCPTransaction(con);
                        trans.setRequest(rreq);

                        //Execute the READ transaction
                        trans.execute();

                        rres = (ReadInputRegistersResponse) trans.getResponse();
                        System.out.println("Hex Value of register " + "= " + rres.getHexMessage());

                        //Close the connection
                        con.close();

                }
                catch (Exception ex) {
                        System.out.println("Error");
                        ex.printStackTrace();
                }           
    }
}
错误:

Error
net.wimpi.modbus.ModbusSlaveException: Error Code = 2
    at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:207)
    at modbusConn.Control_ADAM.main(modbus_conn.java:48)

错误代码2表示存在非法数据地址。 在您的情况下,30001是寄存器地址,您正在读取2个字节。 要解决这个问题,请使用寄存器的十六进制地址,如果您不知道十六进制地址,请参阅手册。(适用于我:-)

给增量不太高也会给你错误代码3,也要小心


错误代码2表示存在非法数据地址。 在您的情况下,30001是寄存器地址,您正在读取2个字节。 要解决这个问题,请使用寄存器的十六进制地址,如果您不知道十六进制地址,请参阅手册。(适用于我:-)

给增量不太高也会给你错误代码3,也要小心


感谢@Shabab Tulve,修复了我的问题,我没有读取正确的寄存器地址,还必须连接两个寄存器才能获得正确的值(在我的情况下!)。感谢@Shabab Tulve,修复了我的问题,我没有读取正确的寄存器地址,还必须连接两个寄存器才能获得正确的值(就我而言!)。