如何在Netty 4中使用RXTX?

3

我正在尝试使用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个回答

2

终于让它工作了。

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

  1. 这个程序没有正确检查/报告异常,
  2. 我遇到了 gnu.io.PortInUseException: Unknown Application 错误的 gnu.io 问题;结果发现,我之前曾经在我的博客上写过这个问题(我的博客帮助了我自己——第一次),但是忘记将 解决方案 应用到我的笔记本电脑上了。

因此,简而言之,不要忘记为运行程序的用户创建一个可写的 /var/lock 目录。

如果有人有关于如何检查我遇到的异常并适当地通知运行程序的用户的建议,那就太好了 :-)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接