繁体   English   中英

直接写入位字段的缓冲区

[英]Writing directly to buffer of a bits field

我正在研究Python中ctypes的位字段结构,我有一个问题。 是否可以直接写入下面定义的数据包联合的缓冲区?

from ctypes import c_uint8

class PacketBits(ctypes.LittleEndianStructure):
  _fields_ = [
      # First byte
      ("a", ctypes.c_uint8, 4),
      ("b", ctypes.c_uint8, 3),
      ("c", ctypes.c_uint8, 1),
      # Second byte
      ("d", ctypes.c_uint8, 5),
      ("e", ctypes.c_uint8, 3),
      # Third byte
      ("f", ctypes.c_uint8, 4),
      ("g", ctypes.c_uint8, 4),
      # Fourth byte
      ("h", ctypes.c_uint8, 2),
      ("i", ctypes.c_uint8, 6),
      # Fifth byte
      ("j", ctypes.c_uint8, 4),
      ("k", ctypes.c_uint8, 3),
      ("l", ctypes.c_uint8, 1),
      # Sixth byte
      ("m", ctypes.c_uint8, 5),
      ("n", ctypes.c_uint8, 3),
      # Seventh byte
      ("o", ctypes.c_uint8, 4),
      ("p", ctypes.c_uint8, 4),
      # Eighth byte
      ("q", ctypes.c_uint8, 2),
      ("r", ctypes.c_uint8, 6),
  ]

class Packet(ctypes.Union):
    _fields_ = [("bits", PacketBits),
                ("binary_data", 8 * c_uint8)]

我问这个问题是因为我需要解析一个65字节的流,其中包含一些位定义。

我试图用位数组直接设置它,...而我尝试的所有方法都不成功。

有人有主意吗?

最好的祝福。

约翰

我找到了一个解决方案,在Packet类中使用了setter和getter:

class Packet(ctypes.Union):
    _fields_ = [("bits", PacketBits),
                ("binary_data", 8 * ctypes.c_uint8)]

    def get_binary_data(self):
        data = []
        for byte in self.binary_data:
            data.append(byte) 

        return data

    def set_binary_data(self, data = b'\x0D\x0E\x0A\x0D\x0B\x0E\x0E\x0F'):
        if len(data) <= len(self.binary_data):
            for index in range(len(data)):
                self.binary_data[index] = data[index]

但是现在当我实例化Packet类时,我可以访问binary_data,setter和getter。

然后有两个访问binary_data的权限。

我想允许一个访问(通过setter和getter),然后尝试使用以下属性:

class Packet(ctypes.Union):
    _fields_ = [("bits", PacketBits),
                ("binary_data", 8 * ctypes.c_uint8)]

    @property
    def binary_data(self):
        data = []
        for byte in self.binary_data:
            data.append(byte) 

        return data

    @binary_data.setter
    def binary_data(self, data = b'\x0D\x0E\x0A\x0D\x0B\x0E\x0E\x0F'):
        if len(data) <= len(self.binary_data):
            for index in range(len(data)):
                self.binary_data[index] = data[index]

In [2]: p = Packet()

In [3]: p.binary_data()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-5caaf0aaa9ce> in <module>()
----> 1 p.binary_data()

TypeError: 'c_ubyte_Array_8' object is not callable

In [4]: p.binary_data
Out[4]: <test.c_ubyte_Array_8 at 0x7f7d538bf400>

In [5]: p.binary_data = b'\xaa'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-6a26479e789c> in <module>()
----> 1 p.binary_data = b'\xaa'

TypeError: expected c_ubyte_Array_8 instance, got bytes

任何人都有一个想法如何正确使用属性和二传手?

约翰

暂无
暂无

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

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