Lolitech@545: #!/usr/bin/env python
Lolitech@545: # -*- coding: utf-8 -*-
Lolitech@545: 
Lolitech@545: #This file is part of Beremiz, a Integrated Development Environment for
Lolitech@545: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
Lolitech@545: #
Lolitech@545: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Lolitech@545: #
Lolitech@545: #See COPYING file for copyrights details.
Lolitech@545: #
Lolitech@545: #This library is free software; you can redistribute it and/or
Lolitech@545: #modify it under the terms of the GNU General Public
Lolitech@545: #License as published by the Free Software Foundation; either
Lolitech@545: #version 2.1 of the License, or (at your option) any later version.
Lolitech@545: #
Lolitech@545: #This library is distributed in the hope that it will be useful,
Lolitech@545: #but WITHOUT ANY WARRANTY; without even the implied warranty of
Lolitech@545: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lolitech@545: #General Public License for more details.
Lolitech@545: #
Lolitech@545: #You should have received a copy of the GNU General Public
Lolitech@545: #License along with this library; if not, write to the Free Software
Lolitech@545: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Lolitech@545: 
Lolitech@545: import ctypes
Lolitech@545: from LPCAppProto import *
Lolitech@545: from LPCObject import *
Edouard@592: from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator
Lolitech@545: 
Lolitech@545: class LPCAppObject(LPCObject):
Lolitech@545:     def connect(self,comport):
Lolitech@545:         self.SerialConnection = LPCAppProto(comport,#number
Lolitech@545:                                          115200, #speed
Lolitech@545:                                          2)      #timeout
Lolitech@545: 
Lolitech@545:     def StartPLC(self, debug=False):
Lolitech@545:         self.HandleSerialTransaction(STARTTransaction())
Lolitech@545:             
Lolitech@545:     def StopPLC(self):
Lolitech@545:         self.HandleSerialTransaction(STOPTransaction())
Lolitech@558:         return True
Lolitech@545: 
Lolitech@548:     def ResetPLC(self):
Lolitech@545:         self.HandleSerialTransaction(RESETTransaction())
Lolitech@545:         return self.PLCStatus
Lolitech@545: 
Lolitech@548:     def GetPLCstatus(self):
edouard@575:         self.HandleSerialTransaction(GET_PLCIDTransaction())
Lolitech@545:         return self.PLCStatus
Lolitech@545: 
Lolitech@545:     def MatchMD5(self, MD5):
Lolitech@545:         data = self.HandleSerialTransaction(GET_PLCIDTransaction())
laurent@562:         if data is not None:
edouard@575:             return data[:32] == MD5[:32]
laurent@562:         return False
Lolitech@545: 
Lolitech@545:     def SetTraceVariablesList(self, idxs):
Lolitech@545:         """
Lolitech@545:         Call ctype imported function to append 
Lolitech@545:         these indexes to registred variables in PLC debugger
Lolitech@545:         """
Lolitech@545:         if idxs:
Lolitech@545:             buff = ""
Lolitech@545:             # keep a copy of requested idx
Lolitech@545:             self._Idxs = idxs[:]
Lolitech@545:             for idx,iectype,force in idxs:
Lolitech@545:                 idxstr = ctypes.string_at(
Lolitech@545:                           ctypes.pointer(
edouard@576:                            ctypes.c_uint32(idx)),4)
Lolitech@545:                 if force !=None:
edouard@593:                     c_type,unpack_func, pack_func = TypeTranslator.get(iectype, (None,None,None))
edouard@576:                     forced_type_size = ctypes.sizeof(c_type)
edouard@577:                     forced_type_size_str = chr(forced_type_size)
Lolitech@545:                     forcestr = ctypes.string_at(
Lolitech@545:                                 ctypes.pointer(
Lolitech@545:                                  pack_func(c_type,force)),
Lolitech@545:                                  forced_type_size)
Lolitech@545:                     buff += idxstr + forced_type_size_str + forcestr
Lolitech@545:                 else:
Lolitech@545:                     buff += idxstr + chr(0)
Lolitech@545:         else:
edouard@578:             buff = ""
Lolitech@545:             self._Idxs =  []
Lolitech@545: 
edouard@578:         self.HandleSerialTransaction(SET_TRACE_VARIABLETransaction(buff))
edouard@578: 
Lolitech@545:     def GetTraceVariables(self):
Lolitech@545:         """
Lolitech@545:         Return a list of variables, corresponding to the list of required idx
Lolitech@545:         """
Lolitech@545:         offset = 0
Lolitech@545:         strbuf = self.HandleSerialTransaction(
Lolitech@545:                                      GET_TRACE_VARIABLETransaction())
edouard@576:         if strbuf is not None and len(strbuf) > 4 and self.PLCStatus == "Started":
edouard@577:             res=[]
edouard@576:             size = len(strbuf) - 4
Lolitech@545:             tick = ctypes.cast(
Lolitech@545:                     ctypes.c_char_p(strbuf[:4]),
Lolitech@545:                     ctypes.POINTER(ctypes.c_int)).contents
edouard@577:             buff = ctypes.cast(
Lolitech@545:                       ctypes.c_char_p(strbuf[4:]),
Lolitech@545:                       ctypes.c_void_p)
Lolitech@545:             for idx, iectype, forced in self._Idxs:
edouard@577:                 cursor = ctypes.c_void_p(buff.value + offset)
edouard@593:                 c_type,unpack_func, pack_func = TypeTranslator.get(iectype, (None,None,None))
Lolitech@545:                 if c_type is not None and offset < size:
Lolitech@545:                     res.append(unpack_func(ctypes.cast(cursor,
Lolitech@545:                                                        ctypes.POINTER(c_type)).contents))
Lolitech@545:                     offset += ctypes.sizeof(c_type)
Lolitech@545:                 else:
Lolitech@557:                     #if c_type is None:
Lolitech@557:                         #PLCprint("Debug error - " + iectype + " not supported !")
Lolitech@557:                     #if offset >= size:
Lolitech@557:                         #PLCprint("Debug error - buffer too small !")
Lolitech@545:                     break
Lolitech@545:             if offset and offset == size:
Lolitech@545:                 return self.PLCStatus, tick.value, res
Lolitech@557:             #PLCprint("Debug error - wrong buffer unpack !")
edouard@578:         return self.PLCStatus, None, [] 
Lolitech@545: