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