public boolean connect(String address, int baudRate) { serialPort = new SerialPort(address); connected = false; try { int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR; serialPort.openPort(); serialPort.setParams(baudRate, 8, 1, 0); serialPort.setEventsMask(mask);// Set mask // serialPort.addEventListener(this); connected = true; } catch (SerialPortException ex) { System.err.println(ex); } return connected; }
@Override public void closePort() throws Exception { if (this.serialPort != null) { try { this.serialPort.removeEventListener(); if (this.serialPort.isOpened()) { this.serialPort.closePort(); } } finally { this.serialPort = null; } } }
/** * 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); } } }
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(); }
@Override public void start() throws IOException { if (!port.isOpened()) { try { port.openPort(); port.setParams( SerialPort.BAUDRATE_57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); port.setEventsMask(SerialPort.MASK_RXCHAR); port.addEventListener(this); } catch (SerialPortException ex) { throw new IOException("Cannot start firmata device", ex); } } }
public static void main(String[] args) { char ESC = (char) 27; // Ascii character for Escape char LN = (char) 10; String message = "TX ENROLL:0 PGX:0 PGY:0 ALARM:0 BEEP:NONE"; String cmd = ESC + message + LN; SerialPort serialPort = new SerialPort("/dev/ttyUSB0"); try { serialPort.openPort(); serialPort.setParams(SerialPort.BAUDRATE_57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); serialPort.writeString(cmd); serialPort.closePort(); } catch (SerialPortException ex) { System.out.println(ex); } }
@Override protected void openImpl() throws SerialPortException { try { if (LOG.isDebugEnabled()) LOG.debug("Opening Serial Port: " + commPortId); this.port = new SerialPort(commPortId.getName()); this.port.openPort(); this.port.setFlowControlMode(flowControlIn | flowControlOut); this.port.setParams(baudRate, dataBits, stopBits,parity); this.is = new JsscSerialPortInputStream(this.port, this.listeners); this.os = new JsscSerialPortOutputStream(this.port); } catch (jssc.SerialPortException e) { throw new SerialPortException(e); } }
public SerialConnection(String port, InputHandler ih) throws SerialPortException { this.serialPort = new SerialPort(port); this.serialPort.openPort(); this.serialPort.setParams(baudrate, databits, stopbits, parity); } catch (SerialPortException e) { logger.error(String.format("Failed to open serial port %s \n %s", port, e.getMessage()));
public SerialTransport(String portName) { this.port = new SerialPort(portName); }
@Override public void closeConnection() { if (portOpened) { if (serialPort != null) { try { serialPort.removeEventListener(); serialPort.closePort(); } catch (SerialPortException e) { } } portOpened = false; } }
@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 disconnect() { try { _serialPort.closePort(); _config.setStatus(STATE.DOWN, "Stopped."); _logger.debug("_serialPort{} closed", _serialPort.getPortName()); } catch (SerialPortException ex) { if (ex.getMessage().contains("Port not opened")) { _logger.debug("unable to close the port, Error: {}", ex.getMessage()); } else { _logger.error("unable to close the port{}", _serialPort.getPortName(), ex); } _config.setStatus(STATE.DOWN, "ERROR:" + ex.getMessage()); } }
@Override public void write(byte[] bytes) throws IOException { try { port.writeBytes(bytes); } catch (SerialPortException ex) { throw new IOException("Cannot send message to device", ex); } }
SerialPort Puerto = new SerialPort(); string buffer = String.Empty; Thread DemoThread; CloseSerial(); OpenSerial(); Puerto.Write(argumento + "\r\n"); CloseSerial(); OpenSerial(); Puerto.Write("Inicio"); Puerto.Write(Input); buffer += sp.ReadExisting(); this.DemoThread = new Thread(new ThreadStart(ThreadProcSafe)); this.DemoThread.Start(); Puerto.Open(); Puerto.Close();
public boolean disconnect() { try { serialPort.closePort(); connected = false; } catch (SerialPortException e) { e.printStackTrace(); } return !connected; }
/** * @param serialPort * @throws SerialPortException */ public JsscSerialPortInputStream(SerialPort serialPort, List<SerialPortProxyEventListener> listeners) throws jssc.SerialPortException { this.listeners = listeners; this.dataStream = new LinkedBlockingQueue<Byte>(); this.port = serialPort; this.port.addEventListener(this, SerialPort.MASK_RXCHAR); //Setup a bounded Pool that will execute the listener tasks in Order this.listenerTasks = new ArrayBlockingQueue<SerialPortProxyEventTask>(this.maxPoolSize); if(LOG.isDebugEnabled()) LOG.debug("Creating Jssc Serial Port Input Stream for: " + serialPort.getPortName()); }
@Override public void write(int arg0) throws IOException { try { byte b = (byte) arg0; if (LOG.isDebugEnabled()) LOG.debug("Writing byte: " + String.format("%02x", b) ); if ((port != null) && (port.isOpened())) { port.writeByte(b); } } catch (jssc.SerialPortException e) { throw new IOException(e); } }
@Override public boolean isOpen() { return serialPort != null && serialPort.isOpened(); }