代码如下:
public class MinaClient {
public static void main(String []args)throws Exception{
//Create TCP/IP connection
// NioSocketConnector connector = new NioSocketConnector();
IoConnector connector = new SerialConnector();
//创建接受数据的过滤器
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
//设定这个过滤器将一行一行(/r/n)的读取数据
chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
//绑定处理handler
connector.setHandler(new MinaClientHandler());
//客户端的消息处理器:一个SamplMinaServerHander对象
// connector.setHandler(new MinaClientHandler());
//set connect timeout
connector.setConnectTimeout(30);
SerialAddress address = new SerialAddress
("COM3", 9600, SerialAddress.DataBits.DATABITS_8, SerialAddress.StopBits.BITS_1, SerialAddress.Parity.NONE, SerialAddress.FlowControl.NONE);
ConnectFuture cf = connector.connect(address);
//Wait for the connection attempt to be finished.
cf.awaitUninterruptibly();
// cf.getSession().getCloseFuture().awaitUninterruptibly(1000);
// //连接到服务器:
// ConnectFuture cf1 = connector.connect(new InetSocketAddress("localhost",8888));
// cf1.awaitUninterruptibly();
//
// cf1.getSession().getCloseFuture().awaitUninterruptibly(1000);
connector.dispose();
}
这是客户端代码,如果在客户端获取串口的数据,并新建一个客户端,与server 端产生通信??