View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.example.rxtx;
17  
18  import io.netty.bootstrap.Bootstrap;
19  import io.netty.channel.ChannelFuture;
20  import io.netty.channel.ChannelInitializer;
21  import io.netty.channel.EventLoopGroup;
22  import io.netty.channel.oio.OioEventLoopGroup;
23  import io.netty.channel.rxtx.RxtxChannel;
24  import io.netty.channel.rxtx.RxtxDeviceAddress;
25  import io.netty.handler.codec.LineBasedFrameDecoder;
26  import io.netty.handler.codec.string.StringDecoder;
27  import io.netty.handler.codec.string.StringEncoder;
28  
29  /**
30   * Sends one message to a serial device
31   */
32  public final class RxtxClient {
33  
34      static final String PORT = System.getProperty("port", "/dev/ttyUSB0");
35  
36      public static void main(String[] args) throws Exception {
37          EventLoopGroup group = new OioEventLoopGroup();
38          try {
39              Bootstrap b = new Bootstrap();
40              b.group(group)
41               .channel(RxtxChannel.class)
42               .handler(new ChannelInitializer<RxtxChannel>() {
43                   @Override
44                   public void initChannel(RxtxChannel ch) throws Exception {
45                       ch.pipeline().addLast(
46                           new LineBasedFrameDecoder(32768),
47                           new StringEncoder(),
48                           new StringDecoder(),
49                           new RxtxClientHandler()
50                       );
51                   }
52               });
53  
54              ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
55  
56              f.channel().closeFuture().sync();
57          } finally {
58              group.shutdownGracefully();
59          }
60      }
61  }