diff --git a/COM/COM_Robot.py b/COM/COM_Robot.py new file mode 100644 index 0000000..8857970 --- /dev/null +++ b/COM/COM_Robot.py @@ -0,0 +1,69 @@ +from COM.COM_TCP import TCPClient +import queue +import json +from Model.RobotModel import DataAddress,DATARequest + +class RobotClient(TCPClient): + + def __init__(self,ip,port,command_quene,status_model): + super.__init__(ip,port) + self.command_quene = command_quene + self.status_model = status_model + self.errorCommands = {} + + def add_sendQuene(self,command): + self.command_quene.put(command) + return + + def send_Command(self): + q = queue.Queue + q.qsize() + while True: # 让出时间间隔 + try: + if self.command_quene.qsize()!=0: + command = self.command_quene.get() + self.client_socket.send(json.dumps(command).encode('utf-8')) + + response = self.client_socket.recv(1024).decode('utf-8') + response_message = json.loads(response) + if True: + print('正确相应') + else: + self.errorCommands[json.dumps(command).encode('utf-8')] = response_message + print('出错') + except Exception as e: + print('连接断开') + + #return False + + def send_Status(self): + request = DATARequest() + + attributes = dir(DataAddress()) + + # 移除特殊属性和方法 + attributes = [attr for attr in attributes if not attr.startswith('__')] + request.queryAddr= attributes + request_status_json = vars(request) + # 转字符串 + while True: + try: + self.client_socket.send(json.dumps(request_status_json).encode('utf-8')) + response = self.client_socket.recv(1024).decode('utf-8') + response_message = json.loads(response) + if True: + data_status = DATARequest() + data_status.__dict__ = response_message + data_address_array = data_status.queryAddr + data_Address = DataAddress() + for i in attributes.count(): + setattr(data_Address,attributes[i],data_address_array[i]) + self.status_model = data_Address + else: + print('转换失败') + except Exception as e: + print('连接断开') + + + + return False diff --git a/COM/COM_TCP.py b/COM/COM_TCP.py index d64c0d4..3521673 100644 --- a/COM/COM_TCP.py +++ b/COM/COM_TCP.py @@ -1,8 +1,11 @@ import json import socket +import threading +import time class TCPClient: def __init__(self,ip,port): + self.error_count=0 self.IPAddress = ip self.port = port self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -20,5 +23,18 @@ class TCPClient: return False + def run(self): + while True: + time.sleep(30) + if (self.send_Command() and self.send_Status()): + self.error_count=0 + + + def send_Command(self): + return False + + def send_Status(self): + return False + diff --git a/Model/RobotModel.py b/Model/RobotModel.py new file mode 100644 index 0000000..8b3e29b --- /dev/null +++ b/Model/RobotModel.py @@ -0,0 +1,74 @@ +class DATARequest: + def __init__(self): + self.dsID = 'www.hc-system.com.RemoteMonitor"' + self.reqType = 'query' + self.queryAddr = [] + + + +class DataAddress: + def __init__(self): + self.version = '' + self.curMold = '' + self.counterList = '' + self.counter_n = '' + self.curMode = '' + self.boardIONum = '' + self.input_n = '' + self.output_n = '' + self.axisNum = '' + self.axis_n = '' + self.world_n = '' + self.curAlarm = '' + self.curCycle = '' + self.lastCycle = '' + self.machineName = '' + self.curTorque_n = '' + self.curSpeed_n = '' + self.curAccount = '' + self.origin = '' + self.moldList = '' + self.isMoving = '' + self.M_n = '' + #return + + + + + +class DATAReply: + def __init__(self): + self.dsID = '' + self.reqType = '' + self.queryData = DataAddress() + return + + + def JsonToObject(self): + return + +class CMDRequest: + def __init__(self): + self.dsID = 'www.hc-system.com.HCRemoteCommand' + self.reqType = '' + self.cmdData = '' + return + + + +class CMDInstructRequest: + def __init__(self): + self.dsID = 'www.hc-system.com.HCRemoteCommand' + self.emptyList = '1' + self.dsData = [] + return + + +class CMDReply: + + def __init__(self,dsID,reqType,cmdReply): + self.dsID = dsID + self.reqType = reqType + self.cmdReply = cmdReply + return + diff --git a/main.py b/main.py index 5fa1a03..b05e53a 100644 --- a/main.py +++ b/main.py @@ -1,44 +1,47 @@ import configparser import sys from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton -from ui_untitled import Ui_MainWindow; -from COM.COM_TCP import TCPClient +from ui_untitled import Ui_MainWindow +from COM.COM_Robot import RobotClient from Expection import ErrorCode +from queue import Queue +from Model.RobotModel import * -class MyWindow(QMainWindow,Ui_MainWindow): +class MainWindow(QMainWindow,Ui_MainWindow): def __init__(self): - super().__init__() + super(MainWindow, self).__init__() + self.setupUi(self) + self.pushButton_17.clicked.connect(self.send_position_button_click) + self.robotClient = None + self.configReader = configparser.ConfigParser() - self.setWindowTitle("PySide6 Test") - - self.setGeometry(100, 100, 300, 200) - - 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 send_position_button_click(self): + return + 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(): + self.robotClient = RobotClient(ip, port,self.command_quene,self.status_address) + self.robotClient.CreatConnect() + if self.robotClient.is_Connect(): return 0 else: return ErrorCode.NETERROR - + def send_position_command(self,position): + self.robotClient.add_sendQuene(position) + + if __name__ == "__main__": app = QApplication(sys.argv) - window = MyWindow() + window = MainWindow() window.show() sys.exit(app.exec()) diff --git a/test.py b/test.py new file mode 100644 index 0000000..99c2935 --- /dev/null +++ b/test.py @@ -0,0 +1,35 @@ +import asyncio + + +async def send_update_command(): + while True: + command = "update_position_command\n" + print('command') + await asyncio.sleep(2) + #await writer.drain() + await asyncio.sleep(1) + + +async def query_robot_status(): + while True: + command = "query_status_command\n" + print('status') + await asyncio.sleep(1) + # await writer.drain() + #feedback = await reader.read(1024) + #if feedback: + # print("Received feedback:", feedback.decode()) + await asyncio.sleep(2) + + +async def main(): + #reader, writer = await asyncio.open_connection('f', 'f') + + await asyncio.gather( + send_update_command(), + query_robot_status() + ) + + print('UI') + +asyncio.run(main())