在RF协议中处理的txt问题

时间:2015-04-30 16:06:04

标签: python python-2.7

我正在编写协议以便通过RF发送信息。我从txt获取信息,我希望将每个字母拆分为一个字节,然后通过Raspberry Pi中的协议SPI将其发送到RF模块。当我尝试发送新的行号代码(\n)时,我的代码出现问题。有谁能够帮我?提前谢谢!

informationpacket1=array.array('B')
informationpacket2=array.array('B')
informationpacket3=array.array('B')
informationpacket4=array.array('B')

origen=open('origen.txt', 'r')

    for i in range (0, 4):
        if i==0:
            for i0 in range (0, 23):
                lectura=origen.read(1)
                origenbinario1=int(binascii.hexlify(lectura), 16)
                informationpacket1.insert(i0,origenbinario1)
        elif i==1:
            for i1 in range (0, 23):
                lectura=origen.read(1)
                origenbinario2=int(binascii.hexlify(lectura), 16)
                informationpacket2.insert(i1,origenbinario2)
        elif i==2:
            for i2 in range (0, 23):
                lectura=origen.read(1)
                origenbinario3=int(binascii.hexlify(lectura), 16)
                informationpacket3.insert(i2,origenbinario3)
        elif i==3:
            for i3 in range (0, 23):
                lectura=origen.read(1)
                origenbinario4=int(binascii.hexlify(lectura), 16)
                informationpacket4.insert(i3,origenbinario4)

destino=open('destino.txt', 'w')

for j in range (0, 4):
    if j==0:
        for j0 in range (0, 23):
            destinoASCII=binascii.unhexlify('%x' % informationpacket1[j0])
            destino.write (destinoASCII)
    elif j==1:
        for j1 in range (0, 23):
            destinoASCII=binascii.unhexlify('%x' % informationpacket2[j1])
            destino.write (destinoASCII)
    elif j==2:
        for j2 in range (0, 23):
            destinoASCII=binascii.unhexlify('%x' % informationpacket3[j2])
            destino.write (destinoASCII)
    elif j==3:
        for j3 in range (0, 23):
            destinoASCII=binascii.unhexlify('%x' % informationpacket4[j3])
            destino.write (destinoASCII)
origen.close()    
destino.close()

回溯:

Traceback (most recent call last):
  File "C:\Users\villap01\Documents\UNI\MTP\protocolo.py", line 79, in <module>
    destinoASCII=binascii.unhexlify('%x' % informationpacket2[j1])
TypeError: Odd-length string

0 个答案:

没有答案