예제 #1
0
 def recv(self) -> bytes:
     """
     Receives a whole DLMS APDU. Gets the total length from the DLMS IP Wrapper.
     """
     try:
         header = WrapperHeader.from_bytes(self._recv_bytes(8))
         data = self._recv_bytes(header.length)
     except (OSError, IOError, socket.timeout, socket.error) as e:
         raise exceptions.CommunicationError("Could not receive data") from e
     return data
예제 #2
0
    def send(self, bytes_to_send: bytes) -> bytes:
        """
        Sends a whole DLMS APDU wrapped in the DLMS IP Wrapper.
        """
        if not self.tcp_socket:
            raise RuntimeError("TCP transport not connected.")
        try:
            self.tcp_socket.sendall(self.wrap(bytes_to_send))
        except (OSError, IOError, socket.timeout, socket.error) as e:
            raise exceptions.CommunicationError("Could no send data") from e

        return self.recv()
예제 #3
0
 def recv(self) -> bytes:
     """
     Receives a whole DLMS APDU. Gets the total length from the DLMS IP Wrapper.
     """
     if not self.tcp_socket:
         raise RuntimeError("TCP transport not connected.")
     try:
         header = WrapperHeader.from_bytes(self.tcp_socket.recv(8))
         data = self.tcp_socket.recv(header.length)
     except (OSError, IOError, socket.timeout, socket.error) as e:
         raise exceptions.CommunicationError(
             "Could not receive data") from e
     return data
예제 #4
0
    def connect(self):
        """
        Create a new socket and set it on the transport
        """
        if self.tcp_socket:
            raise RuntimeError(f"There is already an active socket to {self.address}")

        try:
            self.tcp_socket = socket.create_connection(
                address=self.address, timeout=self.timeout
            )
        except (
            OSError,
            IOError,
            socket.timeout,
            socket.error,
            ConnectionRefusedError,
        ) as e:
            raise exceptions.CommunicationError("Unable to connect socket") from e
        LOG.info(f"Connected to {self.address}")