py-test/send_wtis.py

62 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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()