use of gnu.io.CommPortIdentifier in project jop by jop-devel.
the class TALWindow method listPortChoices.
private void listPortChoices() {
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
getJComboBox().addItem("null");
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
getJComboBox().addItem(portId.getName());
}
}
// portChoice.select(parameters.getPortName());
}
use of gnu.io.CommPortIdentifier in project jop by jop-devel.
the class RXTXCommUtils method getAvailablePorts.
/**
* @return A HashSet containing the CommPortIdentifier for all ports
* of type <i>commPortIdentifier</i> that are not currently
* being used.
*/
public static Set<CommPortIdentifier> getAvailablePorts(int commPortIdentifier) {
Set<CommPortIdentifier> identifiers = new HashSet<CommPortIdentifier>();
Enumeration<CommPortIdentifier> portIdentifiers = CommPortIdentifier.getPortIdentifiers();
while (portIdentifiers.hasMoreElements()) {
CommPortIdentifier com = portIdentifiers.nextElement();
if (com.getPortType() == commPortIdentifier) {
try {
com.open("CommUtil", 50).close();
identifiers.add(com);
} catch (PortInUseException e) {
} catch (Exception e) {
}
}
}
return identifiers;
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class SerialPortConnector method connectPort.
@Override
protected void connectPort() throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(device);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
setSerialPortParameters(baudrate);
in = serialPort.getInputStream();
out = new DataOutputStream(serialPort.getOutputStream());
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class SerialDevice method initialize.
/**
* Initialize this device and open the serial port
*
* @throws InitializationException if port can not be opened
*/
@SuppressWarnings("rawtypes")
public void initialize() throws InitializationException {
// parse ports and if the default port is found, initialized the reader
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (id.getName().equals(port)) {
logger.debug("Serial port '{}' has been found.", port);
portId = id;
}
}
}
if (portId != null) {
// initialize serial port
try {
serialPort = portId.open("openHAB", 2000);
} catch (PortInUseException e) {
throw new InitializationException(e);
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
throw new InitializationException(e);
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
throw new InitializationException(e);
}
// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);
try {
// set port parameters
serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
throw new InitializationException(e);
}
try {
// get the output stream
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
throw new InitializationException(e);
}
} 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 InitializationException("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 IntRSModule method connect.
@Override
protected CommunicationChannel connect() {
logger.info("Connecting to INT-RS module at {}", this.port);
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(this.port);
SerialPort serialPort = portIdentifier.open("org.openhab.binding.satel", 2000);
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveTimeout(this.getTimeout());
// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event
// loop
serialPort.addEventListener(new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent ev) {
try {
logger.trace("RXTX library CPU load workaround, sleep forever");
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
}
}
});
serialPort.notifyOnDataAvailable(true);
logger.info("INT-RS module connected successfuly");
return new SerialCommunicationChannel(serialPort);
} catch (NoSuchPortException e) {
logger.error("Port {} does not exist", this.port);
} catch (PortInUseException e) {
logger.error("Port {} in use.", this.port);
} catch (UnsupportedCommOperationException e) {
logger.error("Unsupported comm operation on port {}.", this.port);
} catch (TooManyListenersException e) {
logger.error("Too many listeners on port {}.", this.port);
}
return null;
}
Aggregations