简体   繁体   English

Python float 和 integer 值到字节数组

[英]Python float and integer values to bytearray

Problem Solved.问题解决了。 Solved code and new problem ( Python bytearray with struct and sending via serial communucation )已解决的代码和新问题( Python bytearray with struct 并通过串行通信发送

I am working on sending data to another program via serial communication.我正在通过串行通信将数据发送到另一个程序。

This receiver program accepts 78 bytes arrays and, each data must be in its own dedicated byte index for the receiver to understand.该接收器程序接受 78 个字节 arrays 并且每个数据必须在其自己的专用字节索引中以便接收器理解。

Ex: I have an integer altitude value=2500例如:我有一个 integer 高度值=2500

and the program accepts 4-byte integer values.并且程序接受 4 字节 integer 值。 So I need to fit this integer to these bytes所以我需要把这个 integer 适应这些字节

I wrote a code like this:我写了这样的代码:


import serial

ser = serial.Serial('COM16', 19200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE)

size = 78
array1 = bytearray(size)

altitude=2800.0


check_sum = 0

array1[0] = 0xFF
array1[1] = 0xFF
array1[2] = 0x54
array1[3] = 0x52
array1[4] = 15
array1[5] = 27
array1[6] = 0
array1[7] = 0
array1[8] = 0
array1[9] = 0
array1[10] = 0
array1[11] = 0
array1[12] = 0
array1[13] = 0
array1[14] = 0
array1[15] = 0
array1[16] = 0
array1[17] = 0
array1[17] = 0
array1[19] = 0
array1[19] = 0
array1[20] = 0
array1[21] = 0
array1[22] = 0
array1[23] = 0
array1[24] = 0
array1[25] = 0
array1[26] = 0
array1[27] = 0
array1[28] = 0
array1[29] = 0
array1[30] = 0
array1[31] = 0
array1[32] = 0
array1[33] = 0
array1[34] = 0
array1[35] = 0
array1[36] = 0
array1[37] = 0
array1[38] = 0
array1[39] = 0
array1[40] = 0
array1[41] = 0
array1[42] = 0
array1[43] = 0
array1[44] = 0
array1[45] = 0
array1[46] = 0
array1[47] = 0
array1[48] = 0
array1[49] = 0
array1[50] = 0
array1[51] = 0
array1[52] = 0
array1[53] = 0
array1[54] = 0
array1[55] = 0
array1[56] = 0
array1[57] = 0
array1[58] = 0
array1[59] = 0
array1[60] = 0
array1[61] = 0
array1[62] = 0
array1[63] = 0
array1[64] = 0
array1[65] = 0
array1[66] = 0
array1[67] = 0
array1[68] = 0
array1[69] = 0
array1[70] = 0
array1[71] = 0
array1[72] = 0
array1[73] = 0
array1[74] = 1

array1[76] = 0x0D
array1[77] = 0x0A

for x in range(4, 75):
    check_sum += array1[x]

check_sum % 256

array1[75] = check_sum

ser.write(array1)


The first 4 bytes and last 2 bytes are constant to receive the requirements前 4 个字节和后 2 个字节是常量,以接收要求

My question is我的问题是

altitude=2800

array1[6] = 0
array1[7] = 0
array1[8] = 0
array1[9] = 0

these 4 bytes are reserved to keep altitude data.这 4 个字节保留用于保存高度数据。

how can I put this altitude data in an array with these byte values?如何将此高度数据放入具有这些字节值的数组中?

I would go with the struct module .我会 go 与struct module It is specifically designed to address what you are trying to do.它专门用于解决您正在尝试做的事情。

To expand on Niko's answer, this problem can be solved with Python's builtin struct library .为了扩展 Niko 的答案,可以使用 Python 的内置struct library解决这个问题。 However, you don't seem to have specified anything about the required byte order of your application.但是,您似乎没有指定有关应用程序所需字节顺序的任何内容。 For example, the following expressions would produce different results:例如,以下表达式会产生不同的结果:

big_endian = struct.pack(">f", 0.24)
little_endian = struct.pack("<f", 0.24)

print([*big_endian]) # [62, 117, 194, 143]
print([*little_endian]) # [143, 194, 117, 62], the order is reversed

You should check your receiver requirement of endianness and use the appropriate one in your Python script.您应该检查接收器的字节序要求,并在 Python 脚本中使用适当的字节序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM