1
0
mirror of https://github.com/JoelBender/modpypes synced 2025-10-19 22:08:20 +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:
Joel Bender 2015-07-09 10:07:32 -04:00
parent 14392ffd51
commit 9cdc89cb49

View File

@ -293,7 +293,11 @@ class String(_Struct):
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):
if _debug: String._debug("pack %r", value)
@ -311,6 +315,35 @@ class String(_Struct):
value = value[:value.find('\x00')]
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
#
@ -327,6 +360,7 @@ ModbusStruct = {
'be-udint': BigEndianUnsignedDoubleInt(),
'be-real': BigEndianReal(),
'str': String(),
'be-str': BigEndianString(),
}
#