/** * Reads data from the serial port. RXTX SerialPortEventListener method. */ @Override public void serialEvent(SerialPortEvent evt) { try { byte[] buf = this.serialPort.readBytes(); if (buf == null || buf.length <= 0) { return; } String s = new String(buf, 0, buf.length); responseMessageHandler.handleResponse(s, comm); } catch ( Exception e ) { e.printStackTrace(); System.exit(-1); } } }
@Override public byte[] readBytes(int i) throws SerialPortException { try { return this.port.readBytes(i); } catch (jssc.SerialPortException e) { throw new SerialPortException(e); } }
@Override public int read() throws IOException { while (true) { try { if (!serialPort.isOpened()) return -1; return serialPort.readBytes(1, 100)[0]; } catch (final SerialPortTimeoutException e) { // ignore and try again } catch (final SerialPortException e) { if (e.getMessage().contains("Port not opened")) return -1; throw new IOException(e); } } } };
@Override public int read() throws IOException { while (true) { try { if (!serialPort.isOpened()) return -1; return serialPort.readBytes(1, 100)[0]; } catch (final SerialPortTimeoutException e) { // ignore and try again } catch (final SerialPortException e) { if (e.getMessage().contains("Port not opened")) return -1; throw new IOException(e); } } } };
@Override public void serialEvent(SerialPortEvent event) { // queueing data from input buffer to processing by FSM logic if (event.isRXCHAR() && event.getEventValue() > 0) { try { parser.parse(port.readBytes()); } catch (SerialPortException ex) { LOGGER.error("Cannot read from device", ex); } } }
@Override public void serialEvent(SerialPortEvent event) { // queueing data from input buffer to processing by FSM logic if (event.isRXCHAR() && event.getEventValue() > 0) { try { parser.parse(port.readBytes()); } catch (SerialPortException ex) { LOGGER.error("Cannot read from device", ex); } } }
SerialPort serialPort = new SerialPort("/dev/ttyUSB0");// For windows "COMX" (e.g : "COM1") should works if (serialPort.openPort()) { serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD); byte[] buffer = serialPort.readBytes(1);// Read one byte String str = new String(buffer); serialPort.closePort(); }
public class Main { public static void main(String[] args) { SerialPort serialPort = new SerialPort("COM1"); try { serialPort.openPort();//Open serial port serialPort.setParams(9600, 8, 1, 0);//Set params. byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port serialPort.closePort();//Close serial port } catch (SerialPortException ex) { System.out.println(ex); } } }
public class Main { public static void main(String[] args) { SerialPort serialPort = new SerialPort("COM1"); try { serialPort.openPort();//Open serial port serialPort.setParams(9600, 8, 1, 0);//Set params. byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port serialPort.closePort();//Close serial port } catch (SerialPortException ex) { System.out.println(ex); } } }
try { len = events.getEventValue(); buffer = serialPort.readBytes(len); } catch (SerialPortException e) {
byte[] buffer = this.port.readBytes(event.getEventValue()); for (int i = 0; i < buffer.length; i++) { this.dataStream.put(buffer[i]);
return; try { tempArray = serialPort.readBytes(); } catch (SerialPortException e) { e.printStackTrace();
if (event.isRXCHAR() && event.getEventValue() > 0) { try { byte[] buffer = _serialPort.readBytes(); for (byte b : buffer) { if ((b == ISerialDriver.MESSAGE_SPLITTER) && rawMessage.length() > 0) {
if (event.isRXCHAR() && event.getEventValue() > 0) { try { byte[] buffer = _serialPort.readBytes(); for (byte b : buffer) { if ((b == ISerialDriver.MESSAGE_SPLITTER) && rawMessage.length() > 0) {