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(); } }
/** * A communication service that manages the serial connection. * It can receive and send serial messages via RXTX. * * @param bootstrap The bootstrap, not null. * @param channelInitializer The channel initializer, not null. */ public RxtxCommunicationService(Bootstrap bootstrap, RxtxChannelInitializer channelInitializer) { requireNonNull(bootstrap); requireNonNull(channelInitializer); this.bootstrap = bootstrap; this.bootstrap.group(new OioEventLoopGroup()); this.bootstrap.channel(RxtxChannel.class); channelInitializer.setRxTxSerialHandler(serialHandler); this.bootstrap.handler(channelInitializer); }
@Override protected void initChannel(RxtxChannel channel) throws Exception { Log.debug(SERIAL, "RXTX channel initialized. Configuring pipeline and channel..."); checkIfRxTxSerialHandlerIsSet(); configureChannel(channel); configurePipeline(channel); }
private void configureChannel(RxtxChannel channel) { RxtxChannelConfig config = channel.config(); config.setBaudrate(19200); config.setDatabits(DATABITS_8); config.setStopbits(STOPBITS_1); config.setParitybit(NONE); logSerialConfiguration(config); }
private void configurePipeline(RxtxChannel channel) { ChannelPipeline pipeline = channel.pipeline(); // Decode messages: pipeline.addLast("framer", new DelimiterBasedFrameDecoder(MAX_FRAME_LENGTH, lineDelimiter())); pipeline.addLast("decoder", new RxtxMessageFrameDecoder(parser)); // Encode messages: pipeline.addLast("encoder", new RxtxMessageFrameEncoder(parser)); // Handle messages and exceptions: pipeline.addLast("handler", serialHandler); Log.debug(SERIAL, "Pipeline configured and handler added."); }