使用校验和将字节数组转换为字符串。在python中重新迭代时获得额外的空字节

时间:2013-12-04 15:26:34

标签: python arrays hex checksum packet

好的,这是我的问题。实际上是两个。

在第一次传递后运行我的代码时,我得到额外的空字节,我做错了什么?

第二个问题,如何从字节数组中创建数据包并添加校验和?

我有一个bytearray,我需要进行XOR以获得校验和值。在XOR之前,起始校验和必须是5A。这需要附加到数组的末尾并发送到我的外部设备。

我想'我正在做这件事,但我没有收到设备的回复。我检查了设备,它工作正常,所以我假设它是我的代码。

因此,我已经提出了以下代码。任何帮助形成数据包发送将是非常有帮助的,因为我一直在撞墙。我不是一个非常有经验的python程序员,但是当他们被解释或给出例子时,我通常可以解决问题。

import struct
import sys
import string
import select
import time
from socket import *
import binascii


port = 8010
# region Read Codes
getBoardTemp = [0x02, 0x53, 0x00, 0x00]
#Communication utilities

class CommUtilities:
    def unhex(self, inp):
        return binascii.unhexlify(inp)

    def hexit(self, inp):
        return binascii.a2b_hex(inp)

    def calculateChecksum(self, buf):
        checksum = 0x5A #Starting checksum
        #For each byte, bitwise XOR it with our checksum
        for b in buf:
            checksum ^= b
        print 'Checksum: {}'.format(checksum)
        return checksum

    #Append the one byte checksum to a command
    #returns the complete byte array
    def appendChecksum(self, buff):
        buff.append(self.calculateChecksum(buff))
        return buff


def send_server(data, host):
        sock=socket(AF_INET,SOCK_STREAM)
        sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        sock.setblocking(0)
        sock.settimeout(3)
        sock.connect((host, 1000))
        sock.sendall(data)
        print 'Sent Data'
        print 'Waiting for return data'
        result = sock.recv(1024)

        sock.close()
        return result


def main():
    c = CommUtilities()
    host = "172.16.2.52"
    while True:
        try:
            #Connect to server and send data
            print 'Requesting Board Temp'
            data = c.appendChecksum(getBoardTemp)
            """ The checksum should have appended a 0B byte into the array
            """
            data = bytearray(data)
            print data
            print'sending "%s"' % data
            """  The sending "%s" is where I noticed extra nulls being appedned
            on each pass.  They accumulate as the application keeps running.
            """
            received = send_server(data, host)
            """ I have yet to receive any return data, other than the ACK
            packets from the device.  So I am assuming my data I am sending is wrong.
            """
        except Exception, e:
            print >>sys.stderr, 'send/receive error {}'.format(e)
            received = e
        finally:
            print "received: {}".format(received)

        time.sleep(2)

if __name__ == '__main__':
    main()

我事先感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

appendChecksum总是在输入参数中添加一个字节,在代码中添加getBoardTemp。我不明白为什么你说“校验和应该在数组中附加一个0B字节”。

答案 1 :(得分:0)

问题是CommUtilities.appendChecksum()修改了它的buff参数的内容(在第一次迭代之后,它计算的校验和为零)。您只需先制作一份副本就可以解决这个问题:

    def appendChecksum(self, buff):
        buff = buff[:]  # make copy
        buff.append(self.calculateChecksum(buff))
        return buff

我不确定,但这也可以回答你的第二个问题。