feat(wits): 添加WITS数据模拟发送脚本- 实现随机生成WITS协议数据功能
- 支持整数和浮点数两种数据格式 - 配置目标主机和端口进行TCP连接 - 循环发送预定义的WITS字段码 - 添加发送间隔控制和异常处理机制- 提供命令行入口直接运行脚本main
parent
ea2d12c74e
commit
123dfc4411
|
|
@ -0,0 +1,61 @@
|
|||
import socket
|
||||
import random
|
||||
import time
|
||||
|
||||
HOST = "192.168.1.5" # 目标地址
|
||||
PORT = 9929 # 目标端口
|
||||
|
||||
# 你给的示例里出现的所有前四位字段
|
||||
WITS_CODES = [
|
||||
"0105",
|
||||
"0106",
|
||||
"0108",
|
||||
"0112",
|
||||
"0114",
|
||||
"0116",
|
||||
"0118",
|
||||
"0120",
|
||||
"0121",
|
||||
"0122"
|
||||
]
|
||||
|
||||
def random_value(prefix):
|
||||
"""
|
||||
生成类似你收到的数据:
|
||||
- 有些是整数:例如 0105 251114
|
||||
- 有些是浮点:例如 0108 37.26745
|
||||
"""
|
||||
# 随机决定生成整数 or 小数
|
||||
if random.random() < 0.3:
|
||||
# 生成整数(6位左右)
|
||||
value = str(random.randint(100000, 999999))
|
||||
else:
|
||||
# 生成浮点(保留4~5位小数)
|
||||
value = f"{random.uniform(0, 500):.5f}"
|
||||
|
||||
return prefix + value
|
||||
|
||||
|
||||
def send_wits_data():
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((HOST, PORT))
|
||||
print("Connected to target. Sending WITS data...")
|
||||
|
||||
try:
|
||||
while True:
|
||||
for code in WITS_CODES:
|
||||
msg = random_value(code)
|
||||
|
||||
sock.sendall((msg + "\r\n").encode())
|
||||
print("Sent:", msg)
|
||||
|
||||
time.sleep(0.2) # 每条间隔 200ms,可根据需要调整
|
||||
|
||||
except Exception as e:
|
||||
print("Error:", e)
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
send_wits_data()
|
||||
Loading…
Reference in New Issue