def crc16(cmd, use_byte=False):
    """
        CRC16 检验
        - 启用``use_byte`` 则返回 bytes 类型.

    :param cmd: 无crc检验的指令
    :type cmd:
    :param use_byte: 是否返回byte类型
    :type use_byte:
    :return: 返回crc值
    :rtype:
    """
    crc = 0xFFFF

    # crc16 计算方法, 需要使用 byte
    if hasattr(cmd, 'encode'):
        cmd = bytes.fromhex(cmd)

    for _ in cmd:
        c = _ & 0x00FF
        crc ^= c
        for i in range(8):
            if crc & 0x0001 > 0:
                crc >>= 1
                crc ^= 0xA001
            else:
                crc >>= 1

    # modbus crc16计算时,需要高/低位倒置
    t = [(crc & 0x00FF), (crc >> 8 & 0xFF)]
    crc = '%02X%02X' % (t[0], t[1])
    if use_byte:
        crc = bytes.fromhex(crc)
    return crc
def crc16 ( cmd , use_byte = False ) : crc = 0xFFFF if hasattr ( cmd , 'encode' ) : cmd = bytes . fromhex ( cmd ) for _ in cmd : c = _ & 0x00FF crc ^= c for i in range ( 8 ) : if crc & 0x0001 > 0 : crc >>= 1 crc ^= 0xA001 else : crc >>= 1 t = [ ( crc & 0x00FF ) , ( crc >> 8 & 0xFF ) ] crc = '%02X%02X' % ( t [ 0 ] , t [ 1 ] ) if use_byte : crc = bytes . fromhex ( crc ) return crc
CRC16 检验
        - 启用``use_byte`` 则返回 bytes 类型.

    :param cmd: 无crc检验的指令
    :type cmd:
    :param use_byte: 是否返回byte类型
    :type use_byte:
    :return: 返回crc值
    :rtype: