2017-03-21 2 views
0

netty rxtx ne peut pas fonctionner avec NioEventLoopGroup. Lorsque vous utilisez Oio c'est bon et fonctionne bien, mais changer pour Nio ce code ne peut pas fonctionner. Ce projet a beaucoup de connexion de port rial. RXTX ne fonctionne que oio?Netty rxtx ne peut pas fonctionner avec NioEventLoopGroup

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()); 

Répondre

0

Oui RxTx fonctionne uniquement avec OIO que sa mise en œuvre utilise le blocage et InputStream OutputStream.