Refactor
This commit is contained in:
parent
fb49b5ccaf
commit
a277a94ce9
@ -57,49 +57,8 @@ def get_mnemonic( m, se, elm, raw = 0 ):
|
||||
if not all(c in string.hexdigits for c in resp): resp = ''
|
||||
resp = ' '.join(a+b for a,b in zip(resp[::2], resp[1::2]))
|
||||
if len(m.startByte)==0: m.startByte = u'01'
|
||||
|
||||
#prepare local variables
|
||||
sb = int(m.startByte) - 1
|
||||
bits = int(m.bitsLength)
|
||||
sbit = int(m.startBit)
|
||||
bytes = (bits+sbit-1)/8+1
|
||||
rshift = ((bytes+1)*8 - (bits+sbit))%8
|
||||
|
||||
#check length of responce
|
||||
if (sb*3+bytes*3-1)>(len(resp)):
|
||||
return '00'
|
||||
|
||||
#extract hex
|
||||
hexval = resp[sb*3:(sb+bytes)*3-1]
|
||||
hexval = hexval.replace(" ","")
|
||||
|
||||
if raw:
|
||||
if resp.startswith(m.positive):
|
||||
return hexval
|
||||
else:
|
||||
return 'ERROR'
|
||||
|
||||
#shift and mask
|
||||
val = (int(hexval,16)>>rshift)&(2**bits-1)
|
||||
|
||||
#format result
|
||||
hexval = hex(val)[2:]
|
||||
#remove 'L'
|
||||
if hexval[-1:].upper()=='L':
|
||||
hexval = hexval[:-1]
|
||||
#add left zero if need
|
||||
if len(hexval)%2:
|
||||
hexval = '0'+hexval
|
||||
|
||||
#revert byte order if little endian
|
||||
if m.littleEndian == '1':
|
||||
a = hexval
|
||||
b = ''
|
||||
if not len(a) % 2:
|
||||
for i in range(0,len(a),2):
|
||||
b = a[i:i+2]+b
|
||||
hexval = b
|
||||
|
||||
hexval = getHexVal(m.startByte, m.bitsLength, m.startBit, m.littleEndian, resp)
|
||||
return hexval
|
||||
|
||||
def get_SnapShotMnemonic(m, se, elm, dataids):
|
||||
@ -137,20 +96,24 @@ def get_SnapShotMnemonic(m, se, elm, dataids):
|
||||
dataId = did
|
||||
startByte = dataids[dataId].mnemolocations[m.name].startByte
|
||||
startBit = dataids[dataId].mnemolocations[m.name].startBit
|
||||
|
||||
hexval = getHexVal(startByte, m.bitsLength, startBit, m.littleEndian, didDict[dataId])
|
||||
return hexval
|
||||
|
||||
def getHexVal(startByte, bitsLength, startBit, littleEndian, resp):
|
||||
#prepare local variables
|
||||
sb = int(startByte) - 1
|
||||
bits = int(m.bitsLength)
|
||||
bits = int(bitsLength)
|
||||
sbit = int(startBit)
|
||||
bytes = (bits+sbit-1)/8+1
|
||||
rshift = ((bytes+1)*8 - (bits+sbit))%8
|
||||
|
||||
#check length of responce
|
||||
if (sb*3+bytes*3-1)>(len(didDict[dataId])):
|
||||
if (sb*3+bytes*3-1)>(len(resp)):
|
||||
return '00'
|
||||
|
||||
#extract hex
|
||||
hexval = didDict[dataId][sb*3:(sb+bytes)*3-1]
|
||||
hexval = resp[sb*3:(sb+bytes)*3-1]
|
||||
hexval = hexval.replace(" ","")
|
||||
|
||||
#shift and mask
|
||||
@ -166,7 +129,7 @@ def get_SnapShotMnemonic(m, se, elm, dataids):
|
||||
hexval = '0'+hexval
|
||||
|
||||
#revert byte order if little endian
|
||||
if m.littleEndian == '1':
|
||||
if littleEndian == '1':
|
||||
a = hexval
|
||||
b = ''
|
||||
if not len(a) % 2:
|
||||
|
Loading…
x
Reference in New Issue
Block a user