use of net.wimpi.modbus.msg.ReadMultipleRegistersRequest 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.ReadMultipleRegistersRequest in project openhab1-addons by openhab.
the class SimultaneousReadWriteTestCase method testSimultaneousReadWrite.
/**
* Testing how binding handles simultaneous read and writes coming in.
*
* Even though the server in this test is able to handle at most one client at a time the binding
* queues requests.
*
* Note higher artificialServerWait in constructor
*
* @throws Exception
*/
@Test
public void testSimultaneousReadWrite() throws Exception {
binding = new ModbusBinding();
binding.updated(addSlave(addSlave(newLongPollBindingConfig(), SLAVE_NAME, type, null, 0, READ_COUNT), SLAVE2_NAME, type, null, 0, READ_COUNT));
configureItems(SLAVE_NAME);
configureItems(SLAVE2_NAME);
/*
* - both slaves read twice -> 4 read requests
* - followed by write (slave1) -> 1 write request
* - both slaves read once -> 2 read requests.
* - Finally three writes (slave2) -> 3 write requets
*/
int expectedRequests = 10;
ExecutorService pool = Executors.newFixedThreadPool(expectedRequests);
binding.execute();
pool.execute(new UpdateThread(binding));
pool.execute(new WriteCommandThread(binding, SLAVE_NAME, command));
pool.execute(new UpdateThread(binding));
pool.execute(new WriteCommandThread(binding, SLAVE2_NAME, command));
pool.execute(new WriteCommandThread(binding, SLAVE2_NAME, command));
pool.execute(new WriteCommandThread(binding, SLAVE2_NAME, command));
pool.shutdown();
pool.awaitTermination(artificialServerWait * 7 + 5000, TimeUnit.MILLISECONDS);
waitForRequests(expectedRequests);
ArrayList<ModbusRequest> values = modbustRequestCaptor.getAllReturnValues();
System.err.println(values);
int readCount = 0;
int writeCount = 0;
for (ModbusRequest request : values) {
if (request instanceof ReadMultipleRegistersRequest) {
readCount++;
} else if (request instanceof WriteSingleRegisterRequest) {
writeCount++;
}
}
Assert.assertEquals(6, readCount);
Assert.assertEquals(4, writeCount);
}
Aggregations