modbus/modbus.py
changeset 2717 b3b6991f1cb6
parent 2715 1215a6b741d5
child 2718 76e8ec46828a
child 2721 367eb26972b1
equal deleted inserted replaced
2715:1215a6b741d5 2717:b3b6991f1cb6
   144             "type": LOCATION_VAR_MEMORY,
   144             "type": LOCATION_VAR_MEMORY,
   145             "size": 8,           # BYTE flag
   145             "size": 8,           # BYTE flag
   146             "IEC_type": "BYTE",  # BYTE flag
   146             "IEC_type": "BYTE",  # BYTE flag
   147             "var_name": "var_name",
   147             "var_name": "var_name",
   148             "location": "B" + ".".join([str(i) for i in current_location]) + ".0.1",
   148             "location": "B" + ".".join([str(i) for i in current_location]) + ".0.1",
   149             "description": "Modbus request status flag",
   149             "description": "Modbus request status flag (0 -> OK, 1 -> Network error, 2 -> Received invalid frame, 3 -> Timeout, 4 -> Received error frame)",
   150             "children": []})        
   150             "children": []})        
   151         entries.append({
   151         entries.append({
   152             "name": "Modbus Error Code",
   152             "name": "Modbus Error Code",
   153             "type": LOCATION_VAR_MEMORY,
   153             "type": LOCATION_VAR_MEMORY,
   154             "size": 8,           # BYTE flag
   154             "size": 8,           # BYTE flag
   184             "LOC" : tuple of interger for IEC location (0,1,2,...)
   184             "LOC" : tuple of interger for IEC location (0,1,2,...)
   185             }, ...]
   185             }, ...]
   186         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
   186         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
   187         """
   187         """
   188         return [], "", False
   188         return [], "", False
       
   189 
   189 
   190 
   190 
   191 
   191 #
   192 #
   192 #
   193 #
   193 #
   194 #
   881                         # absloute address - start address
   882                         # absloute address - start address
   882                         relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
   883                         relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
   883                         # test if the located variable 
   884                         # test if the located variable 
   884                         #    (a) has relative address in request specified range
   885                         #    (a) has relative address in request specified range
   885                         #  AND is NOT
   886                         #  AND is NOT
   886                         #    (b) is a control flag added by this modbus plugin
   887                         #    (b) is a flag added by this modbus plugin.
   887                         #        to control its execution at runtime.
   888                         #        We currently add 3 flags: An execution control flag
   888                         #        Currently, we only add the "Execution Control Flag"
   889                         #        and another two status flags.
   889                         #        to each client request (one flag per request)
   890                         #        We add the "Execution Control Flag" to each client request (one flag per request)
   890                         #        to control when to execute the request (if not executed periodically)
   891                         #        to allow the user program to control when to execute the request (if not executed periodically)
   891                         #        While all Modbus registers/coils are mapped onto a location
   892                         #        While all Modbus registers/coils are mapped onto a location
   892                         #        with 4 numbers (e.g. %QX0.1.2.55), this control flag is mapped
   893                         #        with 4 numbers (e.g. %QX0.1.2.55), this control flag is mapped
   893                         #        onto a location with 4 numbers (e.g. %QX0.1.2.0.0), where the last
   894                         #        onto a location with 4 numbers (e.g. %QX0.1.2.0.0), where the last
   894                         #        two numbers are always '0.0', and the first two identify the request.
   895                         #        two numbers are always '0.0', and the first two identify the request.
   895                         #        In the following if, we check for this condition by checking
   896                         #        In the following if, we check for this condition by checking
   896                         #        if their are at least 4 or more number in the location's address.
   897                         #        if there are at least 4 or more number in the location's address.
   897                         if (        relative_addr in xrange(int(GetCTVal(subchild, 2)))  # condition (a) explained above
   898                         if (        relative_addr in xrange(int(GetCTVal(subchild, 2)))  # condition (a) explained above
   898                             and len(iecvar["LOC"]) < 5):                                  # condition (b) explained above
   899                             and len(iecvar["LOC"]) < 5):                                  # condition (b) explained above
   899                             if str(iecvar["NAME"]) not in loc_vars_list:
   900                             if str(iecvar["NAME"]) not in loc_vars_list:
   900                                 loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].plcv_buffer[%d];" % (client_requestid, relative_addr))
   901                                 loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].plcv_buffer[%d];" % (client_requestid, relative_addr))
   901                                 loc_vars_list.append(str(iecvar["NAME"]))
   902                                 loc_vars_list.append(str(iecvar["NAME"]))
   905                                 # Add if it is a Execution Request Flag (mapped onto %QXa.b.c.0.0), so last number is a '0'
   906                                 # Add if it is a Execution Request Flag (mapped onto %QXa.b.c.0.0), so last number is a '0'
   906                                 if iecvar["LOC"][4] == 0:
   907                                 if iecvar["LOC"][4] == 0:
   907                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
   908                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
   908                                     loc_vars_list.append(str(iecvar["NAME"]))
   909                                     loc_vars_list.append(str(iecvar["NAME"]))
   909                                 # Add if it is a "Modbus Request Status flag" (mapped onto %QWa.b.c.0.1), so last number is a '1'
   910                                 # Add if it is a "Modbus Request Status flag" (mapped onto %QWa.b.c.0.1), so last number is a '1'
       
   911                                 #    -> will store the result of the last executed MB transaction
       
   912                                 #         1 -> error accessing IP network, or serial interface
       
   913                                 #         2 -> reply received from server was an invalid frame
       
   914                                 #         3 -> server did not reply before timeout expired
       
   915                                 #         4 -> server returned a valid Modbus error frame
       
   916                                 #    -> will be reset (set to 0) once this MB transaction has completed sucesfully 
   910                                 if iecvar["LOC"][4] == 1:
   917                                 if iecvar["LOC"][4] == 1:
   911                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_tn_error_code;" % (client_requestid))
   918                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_tn_error_code;" % (client_requestid))
   912                                     loc_vars_list.append(str(iecvar["NAME"]))
   919                                     loc_vars_list.append(str(iecvar["NAME"]))
   913                                 # Add if it is a "Modbus Error code" (mapped onto %QWa.b.c.0.2), so last number is a '2'
   920                                 # Add if it is a "Modbus Error code" (mapped onto %QWa.b.c.0.2), so last number is a '2'
       
   921                                 #    -> if "Modbus Request Status flag" is 4, this flag will store the MB error code returned by the MB server in a MB error frame
       
   922                                 #    -> will be reset (set to 0) once this MB transaction has completed succesfully                                
   914                                 if iecvar["LOC"][4] == 2:
   923                                 if iecvar["LOC"][4] == 2:
   915                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_mb_error_code;" % (client_requestid))
   924                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_mb_error_code;" % (client_requestid))
   916                                     loc_vars_list.append(str(iecvar["NAME"]))
   925                                     loc_vars_list.append(str(iecvar["NAME"]))
   917                     client_requestid += 1
   926                     client_requestid += 1
   918                 tcpclient_node_count += 1
   927                 tcpclient_node_count += 1
   934                         # absloute address - start address
   943                         # absloute address - start address
   935                         relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
   944                         relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
   936                         # test if the located variable 
   945                         # test if the located variable 
   937                         #    (a) has relative address in request specified range
   946                         #    (a) has relative address in request specified range
   938                         #  AND is NOT
   947                         #  AND is NOT
   939                         #    (b) is a control flag added by this modbus plugin
   948                         #    (b) is a flag added by this modbus plugin.
   940                         #        to control its execution at runtime.
   949                         #        We currently add 3 flags: An execution control flag
   941                         #        Currently, we only add the "Execution Control Flag"
   950                         #        and another two status flags.
   942                         #        to each client request (one flag per request)
   951                         #        We add the "Execution Control Flag" to each client request (one flag per request)
   943                         #        to control when to execute the request (if not executed periodically)
   952                         #        to allow the user program to control when to execute the request (if not executed periodically)
   944                         #        While all Modbus registers/coils are mapped onto a location
   953                         #        While all Modbus registers/coils are mapped onto a location
   945                         #        with 4 numbers (e.g. %QX0.1.2.55), this control flag is mapped
   954                         #        with 4 numbers (e.g. %QX0.1.2.55), this control flag is mapped
   946                         #        onto a location with 4 numbers (e.g. %QX0.1.2.0.0), where the last
   955                         #        onto a location with 4 numbers (e.g. %QX0.1.2.0.0), where the last
   947                         #        two numbers are always '0.0', and the first two identify the request.
   956                         #        two numbers are always '0.0', and the first two identify the request.
   948                         #        In the following if, we check for this condition by checking
   957                         #        In the following if, we check for this condition by checking
   959                                 # Add if it is a Execution Request Flag (mapped onto %QXa.b.c.0.0), so last number is a '0'
   968                                 # Add if it is a Execution Request Flag (mapped onto %QXa.b.c.0.0), so last number is a '0'
   960                                 if iecvar["LOC"][4] == 0:
   969                                 if iecvar["LOC"][4] == 0:
   961                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
   970                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
   962                                     loc_vars_list.append(str(iecvar["NAME"]))
   971                                     loc_vars_list.append(str(iecvar["NAME"]))
   963                                 # Add if it is a "Modbus Request Status flag" (mapped onto %QWa.b.c.0.1), so last number is a '1'
   972                                 # Add if it is a "Modbus Request Status flag" (mapped onto %QWa.b.c.0.1), so last number is a '1'
       
   973                                 #    -> will store the result of the last executed MB transaction
       
   974                                 #         1 -> error accessing IP network, or serial interface
       
   975                                 #         2 -> reply received from server was an invalid frame
       
   976                                 #         3 -> server did not reply before timeout expired
       
   977                                 #         4 -> server returned a valid Modbus error frame
       
   978                                 #    -> will be reset (set to 0) once this MB transaction has completed sucesfully 
   964                                 if iecvar["LOC"][4] == 1:
   979                                 if iecvar["LOC"][4] == 1:
   965                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_tn_error_code;" % (client_requestid))
   980                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_tn_error_code;" % (client_requestid))
   966                                     loc_vars_list.append(str(iecvar["NAME"]))
   981                                     loc_vars_list.append(str(iecvar["NAME"]))
   967                                 # Add if it is a "Modbus Error code" (mapped onto %QWa.b.c.0.2), so last number is a '2'
   982                                 # Add if it is a "Modbus Error code" (mapped onto %QWa.b.c.0.2), so last number is a '2'
       
   983                                 #    -> if "Modbus Request Status flag" is 4, this flag will store the MB error code returned by the MB server in a MB error frame
       
   984                                 #    -> will be reset (set to 0) once this MB transaction has completed succesfully                                
   968                                 if iecvar["LOC"][4] == 2:
   985                                 if iecvar["LOC"][4] == 2:
   969                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_mb_error_code;" % (client_requestid))
   986                                     loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_mb_error_code;" % (client_requestid))
   970                                     loc_vars_list.append(str(iecvar["NAME"]))
   987                                     loc_vars_list.append(str(iecvar["NAME"]))
   971                     client_requestid += 1
   988                     client_requestid += 1
   972                 rtuclient_node_count += 1
   989                 rtuclient_node_count += 1