我正在尝试将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();
答案 0 :(得分:2)
最后让它发挥作用。
事实证明,我遇到了两个不同的问题:
gnu.io
报告gnu.io.PortInUseException: Unknown Application
错误的问题;事实证明I had blogged about that before(我的博客帮助自己 - 第一个)并且忘了将修复应用到我的笔记本电脑上。简而言之,不要忘记创建一个/var/lock
目录,并让运行程序的用户可以写入。
如果有人建议如何检查我得到的例外并正确通知运行该程序的用户,那就太棒了: - )