From e8e96d9a97e09ae9068112675f3ddf691e5590ea Mon Sep 17 00:00:00 2001 From: FrankCV2048 <1395405735@qq.com> Date: Tue, 6 Aug 2024 22:57:27 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=B7=BB=E5=8A=A0=E9=80=9A=E8=AE=AF?= =?UTF-8?q?=E7=AD=89=E5=9F=BA=E6=9C=AC=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- COM/COM_TCP.py | 24 ++++++++++++++++++++++++ Expection.py | 3 +++ Seting.ini | 6 ++++++ main.py | 21 ++++++++++++++++++++- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 COM/COM_TCP.py create mode 100644 Expection.py create mode 100644 Seting.ini diff --git a/COM/COM_TCP.py b/COM/COM_TCP.py new file mode 100644 index 0000000..d64c0d4 --- /dev/null +++ b/COM/COM_TCP.py @@ -0,0 +1,24 @@ +import json +import socket + +class TCPClient: + def __init__(self,ip,port): + self.IPAddress = ip + self.port = port + self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + + def CreatConnect(self): + self.client_socket.connect(self.IPAddress, self.port) + + + def is_Connect(self): + try: + self.client_socket.send(b'', socket.MSG_DONTWAIT) + return True + except OSError: + return False + + + + diff --git a/Expection.py b/Expection.py new file mode 100644 index 0000000..1e7e7f5 --- /dev/null +++ b/Expection.py @@ -0,0 +1,3 @@ +from enum import Enum +class ErrorCode(Enum): + NETERROR = 404 #网络异常 diff --git a/Seting.ini b/Seting.ini new file mode 100644 index 0000000..ee83d1c --- /dev/null +++ b/Seting.ini @@ -0,0 +1,6 @@ +[Main] + + +[Robot] +IPAddress=127.0.0.1 +Port=8088 diff --git a/main.py b/main.py index 43cfd3b..5fa1a03 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,9 @@ +import configparser import sys from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton from ui_untitled import Ui_MainWindow; +from COM.COM_TCP import TCPClient +from Expection import ErrorCode class MyWindow(QMainWindow,Ui_MainWindow): def __init__(self): @@ -13,11 +16,27 @@ class MyWindow(QMainWindow,Ui_MainWindow): self.button = QPushButton("Click Me!", self) self.button.setGeometry(100, 100, 100, 40) self.button.clicked.connect(self.on_button_click) - + self.tcpClient=None + self.configReader=configparser.ConfigParser() def on_button_click(self): self.button.setText("Clicked!") + + def init_Run(self): + self.configReader.read('Seting.ini') + ip = self.configReader.get('Robot', 'IPAddress') + port= self.configReader.get('Robot', 'Port') + self.tcpClient = TCPClient(ip, port) + self.tcpClient.CreatConnect() + if self.tcpClient.is_Connect(): + return 0 + else: + return ErrorCode.NETERROR + + + + if __name__ == "__main__": app = QApplication(sys.argv) window = MyWindow()