use of gnu.io.CommPortIdentifier in project marine-api by ktuukkan.
the class SerialPortExample method getSerialPort.
/**
* Scan serial ports for NMEA data.
*
* @return SerialPort from which NMEA data was found, or null if data was
* not found in any of the ports.
*/
private SerialPort getSerialPort() {
try {
Enumeration<?> e = CommPortIdentifier.getPortIdentifiers();
while (e.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) e.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
SerialPort sp = (SerialPort) id.open("SerialExample", 30);
sp.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream is = sp.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buf = new BufferedReader(isr);
System.out.println("Scanning port " + sp.getName());
// try each port few times before giving up
for (int i = 0; i < 5; i++) {
try {
String data = buf.readLine();
if (SentenceValidator.isValid(data)) {
System.out.println("NMEA data found!");
return sp;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
is.close();
isr.close();
buf.close();
}
}
System.out.println("NMEA data was not found..");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of gnu.io.CommPortIdentifier in project jop by jop-devel.
the class JavaDown method run.
public void run(IProgressMonitor monitor) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, FileNotFoundException, IOException {
final CommPortIdentifier commPortId = CommPortIdentifier.getPortIdentifier(this.commPortId);
final SerialPort serialPort = openSerialPort(commPortId);
download(jopFile, serialPort, usb, monitor);
serialPort.close();
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class SerialConnector method open.
/**
* {@inheritDoc}
**/
public void open() {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
serialOutput = new OutputStreamWriter(serialPort.getOutputStream(), "US-ASCII");
serialInput = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
setSerialEventHandler(this);
connected = true;
} catch (NoSuchPortException noSuchPortException) {
logger.error("open(): No Such Port Exception: ", noSuchPortException);
connected = false;
} catch (PortInUseException portInUseException) {
logger.error("open(): Port in Use Exception: ", portInUseException);
connected = false;
} catch (UnsupportedCommOperationException unsupportedCommOperationException) {
logger.error("open(): Unsupported Comm Operation Exception: ", unsupportedCommOperationException);
connected = false;
} catch (UnsupportedEncodingException unsupportedEncodingException) {
logger.error("open(): Unsupported Encoding Exception: ", unsupportedEncodingException);
connected = false;
} catch (IOException ioException) {
logger.error("open(): IO Exception: ", ioException);
connected = false;
}
}
use of gnu.io.CommPortIdentifier in project openhab1-addons by openhab.
the class DavisBinding method openPort.
public void openPort() throws InitializationException {
CommPortIdentifier portIdentifier;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(port);
try {
serialPort = portIdentifier.open("openhab", 3000);
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
serialPort.enableReceiveTimeout(100);
serialPort.enableReceiveThreshold(1);
inputStream = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
outputStream = serialPort.getOutputStream();
logger.debug("port opened: " + port);
} catch (PortInUseException e) {
throw new InitializationException(e);
} catch (UnsupportedCommOperationException e) {
throw new InitializationException(e);
} catch (IOException e) {
throw new InitializationException(e);
}
} catch (NoSuchPortException e) {
StringBuilder sb = new StringBuilder();
Enumeration 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 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;
}
}
Aggregations