Пример #1
0
    def func_wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Snap7Exception as e:
            # Function refused by inverter/CPU.
            s7_refused = 'CLI : function refused by CPU (Unknown error)'
            # Could not reach the device via TCP.
            s7_unreach = ' TCP : Unreachable peer'

            if e.message == s7_refused:
                text = """Write/Read Error : Request for inverter was refused!
                Check:
                - Parameter number exists? See inverter list manual.
                - The index specified exists in the inverter?
                - See arguments used for any possible incorrect use.
                - Are you trying to write a parameter with
                  special conditions? e.g., trying to change P0304
                  with P0010 = 0, or writing a parameter to a telegram
                  value that it's not in the current telegram (P0922)?
                """
                raise Snap7Exception(text)
            elif e.message == s7_unreach:
                text = """Connection Error : Inverter could not be reached!
                Check:
                - The IP Address argument is correct?
                  Is it possible to ping the inverter?
                - For G120, make sure the FW is >= version 4.7
                  (internal versional, r0018, should start with 47).
                """
                raise Snap7Exception(text)
            else:
                raise Snap7Exception(e.message)

        return None
Пример #2
0
    def list_blocks_of_type(self, blocktype, size):
        """This function returns the AG list of a specified block type."""

        blocktype = snap7.snap7types.block_types.get(blocktype)

        if not blocktype:
            raise Snap7Exception("The blocktype parameter was invalid")

        logger.debug("listing blocks of type: %s size: %s" %
                      (blocktype, size))

        if (size == 0):
            return 0

        data = (c_uint16 * size)()
        count = c_int(size)
        result = self.library.Cli_ListBlocksOfType(
            self.pointer, blocktype,
            byref(data),
            byref(count))

        logger.debug("number of items found: %s" % count)

        check_error(result, context="client")
        return data
Пример #3
0
 def __init__(self, lib_location=None):
     if self.cdll:
         return
     self.lib_location = lib_location or self.lib_location or 'libsnap7.so'
     if not self.lib_location:
         msg = "can't find snap7 library. If installed, try running ldconfig"
         raise Snap7Exception(msg)
     self.cdll = cdll.LoadLibrary(self.lib_location)
Пример #4
0
def check_error(code, context="client"):
    """
    check if the error code is set. If so, a Python log message is generated
    and an error is raised.
    """
    if code:
        error = error_text(code, context)
        logger.error(error)
        raise Snap7Exception(error)
Пример #5
0
    def set_connection_type(self, connection_type):
        """
        Sets the connection resource type, i.e the way in which the Clients
        connects to a PLC.

        :param connection_type: 1 for PG, 2 for OP, 3 to 10 for S7 Basic
        """
        result = self.library.Cli_SetConnectionType(self.pointer,
                                                    c_uint16(connection_type))
        if result != 0:
            raise Snap7Exception("The parameter was invalid")
Пример #6
0
 def __init__(self, lib_location=None):
     if self.cdll:
         return
     self.lib_location = lib_location or self.lib_location or find_library(
         'snap7')
     if not self.lib_location:
         msg = "can't find snap7 library. If installed, try running ldconfig"
         self.lib_location = GetSnap7Dll()
         print self.lib_location
         #print self.lib_location
         if not os.path.isfile(self.lib_location):
             raise Snap7Exception(msg)
     self.cdll = cdll.LoadLibrary(self.lib_location)
Пример #7
0
    def set_connection_params(self, address, local_tsap, remote_tsap):
        """
        Sets internally (IP, LocalTSAP, RemoteTSAP) Coordinates.
        This function must be called just before Cli_Connect().

        :param address: PLC/Equipment IPV4 Address, for example "192.168.1.12"
        :param local_tsap: Local TSAP (PC TSAP)
        :param remote_tsap: Remote TSAP (PLC TSAP)
        """
        assert re.match(ipv4, address), '%s is invalid ipv4' % address
        result = self.library.Cli_SetConnectionParams(self.pointer, address,
                                                      c_uint16(local_tsap),
                                                      c_uint16(remote_tsap))
        if result != 0:
            raise Snap7Exception("The parameter was invalid")
Пример #8
0
    def set_connection_params(self, ip_address, tsap_snap7, tsap_logo):
        """
        Sets internally (IP, LocalTSAP, RemoteTSAP) Coordinates.
        This function must be called just before Cli_Connect().

        :param ip_address: IP ip_address of server
        :param tsap_snap7: TSAP SNAP7 Client (e.g. 10.00 = 0x1000)
        :param tsap_logo: TSAP Logo Server (e.g. 20.00 = 0x2000)
        """
        assert re.match(ipv4, ip_address), '%s is invalid ipv4' % ip_address
        result = self.library.Cli_SetConnectionParams(self.pointer, ip_address,
                                                      c_uint16(tsap_snap7),
                                                      c_uint16(tsap_logo))
        if result != 0:
            raise Snap7Exception("The parameter was invalid")
Пример #9
0
    def get_cpu_state(self):
        """
        Retrieves CPU state from client
        """
        state = c_int(0)
        self.library.Cli_GetPlcStatus(self.pointer, byref(state))

        try:
            status_string = cpu_statuses[state.value]
        except KeyError:
            status_string = None

        if not status_string:
            raise Snap7Exception("The cpu state (%s) is invalid" % state.value)

        logging.debug("CPU state is %s" % status_string)
        return status_string
Пример #10
0
    def get_block_info(self, blocktype, db_number):
        """Returns the block information for the specified block."""

        blocktype = snap7.snap7types.block_types.get(blocktype)

        if not blocktype:
            raise Snap7Exception("The blocktype parameter was invalid")

        logging.debug("retrieving block info for block %s of type %s" %
                      (db_number, blocktype))

        data = TS7BlockInfo()

        result = self.library.Cli_GetAgBlockInfo(self.pointer, blocktype,
                                                 db_number, byref(data))
        check_error(result, context="client")
        return data
Пример #11
0
    def check_as_b_send_completion(self):
        """
        Checks if the current asynchronous send job was completed and terminates
        immediately.
        """
        op_result = ctypes.c_int32()
        result = self.library.Par_CheckAsBSendCompletion(
            self.pointer, ctypes.byref(op_result))
        return_values = {
            0: "job complete",
            1: "job in progress",
            -2: "invalid handled supplied",
        }

        if result == -2:
            raise Snap7Exception("The Client parameter was invalid")

        return return_values[result], op_result