|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 #This file is part of Beremiz, a Integrated Development Environment for |
|
5 #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 # |
|
7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 # |
|
9 #See COPYING file for copyrights details. |
|
10 # |
|
11 #This library is free software; you can redistribute it and/or |
|
12 #modify it under the terms of the GNU General Public |
|
13 #License as published by the Free Software Foundation; either |
|
14 #version 2.1 of the License, or (at your option) any later version. |
|
15 # |
|
16 #This library is distributed in the hope that it will be useful, |
|
17 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 #General Public License for more details. |
|
20 # |
|
21 #You should have received a copy of the GNU General Public |
|
22 #License along with this library; if not, write to the Free Software |
|
23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
|
25 import ctypes |
|
26 from LPCAppProto import * |
|
27 from LPCObject import * |
|
28 from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator |
|
29 |
|
30 class LPCAppObject(LPCObject): |
|
31 def connect(self,comport): |
|
32 self.SerialConnection = LPCAppProto(comport,#number |
|
33 115200, #speed |
|
34 2) #timeout |
|
35 |
|
36 def StartPLC(self, debug=False): |
|
37 self.HandleSerialTransaction(STARTTransaction()) |
|
38 |
|
39 def StopPLC(self): |
|
40 self.HandleSerialTransaction(STOPTransaction()) |
|
41 return True |
|
42 |
|
43 def ResetPLC(self): |
|
44 self.HandleSerialTransaction(RESETTransaction()) |
|
45 return self.PLCStatus |
|
46 |
|
47 def GetPLCstatus(self): |
|
48 self.HandleSerialTransaction(GET_PLCIDTransaction()) |
|
49 return self.PLCStatus |
|
50 |
|
51 def MatchMD5(self, MD5): |
|
52 data = self.HandleSerialTransaction(GET_PLCIDTransaction()) |
|
53 if data is not None: |
|
54 return data[:32] == MD5[:32] |
|
55 return False |
|
56 |
|
57 def SetTraceVariablesList(self, idxs): |
|
58 """ |
|
59 Call ctype imported function to append |
|
60 these indexes to registred variables in PLC debugger |
|
61 """ |
|
62 if idxs: |
|
63 buff = "" |
|
64 # keep a copy of requested idx |
|
65 self._Idxs = idxs[:] |
|
66 for idx,iectype,force in idxs: |
|
67 idxstr = ctypes.string_at( |
|
68 ctypes.pointer( |
|
69 ctypes.c_uint32(idx)),4) |
|
70 if force !=None: |
|
71 c_type,unpack_func, pack_func = TypeTranslator.get(iectype, (None,None,None)) |
|
72 forced_type_size = ctypes.sizeof(c_type) |
|
73 forced_type_size_str = chr(forced_type_size) |
|
74 forcestr = ctypes.string_at( |
|
75 ctypes.pointer( |
|
76 pack_func(c_type,force)), |
|
77 forced_type_size) |
|
78 buff += idxstr + forced_type_size_str + forcestr |
|
79 else: |
|
80 buff += idxstr + chr(0) |
|
81 else: |
|
82 buff = "" |
|
83 self._Idxs = [] |
|
84 |
|
85 self.HandleSerialTransaction(SET_TRACE_VARIABLETransaction(buff)) |
|
86 |
|
87 def GetTraceVariables(self): |
|
88 """ |
|
89 Return a list of variables, corresponding to the list of required idx |
|
90 """ |
|
91 offset = 0 |
|
92 strbuf = self.HandleSerialTransaction( |
|
93 GET_TRACE_VARIABLETransaction()) |
|
94 if strbuf is not None and len(strbuf) > 4 and self.PLCStatus == "Started": |
|
95 res=[] |
|
96 size = len(strbuf) - 4 |
|
97 tick = ctypes.cast( |
|
98 ctypes.c_char_p(strbuf[:4]), |
|
99 ctypes.POINTER(ctypes.c_int)).contents |
|
100 buff = ctypes.cast( |
|
101 ctypes.c_char_p(strbuf[4:]), |
|
102 ctypes.c_void_p) |
|
103 for idx, iectype, forced in self._Idxs: |
|
104 cursor = ctypes.c_void_p(buff.value + offset) |
|
105 c_type,unpack_func, pack_func = TypeTranslator.get(iectype, (None,None,None)) |
|
106 if c_type is not None and offset < size: |
|
107 res.append(unpack_func(ctypes.cast(cursor, |
|
108 ctypes.POINTER(c_type)).contents)) |
|
109 offset += ctypes.sizeof(c_type) |
|
110 else: |
|
111 #if c_type is None: |
|
112 #PLCprint("Debug error - " + iectype + " not supported !") |
|
113 #if offset >= size: |
|
114 #PLCprint("Debug error - buffer too small !") |
|
115 break |
|
116 if offset and offset == size: |
|
117 return self.PLCStatus, tick.value, res |
|
118 #PLCprint("Debug error - wrong buffer unpack !") |
|
119 return self.PLCStatus, None, [] |
|
120 |