use of gnu.io.SerialPort 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.SerialPort in project marine-api by ktuukkan.
the class SerialPortExample method init.
/**
* Init serial port and reader.
*/
private void init() {
try {
SerialPort sp = getSerialPort();
if (sp != null) {
InputStream is = sp.getInputStream();
SentenceReader sr = new SentenceReader(is);
sr.addSentenceListener(this);
sr.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of gnu.io.SerialPort 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.SerialPort 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.SerialPort 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;
}
Aggregations