netty - 如何在 Netty 4 中使用 RXTX?

标签 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. 这个程序没有正确检查/报告异常,
  2. 我遇到了 gnu.io 报告 gnu.io.PortInUseException: Unknown Application 错误的问题;结果 I had blogged about that before (我的博客帮助自己 -- 第一次)忘记对我的笔记本电脑应用修复

所以简而言之,不要忘记创建一个 /var/lock 目录,并使其对运行程序的用户可写。

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

关于netty - 如何在 Netty 4 中使用 RXTX?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10697629/

相关文章:

java - ECDH 服务器 key 交换消息上的签名无效

java - 运行 JAR 文件时右键菜单损坏

java - 如何在 java.library.path 中添加 rxtxSerial?

scala - 使用Play.async会产生什么效果,因为Play使用非阻塞的Netty

java - org.jboss.netty.buffer.ChannelBufferInputStream.available() 中的错误?

java - 使用下划线 '_' 作为 SNI 服务器名称的一部分时 JVM 崩溃

kotlin - 如何弥合Netty ChannelInboundHandler与kotlinx协程调度程序之间的差距?

java - OS X 上 Java 中的串行通信

java - 无法在 JavaFX 应用程序中找到 RXTX native DLL

java - 在Linux 64位下使用Java实现串口工作