use of net.wimpi.modbus.msg.ReadInputDiscretesResponse in project openhab1-addons by openhab.
the class ModbusSlave method update.
/**
* Reads data from the connected device and updates items with the new data
*
* @param binding ModbusBindig that stores providers information
*/
public void update(ModbusBinding binding) {
try {
Object local = null;
Exception localReadError = null;
try {
if (ModbusBindingProvider.TYPE_COIL.equals(getType())) {
ModbusRequest request = new ReadCoilsRequest(getStart(), getLength());
if (this instanceof ModbusSerialSlave) {
request.setHeadless();
}
ReadCoilsResponse response = (ReadCoilsResponse) getModbusData(request);
local = response.getCoils();
} else if (ModbusBindingProvider.TYPE_DISCRETE.equals(getType())) {
ModbusRequest request = new ReadInputDiscretesRequest(getStart(), getLength());
ReadInputDiscretesResponse response = (ReadInputDiscretesResponse) getModbusData(request);
local = response.getDiscretes();
} else if (ModbusBindingProvider.TYPE_HOLDING.equals(getType())) {
ModbusRequest request = new ReadMultipleRegistersRequest(getStart(), getLength());
ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) getModbusData(request);
local = response.getRegisters();
} else if (ModbusBindingProvider.TYPE_INPUT.equals(getType())) {
ModbusRequest request = new ReadInputRegistersRequest(getStart(), getLength());
ReadInputRegistersResponse response = (ReadInputRegistersResponse) getModbusData(request);
local = response.getRegisters();
}
} catch (ModbusException e) {
// Logging already done in getModbusData
localReadError = e;
} catch (ModbusConnectionException e) {
// Logging already done in getModbusData
localReadError = e;
} catch (ModbusUnexpectedTransactionIdException e) {
// Logging already done in getModbusData
localReadError = e;
}
if (storage == null) {
storage = local;
readError = localReadError;
} else {
synchronized (storage) {
storage = local;
readError = localReadError;
}
}
Collection<String> items = binding.getItemNames();
for (String item : items) {
updateItem(binding, item);
}
} catch (Exception e) {
logger.error("ModbusSlave ({}) error getting response from slave", name, e);
}
}
use of net.wimpi.modbus.msg.ReadInputDiscretesResponse in project openhab1-addons by openhab.
the class ModbusUDPMaster method readInputDiscretes.
// writeMultipleCoils
/**
* Reads a given number of input discrete states from the slave.
* <p/>
* Note that the number of bits in the bit vector will be
* forced to the number originally requested.
*
* @param ref the offset of the input discrete to start reading from.
* @param count the number of input discrete states to be read.
* @return a <tt>BitVector</tt> instance holding the received input discrete
* states.
* @throws ModbusException if an I/O error, a slave exception or
* a transaction error occurs.
*/
public synchronized BitVector readInputDiscretes(int ref, int count) throws ModbusException {
m_ReadInputDiscretesRequest.setReference(ref);
m_ReadInputDiscretesRequest.setBitCount(count);
m_Transaction.setRequest(m_ReadInputDiscretesRequest);
m_Transaction.execute();
BitVector bv = ((ReadInputDiscretesResponse) m_Transaction.getResponse()).getDiscretes();
bv.forceSize(count);
return bv;
}
use of net.wimpi.modbus.msg.ReadInputDiscretesResponse in project openhab1-addons by openhab.
the class DITest method main.
public static void main(String[] args) {
TCPMasterConnection con = null;
ModbusTCPTransaction trans = null;
ReadInputDiscretesRequest req = null;
ReadInputDiscretesResponse res = null;
InetAddress addr = null;
int ref = 0;
int count = 0;
int repeat = 1;
int port = Modbus.DEFAULT_PORT;
try {
// 1. Setup the parameters
if (args.length < 3) {
printUsage();
System.exit(1);
} else {
try {
String astr = args[0];
int idx = astr.indexOf(':');
if (idx > 0) {
port = Integer.parseInt(astr.substring(idx + 1));
astr = astr.substring(0, idx);
}
addr = InetAddress.getByName(astr);
ref = Integer.parseInt(args[1]);
count = Integer.parseInt(args[2]);
if (args.length == 4) {
repeat = Integer.parseInt(args[3]);
}
} catch (Exception ex) {
ex.printStackTrace();
printUsage();
System.exit(1);
}
}
// 2. Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();
if (Modbus.debug) {
System.out.println("Connected to " + addr.toString() + ":" + con.getPort());
}
// 3. Prepare the request
req = new ReadInputDiscretesRequest(ref, count);
// ReadCoilsRequest req = new ReadCoilsRequest(ref, count);
req.setUnitID(0);
if (Modbus.debug) {
System.out.println("Request: " + req.getHexMessage());
}
// 4. Prepare the transaction
trans = new ModbusTCPTransaction(con);
trans.setRequest(req);
trans.setReconnecting(false);
// 5. Execute the transaction repeat times
int k = 0;
do {
trans.execute();
res = (ReadInputDiscretesResponse) trans.getResponse();
if (Modbus.debug) {
System.out.println("Response: " + res.getHexMessage());
}
System.out.println("Digital Inputs Status=" + res.getDiscretes().toString());
// System.out.println("Coils Status=" + res.getCoils().toString());
k++;
} while (k < repeat);
// 6. Close the connection
con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of net.wimpi.modbus.msg.ReadInputDiscretesResponse in project openhab1-addons by openhab.
the class SerialDITest method main.
public static void main(String[] args) {
SerialConnection con = null;
ModbusSerialTransaction trans = null;
ReadInputDiscretesRequest req = null;
ReadInputDiscretesResponse res = null;
String portname = null;
int unitid = 0;
int ref = 0;
int count = 0;
int repeat = 1;
try {
// 1. Setup the parameters
if (args.length < 4) {
printUsage();
System.exit(1);
} else {
try {
portname = args[0];
unitid = Integer.parseInt(args[1]);
ref = Integer.parseInt(args[2]);
count = Integer.parseInt(args[3]);
if (args.length == 5) {
repeat = Integer.parseInt(args[4]);
}
} catch (Exception ex) {
ex.printStackTrace();
printUsage();
System.exit(1);
}
}
// 2. Set slave identifier for master response parsing
ModbusCoupler.getReference().setUnitID(unitid);
System.out.println("net.wimpi.modbus.debug set to: " + System.getProperty("net.wimpi.modbus.debug"));
// 3. Setup serial parameters
SerialParameters params = new SerialParameters();
params.setPortName(portname);
params.setBaudRate(115200);
params.setDatabits(7);
params.setParity("None");
params.setStopbits(2);
// params.setEcho(true);
if (Modbus.debug) {
System.out.println("Encoding [" + params.getEncoding() + "]");
}
// 4. Open the connection
con = new SerialConnection(params);
con.open();
// 5. Prepare a request
req = new ReadInputDiscretesRequest(ref, count);
req.setUnitID(unitid);
req.setHeadless();
if (Modbus.debug) {
System.out.println("Request: " + req.getHexMessage());
}
// 6. Prepare the transaction
trans = new ModbusSerialTransaction(con);
trans.setRequest(req);
// 7. Execute the transaction repeat times
int k = 0;
do {
trans.execute();
res = (ReadInputDiscretesResponse) trans.getResponse();
if (Modbus.debug) {
System.out.println("Response: " + res.getHexMessage());
}
BitVector inputs = res.getDiscretes();
byte[] ret = new byte[inputs.size()];
for (int i = 0; i < count; i++) {
System.out.println("Bit " + i + " = " + inputs.getBit(i));
}
k++;
} while (k < repeat);
// 8. Close the connection
con.close();
} catch (Exception ex) {
ex.printStackTrace();
// Close the connection
con.close();
}
}
use of net.wimpi.modbus.msg.ReadInputDiscretesResponse in project openhab1-addons by openhab.
the class UDPDITest method main.
public static void main(String[] args) {
UDPMasterConnection conn = null;
ModbusUDPTransaction trans = null;
ReadInputDiscretesRequest req = null;
ReadInputDiscretesResponse res = null;
InetAddress addr = null;
int ref = 0;
int count = 0;
int repeat = 1;
int port = Modbus.DEFAULT_PORT;
try {
// 1. Setup the parameters
if (args.length < 3) {
printUsage();
System.exit(1);
} else {
try {
String astr = args[0];
int idx = astr.indexOf(':');
if (idx > 0) {
port = Integer.parseInt(astr.substring(idx + 1));
astr = astr.substring(0, idx);
}
addr = InetAddress.getByName(astr);
ref = Integer.parseInt(args[1]);
count = Integer.parseInt(args[2]);
if (args.length == 4) {
repeat = Integer.parseInt(args[3]);
}
} catch (Exception ex) {
ex.printStackTrace();
printUsage();
System.exit(1);
}
}
// 2. Open the connection
conn = new UDPMasterConnection(addr);
conn.setPort(port);
conn.connect();
// 3. Prepare the request
req = new ReadInputDiscretesRequest(ref, count);
req.setUnitID(0);
if (Modbus.debug) {
System.out.println("Request: " + req.getHexMessage());
}
// 4. Prepare the transaction
trans = new ModbusUDPTransaction(conn);
trans.setRequest(req);
// 5. Execute the transaction repeat times
int k = 0;
do {
trans.execute();
res = (ReadInputDiscretesResponse) trans.getResponse();
if (Modbus.debug) {
System.out.println("Response: " + res.getHexMessage());
}
System.out.println("Digital Inputs Status=" + res.getDiscretes().toString());
k++;
} while (k < repeat);
// 6. Close the connection
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Aggregations