use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.
the class StuderHandler method registerPollTask.
/**
* Register poll task
* This is where we set up our regular poller
*/
private synchronized void registerPollTask(int registerNumber) {
if (pollTasks.size() >= registers.length) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
throw new IllegalStateException("New pollTask invalid");
}
ModbusCommunicationInterface mycomms = comms;
StuderConfiguration studerConfig = config;
if (studerConfig == null || mycomms == null) {
throw new IllegalStateException("registerPollTask called without proper configuration");
}
logger.debug("Setting up regular polling");
ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(studerConfig.slaveAddress, ModbusReadFunctionCode.READ_INPUT_REGISTERS, registerNumber, 2, studerConfig.maxTries);
long refreshMillis = studerConfig.refresh * 1000;
PollTask pollTask = mycomms.registerRegularPoll(request, refreshMillis, 1000, result -> {
if (result.getRegisters().isPresent()) {
ModbusRegisterArray reg = result.getRegisters().get();
handlePolledData(registerNumber, reg);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
return;
}
if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);
}
}, this::handleError);
pollTasks.add(pollTask);
}
use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.
the class E3DCHandlerStateTest method getDataResult.
private AsyncModbusReadResult getDataResult() {
byte[] dataBlockBytes = new byte[] { 0, -14, 0, 0, -2, -47, -1, -1, 2, 47, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 125, 2, 21, 0, 0, 0, 27, 0, 26, 0, 0, 0, 103, 0, -117, 0, 0 };
ModbusReadRequestBlueprint readRequest = mock(ModbusReadRequestBlueprint.class);
return new AsyncModbusReadResult(readRequest, new ModbusRegisterArray(dataBlockBytes));
}
use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.
the class SunspecDiscoveryProcess method readCommonBlock.
/**
* Start reading common block
*
* @param block
*/
private void readCommonBlock(ModelBlock block) {
ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(slaveId, // Start address
ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, // Start address
block.address, // number or words to return
block.length, maxTries);
comms.submitOneTimePoll(request, result -> result.getRegisters().ifPresent(this::parseCommonBlock), this::handleError);
}
use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.
the class SunspecDiscoveryProcess method detectModel.
/**
* Start model detection
*
* @param uid the thing type to look for
* @throws EndpointNotInitializedException
*/
public void detectModel() {
if (possibleAddresses.isEmpty()) {
parsingFinished();
return;
}
// Try the next address from the possibles
baseAddress = possibleAddresses.poll();
logger.trace("Beginning scan for SunSpec device at address {}", baseAddress);
ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(slaveId, // Start address
ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, // Start address
baseAddress, // number or words to return
SUNSPEC_ID_SIZE, maxTries);
comms.submitOneTimePoll(request, result -> result.getRegisters().ifPresent(this::headerReceived), this::handleError);
}
use of org.openhab.core.io.transport.modbus.ModbusReadRequestBlueprint in project openhab-addons by openhab.
the class SunspecDiscoveryProcess method handleError.
/**
* Handle errors received during communication
*/
private void handleError(AsyncModbusFailure<ModbusReadRequestBlueprint> failure) {
if (blocksFound > 1 && failure.getCause() instanceof ModbusSlaveErrorResponseException) {
int code = ((ModbusSlaveErrorResponseException) failure.getCause()).getExceptionCode();
if (code == ModbusSlaveErrorResponseException.ILLEGAL_DATA_ACCESS || code == ModbusSlaveErrorResponseException.ILLEGAL_DATA_VALUE) {
// It is very likely that the slave does not report an end block (0xffff) after the main blocks
// so we treat this situation as normal.
logger.debug("Seems like slave device does not report an end block. Continuing with the dectected blocks");
parsingFinished();
return;
}
}
String cls = failure.getCause().getClass().getName();
String msg = failure.getCause().getMessage();
logger.warn("Error with read at address {}: {} {}", baseAddress, cls, msg);
detectModel();
}
Aggregations