use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class DSMRPort method open.
/**
* Opens the Operation System Serial Port
* <p>
* This method opens the port and set Serial Port parameters according to
* the DSMR specification. Since the specification is clear about these
* parameters there are not configurable.
* <p>
* If there are problem while opening the port, it is the responsibility of
* the calling method to handle this situation (and for example close the
* port again).
* <p>
* Opening an already open port is harmless. The method will return
* immediately
*
* @return true if opening was successful (or port was already open), false
* otherwise
*/
private boolean open() {
synchronized (portLock) {
// Sanity check
if (portState != PortState.CLOSED) {
return true;
}
try {
// Add non standard port names if not exists (fixes part of #4175)
if (!portExists(portName)) {
logger.warn("Port {} does not exists according to the system, we will still try to open it", portName);
}
// Opening Operating System Serial Port
logger.debug("Creating CommPortIdentifier");
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
logger.debug("Opening CommPortIdentifier");
CommPort commPort = portIdentifier.open("org.openhab.binding.dsmr", readTimeoutMSec);
logger.debug("Configure serial port");
serialPort = (SerialPort) commPort;
serialPort.enableReceiveThreshold(1);
serialPort.enableReceiveTimeout(readTimeoutMSec);
// Configure Serial Port based on specified port speed
logger.debug("Configure serial port parameters: {}", portSettings);
if (portSettings != null) {
serialPort.setSerialPortParams(portSettings.getBaudrate(), portSettings.getDataBits(), portSettings.getStopbits(), portSettings.getParity());
/* special settings for low speed port (checking reference here) */
if (portSettings == DSMRPortSettings.LOW_SPEED_SETTINGS) {
serialPort.setDTR(false);
serialPort.setRTS(true);
}
} else {
logger.error("Invalid port parameters, closing port:{}", portSettings);
return false;
}
} catch (NoSuchPortException nspe) {
logger.error("Could not open port: {}", portName, nspe);
return false;
} catch (PortInUseException piue) {
logger.error("Port already in use: {}", portName, piue);
return false;
} catch (UnsupportedCommOperationException ucoe) {
logger.error("Port does not support requested port settings " + "(invalid dsmr:portsettings parameter?): {}", portName, ucoe);
return false;
}
// SerialPort is ready, open the reader
logger.info("SerialPort opened successful");
try {
bis = new BufferedInputStream(serialPort.getInputStream());
} catch (IOException ioe) {
logger.error("Failed to get inputstream for serialPort. Closing port", ioe);
return false;
}
logger.info("DSMR Port opened successful");
return true;
}
}
use of gnu.io.UnsupportedCommOperationException in project JMRI by JMRI.
the class SerialDriverAdapter method openPort.
@Override
@SuppressWarnings("CallToPrintStackTrace")
public String openPort(String portName, String appName) {
try {
// get and open the primary port
CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
try {
// name of program, msec to wait
activeSerialPort = (SerialPort) portID.open(appName, 2000);
} catch (PortInUseException p) {
return handlePortBusy(p, portName, log);
}
// try to set it for serial
try {
setSerialPort();
} catch (gnu.io.UnsupportedCommOperationException e) {
log.error("Cannot set serial parameters on port " + portName + ": " + e.getMessage());
return "Cannot set serial parameters on port " + portName + ": " + e.getMessage();
}
// set framing (end) character
try {
log.debug("Serial framing was observed as: " + activeSerialPort.isReceiveFramingEnabled() + " " + activeSerialPort.getReceiveFramingByte());
} catch (Exception ef) {
log.debug("failed to set serial framing: " + ef);
}
// set timeout; framing should work before this anyway
try {
activeSerialPort.enableReceiveTimeout(10);
log.debug("Serial timeout was observed as: " + activeSerialPort.getReceiveTimeout() + " " + activeSerialPort.isReceiveTimeoutEnabled());
} catch (UnsupportedCommOperationException et) {
log.info("failed to set serial timeout: " + et);
}
// get and save stream
serialStream = activeSerialPort.getInputStream();
// purge contents, if any
purgeStream(serialStream);
// report status?
if (log.isInfoEnabled()) {
// report now
log.info(portName + " port opened at " + activeSerialPort.getBaudRate() + " baud with" + " DTR: " + activeSerialPort.isDTR() + " RTS: " + activeSerialPort.isRTS() + " DSR: " + activeSerialPort.isDSR() + " CTS: " + activeSerialPort.isCTS() + " CD: " + activeSerialPort.isCD());
}
if (log.isDebugEnabled()) {
// report additional status
log.debug(" port flow control shows " + (activeSerialPort.getFlowControlMode() == SerialPort.FLOWCONTROL_RTSCTS_OUT ? "hardware flow control" : "no flow control"));
}
if (log.isDebugEnabled()) {
// arrange to notify later
activeSerialPort.addEventListener(new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent e) {
int type = e.getEventType();
switch(type) {
case SerialPortEvent.DATA_AVAILABLE:
log.info("SerialEvent: DATA_AVAILABLE is " + e.getNewValue());
return;
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
log.info("SerialEvent: OUTPUT_BUFFER_EMPTY is " + e.getNewValue());
return;
case SerialPortEvent.CTS:
log.info("SerialEvent: CTS is " + e.getNewValue());
return;
case SerialPortEvent.DSR:
log.info("SerialEvent: DSR is " + e.getNewValue());
return;
case SerialPortEvent.RI:
log.info("SerialEvent: RI is " + e.getNewValue());
return;
case SerialPortEvent.CD:
log.info("SerialEvent: CD is " + e.getNewValue());
return;
case SerialPortEvent.OE:
log.info("SerialEvent: OE (overrun error) is " + e.getNewValue());
return;
case SerialPortEvent.PE:
log.info("SerialEvent: PE (parity error) is " + e.getNewValue());
return;
case SerialPortEvent.FE:
log.info("SerialEvent: FE (framing error) is " + e.getNewValue());
return;
case SerialPortEvent.BI:
log.info("SerialEvent: BI (break interrupt) is " + e.getNewValue());
return;
default:
log.info("SerialEvent of unknown type: " + type + " value: " + e.getNewValue());
}
}
});
try {
activeSerialPort.notifyOnFramingError(true);
} catch (Exception e) {
log.debug("Could not notifyOnFramingError: " + e);
}
try {
activeSerialPort.notifyOnBreakInterrupt(true);
} catch (Exception e) {
log.debug("Could not notifyOnBreakInterrupt: " + e);
}
try {
activeSerialPort.notifyOnParityError(true);
} catch (Exception e) {
log.debug("Could not notifyOnParityError: " + e);
}
try {
activeSerialPort.notifyOnOverrunError(true);
} catch (Exception e) {
log.debug("Could not notifyOnOverrunError: " + e);
}
}
opened = true;
} catch (gnu.io.NoSuchPortException p) {
return handlePortNotFound(p, portName, log);
} catch (IOException ex) {
log.error("Unexpected exception while opening port " + portName + " trace follows: " + ex);
ex.printStackTrace();
return "Unexpected error while opening port " + portName + ": " + ex;
} catch (TooManyListenersException ex) {
log.error("Unexpected exception while opening port " + portName + " trace follows: " + ex);
ex.printStackTrace();
return "Unexpected error while opening port " + portName + ": " + ex;
}
// normal operation
return null;
}
Aggregations