前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python计算CRC16等

python计算CRC16等

作者头像
py3study
发布2020-01-07 16:01:15
1.5K0
发布2020-01-07 16:01:15
举报
文章被收录于专栏:python3python3

python计算CRC16等

智能电能表数据通信协议DL/T 645 - 2007;本部分实现了该协议的部分功能。

'''
计算645数据域数据长度
@Source 2013-5-10 16:30
'''
def LenCal(s):
    L=''
    L=hex(len(s)/2).replace('0x','')
    if len(L)<=1:
        L=('00'+L)[-2:]

    return L

'''
对16进制字符串进行加0x33处理
@Source 2013-5-10 12:00
'''
def Add33(s):
    h=''
    for i in range(len(s)/2):
        temper=''
        temper=hex(int('33',16)+int(s[2*i:2*i+2],16)).replace('0x','')
        if len(temper)<=1:
            temper=('00'+temper).upper()[-2:]

        h=h+temper
    ##返回加0x33后的数据
    return h

'''
对16进制字符串进行减0x33处理
@Source 2013-5-10 12:10
'''
def Reduce33(s):
    h=''
    for i in range(len(s)/2):
        temper=''
        temper=hex(int(s[2*i:2*i+2],16)-int('33',16)).replace('0x','')
        ##如果余值为负数,去除'-'
        temper=temper.replace('-','')

        if len(temper)<=1:
            temper=('00'+temper).upper()[-2:]

        h=h+temper
    ##返回减0x33后的数据
    return h  

'''
计算16进制字符串的CRC16校验
@Source 2013-5-10 11:00
'''
def CRC16(s):
    h='0'
    for i in range(len(s)/2):
        h=hex(int(h,16)+int(s[2*i:2*i+2],16))

    h=h.replace('0x','')
    if len(h)<=1:
        h='00'+h

    return h.upper()[-2:]

##68AAAAAAAAAAAA681300DF16
##print CRC16('68AAAAAAAAAAAA681300')
##print Add33('010101010101')
##print Reduce33('010101010101')
print LenCal('010101010101')
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档