diff --git a/.idea/UDP_test.iml b/.idea/UDP_test.iml
index d0876a7..24ce7f3 100644
--- a/.idea/UDP_test.iml
+++ b/.idea/UDP_test.iml
@@ -1,8 +1,10 @@
-
-
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 812ab5a..5cf2b09 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -3,5 +3,5 @@
-
+
\ No newline at end of file
diff --git a/receive.py b/receive.py
index b5c9bc6..51e79ae 100644
--- a/receive.py
+++ b/receive.py
@@ -1,23 +1,78 @@
# 相当于天线,默认目标地址为192.168.49.160,端口号为 8080
+# 下行报文指上位机发送给接收机
+import tkinter as tk
+from threading import Thread
import socket
-# 1.创建udp对象
-sk = socket.socket(type=socket.SOCK_DGRAM)
-# 2.在网络中注册该主机(绑定ip和端口号)
-sk.bind( ("127.0.0.1",9000) )
+class ReceiveApp:
+ def __init__(self, window):
+ self.window = window
+ self.window.title("Receive端")
-while True:
- # 3.收发数据的逻辑
- """udp协议下,默认第一次只能接收数据(没有三次握手,不清楚对方的ip和端口号)"""
- # 接受数据
- msg, addr = sk.recvfrom(1024)
- print(msg.decode())
- print(addr)
+ # 接收区域
+ self.recv_frame = tk.Frame(window)
+ self.recv_frame.pack(padx=10, pady=10)
- send_data = input("请输入要发送的数据:")
- # 发送数据
- sk.sendto(send_data.encode(), addr)
+ self.recv_label = tk.Label(self.recv_frame, text="接收数据")
+ self.recv_label.pack()
+
+ self.recv_text = tk.Text(self.recv_frame, height=15, width=50)
+ self.recv_text.pack()
+
+ # 发送区域
+ self.send_frame = tk.Frame(window)
+ self.send_frame.pack(padx=10, pady=10)
+
+ self.style_label = tk.Label(self.send_frame, text="指令类型:")
+ self.style_label.pack(side=tk.LEFT)
+
+ self.style_entry = tk.Entry(self.send_frame)
+ self.style_entry.pack(side=tk.LEFT)
+
+ self.content_label = tk.Label(self.send_frame, text="指令内容:")
+ self.content_label.pack(side=tk.LEFT)
+
+ self.content_entry = tk.Entry(self.send_frame)
+ self.content_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.sk = socket.socket(type=socket.SOCK_DGRAM)
+ self.sk.bind(("127.0.0.1", 9000))
+
+ # 开始接收数据的线程
+ self.thread = Thread(target=self.receive_data)
+ self.thread.daemon = True
+ self.thread.start()
+
+ # 下行报文18字节固定长度
+ """帧格式:
+ 0~3 帧头 BYTE[4] 4 固定为0x58443341
+ 4 指令类型 UINT8 1 0x00:开始停止 0x01:工况查询 0x02:设备重置 0x03:授时指令 0x04:频率设置
+ 0x05:本机地址设置 0x06:本机位置设置 0x07:开启关闭测试模式
+ 5~14 指令内容 BYTE[10] 10 根据指令类型填充,当指令类型为0x01、0x02时可不填充
+ 15~17 帧尾 BYTE[3] 3 固定为0x334441
+ """
+ def send_message(self):
+ # 1.创建udp对象
+ style = self.style_entry.get()
+ content = self.content_entry.get()
+ head = "58443341"
+ tail = "334441"
+ msg = head + style + content + tail
+ # sendto( 二进制字节流,ip端口号 )
+ self.sk.sendto(msg.encode(), ("127.0.0.1", 5010))
+
+ 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 = ReceiveApp(root)
+ root.mainloop()
-# 4.关闭连接
-sk.close()
diff --git a/send.py b/send.py
index dc3fc12..9ba97f7 100644
--- a/send.py
+++ b/send.py
@@ -1,22 +1,57 @@
# 接收机,相当于本机(192.168.49.10,端口号为 5010)
+#,上行报文指接收机发送给上位机
+import tkinter as tk
+from threading import Thread
import socket
import time
-while True:
- # 1.创建udp对象
- sk = socket.socket(type=socket.SOCK_DGRAM)
+class SendApp:
+ def __init__(self, window):
+ self.window = window
+ self.window.title("Send端")
- # 2.收发数据的逻辑
- # 发送数据
- msg = input("请输入要发送的数据:")
+ # 发送区域
+ self.send_frame = tk.Frame(window)
+ self.send_frame.pack(padx=10, pady=10)
- # sendto( 二进制字节流 , ip端口号 )
- sk.sendto(msg.encode(), ("127.0.0.1", 9000))
+ self.send_label = tk.Label(self.send_frame, text="发送数据")
+ self.send_label.pack(side=tk.LEFT)
- # 接受数据
- msg, addr = sk.recvfrom(1024)
- print(msg.decode())
- print(addr)
- time.sleep(1)
- # 3.关闭连接
- sk.close()
+ 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()
\ No newline at end of file