|
|
# 相当于天线,默认目标地址为192.168.49.160,端口号为 8080
|
|
|
# 下行报文指上位机发送给接收机
|
|
|
import tkinter as tk
|
|
|
from threading import Thread
|
|
|
import socket
|
|
|
|
|
|
|
|
|
class ReceiveApp:
|
|
|
def __init__(self, window):
|
|
|
self.window = window
|
|
|
self.window.title("Receive端")
|
|
|
|
|
|
# 接收区域
|
|
|
self.recv_frame = tk.Frame(window)
|
|
|
self.recv_frame.pack(padx=10, pady=10)
|
|
|
|
|
|
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.all_msg = tk.Label(self.send_frame, text="完整指令:")
|
|
|
self.all_msg.pack(side=tk.LEFT)
|
|
|
|
|
|
self.all_msg = tk.Entry(self.send_frame)
|
|
|
self.all_msg.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()
|
|
|
print(style)
|
|
|
content = self.content_entry.get()
|
|
|
print(content)
|
|
|
if style != "" or content != "":
|
|
|
head = "58443341"
|
|
|
tail = "334441"
|
|
|
msg = head + style + content + tail
|
|
|
else:
|
|
|
all_message = self.all_msg.get()
|
|
|
msg = all_message.split(" ")
|
|
|
msg = "".join(msg)
|
|
|
print(msg)
|
|
|
# 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()
|
|
|
|