fg71
I'm testing other approach for your _rpc() function in openplc.py
I use with statement so I do not need close(), I also use sendall to make sure that all data is transmitted.
I use standard buffer of 4096 byte
 
I believe timeout in the socket is in seconds so maybe timeout=5 and use timeout=timeout in the with statement.
5-10 seconds timeout is normal for HTTP request.
 
Let me know what you think ?
def _rpc(self, msg, timeout=5):
        data = ""
        if self.runtime_status != "Running":
            return data
        try:
            with socket.create_connection(('localhost', 43628), timeout=timeout) as s:
                s.sendall(f'{msg}\n'.encode('utf-8'))
                data = s.recv(4096).decode('utf-8')
            self.runtime_status = "Running"
        except socket.timeout:
            print(f"Timeout during {msg}, server did not respond in {timeout}ms.")
            self.runtime_status = "Stopped"
        except socket.error as serr:
            print(f"Socket error during {msg}: {serr}. Is the runtime active?")
            self.runtime_status = "Stopped"
        except Exception as e:
            print(f"Unexpected error during {msg}: {e}")
            self.runtime_status = "Stopped"
        return data
 
Reply 0 0
Reply