use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class NikobusInterface method openCommPort.
/**
* Locate serial port and open connection.
*
* @param portName
* @throws Exception
*/
@SuppressWarnings("rawtypes")
private void openCommPort(String portName) throws Exception {
if (log.isDebugEnabled()) {
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
while (portIdentifiers.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portIdentifiers.nextElement();
log.debug("Found port: {} x {}", id.getName(), id.getCurrentOwner());
}
}
CommPortIdentifier portIdentifier = null;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
} catch (NoSuchPortException e) {
log.debug("Port not found during first attempt : {}", e.getMessage());
}
if (portIdentifier == null) {
try {
System.setProperty(SERIAL_PORT_PROPERTY_NAME, portName);
portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
} catch (Exception e) {
log.debug("Port not found during second attempt : {}", e.getMessage());
System.clearProperty(SERIAL_PORT_PROPERTY_NAME);
}
}
if (portIdentifier == null) {
throw new Exception("Serial port '" + portName + "' not found.");
}
if (portIdentifier.isCurrentlyOwned()) {
throw new Exception("Serial port '" + portName + "' is in use.");
}
// open port
port = portIdentifier.open(this.getClass().getSimpleName(), CONNECT_TIMEOUT);
log.info("Connected to serial port '{}'", portIdentifier.getName());
// configure
port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.notifyOnDataAvailable(true);
port.addEventListener(this);
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class SerialWR3223Connector method connect.
/**
* Connect to WR2332 over serial port.
*
* @param port
* @param baud
* @throws IOException
*/
public void connect(String port, int baud) throws IOException {
// parse ports and if the default port is found, initialized the reader
CommPortIdentifier portId = null;
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (id.getName().equals(port)) {
portId = id;
}
}
}
if (portId != null) {
// initialize serial port
try {
serialPort = portId.open("wr3223", 2000);
} catch (PortInUseException e) {
throw new IOException("Serial port '" + port + "' is already in use.", e);
}
try {
// set port parameters
serialPort.setSerialPortParams(baud, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
} catch (UnsupportedCommOperationException e) {
throw new IOException("Serial port '" + port + "' doesn't support the configuration 7 data bit, 1 stop bit and parity even.", e);
}
if (!serialPort.isReceiveTimeoutEnabled()) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Add a receive timeout of 2000ms.");
}
serialPort.enableReceiveTimeout(2000);
} catch (UnsupportedCommOperationException e) {
logger.warn("Error by adding receive timeout.", e);
}
}
DataInputStream inputStream = new DataInputStream(serialPort.getInputStream());
DataOutputStream outputStream = new DataOutputStream(serialPort.getOutputStream());
connect(inputStream, outputStream);
} else {
StringBuilder sb = new StringBuilder();
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
sb.append(id.getName() + "\n");
}
}
throw new IOException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
}
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class UPBBinding method openSerialPort.
private SerialPort openSerialPort() {
SerialPort serialPort = null;
CommPortIdentifier portId;
try {
portId = CommPortIdentifier.getPortIdentifier(port);
} catch (NoSuchPortException e1) {
throw new RuntimeException("Port does not exist", e1);
}
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(port)) {
try {
serialPort = portId.open("UPB", 1000);
} catch (PortInUseException e) {
throw new RuntimeException("Port is in use", e);
}
try {
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.enableReceiveTimeout(100);
} catch (UnsupportedCommOperationException e) {
throw new RuntimeException("Failed to configure serial port");
}
}
}
return serialPort;
}
use of gnu.io.CommPortIdentifier in project JMRI by JMRI.
the class SerialDriverAdapter method openPort.
@Override
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 CMRI 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 {
activeSerialPort.enableReceiveFraming(0x03);
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 (Exception 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());
return;
}
}
});
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 (Exception 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;
}
use of gnu.io.CommPortIdentifier in project JMRI by JMRI.
the class XBeeAdapter method openPort.
@Override
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();
}
// 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"));
}
opened = true;
} catch (gnu.io.NoSuchPortException p) {
return handlePortNotFound(p, portName, log);
} catch (Exception 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