2012-05-22 66 views
3

我試圖使用Netty 4與RXTX(它不是正式在Netty 3.x,如果我得到這個權利)。如何在Netty 4上使用RXTX?

我想我已經設置了管道工廠正常,但是當一些數據發送到串行端口(我已確認與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(); 

回答

2

終於得到它的工作

事實證明,我打兩個不同的問題:

  1. 這個程序沒有正確檢查/報告的異常,
  2. 我遇到了gnu.io報告的gnu.io.PortInUseException: Unknown Application錯誤;原來I had blogged about that before(我的博客幫助自己 - 第一個),並忘記將修復程序應用於我的筆記本電腦。

簡而言之,不要忘記創建一個/var/lock目錄,並讓它可以運行程序的用戶寫入。

如果任何人有關於如何檢查我得到的異常的建議,並適當通知運行該程序的用戶,那將是非常好的:-)