如何构建字节数组帧并计算校验和 [英] How to build byte array frame and calculate checksum

查看:480
本文介绍了如何构建字节数组帧并计算校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ser = serial.Serial( / dev / ttyUSB0,baudrate =115200)

frame = bytearray([
0x00,0x00,#frame control(2 bytes)
0x00,0x00,#machine id(2字节)
0x07,#数据字段中的字节数
0x01,0x01,0x01,0x00,0x00,0x00,0x00,#数据字段本身
0x00,0x0A #checksum
$)

ser.write(frame)
ser.close()

代码执行时没有错误,并且我在单独的脚本/进程中监视相同的端口。当设备接收到成功的设备时,它应该返回一个帧。



在这个例子中,我手动计算了校验和,它被定义为:



在FSN.msb ... DATA [dsize]返回的整个帧上计算两字节校验和,MSB优先。校验和是通过一个简单的16位无符号字节加法来计算的。因此,在这种情况下,除了校验和之外,还要在帧中添加所有内容等于10,如通过执行求和(帧)所示,而不添加它。添加它后,总和为20。



另一端的设备可能有故障,所以这是一个困难的工作环境,但它会很好任何人都可以审核我的方法到目前为止?

生成一个校验和的字面意思很简单,还是需要别的?

是的,就是这么简单 -
你通常会填写你的框架,并在另一个阶段追加校验和 - 例如:

  In [73]:frame = bytearray([
...:0x00,0x00,#frame control(2 bytes)
。 ..:0x00,0x00,#machine id(2字节)
...:0x07,#数据字段中的字节数
...:0x01,0x01,0x01,0x00,0x00,0x00 ,0x00,#数据字段本身
...:])


In [75]:checksum = sum(frame)

In [ 76]:frame.extend((校验和// 256,校验和%256))

在[80]中:print(,.join(\\x%02X%v for v in frame))
\\ x00,\ 0x00,\ 0x00,\ 0x00,\ 0x07,\ 0x01,\ 0x01,\ 0x01,\ 0x00,\ 0x00,\ 0x00,\ 0x00,\ \x0A

现在请注意一个细节:我在natual顺序 - 最先是MSB(最高有效字节)。正如你的规格。这应该可行 - 如果不是的话,你可能在其他字段之一出现格式错误。


I'm trying to communicate with a serial port as defined in a specification.

ser = serial.Serial("/dev/ttyUSB0", baudrate="115200")  

frame = bytearray([
    0x00, 0x00, #frame control (2 bytes)
    0x00, 0x00, #machine id (2 bytes)
    0x07, # number of bytes in data field
    0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, #data field itself 
    0x00, 0x0A #checksum
])

ser.write(frame)
ser.close()

The code executes without error, and I am monitoring the same port in a separate script/process. The device is supposed to return a frame when it receives a successful one.

In this example, I have manually calculated the checksum, which is defined as:

Two-byte checksum, MSB first, computed over the whole frame reanging from FSN.msb...DATA[dsize]. The checksum is calculated by a simple 16-bit unsigned addition of bytes

So in this case, adding everything up in the frame, apart from the checksum would equal 10, as shown by doing sum(frame) without it added. With it added, the sum is 20.

The device on the other end possibly has a fault, so this is a difficult environment to work in, but it would great if anyone could vet my approach so far?

Is generating a checksum literally that simple, or does it require something else?

解决方案

Yes it is that simple - you will normally fill in your frame, and append the checksum in another stage - like in:

In [73]: frame = bytearray([
    ...:     0x00, 0x00, #frame control (2 bytes)
    ...:     0x00, 0x00, #machine id (2 bytes)
    ...:     0x07, # number of bytes in data field
    ...:     0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, #data field itself 
    ...:     ])


In [75]: checksum = sum(frame)

In [76]: frame.extend((checksum // 256, checksum % 256))

In [80]: print (", ".join("\\x%02X" % v for v in frame))
\x00, \x00, \x00, \x00, \x07, \x01, \x01, \x01, \x00, \x00, \x00, \x00, \x00, \x0A

Now, note a detail: I added the 2 bytes of the checksum in the "natual order" - which is "MSB" (most significant byte) first. As is in your spec. That should work - if not you likely hae some formatting error in one of the other fields.

这篇关于如何构建字节数组帧并计算校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