class TiVoClient(QObject): """Implements the TiVo version 1.1 TCP Remote Protocol.""" channel_changed = Signal(str) error_message = Signal(str) connection_error = Signal(str) def __init__(self, ip): super(TiVoClient, self).__init__() self.socket = QTcpSocket(self) # TiVos *always* serve on port 31339. self.socket.connectToHost(ip, 31339) self.socket.readyRead.connect(self.handle_read) def send_command(self, command): print(f'Sending {command}...') # All commands are terminated with a carriage return. The end user # shouldn't have to care about this detail. command += "\r" data = QByteArray(bytes(command, encoding='ascii')) sent_bytes = self.socket.write(data) data_len = len(data) if sent_bytes != data_len: error_string = "Network error: Not all of the data was sent.\n\n" \ f"Command: {command}\n" \ f"Number of bytes sent: {sent_bytes}\n" \ f"Expected: {data_len}" self.connection_error.emit(error_string) def handle_read(self): """Handles data received by the socket.""" # Grab the data from the socket. data = self.socket.readAll().data() # Remove whitespace from the data. data = data.strip() # Convert the data to a string. data = data.decode('utf-8') print(f"Received {data}") # Parameterize the string. data = data.split(' ') if data[0] == "CH_STATUS": self.channel_changed.emit(data[1]) elif data[0] == "CH_FAILED": self.error_message.emit(data[1]) else: self.error_message.emit(data[0])
def run(self): tcpSocket = QTcpSocket() if not tcpSocket.setSocketDescriptor(self.socketDescriptor): self.error.emit(tcpSocket.error()) return block = QByteArray() outstr = QDataStream(block, QIODevice.WriteOnly) outstr.setVersion(QDataStream.Qt_4_0) outstr.writeUInt16(0) outstr.writeQString(self.text) outstr.device().seek(0) outstr.writeUInt16(block.size() - 2) msg = tcpSocket.readAll() self.msg.emit(msg) print(msg) tcpSocket.write(block) tcpSocket.disconnectFromHost() tcpSocket.waitForDisconnected()
class Server(QTcpServer): def __init__(self): super(Server, self).__init__() self.socket = QTcpSocket() self.socket.readyRead.connect(self.read_receive_data) self.socket.disconnected.connect(self.disconnect_socket) def read_receive_data(self): data = self.socket.readAll() utf_str = data.data().decode() # format to json json_data = json.loads(utf_str) event_dispatcher.emit_event(event_key.RECV_LOG, json_data) def disconnect_socket(self): event_dispatcher.emit_event(event_key.DISCONNECT_CLIENT, "null") def incomingConnection(self, socket_desc): if not self.socket.setSocketDescriptor(socket_desc): self.error.emit(self.socket.error()) print("faild create socket") return event_dispatcher.emit_event(event_key.CONNECT_CLIENT, "null")
class MantaSocket: def __init__(self) -> None: self.socket = QTcpSocket() self.timer = QTimer() self.timer.timeout.connect(self.client_listening) self.timer.start(1000) self.socket.readyRead.connect(self.read_data) self.socket.disconnected.connect(self.client_socket_disconnected) self.writeflag = 0 def client_listening(self): if self.socket.state() == QAbstractSocket.ConnectedState: pass else: self.socket.connectToHost('192.168.7.2', 2345) if not self.socket.waitForConnected(200): return print('Connected!!!') # self.write_data() def read_data(self): print("read!!!") receive_data = self.socket.readAll() if not receive_data.isEmpty(): # print(socketdata) self.socket_data_analysis(receive_data) def write_data(self): # obj=bytes('hello'.encode()) # send_str=QByteArray(obj) send_str = 'hello yeah world' if self.writeflag: self.socket.writeData(send_str, len(send_str)) def client_socket_disconnected(self): print('Socket Disconnect!!!') def socket_data_analysis(self, receive): ''' pos_left,pos_right,pos_left_roll,pos_right_roll,dpos_left,dpos_right,dpos_left_roll,dpos_right_roll,pos_behind_left,pos_behind_right, roll,yaw,left_A,right_A,yaw_desire,pitch,left_behind,right_behind,pitch_desire,depth_c,depth; ''' # print(receive) pos_str = "$POS" pos_str_index = receive.indexOf(pos_str.encode()) aux_num = 0 data_index = 0 data_index = pos_str_index + (4 + 1) aux_num = 5 pos_left = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 pos_right = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 pos_left_roll = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 pos_right_roll = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 dpos_left = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 dpos_right = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 dpos_left_roll = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 dpos_right_roll = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 pos_behind_left = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 5 pos_behind_right = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 roll = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 yaw = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 left_A = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 right_A = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 yaw_desire = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 pitch = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 left_behind = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 right_behind = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 pitch_desire = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 6 depth_c = receive.mid(data_index, aux_num).toFloat()[0] data_index += aux_num + 1 aux_num = 7 depth = receive.mid(data_index, aux_num).toFloat()[0] # Window UI Display window.ui.pitch_label.setText(str(pitch)) window.ui.roll_label.setText(str(roll)) window.ui.yaw_label.setText(str(yaw)) window.ui.depth_label.setText(str(depth))