use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class DSMRPort method portExists.
/**
* Checks if the given port name is autodetected by gnu.io or already listed
* in the system property gnu.io.rxtx.SerialPorts
*
* @param portName String containing the port name to lookup
* @return true if port exists, false otherwise
*/
private boolean portExists(String portName) {
@SuppressWarnings("unchecked") Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
boolean portExists = false;
logger.debug("Searching autodetected ports for: {}", portName);
while (portEnum.hasMoreElements()) {
CommPortIdentifier portIdentifier = portEnum.nextElement();
if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
logger.debug("Found serial port: {}", portIdentifier.getName());
if (portIdentifier.getName().equals(portName)) {
portExists = true;
}
}
}
if (!portExists) {
Properties properties = System.getProperties();
String currentPorts = properties.getProperty("gnu.io.rxtx.SerialPorts", "");
if (currentPorts.indexOf(portName) >= 0) {
logger.debug("{} is listed in system property gnu.io.rxtx.SerialPorts", portName);
portExists = true;
} else {
logger.debug("{} is not listed in system property gnu.io.rxtx.SerialPorts", portName);
}
}
return portExists;
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class Connection method open.
/**
* Opens the serial port associated with this connection.
*
* @throws IOException
* if any kind of error occurs opening the serial port.
*/
public void open() throws IOException {
CommPortIdentifier portIdentifier;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
} catch (NoSuchPortException e) {
throw new IOException("Serial port with given name does not exist", e);
}
if (portIdentifier.isCurrentlyOwned()) {
throw new IOException("Serial port is currently in use.");
}
// fixed issue as rxtx library originally used in j62056 does use
// different version of rxtx
// com port in their version is using gnu.io.CommPort
RXTXPort commPort;
try {
commPort = portIdentifier.open(this.getClass().getName(), 2000);
} catch (PortInUseException e) {
throw new IOException("Serial port is currently in use.", e);
}
if (!(commPort instanceof SerialPort)) {
commPort.close();
throw new IOException("The specified CommPort is not a serial port");
}
serialPort = commPort;
try {
os = new DataOutputStream(serialPort.getOutputStream());
is = new DataInputStream(serialPort.getInputStream());
} catch (IOException e) {
serialPort.close();
serialPort = null;
throw new IOException("Error getting input or output or input stream from serial port", e);
}
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class SerialIOStream method updateSerialProperties.
private void updateSerialProperties(String devName) {
/*
* By default, RXTX searches only devices /dev/ttyS* and
* /dev/ttyUSB*, and will therefore not find devices that
* have been symlinked. Adding them however is tricky, see below.
*/
//
// first go through the port identifiers to find any that are not in
// "gnu.io.rxtx.SerialPorts"
//
ArrayList<String> allPorts = new ArrayList<String>();
@SuppressWarnings("rawtypes") Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
allPorts.add(id.getName());
}
}
logger.trace("ports found from identifiers: {}", StringUtils.join(allPorts, ":"));
//
if (!allPorts.contains(devName)) {
allPorts.add(devName);
}
//
// add any that are already in "gnu.io.rxtx.SerialPorts"
// so we don't accidentally overwrite some of those ports
String ports = System.getProperty("gnu.io.rxtx.SerialPorts");
if (ports != null) {
ArrayList<String> propPorts = new ArrayList<String>(Arrays.asList(ports.split(":")));
for (String p : propPorts) {
if (!allPorts.contains(p)) {
allPorts.add(p);
}
}
}
String finalPorts = StringUtils.join(allPorts, ":");
logger.trace("final port list: {}", finalPorts);
//
// Finally overwrite the "gnu.io.rxtx.SerialPorts" System property.
//
// Note: calling setProperty() is not threadsafe. All bindings run in
// the same address space, System.setProperty() is globally visible
// to all bindings.
// This means if multiple bindings use the serial port there is a
// race condition where two bindings could be changing the properties
// at the same time
//
System.setProperty("gnu.io.rxtx.SerialPorts", finalPorts);
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class SerialIOStream method open.
@Override
public boolean open() {
try {
updateSerialProperties(m_devName);
CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_devName);
CommPort cp = ci.open(m_appName, 1000);
if (cp instanceof SerialPort) {
m_port = (SerialPort) cp;
} else {
throw new IllegalStateException("unknown port type");
}
m_port.setSerialPortParams(m_speed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
m_port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
logger.debug("setting port speed to {}", m_speed);
m_port.disableReceiveFraming();
m_port.enableReceiveThreshold(1);
// m_port.disableReceiveTimeout();
m_port.enableReceiveTimeout(1000);
m_in = m_port.getInputStream();
m_out = m_port.getOutputStream();
logger.info("successfully opened port {}", m_devName);
return true;
} catch (IOException e) {
logger.error("cannot open port: {}, got IOException ", m_devName, e);
} catch (PortInUseException e) {
logger.error("cannot open port: {}, it is in use!", m_devName);
} catch (UnsupportedCommOperationException e) {
logger.error("got unsupported operation {} on port {}", e.getMessage(), m_devName);
} catch (NoSuchPortException e) {
logger.error("got no such port for {}", m_devName);
} catch (IllegalStateException e) {
logger.error("got unknown port type for {}", m_devName);
}
return false;
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class EiscpSerial method connect.
/**
* {@inheritDoc}
*/
public boolean connect(String serialPortName) {
try {
logger.debug("Open connection to serial port '{}'", serialPortName);
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
out.flush();
if (in.markSupported()) {
in.reset();
}
// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event
// loop
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
return true;
} catch (Exception e) {
logger.error("serial port connect error", e);
e.printStackTrace();
return false;
}
}
Aggregations