PLCGenerator.py
changeset 141 c0242a51774c
parent 128 d16a8df4d322
child 151 aaa80b48bead
equal deleted inserted replaced
140:06d28f03f6f4 141:c0242a51774c
    59         datatypeComputed[datatype_name] = True
    59         datatypeComputed[datatype_name] = True
    60         global currentProject, currentProgram
    60         global currentProject, currentProgram
    61         datatype = currentProject.getDataType(datatype_name)
    61         datatype = currentProject.getDataType(datatype_name)
    62         datatype_def = "  %s :"%datatype.getName()
    62         datatype_def = "  %s :"%datatype.getName()
    63         basetype_content = datatype.baseType.getContent()
    63         basetype_content = datatype.baseType.getContent()
    64         if basetype_content["value"] is None:
    64         if basetype_content["name"] in ["string", "wstring"]:
    65             datatype_def += " %s"%basetype_content["name"]
    65             datatype_def += " %s"%basetype_content["name"].upper()
    66         elif basetype_content["name"] == "derived":
    66         elif basetype_content["name"] == "derived":
    67             basetype_name = basetype_content["value"].getName()
    67             basetype_name = basetype_content["value"].getName()
    68             GenerateDataType(basetype_name)
    68             GenerateDataType(basetype_name)
    69             datatype_def += " %s"%basetype_name
    69             datatype_def += " %s"%basetype_name
    70         elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]:
    70         elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]:
    71             base_type = basetype_content["value"].baseType.getContent()
    71             base_type = basetype_content["value"].baseType.getContent()
    72             if base_type["value"] is None:
    72             if base_type["name"] == "derived":
       
    73                 basetype_name = base_type["value"].getName()
       
    74             else:
    73                 basetype_name = base_type["name"]
    75                 basetype_name = base_type["name"]
    74             else:
       
    75                 basetype_name = base_type["value"].getName()
       
    76                 GenerateDataType(basetype_name)
    76                 GenerateDataType(basetype_name)
    77             min_value = basetype_content["value"].range.getLower()
    77             min_value = basetype_content["value"].range.getLower()
    78             max_value = basetype_content["value"].range.getUpper()
    78             max_value = basetype_content["value"].range.getUpper()
    79             datatype_def += " %s (%d..%d)"%(basetype_name, min_value, max_value)
    79             datatype_def += " %s (%d..%d)"%(basetype_name, min_value, max_value)
    80         elif basetype_content["name"] == "enum":
    80         elif basetype_content["name"] == "enum":
    82             for value in basetype_content["value"].values.getValue():
    82             for value in basetype_content["value"].values.getValue():
    83                 values.append(value.getName())
    83                 values.append(value.getName())
    84             datatype_def += " (%s)"%", ".join(values)
    84             datatype_def += " (%s)"%", ".join(values)
    85         elif basetype_content["name"] == "array":
    85         elif basetype_content["name"] == "array":
    86             base_type = basetype_content["value"].baseType.getContent()
    86             base_type = basetype_content["value"].baseType.getContent()
    87             if base_type["value"] is None:
    87             if base_type["name"] == "derived":
       
    88                 basetype_name = base_type["value"].getName()
       
    89             elif base_type["name"] in ["string", "wstring"]:
       
    90                 basetype_name = base_type["name"].upper()
       
    91             else:
    88                 basetype_name = base_type["name"]
    92                 basetype_name = base_type["name"]
    89             else:
       
    90                 basetype_name = base_type["value"].getName()
       
    91                 GenerateDataType(basetype_name)
    93                 GenerateDataType(basetype_name)
    92             dimensions = []
    94             dimensions = []
    93             for dimension in basetype_content["value"].getDimension():
    95             for dimension in basetype_content["value"].getDimension():
    94                 dimensions.append("0..%d"%(dimension.getUpper() - 1))
    96                 dimensions.append("0..%d"%(dimension.getUpper() - 1))
    95             datatype_def += " ARRAY [%s] OF %s"%(",".join(dimensions), basetype_name)
    97             datatype_def += " ARRAY [%s] OF %s"%(",".join(dimensions), basetype_name)
       
    98         else:
       
    99             datatype_def += " %s"%basetype_content["name"]
    96         if datatype.initialValue is not None:
   100         if datatype.initialValue is not None:
    97             datatype_def += " := %s"%str(datatype.initialValue.getValue())
   101             datatype_def += " := %s"%str(datatype.initialValue.getValue())
    98         currentProgram += "%s;\n"%datatype_def
   102         currentProgram += "%s;\n"%datatype_def
    99 
   103 
   100 def GeneratePouProgram(pou_name):
   104 def GeneratePouProgram(pou_name):
   121         if varlist.getConstant():
   125         if varlist.getConstant():
   122             config += " CONSTANT"
   126             config += " CONSTANT"
   123         config += "\n"
   127         config += "\n"
   124         for var in varlist.getVariable():
   128         for var in varlist.getVariable():
   125             vartype_content = var.getType().getContent()
   129             vartype_content = var.getType().getContent()
   126             if vartype_content["value"] is None:
   130             if vartype_content["name"] == "derived":
       
   131                 var_type = vartype_content["value"].getName()
       
   132             elif vartype_content["name"] in ["string", "wstring"]:
       
   133                 var_type = vartype_content["name"].upper()
       
   134             else:
   127                 var_type = vartype_content["name"]
   135                 var_type = vartype_content["name"]
   128             else:
       
   129                 var_type = vartype_content["value"].getName()
       
   130             config += "    %s "%var.getName()
   136             config += "    %s "%var.getName()
   131             address = var.getAddress()
   137             address = var.getAddress()
   132             if address:
   138             if address:
   133                 config += "AT %s "%address
   139                 config += "AT %s "%address
   134             config += ": %s"%var_type
   140             config += ": %s"%var_type
   158         if varlist.getConstant():
   164         if varlist.getConstant():
   159             resrce += " CONSTANT"
   165             resrce += " CONSTANT"
   160         resrce += "\n"
   166         resrce += "\n"
   161         for var in varlist.getVariable():
   167         for var in varlist.getVariable():
   162             vartype_content = var.getType().getContent()
   168             vartype_content = var.getType().getContent()
   163             if vartype_content["value"] is None:
   169             if vartype_content["name"] == "derived":
       
   170                 var_type = vartype_content["value"].getName()
       
   171             elif vartype_content["name"] in ["string", "wstring"]:
       
   172                 var_type = vartype_content["name"].upper()
       
   173             else:
   164                 var_type = vartype_content["name"]
   174                 var_type = vartype_content["name"]
   165             else:
       
   166                 var_type = vartype_content["value"].getName()
       
   167             resrce += "      %s "%var.getName()
   175             resrce += "      %s "%var.getName()
   168             address = var.getAddress()
   176             address = var.getAddress()
   169             if address:
   177             if address:
   170                 resrce += "AT %s "%address
   178                 resrce += "AT %s "%address
   171             resrce += ": %s"%var_type
   179             resrce += ": %s"%var_type
   304         for varlist in interface.getContent():
   312         for varlist in interface.getContent():
   305             variables = []
   313             variables = []
   306             located = False
   314             located = False
   307             for var in varlist["value"].getVariable():
   315             for var in varlist["value"].getVariable():
   308                 vartype_content = var.getType().getContent()
   316                 vartype_content = var.getType().getContent()
   309                 if vartype_content["value"] is None:
   317                 if vartype_content["name"] == "derived":
   310                     initial = var.getInitialValue()
       
   311                     if initial:
       
   312                         initial_value = initial.getValue()
       
   313                     else:
       
   314                         initial_value = None
       
   315                     address = var.getAddress()
       
   316                     if address:
       
   317                         located = True
       
   318                     variables.append((vartype_content["name"], var.getName(), address, initial_value))
       
   319                 else:
       
   320                     var_type = vartype_content["value"].getName()
   318                     var_type = vartype_content["value"].getName()
   321                     GeneratePouProgram(var_type)
   319                     GeneratePouProgram(var_type)
   322                     blocktype = GetBlockType(var_type)
   320                     blocktype = GetBlockType(var_type)
   323                     if blocktype is not None:
   321                     if blocktype is not None:
   324                         variables.extend(blocktype["initialise"](var_type, var.getName()))
   322                         variables.extend(blocktype["initialise"](var_type, var.getName()))
   331                             initial_value = None
   329                             initial_value = None
   332                         address = var.getAddress()
   330                         address = var.getAddress()
   333                         if address:
   331                         if address:
   334                             located = True
   332                             located = True
   335                         variables.append((vartype_content["value"].getName(), var.getName(), address, initial_value))
   333                         variables.append((vartype_content["value"].getName(), var.getName(), address, initial_value))
       
   334                 else:
       
   335                     initial = var.getInitialValue()
       
   336                     if initial:
       
   337                         initial_value = initial.getValue()
       
   338                     else:
       
   339                         initial_value = None
       
   340                     address = var.getAddress()
       
   341                     if address:
       
   342                         located = True
       
   343                     if vartype_content["name"] in ["string", "wstring"]:
       
   344                         variables.append((vartype_content["name"].upper(), var.getName(), address, initial_value))
       
   345                     else:
       
   346                         variables.append((vartype_content["name"], var.getName(), address, initial_value))
   336             if len(variables) > 0:
   347             if len(variables) > 0:
   337                 self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), 
   348                 self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), 
   338                             varlist["value"].getConstant(), located, variables))
   349                             varlist["value"].getConstant(), located, variables))
   339     
   350     
   340     def GenerateConnectionTypes(self, pou):
   351     def GenerateConnectionTypes(self, pou):
   346                 if isinstance(instance, (plcopen.inVariable, plcopen.outVariable, plcopen.inOutVariable)):
   357                 if isinstance(instance, (plcopen.inVariable, plcopen.outVariable, plcopen.inOutVariable)):
   347                     expression = instance.getExpression()
   358                     expression = instance.getExpression()
   348                     var_type = self.GetVariableType(expression)
   359                     var_type = self.GetVariableType(expression)
   349                     if expression == pou.getName():
   360                     if expression == pou.getName():
   350                         returntype_content = pou.interface.getReturnType().getContent()
   361                         returntype_content = pou.interface.getReturnType().getContent()
   351                         if returntype_content["value"] is None:
   362                         if returntype_content["name"] == "derived":
       
   363                             var_type = returntype_content["value"].getName()
       
   364                         elif returntype_content["name"] in ["string", "wstring"]:
       
   365                             var_type = returntype_content["name"].upper()
       
   366                         else:
   352                             var_type = returntype_content["name"]
   367                             var_type = returntype_content["name"]
   353                         else:
       
   354                             var_type = returntype_content["value"].getName()
       
   355                     elif var_type is None:
   368                     elif var_type is None:
   356                         var_type = expression.split("#")[0]
   369                         var_type = expression.split("#")[0]
   357                     if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
   370                     if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
   358                         self.ConnectionTypes[instance.connectionPointOut] = var_type
   371                         self.ConnectionTypes[instance.connectionPointOut] = var_type
   359                     if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)):
   372                     if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)):