3

I'm trying to use Netty 4 with RXTX (it's not officially in Netty 3.x if I got this right).

I think I have set up the pipeline factory correctly, but I don't get any event generated when some data is sent to the serial port (I have confirmed with CoolTerm that there is some data coming in regularly from my device).

Here is below the code I use for testing (where serialPort is something like /dev/tty.usbserial-A100MZ0L (this is a FTDI device):

// Configure the client.
final ExecutorService executorService = Executors.newCachedThreadPool();
RxtxChannelFactory rxtxChannelFactory = new RxtxChannelFactory(executorService);
ClientBootstrap bootstrap = new ClientBootstrap(rxtxChannelFactory);

// Set up the pipeline factory.
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    public ChannelPipeline getPipeline() throws Exception {
        // Create and configure a new pipeline for a new channel.
        ChannelPipeline pipeline = Channels.pipeline();
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("logger", new LoggerHandler());
        return pipeline;
    }
});

// Start the connection attempt.
ChannelFuture future = bootstrap.connect(new RxtxDeviceAddress(serialPort));

// Wait until the connection is made successfully.
Channel channel = future.awaitUninterruptibly().getChannel();

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

boolean exit = false;
while (!exit) {
    try {
        String line = reader.readLine();
        if ("exit".equals(line)) {
            exit = true;
        }
    } catch (IOException e) {
        // ignore
    }
}

// Close the connection.
channel.close().awaitUninterruptibly();

// Shut down all thread pools to exit.
bootstrap.releaseExternalResources();
jeje
  • 61
  • 4

1 Answers1

2

Finally got it to work.

Turns out, I hit two different issues:

  1. this program was not properly checking for/reporting exceptions,
  2. I had an issue with gnu.io reporting an gnu.io.PortInUseException: Unknown Application error; turns out I had blogged about that before (my blog helping myself -- a first) and forgot to apply the fix to my laptop.

So in short, don't forget to create a /var/lock directory and have it writable for the user running your program.

If anyone has a suggestion on how to check for the exception I got and inform appropriately the user running the program, that would be great :-)

jeje
  • 61
  • 4