Edouard@592: #!/usr/bin/env python Edouard@592: # -*- coding: utf-8 -*- andrej@1571: andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. Edouard@592: # andrej@1571: # Copyright (C) 2011: Edouard TISSERANT and Laurent BESSARD Edouard@592: # andrej@1571: # See COPYING file for copyrights details. Edouard@592: # andrej@1571: # This program is free software; you can redistribute it and/or andrej@1571: # modify it under the terms of the GNU General Public License andrej@1571: # as published by the Free Software Foundation; either version 2 andrej@1571: # of the License, or (at your option) any later version. Edouard@592: # andrej@1571: # This program is distributed in the hope that it will be useful, andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1571: # GNU General Public License for more details. Edouard@592: # andrej@1571: # You should have received a copy of the GNU General Public License andrej@1571: # along with this program; if not, write to the Free Software andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Edouard@592: Edouard@1433: import ctypes Edouard@1433: ctypes.pythonapi.PyString_AsString.argtypes = (ctypes.c_void_p,) Edouard@1433: ctypes.pythonapi.PyString_AsString.restype = ctypes.POINTER(ctypes.c_char) Edouard@1433: Edouard@1433: Edouard@592: from ctypes import * edouard@593: from datetime import timedelta as td Edouard@592: Edouard@592: class IEC_STRING(Structure): Edouard@592: """ Edouard@592: Must be changed according to changes in iec_types.h Edouard@592: """ Edouard@592: _fields_ = [("len", c_uint8), Edouard@1433: ("body", c_char * 126)] Edouard@592: Edouard@592: class IEC_TIME(Structure): Edouard@592: """ Edouard@592: Must be changed according to changes in iec_types.h Edouard@592: """ Edouard@592: _fields_ = [("s", c_long), #tv_sec Edouard@592: ("ns", c_long)] #tv_nsec Edouard@592: Edouard@592: def _t(t, u=lambda x:x.value, p=lambda t,x:t(x)): return (t, u, p) Edouard@1433: def _ttime(): return (IEC_TIME, Edouard@1433: lambda x:td(0, x.s, x.ns/1000), Edouard@595: lambda t,x:t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000)) Edouard@592: Edouard@592: SameEndianessTypeTranslator = { Edouard@592: "BOOL" : _t(c_uint8, lambda x:x.value!=0), Edouard@592: "STEP" : _t(c_uint8), Edouard@592: "TRANSITION" : _t(c_uint8), Edouard@592: "ACTION" : _t(c_uint8), Edouard@592: "SINT" : _t(c_int8), Edouard@592: "USINT" : _t(c_uint8), Edouard@592: "BYTE" : _t(c_uint8), Edouard@1433: "STRING" : (IEC_STRING, Edouard@1433: lambda x:x.body[:x.len], Edouard@592: lambda t,x:t(len(x),x)), Edouard@592: "INT" : _t(c_int16), Edouard@592: "UINT" : _t(c_uint16), Edouard@592: "WORD" : _t(c_uint16), Edouard@592: "DINT" : _t(c_int32), Edouard@592: "UDINT" : _t(c_uint32), Edouard@592: "DWORD" : _t(c_uint32), Edouard@592: "LINT" : _t(c_int64), Edouard@592: "ULINT" : _t(c_uint64), Edouard@592: "LWORD" : _t(c_uint64), Edouard@592: "REAL" : _t(c_float), Edouard@592: "LREAL" : _t(c_double), Edouard@595: "TIME" : _ttime(), Edouard@595: "TOD" : _ttime(), Edouard@595: "DATE" : _ttime(), Edouard@595: "DT" : _ttime(), Edouard@1433: } Edouard@592: Edouard@592: SwapedEndianessTypeTranslator = { Edouard@592: #TODO Edouard@1433: } Edouard@592: Edouard@1075: TypeTranslator=SameEndianessTypeTranslator Edouard@1075: Edouard@592: # Construct debugger natively supported types Edouard@592: DebugTypesSize = dict([(key,sizeof(t)) for key,(t,p,u) in SameEndianessTypeTranslator.iteritems() if t is not None]) Edouard@592: Edouard@1433: def UnpackDebugBuffer(buff, indexes): Edouard@1433: res = [] Edouard@1433: buffoffset = 0 Edouard@1433: buffsize = len(buff) Edouard@1433: buffptr = cast(ctypes.pythonapi.PyString_AsString(id(buff)),c_void_p).value Edouard@1433: for iectype in indexes: Edouard@1075: c_type,unpack_func, pack_func = \ Edouard@1075: TypeTranslator.get(iectype, Edouard@1075: (None,None,None)) Edouard@1433: if c_type is not None and buffoffset < buffsize: Edouard@1433: cursor = c_void_p( buffptr + buffoffset) Edouard@1433: value = unpack_func( cast(cursor, Edouard@1433: POINTER(c_type)).contents) Edouard@1433: buffoffset += sizeof(c_type) if iectype != "STRING" else len(value)+1 Edouard@1433: res.append(value) Edouard@1075: else: Edouard@1075: break Edouard@1433: if buffoffset and buffoffset == buffsize: Edouard@1075: return res Edouard@1075: return None Edouard@1075: Edouard@1075: Edouard@1075: Edouard@917: LogLevels = ["CRITICAL","WARNING","INFO","DEBUG"] Edouard@917: LogLevelsCount = len(LogLevels) Edouard@917: LogLevelsDict = dict(zip(LogLevels,range(LogLevelsCount))) Edouard@917: LogLevelsDefault = LogLevelsDict["DEBUG"] Edouard@972: