1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
class DataField(object):
def __init__(self, name, offset):
self.name = name
self.offset = offset
def value(self,data):
raise NotImplemented()
class Data1b(DataField):
"""
Beispiel für die Berechnung:
if ((x & 80h) == 80h) // y negativ
y = - [dez(!x) + 1]
else
y = dez(x)
"""
def value(self,data):
x = ord(data[self.offset])
if x & 0x80 == 0x80:
return (-1) * (0xff^x + 1)
else:
return x
class Data1c(DataField):
def value(self,data):
return ord(data[self.offset])/2.0
class Data2c(DataField):
"""
Beispiel für die Berechnung:
if ((x & 8000h) == 8000h) // y negativ
y = - [dez(High_Byte(!x)) 16 + dez(High_Nibble (Low_Byte (!x)))
+ (dez(Low_Nibble (Low_Byte (!x))) +1 ) / 16]
else // y positiv
y = dez(High_Byte(x)) 16 + dez(High_ Nibble (Low Byte (x)))
+ dez(Low_ Nibble (Low Byte (x))) / 16
"""
def value(self,data):
highByte = ord(data[self.offset+1])
lowByte= ord(data[self.offset])
if (0x8000 & (highByte<<8 | lowByte)) == 0x8000:
return (-1) * ( (0xff^highByte)*16 + (0xff^(lowByte>>4)) + (0x0ff^(0xf&lowByte) + 1)/16.0 )
else:
return highByte*16 + (lowByte>>4) + (lowByte&0xf)/16.0
class Data2b(DataField):
"""
if ((x&8000h) == 8000h) // y negativ
y = - [dez(High_Byte(!x)) + (dez(Low_Byte(!x)) + 1) / 256]
else // y positiv
y = dez(High_Byte (x)) + dez(Low_Byte (x)) / 256
"""
def value(self,data):
highByte = ord(data[self.offset+1])
lowByte= ord(data[self.offset])
if (0x8000 & (highByte<<8 | lowByte)) == 0x8000:
return (-1) * ((0xff^highByte) + (0xff^lowByte+1)/256.0)
else:
return highByte + lowByte/256.0
class Bit(DataField):
def value(self, data):
return ord(data[self.offset]) == 0x1
class Word(DataField):
def value(self, data):
lb, hb = data[self.offset], data[self.offset+1]
return ord(lb) + (ord(hb)<<8)
class Bcd(DataField):
"""
y = dez(High_Nibble(x))*10 + dez(Low_Nibble(x))
"""
def value(self, data):
byte = ord(data[self.offset])
return (byte >> 4) * 10 + (byte & 0xf)
class Byte(DataField):
def value(self, data):
return ord(data[self.offset])
class ByteEnum(DataField):
def __init__(self, name, offset, values):
fields.DataField.__init__(self, name, offset)
self.values = values
def value(self, data):
value = ord(data[self.offset])
if self.values.has_key(value):
return self.values[value]
else:
return None
|