/** * Connect to the rowing computer. * * @param address The serial port, must not be null. * * @throws IOException If connect fails. */ public void connect(RxtxDeviceAddress address) throws IOException { requireNonNull(address); lock.lock(); try { if (isConnected()) throw new IOException("Service is already connected! Can not connect."); Log.debug(LIBRARY, "Opening RXTX channel at '" + address.value() + "' connection."); communicationService.open(address); } finally { lock.unlock(); } }
public static void main(String[] args) throws Exception { EventLoopGroup group = new OioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(RxtxChannel.class) .handler(new ChannelInitializer<RxtxChannel>() { @Override public void initChannel(RxtxChannel ch) throws Exception { ch.pipeline().addLast( new LineBasedFrameDecoder(32768), new StringEncoder(), new StringDecoder(), new RxtxClientHandler() ); } }); ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
public static void main(String[] args) throws Exception { EventLoopGroup group = new OioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(RxtxChannel.class) .handler(new ChannelInitializer<RxtxChannel>() { @Override public void initChannel(RxtxChannel ch) throws Exception { ch.pipeline().addLast( new LineBasedFrameDecoder(32768), new StringEncoder(), new StringDecoder(), new RxtxClientHandler() ); } }); ChannelFuture f = b.connect(new RxtxDeviceAddress("/dev/ttyUSB0")).sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
/** * Connect to the rowing computer. * * @param address The serial port, must not be null. * * @throws IOException If connect fails. */ public void connect(RxtxDeviceAddress address) throws IOException { Log.debug(LIBRARY, "Connecting..."); if (connector.isConnected()) throw new IOException("Already connected! Can not connect again."); connector.connect(requireNonNull(address)); }
/** * Opens the connection to the given serial port. * * @param address The serial port, must not be null. * @throws IOException if opening of the channel fails. */ public void open(RxtxDeviceAddress address) throws IOException { requireNonNull(address); lock.lock(); try { checkIfChannelIsClose(); Log.debug(SERIAL, "Opening channel at serial port '" + address.value() + "'."); ChannelFuture future = bootstrap.connect(address).syncUninterruptibly(); if (!future.isSuccess()) { fireOnError(); throw new IOException("Serial channel couldn't be opened!"); } Log.debug(SERIAL, "Serial channel was successfully opened."); currentChannel = future.channel(); } catch (Exception e) { throw new IOException("Can not connect to '"+address.value()+"'!", e); } finally { lock.unlock(); } }
private void tryNextConnectionAttempt() { lock.lock(); try { if (!isActive.get()) return; // If no ports are available anymore, update list of ports. if (availablePorts.empty()) updateAvailablePorts(); if (availablePorts.empty()) { // Still no serial ports available! Log.warn(AUTO_DISCOVERY, "Currently no serial ports available! Trying again in "+TRY_AGAIN_INTERVAL.getSeconds()+" second(s)..."); executorService.schedule(this::tryNextConnectionAttempt, TRY_AGAIN_INTERVAL.getSeconds(), SECONDS); return; } RxtxDeviceAddress address = availablePorts.pop(); currentSerialPort = address.value(); Log.debug(AUTO_DISCOVERY, "Auto-connecting serial port '"+address.value()+"'."); waterRower.connect(address); } catch (IOException e) { Log.error("Couldn't connect, due to errors! Trying next port.", e); executorService.schedule(this::tryNextConnectionAttempt, TRY_AGAIN_INTERVAL.getSeconds(), SECONDS); } finally { lock.unlock(); } }
private void updateAvailablePorts() { Log.debug(AUTO_DISCOVERY, "Updating list of available serial ports."); Enumeration<CommPortIdentifier> portIdentifiers = CommPortIdentifier.getPortIdentifiers(); while (portIdentifiers.hasMoreElements()) { CommPortIdentifier portIdentifier = portIdentifiers.nextElement(); // Ignore /dev/cu ports. if (portIdentifier.getName().startsWith("/dev/cu.")) continue; // Ignore bluetooth ports. if (portIdentifier.getName().contains("Bluetooth") || portIdentifier.getName().contains("BT")) continue; if (portIdentifier.isCurrentlyOwned()) { Log.warn(AUTO_DISCOVERY, "Skipping serial port '"+portIdentifier.getName()+"', because it is currently owned by another thread or application."); continue; } availablePorts.push(new RxtxDeviceAddress(portIdentifier.getName())); Log.debug(AUTO_DISCOVERY, "Serial port found: " + portIdentifier.getName()); } putLastSuccessfulPortFirst(); }
private void putLastSuccessfulPortFirst() { String lastSuccessfulPort = sharedPreferences.getLastSuccessfulSerialPort(); if (lastSuccessfulPort == null) return; RxtxDeviceAddress lastAddress = new RxtxDeviceAddress(lastSuccessfulPort); boolean wasRemoved = availablePorts.removeIf(address -> address.value().equals(lastSuccessfulPort)); if (!wasRemoved) return; availablePorts.push(lastAddress); }