netty rxtx
无法与NioEventLoopGroup
一起使用。
当使用Oio时它没问题并且工作正常,但是改为Nio,代码无法正常工作。
这个项目有很多rial端口连接。
RXTX只工作oio?
Netty 4.1.6,Java 1.8.0_112
EventLoopGroup event = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(event);
bootstrap.channel(RxtxChannel.class);
bootstrap.remoteAddress(new RxtxDeviceAddress(device.getSerialPort()));
// 设置端口参数
if (device.getSerialBaudRate() > 0)
bootstrap.option(RxtxChannelOption.BAUD_RATE, device.getSerialBaudRate());
// bootstrap.option(RxtxChannelOption.DATA_BITS,
// RxtxChannelConfig.Databits.DATABITS_8);
// bootstrap.option(RxtxChannelOption.STOP_BITS,
// RxtxChannelConfig.Stopbits.STOPBITS_1);
// bootstrap.option(RxtxChannelOption.PARITY_BIT,
// RxtxChannelConfig.Paritybit.NONE);
// bootstrap.option(RxtxChannelOption.READ_TIMEOUT, 3000);
// 等待时间量
// bootstrap.option(RxtxChannelOption.WAIT_TIME, 100);
// bootstrap.option(RxtxChannelOption.DTR, false);
// bootstrap.option(RxtxChannelOption.RTS, false);
bootstrap.handler(new ChannelInitializer<RxtxChannel>() {
@Override
protected void initChannel(RxtxChannel sc) throws Exception {
ChannelPipeline pipeline = sc.pipeline();
// 空闲时间监测(秒)
pipeline.addLast(new IdleStateHandler(120, 60, 60));
// 创建基于报头和长度的解码器
pipeline.addLast(new KSFrameLengthDecoder());
pipeline.addLast(new KSFrameDecoder(coder));
// 创建业务逻辑解码器
pipeline.addLast(new SimpleChannelInboundHandler<Message>() {
@Override
public void channelActive(ChannelHandlerContext ctx) {
busy.set(true);
Message msg = Message.newBuilder().setCommand(Command.CONNECT).build();
msg.device = device;
channel.writeAndFlush(msg);
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
received(msg);
if (queue.isEmpty()) {
busy.set(false);
} else {
channel.writeAndFlush(queue.poll());
}
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
if (e.state() == IdleState.READER_IDLE) {
restart();
} else {
busy.set(true);
Message msg = Message.newBuilder().setCommand(Command.GET_DEVICE_DATE).build();
msg.device = device;
channel.writeAndFlush(msg);
}
}
}
// 异常
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// 出现异常时,记录错误,稍后重连
Log.error(cause);
restart();
}
});
pipeline.addLast(new KSFrameEncoder(coder));
}
});
ChannelFuture future = bootstrap.connect();
channel = future.channel();
Log.info("START" + device.getName());
答案 0 :(得分:0)
是的rxtx仅适用于OIO,因为它的实现使用阻塞InputStream和OutputStream。