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

将字节数组转换为带校验和的字符串。在python中重新迭代时获取额外的空字节,python,arrays,hex,checksum,packet,Python,Arrays,Hex,Checksum,Packet,好的,这是我的问题。实际上是两个 在第一次通过后运行代码时,我会得到额外的空字节,我做错了什么 第二个问题,如何从字节数组和添加的校验和形成数据包 我有一个字节数组,我需要对它执行异或运算以获得校验和值。在XOR之前,起始校验和必须为5A。这需要附加到数组的末尾并发送到我的外部设备 我“认为”我这样做是对的,但我没有收到设备的响应。我已经检查了设备,它工作正常,所以我假设它是我的代码 因此,我提出了下面的代码。任何帮助组成发送包的人都会非常有帮助,因为我的头一直撞在墙上。我不是一个非常有经验的p

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

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

第二个问题,如何从字节数组和添加的校验和形成数据包

我有一个字节数组,我需要对它执行异或运算以获得校验和值。在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()

我提前感谢你的帮助

appendChecksum始终在代码中的输入参数getBoardTemp后面追加一个字节。我不明白你为什么说“校验和应该在数组中追加一个0B字节”。

问题在于交换性。appendChecksum()修改它的
buff
参数的内容(在第一次迭代后,它计算的校验和为零)。您只需先复制一份即可解决此问题:

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

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

太好了。这解决了第一个问题。非常感谢。但是第二个问题,我仍然有一个地狱般的时间。我发送数据,但没有得到任何回报。校验和是正确的,但我的分组正确吗?我把你的标记为已应答。我进行了窃听,得到了正确的数据有效载荷。只是没有收到设备的响应。现在必须介于两者之间。。。。但是什么?!?!?!啊。我希望最终能解决这个问题。也许您可以设置一个测试服务器来帮助确定问题所在,如图所示。在字节数组上使用XOR应该得到的值应该是0x0B。对于那个数组,它应该始终是那个。是的,但在它跑了一次之后,我一直在填充。马蒂诺对此的解决方案有助于解决这一问题。但是关于我是否正确地进行了数据包生成的部分仍然没有被我理解。