public void serialEvent(SerialPortEvent spe) { if (spe.getEventType() == SerialPortEvent.DATA_AVAILABLE) { StringBuilder line = new StringBuilder(); try { while (isr.ready()) { if (hexi < 0) { line.append((char) isr.read()); } else { int value = isr.read(); if (value >= 0) { line.append(String.format("%02X ", (value & 0xFF))); if (++hexi % 0x10 == 0) { line.append("\n"); } } } } appendString(line.toString()); } catch (IOException ex) { Logger.getLogger(HyperTerminal.class.getName()).log(Level.WARNING, null, ex); } } updateFlags(); }
@Override public void serialEvent(SerialPortEvent e) { int type = e.getEventType(); switch (type) { case SerialPortEvent.DATA_AVAILABLE: try { buffer.readData(is); byte[] newline; while ((newline = buffer.getNextLine()) != null) processData(newline); } catch (IOException ex) { log.log(Level.WARNING, "serial port read error: " + ex, ex); } break; default: log.log(Level.INFO, "Recieved serialEvent {0}", type); break; } }
@Override public void serialEvent(SerialPortEvent event) { if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { synchronized (bufferSynchronisationObject) { int recv; while ((recv = inputStream.read()) != -1) { buffer[end++] = recv; if (end >= maxLength) { end = 0; } } } } catch (IOException e) { } synchronized (this) { this.notify(); } } }
/** * This Method is called when Serialdata is recieved */ public synchronized void serialEvent (SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { int available = input.available(); for(int i=0;i<available;i++){ int receivedVal=input.read(); if(receivedVal!=10 && receivedVal!=13){ inputBuffer+=(char)receivedVal; }else if(receivedVal==10){ te.log("ARDUINO:-> "+inputBuffer); inputBuffer=""; } } } catch (Exception e) { System.err.println(e.toString()); } } }
/** * Handle an event on the serial port. Read the data and print it. */ @Override public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine = input.readLine(); System.out.println(inputLine); } catch (Exception e) { System.err.println(e.toString()); } } // Ignore all the other eventTypes, but you should consider the other ones. }
public void serialEvent(SerialPortEvent arg0) { int data; try { int len = 0; while ( ( data = inStream.read()) > -1 ) { if ( data == '\n' ) { break; } if (data >= 48 && data <= 57 ){buffer[len++] = (byte) data;} } inputData = Integer.parseInt(new String(buffer,0,len)); inputWaiting = true; } catch ( IOException e ) { e.printStackTrace(); } }
@Override public void serialEvent(SerialPortEvent event) { if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { synchronized (serialPort) { try { if (br == null) { logger.warn("BufferedReader for serial connection is null"); } else { String line = br.readLine(); logger.trace("Serial event: received '{}'", line); processNextLine(line); } } catch (IOException e) { logger.warn("Can't read from serial device {}", config.getDeviceName(), e); tryReopenHardware(); } } } }
/** * Handle an event on the serial port. Read the data and print it. */ public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine=input.readLine(); if (inputLine.length() > 0) { for(SerialListener s : listeners) { s.readPerformed(inputLine); } } } catch (Exception e) { System.err.println(e.toString()); } } // Ignore all the other eventTypes, but you should consider the other ones. }
/** * Handles SerialPortEvents. The two types of SerialPortEvents that this program is registered * to listen for are DATA_AVAILABLE and BI. During DATA_AVAILABLE the port buffer is read until * it is drained, when no more data is available and 30ms has passed the method returns. When a * BI event occurs the words BREAK RECEIVED are written to the messageAreaIn. */ public void serialEvent(final SerialPortEvent e){ if (adjustEndTime) { endTime = System.currentTimeMillis() + (timeout * 1000); } if (e.getEventType() == SerialPortEvent.BI) { breakInterrupt(state); } else { try { serialEvent(this.state, is, e); } catch (Exception ex) { ExHandler.handle(ex); } } }
/** * Handles serial event. */ public void serialEvent(final int state, final InputStream inputStream, final SerialPortEvent e) throws IOException{ switch (e.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: dataAvailable(inputStream); break; } }
@Override public synchronized void serialEvent(SerialPortEvent serialPortEvent) { if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { if (input.ready()) { processData(input.readLine()); } } catch (Exception e) { log.error("Error ", e); } } }
@Override public void serialEvent(SerialPortEvent event) { if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) fireDataAvailable(); else System.out.println("other event"); }
/** * Handle an event on the serial port. Read the data and print it. */ public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine = input.readLine(); log.info("Reçoit : " + inputLine); } catch (Exception e) { log.error("error 2 " + e.toString()); } } // Ignore all the other eventTypes, but you should consider the other ones. }
@Override public void serialEvent(SerialPortEvent spe) { // we're only interested in streamed data if (spe.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { if (synced) { // either synced data (messages), or... continueMessage(); } else { // ...some unsynced characters; see setSynced() while(true) { int i= saveRead(); if (i == -1) { break; } listener.unsyncedChar((char) i); } } } catch (IOException ex) { listener.streamingError(StreamCommandListener.STREAMING_IO_EXCEPTION, "IOException while receiving (see log) : " + ex); } } }
@Override public void serialEvent(SerialPortEvent event) { if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { synchronized (inputStream) { inputStream.notifyAll(); } } }
/** * Handle an event on the serial port. Read the data and print it. */ public synchronized void serialEvent(SerialPortEvent oEvent) { //System.out.println("Serial event successfully triggered"); if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine = null; if(input.ready()){ inputLine=input.readLine(); System.out.println(inputLine); //StringTokenizer to read an entire line of tata. It is expected that successive //reads are in successive lines. StringTokenizer stringTokenizer = new StringTokenizer(inputLine,","); String arduinoID = stringTokenizer.nextToken(); statement.setString(1,arduinoID); System.out.println(arduinoID); String humidity = stringTokenizer.nextToken(); System.out.println(humidity); statement.setString(2,humidity); String temperature = stringTokenizer.nextToken(); System.out.println(temperature); statement.setString(3,temperature); statement.executeUpdate(); } } catch (Exception e) { System.err.println(e.toString()); } } // Ignore all the other eventTypes, but you should consider the other ones. }
/** * rxtxlib's "serial event handling" - would be more simple if they just * implemented InputStream correctly :P */ @Override public void serialEvent(SerialPortEvent event) { log.info(String.format("rxtx event on port %s", portName)); Integer newByte = -1; try { while (listening && ((newByte = read()) > -1)) { // listener.onByte(newByte); // <-- FIXME ?? onMsg() < ??? for (String key : listeners.keySet()) { listeners.get(key).onByte(newByte); } ++stats.total; if (stats.total % stats.interval == 0) { stats.ts = System.currentTimeMillis(); log.error(String.format("===stats - dequeued total %d - %d bytes in %d ms %d Kbps", stats.total, stats.interval, stats.ts - stats.lastTS, 8 * stats.interval / (stats.ts - stats.lastTS))); // publishQueueStats(stats); stats.lastTS = stats.ts; } // log.info(String.format("%d",newByte)); // rxtx leave whenever it has no new data to delver with a -1 // which is not what an Java InputStream is supposed to do.. } log.info(String.format("%d",newByte)); } catch (Exception e) { ++rxErrors; Logging.logError(e); } }
@Override public void serialEvent(SerialPortEvent spe) { if (SerialPortEvent.DATA_AVAILABLE == spe.getEventType()) { // avoid the high frequency of the notifying // just check the read thread first // and then choose to restart or ignore the notity if((null != serialReadThread) && serialReadThread.isAlive()){ // do nothing here // or you can close the notify here // and open when the thread is done serialPort.notifyOnDataAvailable(false); }else{ // you must re-run the thread // here you need only one method but using thread.start() with two // same thread // here you can use start, because we already know that the // thread is dead // here you can't use the start method again // the data is already exist, // so just use run to make it run again //srialReadThread.start(); // you can never restart a thread again serialReadThread = new Thread(new SerialReader(serialIn)); // start it serialReadThread.start(); } }// end if }
public void serialEvent(SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: try { BufferedReader br = new BufferedReader(new InputStreamReader(inputStream), 32*1024*1024); if(br.ready()) { String line = br.readLine(); line = StringUtils.chomp(line); line = line.replace(",","."); response = line.trim(); } } catch (IOException e) { logger.debug("Error receiving data on serial port {}: {}", new Object[] { port, e.getMessage() }); } break; } }
@Override public void serialEvent(SerialPortEvent arg0) { try { logger.trace("RXTX library CPU load workaround, sleep forever"); Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException e) { } }
/** * {@inheritDoc} * * Any data received from a serial event, is immediately passed on to the * command receiver thread. */ @Override public void serialEvent(SerialPortEvent event) { if (event.getEventType() != SerialPortEvent.DATA_AVAILABLE) { return; } InputStream is = null; try { lastEventTimestamp = System.currentTimeMillis(); is = port.getInputStream(); int available = is.available(); if (available == 0) { log.warn("Received data available event, but no data was found!"); return; } byte[] buffer = new byte[available]; is.read(buffer, 0, available); bufferQueue.add(buffer); if (available != 1 || buffer[0] != 13) { // don't print single CR's in log.. log.trace("Received: {}", new String(buffer)); } } catch (IOException e) { log.error("Error receiving data on serial port {}: {}", port.getName(), e.getMessage()); } }
@Override public void serialEvent(SerialPortEvent arg0) { try { logger.trace("RXTX library CPU load workaround, sleep forever"); sleep(Long.MAX_VALUE); } catch (InterruptedException e) { } }
/** * Receives Serial Port Events and reads Serial Port Data */ public synchronized void serialEvent(SerialPortEvent serialPortEvent) { if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String messageLine=serialInput.readLine(); handleIncomingMessage(messageLine); } catch (IOException ioException) { logger.error("serialEvent(): IO Exception: ", ioException); } } }
@Override public void serialEvent(SerialPortEvent event) { if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String data = bReader.readLine(); logger.trace("Received raw message to process: '{}'", data); notifyEventProcessors(data); } catch (IOException e) { logger.error("Exception while reading from serial port", e); } } }
@Override public void serialEvent(SerialPortEvent event) { if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { processNextLine(); } catch (CULCommunicationException e) { log.error("Serial CUL connection read failed for " + deviceName); } } }
public void serialEvent(SerialPortEvent evt) { // if data available on serial port if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String inputLine = input.readLine(); System.out.println("Recieved: " + inputLine); if (inputLine.equals("FINISHED")) System.out.println("ACCORDING TO THE ARDUINO, ARM MOVEMENT IS COMPLETE"); } catch (Exception e) { System.err.println(e.toString()); } } }
public void serialEvent(SerialPortEvent event) { // byte[] readBuffer = new byte[2000]; try { while (inStream.available() > 0) { // inStream.read(readBuffer); SerialMonitor.display(String.valueOf((char) inStream.read())); } // SerialMonitor.display(new String(readBuffer)); } catch (IOException ioe) { System.out.println("Exception " + ioe); ioe.printStackTrace(); } }
public void serialEvent(SerialPortEvent portEvent) { // Determine type of event switch (portEvent.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: int iData = 0; // Read data until -1 is returned while (iData != -1) { try { iData = fInStream.read(); if (iData != -1) { fDataListener.dataArrived(iData & 0xff); } } catch (IOException ex) { System.out.println(ex); return; } } break; } }
protected void handleSerial(SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[32]; try { int numBytes = getInputStream().read(readBuffer); for (int i = 0; i < numBytes; i++) { //System.out.println("read " + (char) readBuffer[i]); // don't add lf/cr chars if ((readBuffer[i] != 10 && readBuffer[i] != 13)) { stringBuilder.append((char) readBuffer[i]); } // got a new line if ((int)readBuffer[i] == 10) { handleSerialReply(stringBuilder.toString()); stringBuilder = new StringBuilder(); } } } catch (Exception e) { log.error("Serial error", e); } break; } }
public void serialEvent(SerialPortEvent event) { try { handleSerial(event); } catch (Throwable t) { t.printStackTrace(); } }
protected void handleSerial(SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: // we get here if data has been received byte[] readBuffer = new byte[20]; try { // read data int numBytes = inputStream.read(readBuffer); for (int i = 0; i < numBytes; i++) { //System.out.println("read " + (char) readBuffer[i]); if ((readBuffer[i] != 10 && readBuffer[i] != 13)) { strBuf.append((char) readBuffer[i]); } //carriage return if ((int)readBuffer[i] == 10) { System.out.println("Arduino:<-" + strBuf.toString()); if (strBuf.toString().equals("ok")) { synchronized (pageAck) { pageAck.notify(); } } strBuf = new StringBuffer(); } } } catch (Exception e) { throw new RuntimeException("serialEvent error ", e); } break; } }
/** * Event notification must be enabled for every event in setSerialPort() above. * We can't call displayMessage.display() from this method, as we are not in FX thread. * * @param event */ @Override public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.FE: case SerialPortEvent.OE: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: case SerialPortEvent.PE: case SerialPortEvent.RI: break; case SerialPortEvent.DATA_AVAILABLE: short[] payload; int b; while (true) { try { if (in.available() == 0) { // No more data available. break; } b = in.read(); } catch (IOException e) { displayMessage.displayLogLater("error on receive: " + e.getMessage()); break; } payload = frameHandler.frameAssembler(b); if (payload != null) { displayMessage.displayFrameLater(frameHandler.displayFrame(payload)); } } break; default: displayMessage.displayLogLater("FrameHandler.serialEvent() - unknown event type: " + event.getEventType()); } }
/** * Serial port event listener. * @param event an event */ public void serialEvent(final SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: // we get here if data has been received String line = ""; try { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); // read data while (inputStream.available() > 0) { while (reader.ready()) { line = reader.readLine(); } } LOG.finer("Read: " + line); if (!gpsFound) { gpsFound = isGpsStream(line); } } catch (IOException e) { LOG.log(Level.FINE, "IOException in serial-port -event", e); } break; default: break; } }
@Override public void serialEvent(SerialPortEvent arg0) { try { /* * The short select() timeout in the native code of the nrjavaserial lib does cause a high CPU load, despite * the fix published (see https://github.com/NeuronRobotics/nrjavaserial/issues/22). A workaround for this * problem is to (1) put the Thread initiated by the nrjavaserial library to sleep forever, so that the * number of calls to the select() function gets minimized, and (2) implement a Threaded streamreader * directly in java */ logger.trace("RXTX library CPU load workaround, sleep forever"); Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException e) { } }
@Override public void serialEvent(SerialPortEvent arg0) { try { /* * See more details from * https://github.com/NeuronRobotics/nrjavaserial/issues/22 */ logger.trace("RXTX library CPU load workaround, sleep forever"); Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignore) { } }
@Override public void serialEvent(SerialPortEvent ev) { switch (ev.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: try { if (!inputReader.ready()) { logger.debug("Serial Data Available but input reader not ready"); return; } String message = inputReader.readLine(); logger.debug("Msg Received: {}", message); RadioRAFeedback feedback = parser.parse(message); if (feedback != null) { logger.debug("Msg Parsed as {}", feedback.getClass().getName()); listener.handleRadioRAFeedback(feedback); } logger.debug("Finished handling feedback"); } catch (IOException e) { logger.debug("IOException occurred", e); } break; default: logger.debug("Unhandled SerialPortEvent raised [{}]", ev.getEventType()); break; } }
/** * Receives Serial Port Events and reads Serial Port Data. * * @param serialPortEvent */ @Override public synchronized void serialEvent(SerialPortEvent serialPortEvent) { if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { String messageLine = serialInput.readLine(); handleIncomingMessage(messageLine); } catch (IOException ioException) { logger.error("serialEvent(): IO Exception: {}", ioException.getMessage()); } } }