runtime/Modbus_config.py
changeset 2657 41c34e7d196d
parent 2656 049b919b7505
child 2658 fdca999c0c1a
equal deleted inserted replaced
2656:049b919b7505 2657:41c34e7d196d
    70 # configured in the loaded PLC (i.e. the .so file loaded into memory)
    70 # configured in the loaded PLC (i.e. the .so file loaded into memory)
    71 # Each entry will be a dictionary. See _AddWebNode() for the details
    71 # Each entry will be a dictionary. See _AddWebNode() for the details
    72 # of the data structure in each entry.
    72 # of the data structure in each entry.
    73 _WebNodeList = []
    73 _WebNodeList = []
    74 
    74 
       
    75 
       
    76 
       
    77 class MB_StopBits(annotate.Choice):
       
    78     _choices = [0, 1, 2]
       
    79 
       
    80     def coerce(self, val, configurable):
       
    81         return int(val)
       
    82     def __init__(self, *args, **kwargs):
       
    83         annotate.Choice.__init__(self, choices = self._choices, *args, **kwargs)
       
    84 
       
    85 
       
    86 class MB_Baud(annotate.Choice):
       
    87     _choices = [110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]
       
    88 
       
    89     def coerce(self, val, configurable):
       
    90         return int(val)
       
    91     def __init__(self, *args, **kwargs):
       
    92         annotate.Choice.__init__(self, choices = self._choices, *args, **kwargs)
       
    93 
       
    94 
       
    95 class MB_Parity(annotate.Choice):
       
    96     # Warning: do _not_ name this variable choice[] without underscore, as that name is
       
    97     # already used for another similar variable by the underlying class annotate.Choice
       
    98     _choices = [  0,      1,      2  ]
       
    99     _label   = ["none", "odd", "even"]
       
   100     
       
   101     def choice_to_label(self, key):
       
   102         #_plcobj.LogMessage("Modbus web server extension::choice_to_label()  " + str(key))
       
   103         return self._label[key]
       
   104     
       
   105     def coerce(self, val, configurable):
       
   106         """Coerce a value with the help of an object, which is the object
       
   107         we are configuring.
       
   108         """
       
   109         # Basically, make sure the value the user introduced is valid, and transform
       
   110         # into something that is valid if necessary or mark it as an error 
       
   111         # (by raising an exception ??).
       
   112         #
       
   113         # We are simply using this functions to transform the input value (a string)
       
   114         # into an integer. Note that although the available options are all
       
   115         # integers (0, 1 or 2), even though what is shown on the user interface
       
   116         # are actually strings, i.e. the labels), these parameters are for some 
       
   117         # reason being parsed as strings, so we need to map them back to an
       
   118         # integer.
       
   119         #
       
   120         #_plcobj.LogMessage("Modbus web server extension::coerce  " + val )
       
   121         return int(val)
       
   122 
       
   123     def __init__(self, *args, **kwargs):
       
   124         annotate.Choice.__init__(self, 
       
   125                                  choices   = self._choices,
       
   126                                  stringify = self.choice_to_label,
       
   127                                  *args, **kwargs)
    75 
   128 
    76 
   129 
    77 
   130 
    78 # Parameters we will need to get from the C code, but that will not be shown
   131 # Parameters we will need to get from the C code, but that will not be shown
    79 # on the web interface. Common to all modbus entry types (client/server, tcp/rtu/ascii)
   132 # on the web interface. Common to all modbus entry types (client/server, tcp/rtu/ascii)
    99 RTUclient_parameters = [                                                   
   152 RTUclient_parameters = [                                                   
   100     #    param. name       label                        ctype type         annotate type
   153     #    param. name       label                        ctype type         annotate type
   101     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   154     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   102     #                                                                      (annotate.String,
   155     #                                                                      (annotate.String,
   103     #                                                                       annotate.Integer, ...)
   156     #                                                                       annotate.Integer, ...)
   104     ("device"           , _("Serial Port")           , ctypes.c_char_p,    annotate.String),
   157     ("device"           , _("Serial Port")           , ctypes.c_char_p,    annotate.String ),
   105     ("baud"             , _("Baud Rate")             , ctypes.c_int,       annotate.Integer),
   158     ("baud"             , _("Baud Rate")             , ctypes.c_int,       MB_Baud         ),
   106     ("parity"           , _("Parity")                , ctypes.c_int,       annotate.Integer),
   159     ("parity"           , _("Parity")                , ctypes.c_int,       MB_Parity       ),
   107     ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       annotate.Integer),
   160     ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       MB_StopBits     ),
   108     ("comm_period"      , _("Invocation Rate (ms)")  , ctypes.c_ulonglong, annotate.Integer)
   161     ("comm_period"      , _("Invocation Rate (ms)")  , ctypes.c_ulonglong, annotate.Integer)
   109     ]
   162     ]
   110 
   163 
   111 TCPserver_parameters = [                                                   
   164 TCPserver_parameters = [                                                   
   112     #    param. name       label                        ctype type         annotate type
   165     #    param. name       label                        ctype type         annotate type
   121 RTUslave_parameters = [                                                   
   174 RTUslave_parameters = [                                                   
   122     #    param. name       label                        ctype type         annotate type
   175     #    param. name       label                        ctype type         annotate type
   123     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   176     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   124     #                                                                      (annotate.String,
   177     #                                                                      (annotate.String,
   125     #                                                                       annotate.Integer, ...)
   178     #                                                                       annotate.Integer, ...)
   126     ("device"           , _("Serial Port")           , ctypes.c_char_p,    annotate.String),
   179     ("device"           , _("Serial Port")           , ctypes.c_char_p,    annotate.String ),
   127     ("baud"             , _("Baud Rate")             , ctypes.c_int,       annotate.Integer),
   180     ("baud"             , _("Baud Rate")             , ctypes.c_int,       MB_Baud         ),
   128     ("parity"           , _("Parity")                , ctypes.c_int,       annotate.Integer),
   181     ("parity"           , _("Parity")                , ctypes.c_int,       MB_Parity       ),
   129     ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       annotate.Integer),
   182     ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       MB_StopBits     ),
   130     ("slave_id"         , _("Slave ID")              , ctypes.c_ulonglong, annotate.Integer)
   183     ("slave_id"         , _("Slave ID")              , ctypes.c_ulonglong, annotate.Integer)
   131     ]
   184     ]
       
   185 
       
   186 
   132 
   187 
   133 
   188 
   134 # Dictionary containing List of Web viewable parameters
   189 # Dictionary containing List of Web viewable parameters
   135 # Note: the dictionary key must be the same as the string returned by the 
   190 # Note: the dictionary key must be the same as the string returned by the 
   136 # __modbus_get_ClientNode_addr_type()
   191 # __modbus_get_ClientNode_addr_type()
   147 _server_WebParamListDict["ascii"] = []  # (Note: ascii not yet implemented in Beremiz modbus plugin)
   202 _server_WebParamListDict["ascii"] = []  # (Note: ascii not yet implemented in Beremiz modbus plugin)
   148 
   203 
   149 WebParamListDictDict = {}
   204 WebParamListDictDict = {}
   150 WebParamListDictDict['client'] = _client_WebParamListDict
   205 WebParamListDictDict['client'] = _client_WebParamListDict
   151 WebParamListDictDict['server'] = _server_WebParamListDict
   206 WebParamListDictDict['server'] = _server_WebParamListDict
       
   207 
       
   208 
       
   209 
       
   210 
   152 
   211 
   153 
   212 
   154 #def _CheckPortnumber(port_number):
   213 #def _CheckPortnumber(port_number):
   155 #    """ check validity of the port number """
   214 #    """ check validity of the port number """
   156 #    try:
   215 #    try:
   494         _SetPLCConfiguration(WebNode_id, SavedConfig)
   553         _SetPLCConfiguration(WebNode_id, SavedConfig)
   495         WebNode_entry["WebviewConfiguration"] = SavedConfig
   554         WebNode_entry["WebviewConfiguration"] = SavedConfig
   496         
   555         
   497     # Define the format for the web form used to show/change the current parameters
   556     # Define the format for the web form used to show/change the current parameters
   498     # We first declare a dynamic function to work as callback to obtain the default values for each parameter
   557     # We first declare a dynamic function to work as callback to obtain the default values for each parameter
       
   558     # Note: We transform every parameter into a string
       
   559     #       This is not strictly required for parameters of type annotate.Integer that will correctly
       
   560     #           accept the default value as an Integer python object
       
   561     #       This is obviously also not required for parameters of type annotate.String, that are
       
   562     #           always handled as strings.
       
   563     #       However, the annotate.Choice parameters (and all parameters that derive from it,
       
   564     #           sucn as Parity, Baud, etc.) require the default value as a string
       
   565     #           even though we store it as an integer, which is the data type expected
       
   566     #           by the set_***() C functions in mb_runtime.c
   499     def __GetWebviewConfigurationValue(ctx, argument):
   567     def __GetWebviewConfigurationValue(ctx, argument):
   500         return _GetWebviewConfigurationValue(ctx, WebNode_id, argument)
   568         return str(_GetWebviewConfigurationValue(ctx, WebNode_id, argument))
   501     
   569     
   502     webFormInterface = [(name, web_dtype (label=web_label, default=__GetWebviewConfigurationValue)) 
   570     webFormInterface = [(name, web_dtype (label=web_label, default=__GetWebviewConfigurationValue)) 
   503                     for name, web_label, c_dtype, web_dtype in WebNode_entry["WebParamList"]]
   571                     for name, web_label, c_dtype, web_dtype in WebNode_entry["WebParamList"]]
   504 
   572 
   505     # Configure the web interface to include the Modbus config parameters
   573     # Configure the web interface to include the Modbus config parameters