我编写了以下程序来了解如何使用JAMOD来访问线圈。不幸的是它抛出了错误 -
java.lang.IndexOutOfBoundsException
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.read(Unknown Source)
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:177)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)
at TempReader.main(TempReader.java:27)
. x2 more times
.
java.io.EOFException
at java.io.DataInputStream.readUnsignedByte(Unknown Source)
at net.wimpi.modbus.io.BytesInputStream.readUnsignedByte(BytesInputStream.java:153)
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:182)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)
at TempReader.main(TempReader.java:30)
net.wimpi.modbus.ModbusIOException: Executing transaction failed (tried 3 times)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:197)
at TempReader.main(TempReader.java:30)
在程序行27中是“trans.execute()”。我试图读取的线圈地址是0x7D1(2001)或通道0数据类型是2个字。 -
import java.net.*;
import java.io.*;
import net.wimpi.modbus.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.util.*;
public class TempReader {
public static void main(String Args[]){
TCPMasterConnection con = null;
ModbusTCPTransaction trans = null;
ReadCoilsRequest rcreq=null;
ReadCoilsResponse rcres=null;
InetAddress addr = null;
int port = Modbus.DEFAULT_PORT;
byte byteArray[]={(byte)192, (byte)168, (byte)0, (byte)182};
try {
addr=InetAddress.getByAddress(byteArray);
con=new TCPMasterConnection(addr);
con.setPort(502);
con.connect();
rcreq=new ReadCoilsRequest(2001, 1);
trans=new ModbusTCPTransaction(con);
trans.setRequest(rcreq);
trans.execute();
rcres=(ReadCoilsResponse)trans.getResponse();
System.out.println("Response : "+rcres.getCoils().toString());
con.close();
}
catch(Exception e){
con.close();
e.printStackTrace();
}
}
}
错误当然重复了ModbusTCPTransport类中设置的3次(我检查了类源),即在退出和程序退出之前尝试了3次。
我哪里错了?
答案 0 :(得分:1)
我的示例Modbus TCP ReadMultipleRegistersRequest。
public static void main(String[] args) {
try {
/**************************************/
//Read And Write Register Sample
int port = Modbus.DEFAULT_PORT;
String refe = "4000";//HEX Address
int ref=Integer.parseInt(refe,16);//Hex to int
int count = 98; //the number Address to read
int SlaveAddr=1;
String astr = "192.168.1.202"; //Modbus Device
InetAddress addr = InetAddress.getByName(astr);
TCPMasterConnection con = new TCPMasterConnection(addr); //the connection
ModbusTCPTransaction trans = null; //the transaction
//1.Prepare the request
/************************************/
ReadMultipleRegistersRequest Rreq = new ReadMultipleRegistersRequest(ref,count);
ReadMultipleRegistersResponse Rres = new ReadMultipleRegistersResponse();
Rreq.setUnitID(SlaveAddr); //set Slave Address
Rres.setUnitID(SlaveAddr); //set Slave Address
//2. Open the connection
con.setPort(port);
con.connect();
con.setTimeout(2500);
//3. Start Transaction
trans = new ModbusTCPTransaction(con);
trans.setRetries(5);
trans.setReconnecting(true);
trans.setRequest(Rreq);
trans.execute();
/*Print Response*/
Rres = (ReadMultipleRegistersResponse) trans.getResponse();
System.out.println("Connected to= "+ astr + con.isConnected() + " / Start Register " + Integer.toHexString(ref));
count=1;
for (int k=0;k<count;k++){
System.out.println("The value READ: " + Rres.getRegisterValue(k));
}
/****************Close Connection**************/
con.close();
System.out.println("\nConnected = " + con.isConnected());
System.exit(0);//edit Java bug error
} catch (Exception ex) {
ex.printStackTrace();
}
}//main
如果你想让奴隶尝试这个例子:
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.procimg.*;
import net.wimpi.modbus.ModbusCoupler;
public class TCPSlaveTest {
public static void main(String[] args) {
try {
/* The important instances and variables */
ModbusTCPListener listener = null;
SimpleProcessImage spi = null;
int port = Modbus.DEFAULT_PORT;
//1. Set port number from commandline parameter
if(args != null && args.length ==1) {
port = Integer.parseInt(args[0]);
}
//2. Prepare a process image
spi = new SimpleProcessImage();
spi.addDigitalOut(new SimpleDigitalOut(true));
spi.addDigitalOut(new SimpleDigitalOut(false));
spi.addDigitalIn(new SimpleDigitalIn(false));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addDigitalIn(new SimpleDigitalIn(false));
spi.addDigitalIn(new SimpleDigitalIn(true));
spi.addRegister(new SimpleRegister(251));
spi.addInputRegister(new SimpleInputRegister(45));
//3. Set the image on the coupler
ModbusCoupler.getReference().setProcessImage(spi);
ModbusCoupler.getReference().setMaster(false);
ModbusCoupler.getReference().setUnitID(15);
//4. Create a listener with 3 threads in pool
listener = new ModbusTCPListener(3);
listener.setPort(port);
listener.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}//main
}//class TCPSlaveTest