如何将字节加在一起并根据 python 中的校验和验证它们

How to add bytes together and validate them against the checksum in python

如何计算传入字节的校验和以查看它是否是有效数据包? 目前我正在读取字节并对它们进行解码并接收信息,但在我这样做之前我希望能够根据校验和验证它以确保我没有收到任何 invalid/corrupt 数据包。

这是我目前拥有的

def batteryConnect(port, baudrate):
    # establishing a serial connection
    ser = serial.Serial(port=port, baudrate= baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,timeout=3)
    return ser

class VictronBmv700:

    def __init__(self,port,baudrate):
        self.port = port
        self.baudrate = baudrate
        self.parameterDict = dict.fromkeys(["PID","V","I","P","CE","SOC","TTG","Alarm","Relay","AR","BMV","FW","H1","H2","H3",
                                  "H4","H5","H6","H7","H8","H9","H10","H11","H12","H17","H18"])


    def getBMVInfo(self):

        bmvdata = batteryConnect(self.port,self.baudrate)

        #getting the data
        bmvdata.flushInput()

        #getting the message then splitting the key, value pairs
        while True:
            print(bmvdata.readline())

            message = bmvdata.readline().decode(encoding='utf-8',errors='ignore')

            #splitting on tabs
            message_parts = message.split('\t')

            if len(message_parts) > 1:
                key = message_parts[0]
                value = message_parts[1].rstrip()  #stripping \r\n after the value


                #updating deictionary based on keys and their values.
                self.parameterDict[key] = value

if __name__ == "__main__":
    print("BATTERY MONITOR")
    bmv700 = VictronBmv700("COM17", 19200)
    bmv700.getBMVInfo()

开始输出以下内容(来自 print(bmvdata.readline()) )

b'\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd7\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd6\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'

我如何根据校验和检查传入的字节,然后在验证后继续解码?

编辑 有时我会得到如下不同的校验和值。 下面是我另外两次 运行 的代码。

b'\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd7\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\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tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'

第二个运行

b'\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25549\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25549\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'

编辑 运行下面的代码

current_block = []
# it's unfortunate that lines start with `\r\n` rather than end with it
# we will read the first empty line separatey, and then include these 
# 2 characters in the last line that is read
message = bmvdata.readline()
while True:
    # Note that we cannot decode yet, since the Checksum value may not be a valid utf8 character
    message = bmvdata.readline()

    # we save the message in the current block before any preprocessing
    current_block.append(message)

    #splitting on tabs (as bytes !!)
    message_parts = message.split(b'\t')

    if len(message_parts) > 1:
        key = message_parts[0].decode('utf8')  # here it is safe to decode the key
        value = message_parts[1].rstrip()  #stripping \r\n after the value

    if key == 'Checksum':
        block_chars = b''.join(current_block)
        checksum = sum(block_chars) % 256
        print('Retrieved  checksum', value[-1])
        print('Calculated checksum', checksum)
        print('----')

        # reset the block
        current_block = []

输出

Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 219
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 220
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 219
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 219
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 220
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 220
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 220
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 219
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------

下面的代码 比较两个文件 在这种情况下,如提问者提到的校验和:

 bmvdata = batteryConnect(self.port,self.baudrate)
 bmvdata.flushInput()
 print(bmvdata.readline())

这是在 hashlib

的帮助下完成的
import hashlib
#File 1 = checksum
hasher1 = hashlib.md5()
#afile1 = open('checksum', 'rb')
buf1 = bmvdata.read()
#buf1 = afile1.read()
a = hasher1.update(buf1)
md5_a=(str(hasher1.hexdigest()))
#File 2
hasher2 = hashlib.md5()
afile2 = open('incoming-byte', 'rb')
buf2 = afile2.read()
b = hasher2.update(buf2)
md5_b=(str(hasher2.hexdigest()))
#Compare md5
if(md5_a==md5_b):
    print("Yes")
else:
    print("No")

发帖者似乎正在尝试从 BMV700 电池监视器读取数据。阅读论文 here,我们看到它使用文本协议通过串行接口进行通信:

