use of net.wimpi.modbus.io.ModbusASCIITransport in project openhab1-addons by openhab.
the class SerialConnection method open.
// getModbusTransport
/**
* Opens the communication port.
*
* @throws Exception if an error occurs.
*/
public void open() throws Exception {
if (isOpen()) {
return;
}
// It is ugly patch but it is too late...
if (SystemUtils.IS_OS_LINUX) {
File portDevFile = new File(m_Parameters.getPortName());
if (!portDevFile.exists()) {
throw new Exception("Modbus serial device " + m_Parameters.getPortName() + " doesn't exist!");
}
}
// 1. obtain a CommPortIdentifier instance
try {
m_PortIdentifyer = CommPortIdentifier.getPortIdentifier(m_Parameters.getPortName());
} catch (NoSuchPortException e) {
final String errMsg = "Could not get port identifier, maybe insufficient permissions. " + e.getMessage();
logger.error(errMsg);
throw new Exception(errMsg);
}
logger.trace("Got Port Identifier");
// 2. open the port, wait for given timeout
try {
m_SerialPort = m_PortIdentifyer.open("Modbus Serial Master", 30000);
} catch (PortInUseException e) {
String msg = "open port failed: " + e.getMessage();
logger.error(msg);
throw new Exception(msg);
}
logger.trace("Got Serial Port");
// 3. set the parameters
try {
setConnectionParameters();
} catch (Exception e) {
// ensure it is closed
m_SerialPort.close();
logger.error("parameter setup failed: " + e.getMessage());
throw e;
}
setReceiveTimeout(m_Parameters.getReceiveTimeoutMillis());
if (Modbus.SERIAL_ENCODING_ASCII.equals(m_Parameters.getEncoding())) {
m_Transport = new ModbusASCIITransport();
} else if (Modbus.SERIAL_ENCODING_RTU.equals(m_Parameters.getEncoding())) {
m_Transport = new ModbusRTUTransport();
} else if (Modbus.SERIAL_ENCODING_BIN.equals(m_Parameters.getEncoding())) {
m_Transport = new ModbusBINTransport();
}
m_Transport.setEcho(m_Parameters.isEcho());
// open, close the port before throwing an exception.
try {
m_SerialIn = m_SerialPort.getInputStream();
m_Transport.setCommPort(m_SerialPort);
// m_Transport.prepareStreams(m_SerialIn,
// m_SerialPort.getOutputStream());
} catch (IOException e) {
m_SerialPort.close();
String msg = "Error opening i/o streams: " + e.getMessage();
logger.error(msg);
throw new Exception(msg);
}
logger.trace("i/o Streams prepared");
// Add this object as an event listener for the serial port.
try {
m_SerialPort.addEventListener(this);
} catch (TooManyListenersException e) {
m_SerialPort.close();
final String errMsg = "too many listeners added: " + e.getMessage();
logger.error(errMsg);
throw new Exception(errMsg);
}
// Set notifyOnBreakInterrup to allow event driven break handling.
m_SerialPort.notifyOnBreakInterrupt(true);
m_Open = true;
}
Aggregations