You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

8 months ago
# 接收机相当于本机192.168.49.10,端口号为 5010
8 months ago
#,上行报文指接收机发送给上位机
import tkinter as tk
from threading import Thread
8 months ago
import socket
import time
8 months ago
class SendApp:
def __init__(self, window):
self.window = window
self.window.title("Send端")
8 months ago
8 months ago
# 发送区域
self.send_frame = tk.Frame(window)
self.send_frame.pack(padx=10, pady=10)
8 months ago
8 months ago
self.send_label = tk.Label(self.send_frame, text="发送数据")
self.send_label.pack(side=tk.LEFT)
8 months ago
8 months ago
self.send_entry = tk.Entry(self.send_frame)
self.send_entry.pack(side=tk.LEFT)
self.send_button = tk.Button(self.send_frame, text="发送", command=self.send_message)
self.send_button.pack(side=tk.LEFT)
# 接收区域
self.recv_frame = tk.Frame(window)
self.recv_frame.pack(padx=10, pady=10)
self.recv_text = tk.Text(self.recv_frame, height=15, width=50)
self.recv_text.pack()
#创建udp对象
self.sk = socket.socket(type=socket.SOCK_DGRAM)
self.sk.bind(("127.0.0.1", 5010))
# 开始接收数据的线程
self.thread = Thread(target=self.receive_data)
self.thread.daemon = True
self.thread.start()
def send_message(self):
msg = self.send_entry.get()
#发送数据
self.sk.sendto(msg.encode(), ("127.0.0.1", 9000))
time.sleep(1)
def receive_data(self):
while True:
#接收数据
msg, addr = self.sk.recvfrom(1024)
message_to_display = "Received: " + msg.decode() + " from " + str(addr)
self.recv_text.insert(tk.END, message_to_display + "\n")
if __name__ == "__main__":
root = tk.Tk()
app = SendApp(root)
root.mainloop()