use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusUDPTransaction method execute.
// setRetries
@Override
public void execute() throws ModbusIOException, ModbusSlaveException, ModbusException {
// 1. assert executeability
assertExecutable();
try {
// 2. Lock transaction
/**
* Note: The way this explicit synchronization is implemented at the moment,
* there is no ordering of pending threads. The Mutex will simply call notify()
* and the JVM will handle the rest.
*/
m_TransactionLock.acquire();
// 3. open the connection if not connected
if (!m_Terminal.isActive()) {
try {
m_Terminal.activate();
m_IO = m_Terminal.getModbusTransport();
} catch (Exception ex) {
throw new ModbusIOException("Activation failed.");
}
}
// 3. Retry transaction m_Retries times, in case of
// I/O Exception problems.
m_RetryCounter = 0;
while (m_RetryCounter <= m_Retries) {
if (m_RetryCounter != 0) {
Thread.sleep(m_RetryDelayMillis);
}
try {
// toggle the id
m_Request.setTransactionID(c_TransactionID.increment());
// while holding the lock on the IO object
synchronized (m_IO) {
// write request message
m_IO.writeMessage(m_Request);
// read response message
m_Response = m_IO.readResponse();
break;
}
} catch (ModbusIOException ex) {
m_RetryCounter++;
continue;
}
}
// 4. deal with "application level" exceptions
if (m_Response instanceof ExceptionResponse) {
throw new ModbusSlaveException(((ExceptionResponse) m_Response).getExceptionCode());
}
if (isCheckingValidity()) {
checkValidity();
}
} catch (InterruptedException ex) {
throw new ModbusIOException("Thread acquiring lock was interrupted.");
} finally {
m_TransactionLock.release();
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusUDPTransport method writeMessage.
// close
@Override
public void writeMessage(ModbusMessage msg) throws ModbusIOException {
try {
synchronized (m_ByteOut) {
m_ByteOut.reset();
msg.writeTo(m_ByteOut);
m_Terminal.sendMessage(m_ByteOut.toByteArray());
}
} catch (Exception ex) {
throw new ModbusIOException("I/O exception - failed to write.");
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusRTUTransport method readResponse.
// cleanInput
@Override
public ModbusResponse readResponse() throws ModbusIOException {
boolean done = false;
ModbusResponse response = null;
int dlength = 0;
try {
do {
// 1. read to function code, create request and read function specific bytes
synchronized (m_ByteIn) {
// Make ComPort blocking
m_CommPort.enableReceiveThreshold(1);
int uid = m_InputStream.read();
logger.trace("Managed to read at least one byte");
if (uid != -1) {
int fc = m_InputStream.read();
m_ByteInOut.reset();
m_ByteInOut.writeByte(uid);
m_ByteInOut.writeByte(fc);
// create response to acquire length of message
response = ModbusResponse.createModbusResponse(fc);
response.setHeadless();
// With Modbus RTU, there is no end frame. Either we assume
// the message is complete as is or we must do function
// specific processing to know the correct length. To avoid
// moving frame timing to the serial input functions, we set the
// timeout and to message specific parsing to read a response.
getResponse(fc, m_ByteInOut);
// less the crc
dlength = m_ByteInOut.size() - 2;
logger.debug("Response: {}", ModbusUtil.toHex(m_ByteInOut.getBuffer(), 0, dlength + 2));
m_ByteIn.reset(m_InBuffer, dlength);
// check CRC
// does not include CRC
int[] crc = ModbusUtil.calculateCRC(m_InBuffer, 0, dlength);
if (ModbusUtil.unsignedByteToInt(m_InBuffer[dlength]) != crc[0] || ModbusUtil.unsignedByteToInt(m_InBuffer[dlength + 1]) != crc[1]) {
throw new IOException("CRC Error in received frame: " + dlength + " bytes: " + ModbusUtil.toHex(m_ByteIn.getBuffer(), 0, dlength));
}
} else {
throw new IOException("Error reading response (EOF)");
}
// read response
m_ByteIn.reset(m_InBuffer, dlength);
if (response != null) {
response.readFrom(m_ByteIn);
}
done = true;
}
// synchronized
} while (!done);
return response;
} catch (Exception ex) {
final String errMsg = "failed to read";
logger.error("Last request: {}", ModbusUtil.toHex(lastRequest));
logger.error("{}: {}", errMsg, ex.getMessage());
throw new ModbusIOException("I/O exception - " + errMsg);
} finally {
m_CommPort.disableReceiveThreshold();
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusRTUTransport method writeMessage.
@Override
public void writeMessage(ModbusMessage msg) throws ModbusIOException {
try {
int len;
synchronized (m_ByteOut) {
// first clear any input from the receive buffer to prepare
// for the reply since RTU doesn't have message delimiters
clearInput();
// write message to byte out
m_ByteOut.reset();
msg.setHeadless();
msg.writeTo(m_ByteOut);
len = m_ByteOut.size();
int[] crc = ModbusUtil.calculateCRC(m_ByteOut.getBuffer(), 0, len);
m_ByteOut.writeByte(crc[0]);
m_ByteOut.writeByte(crc[1]);
// write message
len = m_ByteOut.size();
byte[] buf = m_ByteOut.getBuffer();
// PDU + CRC
m_OutputStream.write(buf, 0, len);
m_OutputStream.flush();
logger.debug("Sent: {}", ModbusUtil.toHex(buf, 0, len));
// for RS485
if (m_Echo) {
readEcho(len);
}
lastRequest = new byte[len];
System.arraycopy(buf, 0, lastRequest, 0, len);
}
} catch (Exception ex) {
throw new ModbusIOException("I/O failed to write");
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusSerialTransaction method execute.
@Override
public void execute() throws ModbusIOException, ModbusSlaveException, ModbusException {
// 1. assert executeability
assertExecutable();
try {
// 2. Lock transaction
/**
* Note: The way this explicit synchronization is implemented at the moment,
* there is no ordering of pending threads. The Mutex will simply call notify()
* and the JVM will handle the rest.
*/
m_TransactionLock.acquire();
// while holding the lock on the IO object
synchronized (m_IO) {
int tries = 0;
// toggle the id
m_Request.setTransactionID(c_TransactionID.increment());
do {
try {
if (m_TransDelayMS > 0) {
try {
Thread.sleep(m_TransDelayMS);
} catch (InterruptedException ex) {
logger.error("InterruptedException: {}", ex.getMessage());
}
}
// write request message
m_IO.writeMessage(m_Request);
// read response message
m_Response = m_IO.readResponse();
break;
} catch (ModbusIOException e) {
tries++;
logger.error("execute try {}/{} error: {}. Request: {} (unit id {} & transaction {}). Serial parameters: {}", tries, m_Retries, e.getMessage(), m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_SerialCon.getParameters());
if (tries >= m_Retries) {
logger.error("execute reached max tries {}, throwing last error: {}. Request: {}. Serial parameters: {}", m_Retries, e.getMessage(), m_Request, m_SerialCon.getParameters());
throw e;
}
Thread.sleep(m_RetryDelayMillis);
}
} while (true);
if (tries > 0) {
logger.info("execute eventually succeeded with {} re-tries. Request: {}. Serial parameters: {}", tries, m_Request, m_SerialCon.getParameters());
}
}
// 4. deal with exceptions
if (m_Response instanceof ExceptionResponse) {
throw new ModbusSlaveException(((ExceptionResponse) m_Response).getExceptionCode());
}
if (isCheckingValidity()) {
checkValidity();
}
} catch (InterruptedException ex) {
throw new ModbusIOException("Thread acquiring lock was interrupted.");
} finally {
m_TransactionLock.release();
}
}
Aggregations