我正在尝试将 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();