diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index f19e430..b60dad3 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -6,6 +6,7 @@ CHANGES: 1. Add val2sphp helper method to convert high precision (9dp) coordinate to separate standard and high precision components, as required by some CFG and NAV messages. 1. Add utc2itow helper method to convert utc datetime to GPS week number and time of week. +1. Add getinputmode helper to determinate mode of input UBX message (SET or POLL) ### RELEASE 1.2.37 diff --git a/src/pyubx2/ubxhelpers.py b/src/pyubx2/ubxhelpers.py index e20a318..205c380 100644 --- a/src/pyubx2/ubxhelpers.py +++ b/src/pyubx2/ubxhelpers.py @@ -20,7 +20,7 @@ import pyubx2.exceptions as ube import pyubx2.ubxtypes_configdb as ubcdb import pyubx2.ubxtypes_core as ubt -from pyubx2.ubxtypes_core import UBX_HDR +from pyubx2.ubxtypes_core import POLL, SET, UBX_HDR from pyubx2.ubxtypes_decodes import FIXTYPE, GNSSLIST EPOCH0 = datetime(1980, 1, 6) # EPOCH start date @@ -529,3 +529,30 @@ def val2sphp(val: float, scale: float = 1e-7) -> tuple: val_sp = trunc(val) val_hp = round((val - val_sp) * 100) return val_sp, val_hp + + +def getinputmode(data: bytes) -> int: + """ + Return input message mode (SET or POLL). + + :param bytes data: raw UBX input message + :return: message mode (1 = SET, 2 = POLL) + :rtype: int + """ + + if ( + len(data) == 8 + or data[2:4] == b"\x06\x8b" # CFG-VALGET + or ( + data[2:4] + in ( + b"\x06\x01", + b"\x06\x02", + b"\x06\x03", + b"\x06\x31", + ) # CFG-INF, CFG-MSG, CFG-PRT, CFG-TP5 + and len(data) <= 10 + ) + ): + return POLL + return SET diff --git a/tests/test_static.py b/tests/test_static.py index 8ab2e9d..712d3b7 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -13,7 +13,7 @@ import unittest from datetime import datetime import pyubx2.ubxtypes_core as ubt -from pyubx2 import POLL, UBX_CLASSES, UBXMessage, UBXReader +from pyubx2 import SET, POLL, UBX_CLASSES, UBXMessage, UBXReader from pyubx2.ubxhelpers import ( att2idx, att2name, @@ -25,6 +25,7 @@ dop2str, escapeall, get_bits, + getinputmode, gnss2str, gpsfix2str, hextable, @@ -290,6 +291,17 @@ def testval2sphp(self): res = val2sphp(5.9876543) self.assertEqual(res, (59876543,0)) + def testgetinputmode(self): + res = getinputmode(UBXMessage("CFG","CFG-ODO", POLL).serialize()) + self.assertEqual(res, POLL) + res = getinputmode(UBXMessage.config_poll(0,0,["CFG_UART1_BAUDRATE", 0x40530001]).serialize()) + self.assertEqual(res, POLL) + res = getinputmode(UBXMessage.config_set(0,0,[("CFG_UART1_BAUDRATE", 9600), (0x40530001, 115200)]).serialize()) + self.assertEqual(res, SET) + res = getinputmode(UBXMessage.config_del(0,0,["CFG_UART1_BAUDRATE", 0x40530001]).serialize()) + self.assertEqual(res, SET) + res = getinputmode(UBXMessage("CFG","CFG-INF", POLL, protocolID=1).serialize()) + self.assertEqual(res, POLL) if __name__ == "__main__": # import sys;sys.argv = ['', 'Test.testName']