use of net.wimpi.modbus.msg.ModbusResponse in project openhab1-addons by openhab.
the class ModbusSlave method executeWriteRequest.
/**
*
* @param request
* @throws ModbusConnectionException when connection cannot be established
* @throws ModbusException ModbusIOException on IO errors, ModbusSlaveException with protocol level exceptions
* @throws ModbusUnexpectedTransactionIdException when response transaction id does not match the request
*/
private void executeWriteRequest(ModbusRequest request) throws ModbusConnectionException, ModbusException, ModbusUnexpectedTransactionIdException {
ModbusSlaveEndpoint endpoint = getEndpoint();
ModbusSlaveConnection connection = null;
try {
connection = getConnection(endpoint);
if (connection == null) {
logger.warn("ModbusSlave ({}): not connected -- aborting request {}", name, request);
throw new ModbusConnectionException(endpoint);
}
transaction.setRequest(request);
try {
transaction.execute();
} catch (Exception e) {
// Note, one could catch ModbusIOException and ModbusSlaveException if more detailed
// exception handling is required. For now, all exceptions are handled the same way with writes.
logger.error("ModbusSlave ({}): error when executing write request ({}): {}", name, request, e.getMessage());
invalidate(endpoint, connection);
// set connection to null such that it is not returned to pool
connection = null;
throw e;
}
ModbusResponse response = transaction.getResponse();
if ((response.getTransactionID() != transaction.getTransactionID()) && !response.isHeadless()) {
logger.warn("ModbusSlave ({}): Transaction id of the response does not match request {}. Endpoint {}. Connection: {}. Ignoring response.", name, request, endpoint, connection);
throw new ModbusUnexpectedTransactionIdException();
}
} finally {
returnConnection(endpoint, connection);
}
}
use of net.wimpi.modbus.msg.ModbusResponse in project openhab1-addons by openhab.
the class ModbusTCPTransport method readResponse.
// readRequest
@Override
public ModbusResponse readResponse() throws ModbusIOException {
try {
ModbusResponse res = null;
synchronized (m_ByteIn) {
// use same buffer
byte[] buffer = m_ByteIn.getBuffer();
// read to byte length of message
if (m_Input.read(buffer, 0, 6) == -1) {
throw new ModbusIOException("Premature end of stream (Header truncated).");
}
// extract length of bytes following in message
int bf = ModbusUtil.registerToShort(buffer, 4);
// read rest
if (m_Input.read(buffer, 6, bf) == -1) {
throw new ModbusIOException("Premature end of stream (Message truncated).");
}
m_ByteIn.reset(buffer, (6 + bf));
m_ByteIn.skip(7);
int functionCode = m_ByteIn.readUnsignedByte();
m_ByteIn.reset();
res = ModbusResponse.createModbusResponse(functionCode);
res.readFrom(m_ByteIn);
}
return res;
/*
* try {
* int transactionID = m_Input.readUnsignedShort();
* //System.out.println("Read tid="+transactionID);
* int protocolID = m_Input.readUnsignedShort();
* //System.out.println("Read pid="+protocolID);
* int dataLength = m_Input.readUnsignedShort();
* //System.out.println("Read length="+dataLength);
* int unitID = m_Input.readUnsignedByte();
* //System.out.println("Read uid="+unitID);
* int functionCode = m_Input.readUnsignedByte();
* //System.out.println("Read fc="+functionCode);
* ModbusResponse response =
* ModbusResponse.createModbusResponse(functionCode, m_Input, false);
* if (response instanceof ExceptionResponse) {
* //skip rest of bytes
* for (int i = 0; i < dataLength - 2; i++) {
* m_Input.readByte();
* }
* }
* //set read parameters
* response.setTransactionID(transactionID);
* response.setProtocolID(protocolID);
* response.setUnitID(unitID);
* return response;
*/
} catch (Exception ex) {
ex.printStackTrace();
throw new ModbusIOException("I/O exception - failed to read.");
}
}
use of net.wimpi.modbus.msg.ModbusResponse in project openhab1-addons by openhab.
the class ModbusSlave method getModbusData.
/**
* Executes Modbus transaction that reads data from the device and returns response data
*
* @param request describes what data are requested from the device
* @return response data
* @throws ModbusConnectionException when connection cannot be established
* @throws ModbusException ModbusIOException on IO errors, ModbusSlaveException with protocol level exceptions
* @throws ModbusUnexpectedTransactionIdException when response transaction id does not match the request
*/
private ModbusResponse getModbusData(ModbusRequest request) throws ModbusConnectionException, ModbusException, ModbusUnexpectedTransactionIdException {
ModbusSlaveEndpoint endpoint = getEndpoint();
ModbusSlaveConnection connection = null;
ModbusResponse response = null;
try {
connection = getConnection(endpoint);
if (connection == null) {
logger.warn("ModbusSlave ({}) not connected -- aborting read request {}. Endpoint {}", name, request, endpoint);
throw new ModbusConnectionException(endpoint);
}
request.setUnitID(getId());
transaction.setRequest(request);
try {
transaction.execute();
} catch (ModbusException e) {
logger.error("ModbusSlave ({}): Error getting modbus data for request {}. Error: {}. Endpoint {}. Connection: {}", name, request, e.getMessage(), endpoint, connection);
invalidate(endpoint, connection);
// Invalidated connections should not be returned
connection = null;
throw e;
}
response = transaction.getResponse();
if ((response.getTransactionID() != transaction.getTransactionID()) && !response.isHeadless()) {
logger.warn("ModbusSlave ({}): Transaction id of the response does not match request {}. Endpoint {}. Connection: {}. Ignoring response.", name, request, endpoint, connection);
throw new ModbusUnexpectedTransactionIdException();
}
} finally {
returnConnection(endpoint, connection);
}
return response;
}
use of net.wimpi.modbus.msg.ModbusResponse 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.msg.ModbusResponse in project openhab1-addons by openhab.
the class ModbusUDPTransport method readResponse.
// readRequest
@Override
public ModbusResponse readResponse() throws ModbusIOException {
try {
ModbusResponse res = null;
synchronized (m_ByteIn) {
m_ByteIn.reset(m_Terminal.receiveMessage());
m_ByteIn.skip(7);
int functionCode = m_ByteIn.readUnsignedByte();
m_ByteIn.reset();
res = ModbusResponse.createModbusResponse(functionCode);
res.readFrom(m_ByteIn);
}
return res;
} catch (InterruptedIOException ioex) {
throw new ModbusIOException("Socket timed out.");
} catch (Exception ex) {
ex.printStackTrace();
throw new ModbusIOException("I/O exception - failed to read.");
}
}
Aggregations