2 TextProtocol

When no VE.Direct queries are sent to the device, the charger periodically sends human readable (TEXT) data to the serial port. See the "VE.Direct Protocol" document for a detailed description of the contents and availability of the information

HEX 协议摘录 (BMV-7xx-HEX-Protocol-public.pdf)

查看 TEXT 协议规范 (VE.Direct-Protocol-3.28.pdf),我们发现:

Message format

The device transmits blocks of data at 1 second intervals. Each field is sent using the following format:

<Newline><Field-Label><Tab><Field-Value>

The identifiers are defined as follows:

+---------------+--------------------------------------------------------------------------------------+
| Identifier    | Meaning                                                                              |
+===============+======================================================================================+
| <Newline>     | A carriage return followed by a line feed (0x0D, 0x0A).                              |
+---------------+--------------------------------------------------------------------------------------+
| <Field-Label> | An arbitrary length label that identifies the field.                                 |
|               | Where applicable, this will be the same as the label that is used on the LCD.        |
+---------------+--------------------------------------------------------------------------------------+
| <Tab>         | A horizontal tab (0x09).                                                             |
+---------------+--------------------------------------------------------------------------------------+
| <Field-Value> | The ASCII formatted value of this field.                                             |
|               | The number of characters transmitted depends on the magnitude and sign of the value. |
+---------------+--------------------------------------------------------------------------------------+

这对应于您正在打印的数据,但有一个例外:行 starts with \r\b,它没有 end和他们在一起。

然后,该文件提供了有关校验和的详细信息:

Data integrity

The statistics are grouped in blocks with a checksum appended. The last field in a block will always be “Checksum”. The value is a single byte, and will not necessarily be a printable ASCII character. The modulo 256 sum of all bytes in a block will equal 0 if there were no transmission errors. Multiple blocks are sent containing different fields.

因此,在您作为输出发布的摘录中,您有两个完整的块和一个不完整的块,因为最后一个块缺少 Checksum

block = (b'\r\nH2\t0\r\n'
    b'H4\t0\r\n'
    b'H6\t-9001\r\n'
    b'H8\t28403\r\n'
    b'H10\t0\r\n'
    b'H12\t0\r\n'
    b'H18\t87\r\n'
    b'PID\t0x203\r\n'
    b'I\t0\r\n'
    b'CE\t0\r\n'
    b'TTG\t-1\r\n'
    b'Relay\tOFF\r\n'
    b'BMV\t700\r\n'
    b'Checksum\t\xd6')

请注意,我在开头添加了 \r\n,并将它们从块的末尾删除,这样最后一个字节就是块的校验和。

现在,我们可以计算区块的校验和了:

>>> sum(block) % 256
213

这应该是零。因此,可能存在一些传输问题,或者他们计算校验和的方式可能与他们在文档中所说的不同。


根据新数据进行编辑。

这是我用来检查您发布的所有块的代码:

current_block = []
# it's unfortunate that lines start with `\r\n` rather than end with it
# we will read the first empty line separatey, and then include these 
# 2 characters in the last line that is read
message = bmvdata.readline()
while True:
    # Note that we cannot decode yet, since the Checksum value may not be a valid utf8 character
    message = bmvdata.readline()

    # we save the message in the current block before any preprocessing
    current_block.append(message)

    #splitting on tabs (as bytes !!)
    message_parts = message.split(b'\t')

    if len(message_parts) > 1:
        key = message_parts[0].decode('utf8')  # here it is safe to decode the key
        value = message_parts[1].rstrip()  #stripping \r\n after the value

    if key == 'Checksum':
        block_chars = b''.join(current_block)
        checksum = sum(block_chars) % 256
        print('Retrieved  checksum', value[-1])
        print('Calculated checksum', checksum)
        print('----')

        # reset the block
        current_block = []

奇怪的是,对于第一个 运行,您总是得到等于检索到的计算值 + 1,但在第二个 运行 中却不是。因此,可能存在一些传输问题。