?部分通信协议:
?
?建立UDP连接:
server_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
address = ('10.0.17.155',4001)
server_socket.bind(address)
接收数据包:?
data,address = server_socket.recvfrom(1000)
接收的数据包:
?根据协议进行解析:
# id号 1 无符号
ID = int((data[100+i*15:101+i*15]+data[99+i*15:100+i*15]).hex(),16)
# Y轴距离 2 有符号 正前侧 负后侧
Y = BitArray(data[102+i*15:103+i*15]+data[101+i*15:102+i*15]).int
# 速度 2 有符号 正远离 负靠近
V = BitArray(data[104+i*15:105+i*15]+data[103+i*15:104+i*15]).int
# X轴距离 2 有符号 正右侧 负左侧
X = BitArray(data[107+i*15:108+i*15]+data[106+i*15:107+i*15]).int
# 直线距离
R = round((Y*Y + X*X)**0.5,2)
# SNR 无符号
SNR = int(data[105:106].hex(),16)
?设置Excel:
# 行 列 数据
worksheet.write(0, 0, label='Time')
worksheet.write(0, 1, label='动目标')
# 目标的标题 ID Y X R AV V SNR
worksheet.write(0, 2, label='ID')
worksheet.write(0, 3, label='Y')
worksheet.write(0, 4, label='X')
worksheet.write(0, 5, label='R')
worksheet.write(0, 6, label='AV')
worksheet.write(0, 7, label='V')
worksheet.write(0, 8, label='SNR')
?
全部代码:
# -*- coding: cp936-*-
import math
import socket
import time
import numpy
from bitstring import BitArray
import re
import xlwt
server_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
address = ('10.0.17.155',4001)
server_socket.bind(address)
workbook = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('My Worksheet')
# 表格循环计数
j = 0 # 行数
# 时间,ID
worksheet.write(0, 0, label='Time')
worksheet.write(0, 1, label='动目标')
# 目标的标题 ID Y X R AV V SNR
worksheet.write(0, 2, label='ID')
worksheet.write(0, 3, label='Y')
worksheet.write(0, 4, label='X')
worksheet.write(0, 5, label='R')
worksheet.write(0, 6, label='AV')
worksheet.write(0, 7, label='V')
worksheet.write(0, 8, label='SNR')
while True: # 循环读数
now = time.time() # 获取当前时间
data,address = server_socket.recvfrom(1000)
# 动目标个数
T_num = BitArray(data[98:99]).int
if T_num > 0: # 判断
# j+1行
# 时间写入表格
worksheet.write(j + 1, 0, label=time.strftime('%H:%M:%S', time.localtime(now)))
# 动目标数写入表格
worksheet.write(j + 1, 1, label=T_num)
i = 0
while i < T_num: # 依次解析
# id号 无符号
# ID = BitArray(data[100+i*15:101+i*15]+data[99+i*15:100+i*15]).int
ID = int((data[100+i*15:101+i*15]+data[99+i*15:100+i*15]).hex(),16)
# Y轴距离 2 进行有符号解析的时候,要用BitArray
Y = BitArray(data[102+i*15:103+i*15]+data[101+i*15:102+i*15]).int
# 速度 2
V = BitArray(data[104+i*15:105+i*15]+data[103+i*15:104+i*15]).int
# X轴距离 2
X = BitArray(data[107+i*15:108+i*15]+data[106+i*15:107+i*15]).int
# 直线距离
R = round((Y*Y + X*X)**0.5,2)
# SNR
# SNR = BitArray(data[105:106]).int
SNR = int(data[105:106].hex(),16)
# 方位 判断
if Y > 0:
if X > 0:
AV = round(numpy.arctan(X / Y) * 180 / math.pi, 2)
elif X == 0:
AV = 0
else:
AV = round(numpy.arctan(X / Y) * 180 / math.pi + 360, 2)
elif Y == 0:
if X > 0:
AV = 90
else:
AV = 270
else:
if X == 0:
AV = 180
else:
AV = round(numpy.arctan(X / Y) * 180 / math.pi + 180, 2)
# 目标的标题 ID Y X R AV V SNR
worksheet.write(j+1, 2, label=ID)
worksheet.write(j+1, 3, label=Y)
worksheet.write(j+1, 4, label=X)
worksheet.write(j+1, 5, label=R)
worksheet.write(j+1, 6, label=AV)
worksheet.write(j+1, 7, label=V)
worksheet.write(j+1, 8, label=SNR)
j=j+1 # 行数加1
i=i+1 # 计数加1 列数加1
workbook.save('Excel_test_1'+time.strftime(' %Y-%m-%d',time.localtime(now))+'.xls')
|