sport.addEventListener(new SerialReader(in)); sport.notifyOnDataAvailable(true);
@Override public void addEventListener(SerialPortEventListener listener) throws TooManyListenersException { sp.addEventListener(new gnu.io.SerialPortEventListener() { @Override public void serialEvent(final @Nullable SerialPortEvent event) { if (event == null) { return; } listener.serialEvent(new SerialPortEventImpl(event)); } }); }
@Override public void addEventListener(SerialPortEventListener listener) throws TooManyListenersException { sp.addEventListener(new gnu.io.SerialPortEventListener() { @Override public void serialEvent(final @Nullable SerialPortEvent event) { if (event == null) { return; } listener.serialEvent(new SerialPortEventImpl(event)); } }); }
@Override public void addEventListener( final SerialPortDataAvailableEventListener listener ) throws TooManyListenersException { port.addEventListener( new SerialPortEventListener() { @Override public void serialEvent( final SerialPortEvent serialPortEvent ) { listener.serialPortDataAvailable( new SerialPortDataAvailableEvent( serialPortEvent.getSource() ) ); } } ); port.notifyOnDataAvailable( true ); }
private void initSerialPortEventListener() throws IOException { try { port.addEventListener(new SerialPortEventListenerImpl()); port.notifyOnDataAvailable(true); } catch (TooManyListenersException e) { throw new IOException(e); } }
private void initSerialPortEventListener() throws IOException { try { port.addEventListener(new SerialPortEventListenerImpl()); port.notifyOnDataAvailable(true); } catch (TooManyListenersException e) { throw new IOException(e); } }
public void addEventListener ( SerialPortEventListener listener ) { try { sPort.addEventListener( listener ); } catch ( TooManyListenersException e ) { sPort.close( ); logger.warn( e.getMessage( ) , e ); } }
// Each of these could be object members so all your class methods have access OutputStream outputStream; InputStream inputStream; SerialPort serialPort; // Open the port one time, then init your settings serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000); serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // Get the input/output streams for use in the application outputStream = serialPort.getOutputStream(); inputStream = serialPort.getInputStream(); // Finally, add your event listener serialPort.addEventListener(this);
} catch (IOException e) {System.out.println(e);} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) { System.out.println(e);
serialPort.addEventListener(new SerialPortEventListener() { @Override public void serialEvent(SerialPortEvent ev) {
void connect(String portName, SerialConfiguration conf) { registerPort(portName); try { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if (portIdentifier.isCurrentlyOwned()) { System.err.println("Error: Port " + portName + " is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000); if (commPort instanceof SerialPort) { serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); in = serialPort.getInputStream(); out = serialPort.getOutputStream(); serialPort.addEventListener(new SerialReader()); serialPort.notifyOnDataAvailable(true); } else { System.err.println("Error: Port " + portName + " is not a valid serial port."); } } } catch (Exception e) { e.printStackTrace(); } }
serialPort.setParams(DATA_RATE, 8, 1, 0); serialPort.addEventListener(new SerialPortReaderListener()); } catch (SerialPortException ex) { System.out.println(ex);
serialPort.addEventListener(this.receiveThread); serialPort.notifyOnDataAvailable(true);
@Override protected boolean connect() throws IOException { try { final CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port); if(portIdentifier != null) { serialPort = (SerialPort) portIdentifier.open("org.openhab.binding.ebus", 2000); serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); // set timeout 10 sec. serialPort.disableReceiveThreshold(); serialPort.enableReceiveTimeout(10000); // use event to let readByte wait until data is available, optimize cpu usage serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); outputStream = serialPort.getOutputStream(); inputStream = serialPort.getInputStream(); return super.connect(); } } catch (NoSuchPortException e) { logger.error("Unable to connect to serial port {}", port); } catch (PortInUseException e) { logger.error("Serial port {} is already in use", port); } catch (UnsupportedCommOperationException e) { logger.error(e.toString(), e); } catch (TooManyListenersException e) { logger.error("Too many listeners error!", e); } serialPort = null; return false; }
output = port.getOutputStream(); port.setSerialPortParams(rate, databits, stopbits, parity); port.addEventListener(this); port.notifyOnDataAvailable(true);
serialPort.setParams(2400,7,2,1, false, true); serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR); serialPort.writeString("connected.");
serialPort.addEventListener(this); } catch (TooManyListenersException e) { throw new InitializationException(e);
serialPort.addEventListener(receiveThread); serialPort.notifyOnDataAvailable(true);
public class YourClass implements SerialPortListener{ private SerialPort serialPort; // ... serial port gets set up at some point ... public void registerEvents(){ serialPort.addEventListener(this); // listen to all the events serialPort.notifyOnBreakInterrupt(true); serialPort.notifyOnCarrierDetect(true); serialPort.notifyOnCTS(true); serialPort.notifyOnDataAvailable(true); serialPort.notifyOnDSR(true); serialPort.notifyOnFramingError(true); serialPort.notifyOnOutputEmpty(true); serialPort.notifyOnOverrunError(true); serialPort.notifyOnParityError(true); serialPort.notifyOnRingIndicator(true); } @Override public void serialEvent(SerialPortEvent event) { System.out.println("Received event. Type: " + event.getEventType() + ", old value: " + event.getOldValue() + ", new value: " + event.getNewValue()); } }