Skip to content

SendDREF fails in Python when sending a list #198

@MadisonTBlake

Description

@MadisonTBlake

In Python the method SendDREF(s) fails when packing a list passing at the following location:

if hasattr(value, "__len__"):
    if len(value) > 255:
        raise ValueError("value must have less than 256 items.")
    fmt = "<B{0:d}sB{1:d}f".format(len(dref), len(value))

    # The next line will fail
    buffer += struct.pack(fmt.encode(), len(dref), dref.encode(), len(value), value)
else:
    fmt = "<B{0:d}sBf".format(len(dref))
    buffer += struct.pack(fmt.encode(), len(dref), dref.encode(), 1, value)

The failure depends on the size of the array being passed. For example if passing an array of size one the following failure is reported:
self._client.sendDREF("sim/multiplayer/controls/yoke_roll_ratio", [1])

buffer += struct.pack(fmt.encode(), len(dref), dref.encode(), len(value), value)
struct.error: required argument is not a float

If passing an array larger than one the error is different:
self._client.sendDREF("sim/multiplayer/controls/yoke_roll_ratio", [0, 1])

buffer += struct.pack(fmt.encode(), len(dref), dref.encode(), len(value), value)
struct.error: pack expected 5 items for packing (got 4)

For the DREF in question the docs state that I can accept an array of size 20:
yoke_roll_ratio | float[20] | 860+ | yes | [-1..1] | The deflection of the axis controlling roll.

For my purposes I did a quick and dirty fix that only works for lists up to size 2:

if len(value) == 1:
    buffer += struct.pack(fmt.encode(), len(dref), dref.encode(), 1, value[0])
elif len(value) == 2:
    buffer += struct.pack(fmt.encode(), len(dref), dref.encode(), 2, value[0], value[1])
else:
    raise "Array of size " + str(len(value)) + " Not implemented"

After putting this kludge in the DREFS in question adjust the simulator as they are supposed to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions