vixexe9871
vixexe9871

Reputation: 21

Python: bytearray to ctypes array

I am trying to write bytearray into the ctypes c_uint8 buffer of a ctypes structure

class RpRes(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("count", c_uint16),
        ("buf", c_uint8 * 512)
    ]

def read_func(req):
    res = RpRes()
    buf = os.read(req.fd, req.count)
    res.buf.from_buffer(buf)
    res.count = len(buf)
    return res

res.buf.from_buffer(buf) gives the below error:

AttributeError: 'c_ubyte_Array_512' object has no attribute 'from_buffer'

How can this be accomplished?

Upvotes: 1

Views: 447

Answers (1)

vixexe9871
vixexe9871

Reputation: 21

This worked for me.

def read_func(req):
    res = RpRes()
    buf = os.read(req.fd, req.count)
    res.buf = (c_uint8 * sizeof(res.buf))(*buf)
    res.count = len(buf)
    return res

Upvotes: 1

Related Questions