0
我正在使用jamod1.2,我試圖通過此代碼發送布爾值,它的工作原理完美。寫整數值不布爾到存儲器字100
現在我試圖發送一個整數值給我有一些寄存器,比如發送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();