runtime/Modbus_config.py
changeset 2668 cca3e5d7d6f3
parent 2666 5f48d5e60a81
equal deleted inserted replaced
2659:09bac1f52b1e 2668:cca3e5d7d6f3
    72 # of the data structure in each entry.
    72 # of the data structure in each entry.
    73 _WebNodeList = []
    73 _WebNodeList = []
    74 
    74 
    75 
    75 
    76 
    76 
       
    77 
       
    78 class MB_StrippedString(annotate.String):
       
    79     def __init__(self, *args, **kwargs):
       
    80         annotate.String.__init__(self, strip = True, *args, **kwargs)
       
    81 
       
    82 
    77 class MB_StopBits(annotate.Choice):
    83 class MB_StopBits(annotate.Choice):
    78     _choices = [0, 1, 2]
    84     _choices = [0, 1, 2]
    79 
    85 
    80     def coerce(self, val, configurable):
    86     def coerce(self, val, configurable):
    81         return int(val)
    87         return int(val)
    91     def __init__(self, *args, **kwargs):
    97     def __init__(self, *args, **kwargs):
    92         annotate.Choice.__init__(self, choices = self._choices, *args, **kwargs)
    98         annotate.Choice.__init__(self, choices = self._choices, *args, **kwargs)
    93 
    99 
    94 
   100 
    95 class MB_Parity(annotate.Choice):
   101 class MB_Parity(annotate.Choice):
       
   102     # For more info on what this class really does, have a look at the code in
       
   103     # file twisted/nevow/annotate.py
       
   104     # grab this code from $git clone https://github.com/twisted/nevow/
       
   105     # 
    96     # Warning: do _not_ name this variable choice[] without underscore, as that name is
   106     # 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
   107     # already used for another similar variable by the underlying class annotate.Choice
    98     _choices = [  0,      1,      2  ]
   108     _choices = [  0,      1,      2  ]
    99     _label   = ["none", "odd", "even"]
   109     _label   = ["none", "odd", "even"]
   100     
   110     
   141     #                                                                       annotate.Integer, ...)
   151     #                                                                       annotate.Integer, ...)
   142     ("config_name"      , _("")                      , ctypes.c_char_p,    annotate.String),
   152     ("config_name"      , _("")                      , ctypes.c_char_p,    annotate.String),
   143     ("addr_type"        , _("")                      , ctypes.c_char_p,    annotate.String)
   153     ("addr_type"        , _("")                      , ctypes.c_char_p,    annotate.String)
   144     ]                                                                      
   154     ]                                                                      
   145                                                                            
   155                                                                            
       
   156 # Parameters we will need to get from the C code, and that _will_ be shown
       
   157 # on the web interface.
   146 TCPclient_parameters = [                                                   
   158 TCPclient_parameters = [                                                   
   147     #    param. name       label                        ctype type         annotate type
   159     #    param. name       label                        ctype type         annotate type
   148     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   160     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   149     #                                                                      (annotate.String,
   161     #                                                                      (annotate.String,
   150     #                                                                       annotate.Integer, ...)
   162     #                                                                       annotate.Integer, ...)
   151     ("host"             , _("Remote IP Address")     , ctypes.c_char_p,    annotate.String),
   163     ("host"             , _("Remote IP Address")     , ctypes.c_char_p,    MB_StrippedString),
   152     ("port"             , _("Remote Port Number")    , ctypes.c_char_p,    annotate.String),
   164     ("port"             , _("Remote Port Number")    , ctypes.c_char_p,    MB_StrippedString),
   153     ("comm_period"      , _("Invocation Rate (ms)")  , ctypes.c_ulonglong, annotate.Integer)
   165     ("comm_period"      , _("Invocation Rate (ms)")  , ctypes.c_ulonglong, annotate.Integer )
   154     ]
   166     ]
   155 
   167 
   156 RTUclient_parameters = [                                                   
   168 RTUclient_parameters = [                                                   
   157     #    param. name       label                        ctype type         annotate type
   169     #    param. name       label                        ctype type         annotate type
   158     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   170     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   159     #                                                                      (annotate.String,
   171     #                                                                      (annotate.String,
   160     #                                                                       annotate.Integer, ...)
   172     #                                                                       annotate.Integer, ...)
   161     ("device"           , _("Serial Port")           , ctypes.c_char_p,    annotate.String ),
   173     ("device"           , _("Serial Port")           , ctypes.c_char_p,    MB_StrippedString),
   162     ("baud"             , _("Baud Rate")             , ctypes.c_int,       MB_Baud         ),
   174     ("baud"             , _("Baud Rate")             , ctypes.c_int,       MB_Baud         ),
   163     ("parity"           , _("Parity")                , ctypes.c_int,       MB_Parity       ),
   175     ("parity"           , _("Parity")                , ctypes.c_int,       MB_Parity       ),
   164     ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       MB_StopBits     ),
   176     ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       MB_StopBits     ),
   165     ("comm_period"      , _("Invocation Rate (ms)")  , ctypes.c_ulonglong, annotate.Integer)
   177     ("comm_period"      , _("Invocation Rate (ms)")  , ctypes.c_ulonglong, annotate.Integer)
   166     ]
   178     ]
   168 TCPserver_parameters = [                                                   
   180 TCPserver_parameters = [                                                   
   169     #    param. name       label                        ctype type         annotate type
   181     #    param. name       label                        ctype type         annotate type
   170     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   182     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   171     #                                                                      (annotate.String,
   183     #                                                                      (annotate.String,
   172     #                                                                       annotate.Integer, ...)
   184     #                                                                       annotate.Integer, ...)
   173     ("host"             , _("Local IP Address")      , ctypes.c_char_p,    annotate.String),
   185     ("host"             , _("Local IP Address")      , ctypes.c_char_p,    MB_StrippedString),
   174     ("port"             , _("Local Port Number")     , ctypes.c_char_p,    annotate.String),
   186     ("port"             , _("Local Port Number")     , ctypes.c_char_p,    MB_StrippedString),
   175     ("slave_id"         , _("Slave ID")              , ctypes.c_ubyte,     annotate.Integer)
   187     ("slave_id"         , _("Slave ID")              , ctypes.c_ubyte,     annotate.Integer )
   176     ]
   188     ]
   177 
   189 
   178 RTUslave_parameters = [                                                   
   190 RTUslave_parameters = [                                                   
   179     #    param. name       label                        ctype type         annotate type
   191     #    param. name       label                        ctype type         annotate type
   180     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   192     # (C code var name)   (used on web interface)      (C data type)       (web data type)
   181     #                                                                      (annotate.String,
   193     #                                                                      (annotate.String,
   182     #                                                                       annotate.Integer, ...)
   194     #                                                                       annotate.Integer, ...)
   183     ("device"           , _("Serial Port")           , ctypes.c_char_p,    annotate.String ),
   195     ("device"           , _("Serial Port")           , ctypes.c_char_p,    MB_StrippedString),
   184     ("baud"             , _("Baud Rate")             , ctypes.c_int,       MB_Baud         ),
   196     ("baud"             , _("Baud Rate")             , ctypes.c_int,       MB_Baud         ),
   185     ("parity"           , _("Parity")                , ctypes.c_int,       MB_Parity       ),
   197     ("parity"           , _("Parity")                , ctypes.c_int,       MB_Parity       ),
   186     ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       MB_StopBits     ),
   198     ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       MB_StopBits     ),
   187     ("slave_id"         , _("Slave ID")              , ctypes.c_ulonglong, annotate.Integer)
   199     ("slave_id"         , _("Slave ID")              , ctypes.c_ulonglong, annotate.Integer)
   188     ]
   200     ]
   375     for par_name, x1, x2, x3 in WebParamList:
   387     for par_name, x1, x2, x3 in WebParamList:
   376         value = kwargs.get(par_name, None)
   388         value = kwargs.get(par_name, None)
   377         if value is not None:
   389         if value is not None:
   378             newConfig[par_name] = value
   390             newConfig[par_name] = value
   379 
   391 
   380     _WebNodeList[WebNode_id]["WebviewConfiguration"] = newConfig
       
   381     
       
   382     # First check if configuration is OK.
   392     # First check if configuration is OK.
   383     # Note that this is not currently required, as we use drop down choice menus
   393     # Note that this is not currently required, as we use drop down choice menus
   384     # for baud, parity and sop bits, so the values should always be correct!
   394     # for baud, parity and sop bits, so the values should always be correct!
   385     #if not _CheckWebConfiguration(newConfig):
   395     #if not _CheckWebConfiguration(newConfig):
   386     #    return
   396     #    return
   390     # has a cold start (i.e. when Beremiz_service.py is retarted)
   400     # has a cold start (i.e. when Beremiz_service.py is retarted)
   391     _SetSavedConfiguration(WebNode_id, newConfig)
   401     _SetSavedConfiguration(WebNode_id, newConfig)
   392 
   402 
   393     # Configure PLC with the current Modbus parameters
   403     # Configure PLC with the current Modbus parameters
   394     _SetPLCConfiguration(WebNode_id, newConfig)
   404     _SetPLCConfiguration(WebNode_id, newConfig)
       
   405 
       
   406     # Update the viewable configuration
       
   407     # The PLC may have coerced the values on calling _SetPLCConfiguration()
       
   408     # so we do not set it directly to newConfig
       
   409     _WebNodeList[WebNode_id]["WebviewConfiguration"] = _GetPLCConfiguration(WebNode_id)
   395 
   410 
   396     # File has just been created => Delete button must be shown on web interface!
   411     # File has just been created => Delete button must be shown on web interface!
   397     _updateWebInterface(WebNode_id)
   412     _updateWebInterface(WebNode_id)
   398 
   413 
   399 
   414