如何在Netty 4中使用RXTX?

时间:2012-05-22 07:30:06

标签: netty rxtx

我正在尝试将Netty 4与RXTX一起使用(如果我做对了,它在Netty 3.x中没有正式使用。)

我认为我已经正确设置了管道工厂,但是当一些数据被发送到串口时我没有生成任何事件(我已经通过CoolTerm确认有一些数据经常来自我的设备)

下面是我用于测试的代码(其中serialPort类似/dev/tty.usbserial-A100MZ0L(这是FTDI设备):

// 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();

1 个答案:

答案 0 :(得分:2)

最后让它发挥作用。

事实证明,我遇到了两个不同的问题:

  1. 此程序未正确检查/报告例外​​,
  2. 我遇到gnu.io报告gnu.io.PortInUseException: Unknown Application错误的问题;事实证明I had blogged about that before(我的博客帮助自己 - 第一个)并且忘了将修复应用到我的笔记本电脑上。
  3. 简而言之,不要忘记创建一个/var/lock目录,并让运行程序的用户可以写入。

    如果有人建议如何检查我得到的例外并正确通知运行该程序的用户,那就太棒了: - )