mirror of
https://github.com/JoelBender/modpypes
synced 2025-10-26 21:49:19 +08:00
add a decoding for strings that are octet flipped, string data types now have an optional register length
This commit is contained in:
parent
14392ffd51
commit
9cdc89cb49
|
|
@ -293,7 +293,11 @@ class String(_Struct):
|
||||||
This class packs and unpacks a list of registers as a null terminated string.
|
This class packs and unpacks a list of registers as a null terminated string.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
registerLength = 6
|
def __init__(self, registerLength=6):
|
||||||
|
if _debug: String._debug("__init__ %r", registerLength)
|
||||||
|
|
||||||
|
# save the length
|
||||||
|
self.registerLength = registerLength
|
||||||
|
|
||||||
def pack(self, value):
|
def pack(self, value):
|
||||||
if _debug: String._debug("pack %r", value)
|
if _debug: String._debug("pack %r", value)
|
||||||
|
|
@ -311,6 +315,35 @@ class String(_Struct):
|
||||||
value = value[:value.find('\x00')]
|
value = value[:value.find('\x00')]
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@class_debugging
|
||||||
|
class BigEndianString(_Struct):
|
||||||
|
|
||||||
|
"""
|
||||||
|
This class packs and unpacks a list of registers as a null terminated string.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, registerLength=6):
|
||||||
|
if _debug: String._debug("__init__ %r", registerLength)
|
||||||
|
|
||||||
|
# save the length
|
||||||
|
self.registerLength = registerLength
|
||||||
|
|
||||||
|
def pack(self, value):
|
||||||
|
if _debug: String._debug("pack %r", value)
|
||||||
|
raise NotImplementedError("packing strings is not implemeted")
|
||||||
|
|
||||||
|
def unpack(self, registers):
|
||||||
|
if _debug: String._debug("unpack %r", registers)
|
||||||
|
|
||||||
|
octets = []
|
||||||
|
for reg in registers:
|
||||||
|
octets.append(reg & 0xFF)
|
||||||
|
octets.append(reg >> 8)
|
||||||
|
|
||||||
|
value = ''.join(chr(c) for c in octets)
|
||||||
|
value = value[:value.find('\x00')]
|
||||||
|
return value
|
||||||
|
|
||||||
#
|
#
|
||||||
# ModbusStruct
|
# ModbusStruct
|
||||||
#
|
#
|
||||||
|
|
@ -327,6 +360,7 @@ ModbusStruct = {
|
||||||
'be-udint': BigEndianUnsignedDoubleInt(),
|
'be-udint': BigEndianUnsignedDoubleInt(),
|
||||||
'be-real': BigEndianReal(),
|
'be-real': BigEndianReal(),
|
||||||
'str': String(),
|
'str': String(),
|
||||||
|
'be-str': BigEndianString(),
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user