我正在使用 jamod1.2 ,我尝试通过此代码发送布尔值并且它运行良好。
现在我正在尝试向我拥有的一些寄存器发送一个整数值,例如将 5 发送到 %mw100 !我如何通过 jamod 库做到这一点,或者是否有更简单的方法可以做到这一点?
TCPMasterConnection con = null;
try {
con = new TCPMasterConnection(InetAddress.getByName(IP));
} catch (UnknownHostException e) {
e.printStackTrace();
}
con.setPort(502);
try {
con.connect();
} catch (Exception e) {
e.printStackTrace();
}
// Prepare the transaction
ModbusTCPTransaction trans = new ModbusTCPTransaction(con);
// Prepare the request
WriteCoilRequest wcr = new WriteCoilRequest();
// Don't know what unit ID is; 1 seems to work
wcr.setUnitID(1);
// This is the number of the coil to set.
wcr.setReference(unit_num);
// Turn coil on or off?
// This doesn't physically do anything; it just specifies
// what state should be set later on.
if(value=="false") {
wcr.setCoil(false);
}else{
wcr.setCoil(true);
}
// Must execute a transaction now to actually do stuff.
trans.setRequest(wcr);
try {
trans.execute();
} catch (ModbusException e) {
e.printStackTrace();
}
WriteCoilResponse res = (WriteCoilResponse) trans.getResponse();
if (res != null) {
System.out.println("Set coil " + res.getReference() + " to "
+ res.getCoil());
}
con.close();