master
jakeallen 8 months ago
parent 472a47535d
commit 22b6f70458

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.11 (UDP_test)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -3,5 +3,5 @@
<component name="Black">
<option name="sdkName" value="Python 3.9" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (UDP_test)" project-jdk-type="Python SDK" />
</project>

@ -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 根据指令类型填充当指令类型为0x010x02时可不填充
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()

@ -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()
Loading…
Cancel
Save