我有一个单元支持 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)