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
|
|
36 |
self._Idxs = []
|
|
37 |
|
|
38 |
def HandleSerialTransaction(self, transaction):
|
|
39 |
if self.SerialConnection is None:
|
|
40 |
try:
|
|
41 |
self.SerialConnection = LPCProto(6,115200,2)
|
|
42 |
except Exception,e:
|
|
43 |
self.pluginsroot.logger.write_error(str(e)+"\n")
|
|
44 |
self.SerialConnection = None
|
|
45 |
return "Disconnected", res
|
|
46 |
try:
|
|
47 |
return self.SerialConnection.HandleTransaction(transaction)
|
|
48 |
except LPCError,e:
|
|
49 |
#pluginsroot.logger.write_error(traceback.format_exc())
|
|
50 |
self.pluginsroot.logger.write_error(str(e)+"\n")
|
|
51 |
self.SerialConnection = None
|
|
52 |
return "Disconnected", res
|
|
53 |
|
448
|
54 |
def StartPLC(self, debug=False):
|
|
55 |
PLCprint("StartPLC")
|
453
|
56 |
self.HandleSerialTransaction(STARTTransaction())
|
448
|
57 |
|
|
58 |
def StopPLC(self):
|
|
59 |
PLCprint("StopPLC")
|
453
|
60 |
self.HandleSerialTransaction(STOPTransaction())
|
448
|
61 |
|
|
62 |
def ForceReload(self):
|
453
|
63 |
pass
|
448
|
64 |
|
|
65 |
def GetPLCstatus(self):
|
453
|
66 |
status,data = self.HandleSerialTransaction(IDLETransaction())
|
|
67 |
return status
|
448
|
68 |
|
|
69 |
def NewPLC(self, md5sum, data, extrafiles):
|
453
|
70 |
pass
|
448
|
71 |
|
|
72 |
def MatchMD5(self, MD5):
|
453
|
73 |
status,data = self.HandleSerialTransaction(PLCIDTransaction())
|
|
74 |
return data == MD5
|
448
|
75 |
|
|
76 |
def SetTraceVariablesList(self, idxs):
|
453
|
77 |
self._Idxs = idxs[]
|
|
78 |
status,data = self.HandleSerialTransaction(
|
|
79 |
SET_TRACE_VARIABLETransaction(
|
|
80 |
''.join(map(chr,idx))))
|
448
|
81 |
|
|
82 |
class IEC_STRING(ctypes.Structure):
|
|
83 |
"""
|
|
84 |
Must be changed according to changes in iec_types.h
|
|
85 |
"""
|
|
86 |
_fields_ = [("len", ctypes.c_uint8),
|
|
87 |
("body", ctypes.c_char * 127)]
|
|
88 |
|
|
89 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0),
|
|
90 |
"STEP" : (ctypes.c_uint8, lambda x:x.value),
|
|
91 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value),
|
|
92 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value),
|
|
93 |
"SINT" : (ctypes.c_int8, lambda x:x.value),
|
|
94 |
"USINT" : (ctypes.c_uint8, lambda x:x.value),
|
|
95 |
"BYTE" : (ctypes.c_uint8, lambda x:x.value),
|
|
96 |
"STRING" : (IEC_STRING, lambda x:x.body[:x.len]),
|
|
97 |
"INT" : (ctypes.c_int16, lambda x:x.value),
|
|
98 |
"UINT" : (ctypes.c_uint16, lambda x:x.value),
|
|
99 |
"WORD" : (ctypes.c_uint16, lambda x:x.value),
|
|
100 |
"WSTRING" : (None, None),#TODO
|
|
101 |
"DINT" : (ctypes.c_int32, lambda x:x.value),
|
|
102 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value),
|
|
103 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value),
|
|
104 |
"LINT" : (ctypes.c_int64, lambda x:x.value),
|
|
105 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value),
|
|
106 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value),
|
|
107 |
"REAL" : (ctypes.c_float, lambda x:x.value),
|
|
108 |
"LREAL" : (ctypes.c_double, lambda x:x.value),
|
|
109 |
}
|
|
110 |
|
|
111 |
def GetTraceVariables(self):
|
|
112 |
"""
|
|
113 |
Return a list of variables, corresponding to the list of required idx
|
|
114 |
"""
|
453
|
115 |
status,data = self.HandleSerialTransaction(GET_TRACE_VARIABLETransaction())
|
|
116 |
if data is not None:
|
|
117 |
# transform serial string to real byte string in memory
|
|
118 |
buffer = ctypes.c_char_p(data)
|
|
119 |
# tick is first value in buffer
|
|
120 |
tick = ctypes.cast(buffer,ctypes.POINTER(ctypes.c_uint32)).contents
|
|
121 |
# variable data starts just after tick
|
|
122 |
cursorp = ctypes.addressof(buffer) = ctypes.sizeof(ctypes.c_uint32)
|
|
123 |
endp = offset + len(data)
|
|
124 |
for idx, iectype in self._Idxs:
|
|
125 |
cursor = ctypes.c_void_p(cursorp)
|
|
126 |
c_type,unpack_func = self.TypeTranslator.get(iectype, (None,None))
|
|
127 |
if c_type is not None and cursorp < endp:
|
|
128 |
res.append(unpack_func(ctypes.cast(cursor,
|
|
129 |
ctypes.POINTER(c_type)).contents))
|
|
130 |
cursorp += ctypes.sizeof(c_type)
|
|
131 |
else:
|
|
132 |
PLCprint("Debug error !")
|
|
133 |
break
|
|
134 |
return self.PLCStatus, tick, res
|
|
135 |
return self.PLCStatus, None, None
|
448
|
136 |
|