private void updateWrappedPort() throws IOException { try { this.rxtxPort.setSerialPortParams(this.baudRate, this.dataBits.getOldValue(), this.stopBits.getOldValue(), this.parity.getOldValue()); } catch (UnsupportedCommOperationException e) { throw new IOException(e.getMessage()); } }
@Override public void setPortParameters(int baudRate, int dataBits, int stopBits, int parity, int flowControl) throws InvalidConfigurationException, ConnectionException { parameters = new SerialPortParameters(baudRate, dataBits, stopBits, parity, flowControl); if (serialPort != null) { try { serialPort.setSerialPortParams(baudRate, dataBits, stopBits, parity); serialPort.setFlowControlMode(flowControl); } catch (UnsupportedCommOperationException e) { throw new InvalidConfigurationException(e.getMessage(), e); } } }
public static JRxTxPort openSerialPort(String portName, int baudRate, Parity parity, DataBits dataBits, StopBits stopBits, FlowControl flowControl) throws SerialPortException { try { System.setProperty("gnu.io.rxtx.SerialPorts", portName); CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); String theOwner = JRxTxPort.class.getCanonicalName() + System.currentTimeMillis(); CommPort comPort = portIdentifier.open(theOwner, 0); if (!(comPort instanceof RXTXPort)) { throw new SerialPortException("Unable to open the serial port. Port is not RXTX."); } RXTXPort rxtxPort = (RXTXPort) comPort; try { rxtxPort.setSerialPortParams(baudRate, dataBits.getOldValue(), stopBits.getOldValue(), parity.getOldValue()); setFlowControl(flowControl, rxtxPort); } catch (UnsupportedCommOperationException e) { String message = format("Unable to apply config on serial port.\n{0}", e.getMessage()); throw new SerialPortException(message); } return new JRxTxPort(rxtxPort, portName, baudRate, parity, dataBits, stopBits, flowControl); } catch (NoSuchPortException e) { String errMessage = format("Serial port {0} not found or port is busy.", portName); throw new PortNotFoundException(errMessage); } catch (PortInUseException e) { String errMessage = format("Serial port {0} is already in use.", portName); throw new PortNotFoundException(errMessage); } }
serial.setSerialPortParams(getBaud(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); setConnected(true);
public boolean connect() { if (isConnected()) { System.err.println(port + " is already connected."); return true; } try { serial = new RxTxPortCreator().createPort(port); serial.setSerialPortParams(getBaud(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); setConnected(true); } catch (NativeResourceException e) { throw new NativeResourceException(e.getMessage()); } catch (Exception e) { System.err.println("Failed to connect on port: " + port + " exception: "); e.printStackTrace(); setConnected(false); } if (isConnected()) { serial.notifyOnDataAvailable(true); } return isConnected(); }
serialPort.setSerialPortParams(baudRate, parameters.dataBits, parameters.stopBits, parameters.parity); serialPort.setFlowControlMode(parameters.flowControl);