Skip to content

UBXMessage.config_set() throws TypeError for config item of X1 type #172

Description

@fronders

Describe the bug

UBXMessage.config_set() tries to lookup attribute types and fails because of val2bytes() function for att to valb conversion

Issue is caused by code on line 281:

def val2bytes(val, att: str) -> bytes:
"""
Convert value to bytes for given UBX attribute type.
:param object val: attribute value e.g. 25
:param str att: attribute type e.g. 'U004'
:return: attribute value as bytes
:rtype: bytes
:raises: UBXTypeError
"""
if att == ubt.CH: # single variable-length string (e.g. INF-NOTICE)
return val.encode("utf-8", "backslashreplace")
atts = attsiz(att)
if atttyp(att) in ("C", "X"): # byte or char
valb = val
elif atttyp(att) in ("E", "L", "U"): # unsigned integer
valb = val.to_bytes(atts, byteorder="little", signed=False)
elif atttyp(att) == "A": # array of unsigned integers
atts = attsiz(att)
valb = b""
for i in range(atts):
valb += val[i].to_bytes(1, byteorder="little", signed=False)
elif atttyp(att) == "I": # signed integer
valb = val.to_bytes(atts, byteorder="little", signed=True)
elif att == ubt.R4: # single precision floating point
valb = struct.pack("<f", val)
elif att == ubt.R8: # double precision floating point
valb = struct.pack("<d", val)
else:
raise ube.UBXTypeError(f"Unknown attribute type {att}")
return valb

which returns integer instead of bytes in case of bitmask attribute.

Adding to_bytes() conversion fixes the issue.
This is especially critical for X8 type, where little-endian conversion is a must.
I don't see why X1/2/4/8 attributes need to be treated differently from U1/2/4/8 ones

Info

  1. The pyubx2 version 1.2.48
  2. The complete Python script.
import pyubx2

command = pyubx2.UBXMessage.config_set(
    layers=pyubx2.SET_LAYER_FLASH,
    transaction=pyubx2.TXN_NONE,
    cfgData=[(0x20920006,0)]
)
print(command)
  1. The error message and full traceback.
Traceback (most recent call last):
  File "D:\test\ublox\ublox_test.py", line 3, in <module>
    command = pyubx2.UBXMessage.config_set(
  File "D:\test\.venv\lib\site-packages\pyubx2\ubxmessage.py", line 742, in config_set
    lis = lis + keyb + valb
TypeError: can't concat int to bytes
  1. Expected Behaviour: code prints
<UBX(CFG-VALSET, version=0, ram=0, bbr=0, flash=1, action=0, reserved0=0, CFG_INFMSG_NMEA_I2C=b'\x00')>

To Reproduce

Steps to reproduce the behaviour:

  1. Try to compose any CFG-VALSET item with "X1" or "X8" type

Desktop:

  • Windows 11 x64.
  • UART1 connection via USB-UART dongle.

GNSS/GPS Device:

  • Device Model/Generation: ublox NEO-F10N
  • Firmware Version: SPG 6.00
  • Protocol: 40.00

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions