2017-03-21 56 views
0

netty rxtx無法與NioEventLoopGroup一起使用。 當使用Oio它是好的,並且工作正常,但更改爲Nio代碼無法工作。 這個項目有很多rial端口連接。 RXTX只能工作嗎?netty rxtx無法使用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()); 

回答

0

是RXTX作爲其實現使用阻塞InputStream和OutputStream的只有OIO工作。