# -*- coding:utf8 -*- 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): 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