448
|
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 |
from threading import Timer, Thread, Lock
|
|
26 |
import ctypes, os, commands, types, sys
|
|
27 |
import traceback
|
453
|
28 |
from LPCProto import *
|
448
|
29 |
|
|
30 |
class LPCObject():
|
|
31 |
def __init__(self,pluginsroot):
|
|
32 |
self.PLCStatus = "Stopped"
|
|
33 |
self.pluginsroot = pluginsroot
|
|
34 |
self.PLCprint = pluginsroot.logger.write
|
453
|
35 |
self.SerialConnection = None
|
502
|
36 |
self.StorageConnection = None
|
453
|
37 |
self._Idxs = []
|
|
38 |
|
|
39 |
def HandleSerialTransaction(self, transaction):
|
|
40 |
if self.SerialConnection is None:
|
|
41 |
try:
|
|
42 |
self.SerialConnection = LPCProto(6,115200,2)
|
|
43 |
except Exception,e:
|
|
44 |
self.pluginsroot.logger.write_error(str(e)+"\n")
|
|
45 |
self.SerialConnection = None
|
|
46 |
return "Disconnected", res
|
|
47 |
try:
|
|
48 |
return self.SerialConnection.HandleTransaction(transaction)
|
|
49 |
except LPCError,e:
|
|
50 |
#pluginsroot.logger.write_error(traceback.format_exc())
|
|
51 |
self.pluginsroot.logger.write_error(str(e)+"\n")
|
|
52 |
self.SerialConnection = None
|
|
53 |
return "Disconnected", res
|
|
54 |
|
448
|
55 |
def StartPLC(self, debug=False):
|
|
56 |
PLCprint("StartPLC")
|
453
|
57 |
self.HandleSerialTransaction(STARTTransaction())
|
448
|
58 |
|
|
59 |
def StopPLC(self):
|
|
60 |
PLCprint("StopPLC")
|
453
|
61 |
self.HandleSerialTransaction(STOPTransaction())
|
448
|
62 |
|
|
63 |
def ForceReload(self):
|
453
|
64 |
pass
|
448
|
65 |
|
|
66 |
def GetPLCstatus(self):
|
453
|
67 |
status,data = self.HandleSerialTransaction(IDLETransaction())
|
|
68 |
return status
|
448
|
69 |
|
|
70 |
def NewPLC(self, md5sum, data, extrafiles):
|
453
|
71 |
pass
|
448
|
72 |
|
|
73 |
def MatchMD5(self, MD5):
|
453
|
74 |
status,data = self.HandleSerialTransaction(PLCIDTransaction())
|
|
75 |
return data == MD5
|
448
|
76 |
|
|
77 |
class IEC_STRING(ctypes.Structure):
|
|
78 |
"""
|
|
79 |
Must be changed according to changes in iec_types.h
|
|
80 |
"""
|
|
81 |
_fields_ = [("len", ctypes.c_uint8),
|
502
|
82 |
("body", ctypes.c_char * 126)]
|
448
|
83 |
|
502
|
84 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0, lambda t,x:t(x)),
|
|
85 |
"STEP" : (ctypes.c_uint8, lambda x:x.value, lambda t,x:t(x)),
|
|
86 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value, lambda t,x:t(x)),
|
|
87 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value, lambda t,x:t(x)),
|
|
88 |
"SINT" : (ctypes.c_int8, lambda x:x.value, lambda t,x:t(x)),
|
|
89 |
"USINT" : (ctypes.c_uint8, lambda x:x.value, lambda t,x:t(x)),
|
|
90 |
"BYTE" : (ctypes.c_uint8, lambda x:x.value, lambda t,x:t(x)),
|
|
91 |
"STRING" : (IEC_STRING, lambda x:x.body[:x.len], lambda t,x:t(len(x),x)),
|
|
92 |
"INT" : (ctypes.c_int16, lambda x:x.value, lambda t,x:t(x)),
|
|
93 |
"UINT" : (ctypes.c_uint16, lambda x:x.value, lambda t,x:t(x)),
|
|
94 |
"WORD" : (ctypes.c_uint16, lambda x:x.value, lambda t,x:t(x)),
|
|
95 |
"WSTRING" : (None, None, None),#TODO
|
|
96 |
"DINT" : (ctypes.c_int32, lambda x:x.value, lambda t,x:t(x)),
|
|
97 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value, lambda t,x:t(x)),
|
|
98 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value, lambda t,x:t(x)),
|
|
99 |
"LINT" : (ctypes.c_int64, lambda x:x.value, lambda t,x:t(x)),
|
|
100 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value, lambda t,x:t(x)),
|
|
101 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value, lambda t,x:t(x)),
|
|
102 |
"REAL" : (ctypes.c_float, lambda x:x.value, lambda t,x:t(x)),
|
|
103 |
"LREAL" : (ctypes.c_double, lambda x:x.value, lambda t,x:t(x)),
|
448
|
104 |
}
|
502
|
105 |
|
|
106 |
def SetTraceVariablesList(self, idxs):
|
|
107 |
self._Idxs = idxs[:]
|
|
108 |
status,data = self.HandleSerialTransaction(
|
|
109 |
SET_TRACE_VARIABLETransaction(
|
|
110 |
''.join(map(chr,idx))))
|
|
111 |
|
|
112 |
def SetTraceVariablesList(self, idxs):
|
|
113 |
"""
|
|
114 |
Call ctype imported function to append
|
|
115 |
these indexes to registred variables in PLC debugger
|
|
116 |
"""
|
|
117 |
if idxs:
|
|
118 |
buff = ""
|
|
119 |
# keep a copy of requested idx
|
|
120 |
self._Idxs = idxs[:]
|
|
121 |
for idx,iectype,force in idxs:
|
|
122 |
idxstr = ctypes.string_at(
|
|
123 |
ctypes.pointer(
|
|
124 |
ctypes.c_uint32(length)),4)
|
|
125 |
if force !=None:
|
|
126 |
c_type,unpack_func, pack_func = self.TypeTranslator.get(iectype, (None,None,None))
|
|
127 |
forcedsizestr = chr(ctypes.sizeof(c_type))
|
|
128 |
forcestr = ctypes.string_at(
|
|
129 |
ctypes.pointer(
|
|
130 |
pack_func(c_type,force)),
|
|
131 |
forced_type_size)
|
|
132 |
buff += idxstr + forced_type_size_str + forcestr
|
|
133 |
else:
|
|
134 |
buff += idxstr + chr(0)
|
|
135 |
status,data = self.HandleSerialTransaction(
|
|
136 |
SET_TRACE_VARIABLETransaction(buff))
|
|
137 |
else:
|
|
138 |
self._Idxs = []
|
|
139 |
|
448
|
140 |
def GetTraceVariables(self):
|
|
141 |
"""
|
|
142 |
Return a list of variables, corresponding to the list of required idx
|
|
143 |
"""
|
502
|
144 |
if self.PLCStatus == "Started":
|
|
145 |
res=[]
|
|
146 |
tick = ctypes.c_uint32()
|
|
147 |
size = ctypes.c_uint32()
|
|
148 |
buffer = ctypes.c_void_p()
|
|
149 |
offset = 0
|
|
150 |
if self.PLClibraryLock.acquire(False) and \
|
|
151 |
self._GetDebugData(ctypes.byref(tick),ctypes.byref(size),ctypes.byref(buffer)) == 0 :
|
|
152 |
if size.value:
|
|
153 |
for idx, iectype, forced in self._Idxs:
|
|
154 |
cursor = ctypes.c_void_p(buffer.value + offset)
|
|
155 |
c_type,unpack_func, pack_func = self.TypeTranslator.get(iectype, (None,None,None))
|
|
156 |
if c_type is not None and offset < size:
|
|
157 |
res.append(unpack_func(ctypes.cast(cursor,
|
|
158 |
ctypes.POINTER(c_type)).contents))
|
|
159 |
offset += ctypes.sizeof(c_type)
|
|
160 |
else:
|
|
161 |
if c_type is None:
|
|
162 |
PLCprint("Debug error - " + iectype + " not supported !")
|
|
163 |
if offset >= size:
|
|
164 |
PLCprint("Debug error - buffer too small !")
|
|
165 |
break
|
|
166 |
self._FreeDebugData()
|
|
167 |
self.PLClibraryLock.release()
|
|
168 |
if offset and offset == size.value:
|
|
169 |
return self.PLCStatus, tick.value, res
|
|
170 |
elif size.value:
|
|
171 |
PLCprint("Debug error - wrong buffer unpack !")
|
453
|
172 |
return self.PLCStatus, None, None
|
448
|
173 |
|