0
netty rxtx
은 NioEventLoopGroup
과 함께 사용할 수 없습니다. Oio를 사용하면 문제가 없지만 작동하지만 Nio로 변경하면 코드가 작동하지 않습니다. 이 프로젝트에는 많은 ial port 연결이 있습니다. RXTX 만 작동합니까? 인 Netty 4.1.6, 자바 1.8.0_112netty rxtx는 NioEventLoopGroup과 함께 작동하지 않습니다.
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());