我正在尝试将 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();
最佳答案
终于成功了。
事实证明,我遇到了两个不同的问题:
- 这个程序没有正确检查/报告异常,
- 我遇到了
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/