MINA框架中的Serial串口通讯

MINA作为优秀的网络应用开发框架,也集成了关于串口通讯的开发,其集成使用的为RXTX串口开发框架,RXTX为各操作系统下的兼容javax.comm串口通讯包API的实现,若不甚了解或遇安装问题请参考我另一篇博文:http://blog.csdn.net/cheng157507947/article/details/43764623
由于我原先使用过RXTX做过串口开发测试,环境并无问题,windows7 64位下创建了两个虚拟串口用于串口调试,linux下也是使用xgcom串口调试工具,其环境问题不在赘述。

MINA官网中有对Serial Transport的大致说明和样例,可参考:
http://mina.apache.org/mina-project/userguide/ch6-transports/serial-transport.html(一些需要注意的问题官网中也有解释)
在开发测试之前,除RXTX环境外,需知在MINA提供的2.0.9的基础包中并不包含Serial开发包,在上述连接中已有标明,并已提供了下载地址,开发环境中需加入mina-transport-serial jar。
此外MINA框架使用slf4j,在不做任何处理的情况下会报出

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

解决方法可参考另一博文:http://blog.csdn.net/cheng157507947/article/details/45039309

下面即可开始Serial通讯开发,根据官网介绍,串口开发只有客户端,不存在服务端代码,即:

IoConnector connector = new SerialConnector();

//添加数据解析Filter
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MessageCodecFactory()));

//添加事件处理handler 
connector.setHandler(new SerialHandler());

SerialAddress portAddress = new SerialAddress("COM1", 9600, DataBits.DATABITS_8, StopBits.BITS_1, Parity.NONE, FlowControl.NONE);

ConnectFuture future = connector.connect(portAddress);
future.await();
IoSession session = future.getSession();
session.write("00");

这里除部分Serial地址设置方法不同外,其余与MINA框架客户端开发并无不同,编写自己的处理方法即可,不了解MINA框架的处理方式可参考另一篇博文http://blog.csdn.net/cheng157507947/article/details/44099637


串口通讯的注意点:
1.需正确添加SerialAddress参数值,可参考RXTX中的串口获得。
2.在我测试的串口通讯中,原先使用的TextLineCodecFactory Filter并不适用,原因为串口通讯中多数使用指定协议(如ModBus协议),使用byte传输16进制命令,形如“01 03 00 01 00 01 D5 CA”。
可使用自定义IoFilter用于数据的编码和解码:
定义编码工厂类:

public class MessageCodecFactory implements ProtocolCodecFactory {

    private final MessageEncoder encoder;

    private final MessageDecoder decoder;

    public MessageCodecFactory() {
        this.encoder = new MessageEncoder();
        this.decoder = new MessageDecoder();
    }

    @Override
    public ProtocolDecoder getDecoder(IoSession session) throws Exception {
        return decoder; 
    }

    @Override
    public ProtocolEncoder getEncoder(IoSession session) throws Exception {
        return encoder;
    }

}

编码器(向串口发送数据时的数据编码加工):

public class MessageEncoder extends ProtocolEncoderAdapter {

    @SuppressWarnings("unused")
    private final static Logger log = LoggerFactory.getLogger(MessageDecoder.class);

    @Override
    public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
        if(message instanceof ...){
            IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true);
            ...
            byte[] send = ...
            buffer.put(send);
            buffer.flip();
            session.write(buffer);
        }
    }
}

解码器(接收串口数据时的数据解码加工):

public class MessageDecoder extends CumulativeProtocolDecoder {

    private static Logger log = LoggerFactory.getLogger(MessageDecoder.class);

    ...

    @Override
    protected boolean doDecode(IoSession session, IoBuffer in,
            ProtocolDecoderOutput out) throws Exception {
        byte[] input = new byte[in.limit()];
        in.get(input, 0, input.length);
        log.info("接收:{}", ByteUtil.toHexString(input));
        ...
                out.write(...);
        ...
            return true;
        }
        return false;
    }

具体处理按照项目要求,不再贴详细代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值