串口通信协议常用校验计算以及一些常用方法

采用标准UART通信接口,逻辑电平为3.3V或5V(取决于上拉电阻)TTL电平,UART工作在8N1模式,即8位数据位,无奇偶校验,一位停止位,常见波特率19200bps, 38400bps。

累加和校验

    /** 累加和**/
    public static char calCheckSum(byte[] data) {
         if (data == null || data.length == 0) {
            return 0xFF;
        }
        char res = 0x00;
        for (byte datum : data) {
            res += datum & 0xFF;
        }
        // res ^= 0xFF; 视情况是否要异或取反
        res &= 0xFF;
        return res;
    }
    
    public static char calCheckSum(char... data) {
        if (data == null || data.length == 0) {
            return 0xFF;
        }
        char res = 0x00;
        for (char datum : data) {
            res += datum;
        }
        // res ^= 0xFF; 视情况是否要异或取反        
        res &= 0xFF;
        return res;
    }

可能有些串口协议定义需要对校验和进行异或(取反),或者加一减一的操作,视情况而定。

CRC7校验

    /** CRC7**/
    public static byte calCheckSum2(byte... data){
        if(data == null || data.length == 0){
            return (byte) 0xFF;
        }
        char res = 0x00;
        for (byte datum : data) {
            for (int j = 0; j < 8; j++) {
                res <<= 1;
                res ^= (((((datum << j) ^ res) & 0x80) > 0) ? 0x09 : 0);
            }
        }
        res&=0x7F;
        return (byte)  res;
    }

byte数组和String的相互转换:

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte b : src) {
            int v = b & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }    

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase(Locale.ENGLISH);
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

计算协议帧中某个字节的值或者指定某个字节中一位或多位的值,可用以下方法:

    /** 计算某个字节指定某几位的值 **/
    public static int captureBits(char src, int start, int length) {
        if ((length + start) > 16) {
            throw new IllegalArgumentException();
        }
        int filter = (1 << length) - 1;
        return (src >> start) & filter;
    }

    /** 计算某个字节指定某一位的值 **/
    public static int captureBit(char src, int position) {
        return captureBits(src, position, 1);
    }

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

言并肃

感谢大哥支持!您的鼓励是我动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值