17 # |
17 # |
18 #You should have received a copy of the GNU General Public |
18 #You should have received a copy of the GNU General Public |
19 #License along with this library; if not, write to the Free Software |
19 #License along with this library; if not, write to the Free Software |
20 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
21 |
|
22 import ctypes |
|
23 ctypes.pythonapi.PyString_AsString.argtypes = (ctypes.c_void_p,) |
|
24 ctypes.pythonapi.PyString_AsString.restype = ctypes.POINTER(ctypes.c_char) |
|
25 |
|
26 |
22 from ctypes import * |
27 from ctypes import * |
23 from datetime import timedelta as td |
28 from datetime import timedelta as td |
24 |
29 |
25 class IEC_STRING(Structure): |
30 class IEC_STRING(Structure): |
26 """ |
31 """ |
27 Must be changed according to changes in iec_types.h |
32 Must be changed according to changes in iec_types.h |
28 """ |
33 """ |
29 _fields_ = [("len", c_uint8), |
34 _fields_ = [("len", c_uint8), |
30 ("body", c_char * 126)] |
35 ("body", c_char * 126)] |
31 |
36 |
32 class IEC_TIME(Structure): |
37 class IEC_TIME(Structure): |
33 """ |
38 """ |
34 Must be changed according to changes in iec_types.h |
39 Must be changed according to changes in iec_types.h |
35 """ |
40 """ |
36 _fields_ = [("s", c_long), #tv_sec |
41 _fields_ = [("s", c_long), #tv_sec |
37 ("ns", c_long)] #tv_nsec |
42 ("ns", c_long)] #tv_nsec |
38 |
43 |
39 def _t(t, u=lambda x:x.value, p=lambda t,x:t(x)): return (t, u, p) |
44 def _t(t, u=lambda x:x.value, p=lambda t,x:t(x)): return (t, u, p) |
40 def _ttime(): return (IEC_TIME, |
45 def _ttime(): return (IEC_TIME, |
41 lambda x:td(0, x.s, x.ns/1000), |
46 lambda x:td(0, x.s, x.ns/1000), |
42 lambda t,x:t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000)) |
47 lambda t,x:t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000)) |
43 |
48 |
44 SameEndianessTypeTranslator = { |
49 SameEndianessTypeTranslator = { |
45 "BOOL" : _t(c_uint8, lambda x:x.value!=0), |
50 "BOOL" : _t(c_uint8, lambda x:x.value!=0), |
46 "STEP" : _t(c_uint8), |
51 "STEP" : _t(c_uint8), |
47 "TRANSITION" : _t(c_uint8), |
52 "TRANSITION" : _t(c_uint8), |
48 "ACTION" : _t(c_uint8), |
53 "ACTION" : _t(c_uint8), |
49 "SINT" : _t(c_int8), |
54 "SINT" : _t(c_int8), |
50 "USINT" : _t(c_uint8), |
55 "USINT" : _t(c_uint8), |
51 "BYTE" : _t(c_uint8), |
56 "BYTE" : _t(c_uint8), |
52 "STRING" : (IEC_STRING, |
57 "STRING" : (IEC_STRING, |
53 lambda x:x.body[:x.len], |
58 lambda x:x.body[:x.len], |
54 lambda t,x:t(len(x),x)), |
59 lambda t,x:t(len(x),x)), |
55 "INT" : _t(c_int16), |
60 "INT" : _t(c_int16), |
56 "UINT" : _t(c_uint16), |
61 "UINT" : _t(c_uint16), |
57 "WORD" : _t(c_uint16), |
62 "WORD" : _t(c_uint16), |
58 "DINT" : _t(c_int32), |
63 "DINT" : _t(c_int32), |
65 "LREAL" : _t(c_double), |
70 "LREAL" : _t(c_double), |
66 "TIME" : _ttime(), |
71 "TIME" : _ttime(), |
67 "TOD" : _ttime(), |
72 "TOD" : _ttime(), |
68 "DATE" : _ttime(), |
73 "DATE" : _ttime(), |
69 "DT" : _ttime(), |
74 "DT" : _ttime(), |
70 } |
75 } |
71 |
76 |
72 SwapedEndianessTypeTranslator = { |
77 SwapedEndianessTypeTranslator = { |
73 #TODO |
78 #TODO |
74 } |
79 } |
75 |
80 |
76 TypeTranslator=SameEndianessTypeTranslator |
81 TypeTranslator=SameEndianessTypeTranslator |
77 |
82 |
78 # Construct debugger natively supported types |
83 # Construct debugger natively supported types |
79 DebugTypesSize = dict([(key,sizeof(t)) for key,(t,p,u) in SameEndianessTypeTranslator.iteritems() if t is not None]) |
84 DebugTypesSize = dict([(key,sizeof(t)) for key,(t,p,u) in SameEndianessTypeTranslator.iteritems() if t is not None]) |
80 |
85 |
81 def UnpackDebugBuffer(buff, size, indexes): |
86 def UnpackDebugBuffer(buff, indexes): |
82 res = [] |
87 res = [] |
83 offset = 0 |
88 buffoffset = 0 |
84 for idx, iectype, forced in indexes: |
89 buffsize = len(buff) |
85 cursor = c_void_p(buff.value + offset) |
90 buffptr = cast(ctypes.pythonapi.PyString_AsString(id(buff)),c_void_p).value |
|
91 for iectype in indexes: |
86 c_type,unpack_func, pack_func = \ |
92 c_type,unpack_func, pack_func = \ |
87 TypeTranslator.get(iectype, |
93 TypeTranslator.get(iectype, |
88 (None,None,None)) |
94 (None,None,None)) |
89 if c_type is not None and offset < size: |
95 if c_type is not None and buffoffset < buffsize: |
90 res.append(unpack_func( |
96 cursor = c_void_p( buffptr + buffoffset) |
91 cast(cursor, |
97 value = unpack_func( cast(cursor, |
92 POINTER(c_type)).contents)) |
98 POINTER(c_type)).contents) |
93 offset += sizeof(c_type) if iectype != "STRING" else len(res[-1])+1 |
99 buffoffset += sizeof(c_type) if iectype != "STRING" else len(value)+1 |
|
100 res.append(value) |
94 else: |
101 else: |
95 #if c_type is None: |
|
96 # PLCprint("Debug error - " + iectype + |
|
97 # " not supported !") |
|
98 #if offset >= size: |
|
99 # PLCprint("Debug error - buffer too small ! %d != %d"%(offset, size)) |
|
100 break |
102 break |
101 if offset and offset == size: |
103 if buffoffset and buffoffset == buffsize: |
102 return res |
104 return res |
103 return None |
105 return None |
104 |
106 |
105 |
107 |
106 |
108 |