使用bytearrays时的TypeError

时间:2018-03-10 12:52:02

标签: python

我正在尝试通过PN532模块读取和写入数据。我正在使用python 3.4。

import binascii
import sys
import struct
import Adafruit_PN532 as PN532

CS   = 18   
MOSI = 23 
MISO = 24
SCLK = 25       
CARD_KEY = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]       
HEADER = b'BG'

pn532 = PN532.PN532(cs=CS, sclk=SCLK, mosi=MOSI, miso=MISO)
pn532.begin()
pn532.SAM_configuration()

print('PN532 NFC Module Writer'
print('== STEP 1 =========================')
print('Place the card to be written on the PN532...')
uid = pn532.read_passive_target()
while uid is None:
    uid = pn532.read_passive_target()
print('')
print('Found card with UID: 0x{0}'.format(binascii.hexlify(uid)))
print('')
print('==============================================================')
print('WARNING: DO NOT REMOVE CARD FROM PN532 UNTIL FINISHED WRITING!')
print('==============================================================')
print('')


print('== STEP 2 =========================')
block_choice = None
while block_choice is None:
    print('')
    block_choice = input('Enter user ID: ')
    try:
        block_choice = int(block_choice)
    except ValueError:
        print('Error! Unrecognized option.')
        continue
    if not (0 <= block_choice < 16777215):
        print('Error! User ID must be within 0 to 4294967295.')
        continue
    print('')
print('You chose the block type: {0}'.format(block_choice))
print('')

print('== STEP 3 =========================')
print('Confirm you are ready to write to the card:')
print('User ID: {0}'.format(block_choice))
choice = input('Confirm card write (Y or N)? ')
if choice.lower() != 'y' and choice.lower() != 'yes':
    print('Aborted!')
    sys.exit(0)
print('Writing card (DO NOT REMOVE CARD FROM PN532)...')

if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B,
                                               CARD_KEY):
    print('Error! Failed to authenticate block 4 with the card.')
    sys.exit(-1)

data = bytearray(16)
# Add header
data[0:2] = HEADER
# Convert int to hex string with up to 6 digits
value = format(block_choice, 'x')
while (6 > len(value)):
    value = '0' + value
data[2:8] = value
# Finally write the card.
if not pn532.mifare_classic_write_block(4, data):
    print('Error! Failed to write to the card.')
    sys.exit(-1)
print('Wrote card successfully! You may now remove the card from the PN532.')

当我运行此代码时,我收到错误&#34; TypeError:只能分配范围(0,256)中的字节,缓冲区或int的可迭代。我相信这是数据[2:8]行的错误,但我不知道如何解决它。

追踪(最近一次通话):    文件&#34; /home/pi/Desktop/pn532_write.py" ;,第93行      数据[2:8] =值 TypeError:只能分配范围(0,256)

中的字节,缓冲区或int的可迭代

1 个答案:

答案 0 :(得分:1)

错误来自以下部分:

value = format(block_choice, 'x')
while (6 > len(value)):
    value = '0' + value
data[2:8] = value

在此,您将value设置为十六进制值的字符串表示形式,然后当它少于6个字符时,您将字符串"0"添加到其中。

问题来自于您正在尝试将此字符串(表示十六进制值,但仍然只是一个字符串)分配给data bytearray。

You can convert this hexadecimal string value that you have into bytes by using bytes.fromhex() or into a bytearray by using bytearray.fromhex().

要在代码中实现此功能,您需要更新以下行:

data[2:8] = value

读作以下任一项:

data[2:8] = bytes.fromhex(value)
data[2:8] = bytearray.fromhex(value)