Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加一个自动计算crc校验码功能 #80

Open
Sober-hyh opened this issue Aug 23, 2024 · 1 comment
Open

添加一个自动计算crc校验码功能 #80

Sober-hyh opened this issue Aug 23, 2024 · 1 comment

Comments

@Sober-hyh
Copy link

可以添加一个自动计算crc校验码功能,`def calculate_crc(data):
# 预置寄存器为0xFFFF
crc = 0xFFFF

for byte in data:
    # 将数据帧的字节与寄存器的高8位进行异或运算
    crc ^= byte
    
    # 右移8次
    for _ in range(8):
        # 检查最低位
        if crc & 0x0001:
            # 如果最低位为1,与0xA001进行异或运算
            crc = (crc >> 1) ^ 0xA001
        else:
            # 否则不进行异或运算
            crc >>= 1

# 返回低8位和高8位
low_byte = crc & 0x00FF
high_byte = (crc >> 8) & 0x00FF
return low_byte, high_byte

示例数据帧

data_frame = [0x01, 0x03, 0x15, 0x20, 0x00, 0x02]

计算CRC校验码

low, high = calculate_crc(data_frame)

print(f"低8位: {low:02X}, 高8位: {high:02X}")`

@wh201906
Copy link
Owner

这个功能以后会做的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants