变频器集成以及增加点动控制(0209)
This commit is contained in:
88
test copy.py
88
test copy.py
@ -1,32 +1,62 @@
|
||||
import socket
|
||||
# 读取原始数据文件
|
||||
import datetime
|
||||
|
||||
# 设备信息
|
||||
IP = "192.168.250.63"
|
||||
PORT = 502
|
||||
TIMEOUT = 5 # 超时时间(秒)
|
||||
input_file = r"C:\Users\fujin\Desktop\fsdownload\weight.txt" # 原始数据文件路径
|
||||
output_file = r"C:\Users\fujin\Desktop\fsdownload\filtered_B_records.txt" # 输出结果文件路径 (使用原始字符串避免Unicode转义错误)
|
||||
|
||||
# 创建TCP socket
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
try:
|
||||
s.settimeout(TIMEOUT) # 设置超时,避免一直阻塞
|
||||
# 连接设备
|
||||
s.connect((IP, PORT))
|
||||
print(f"✅ 已通过TCP连接到 {IP}:{PORT}")
|
||||
|
||||
# 尝试接收数据(不发送任何请求,纯等待)
|
||||
print("等待设备发送数据...(若5秒内无响应则超时)")
|
||||
data = s.recv(1024) # 最多接收1024字节
|
||||
|
||||
if data:
|
||||
# 打印收到的原始数据(16进制和字节列表)
|
||||
# print(f"收到数据(16进制):{data.hex()}")
|
||||
print(f"收到数据(字节列表):{list(data)}")
|
||||
else:
|
||||
print("❌ 未收到任何数据(设备未主动发送)")
|
||||
# 存储包含"B"标记的记录
|
||||
filtered_records = []
|
||||
|
||||
# 读取并过滤数据
|
||||
with open(input_file, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
# 筛选包含"B,"的行(避免误匹配其他含B的字符)
|
||||
if "B," in line or "F," in line:
|
||||
# 移除等号
|
||||
cleaned_line = line.strip().replace('=', '')
|
||||
|
||||
except ConnectionRefusedError:
|
||||
print(f"❌ 连接失败:{IP}:{PORT} 拒绝连接(设备离线/端口错误)")
|
||||
except socket.timeout:
|
||||
print(f"❌ 超时:{TIMEOUT}秒内未收到设备数据(设备未主动发送)")
|
||||
except Exception as e:
|
||||
print(f"❌ 发生错误:{str(e)}")
|
||||
# 分割行数据
|
||||
parts = cleaned_line.split(',')
|
||||
|
||||
if len(parts) >= 2:
|
||||
try:
|
||||
# 解析前两个日期(假设格式为YYYY-MM-DD或YYYY/MM/DD)
|
||||
date1_str = parts[0].strip()
|
||||
date2_str = parts[1].strip()
|
||||
|
||||
# 尝试不同的日期格式
|
||||
date_formats = ['%Y-%m-%d %H:%M:%S', '%Y-%m-%d', '%Y/%m/%d']
|
||||
date1 = None
|
||||
date2 = None
|
||||
|
||||
for fmt in date_formats:
|
||||
try:
|
||||
if date1 is None:
|
||||
date1 = datetime.datetime.strptime(date1_str, fmt)
|
||||
if date2 is None:
|
||||
date2 = datetime.datetime.strptime(date2_str, fmt)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
if date1 and date2:
|
||||
# 计算时间差(秒数)
|
||||
total_seconds = (date2 - date1).total_seconds()
|
||||
# 转换为分:秒格式
|
||||
minutes = int(total_seconds // 60)
|
||||
seconds = int(total_seconds % 60)
|
||||
time_diff = f"{minutes:02d}:{seconds:02d}"
|
||||
# 在行末尾添加时间差
|
||||
cleaned_line = f"{cleaned_line},{time_diff}"
|
||||
except Exception as e:
|
||||
# 如果日期解析失败,保持原始行不变
|
||||
pass
|
||||
|
||||
filtered_records.append(cleaned_line)
|
||||
|
||||
# 保存过滤后的数据
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(filtered_records))
|
||||
|
||||
print(f"过滤完成!共提取到 {len(filtered_records)} 条含'B'标记的记录")
|
||||
print(f"结果已保存至:{output_file}")
|
||||
Reference in New Issue
Block a user