2025-09-26 13:32:34 +08:00
|
|
|
|
# hardware/transmitter.py
|
|
|
|
|
|
from pymodbus.exceptions import ModbusException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TransmitterController:
|
|
|
|
|
|
def __init__(self, relay_controller):
|
|
|
|
|
|
self.relay_controller = relay_controller
|
|
|
|
|
|
|
|
|
|
|
|
# 变送器配置
|
|
|
|
|
|
self.config = {
|
|
|
|
|
|
1: { # 上料斗
|
|
|
|
|
|
'slave_id': 1,
|
|
|
|
|
|
'weight_register': 0x01,
|
|
|
|
|
|
'register_count': 2
|
|
|
|
|
|
},
|
|
|
|
|
|
2: { # 下料斗
|
|
|
|
|
|
'slave_id': 2,
|
|
|
|
|
|
'weight_register': 0x01,
|
|
|
|
|
|
'register_count': 2
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-04 15:00:13 +08:00
|
|
|
|
# 备份 (modbus 读取数据)
|
|
|
|
|
|
def read_data_bak(self, transmitter_id):
|
2025-09-26 13:32:34 +08:00
|
|
|
|
"""读取变送器数据"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
if transmitter_id not in self.config:
|
|
|
|
|
|
print(f"无效变送器ID: {transmitter_id}")
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
config = self.config[transmitter_id]
|
|
|
|
|
|
|
|
|
|
|
|
if not self.relay_controller.modbus_client.connect():
|
|
|
|
|
|
print("无法连接网络继电器Modbus服务")
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
result = self.relay_controller.modbus_client.read_holding_registers(
|
|
|
|
|
|
address=config['weight_register'],
|
|
|
|
|
|
count=config['register_count'],
|
|
|
|
|
|
slave=config['slave_id']
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(result, Exception):
|
|
|
|
|
|
print(f"读取变送器 {transmitter_id} 失败: {result}")
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# 根据图片示例,正确解析数据
|
|
|
|
|
|
if config['register_count'] == 2:
|
|
|
|
|
|
# 获取原始字节数组
|
|
|
|
|
|
raw_data = result.registers
|
|
|
|
|
|
# 组合成32位整数
|
|
|
|
|
|
weight = (raw_data[0] << 16) + raw_data[1]
|
|
|
|
|
|
weight = weight / 1000.0 # 单位转换为千克
|
|
|
|
|
|
elif config['register_count'] == 1:
|
|
|
|
|
|
weight = float(result.registers[0])
|
|
|
|
|
|
else:
|
|
|
|
|
|
print(f"不支持的寄存器数量: {config['register_count']}")
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
print(f"变送器 {transmitter_id} 读取重量: {weight}kg")
|
|
|
|
|
|
return weight
|
|
|
|
|
|
|
|
|
|
|
|
except ModbusException as e:
|
|
|
|
|
|
print(f"Modbus通信错误: {e}")
|
|
|
|
|
|
return None
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
print(f"数据解析错误: {e}")
|
|
|
|
|
|
return None
|
|
|
|
|
|
finally:
|
|
|
|
|
|
self.relay_controller.modbus_client.close()
|
2025-11-04 11:40:52 +08:00
|
|
|
|
|
2025-11-04 15:00:13 +08:00
|
|
|
|
# 直接读取 变送器返回的数据并解析
|
2025-11-04 11:40:52 +08:00
|
|
|
|
def read_data(self, transmitter_id):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Args: transmitter_id 为1 表示上料斗, 为2 表示下料斗
|
|
|
|
|
|
return: 读取成功返回重量 weight: int, 失败返回 None
|
|
|
|
|
|
"""
|
|
|
|
|
|
if transmitter_id == 1:
|
|
|
|
|
|
# 上料斗变送器的信息:
|
|
|
|
|
|
IP = "192.168.250.63"
|
|
|
|
|
|
PORT = 502
|
2025-11-04 16:20:50 +08:00
|
|
|
|
TIMEOUT = 2 # 超时时间为 2秒
|
2025-11-04 15:00:13 +08:00
|
|
|
|
BUFFER_SIZE= 1024
|
2025-11-04 11:40:52 +08:00
|
|
|
|
weight = None
|
|
|
|
|
|
|
|
|
|
|
|
import socket
|
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
|
|
try:
|
|
|
|
|
|
s.settimeout(TIMEOUT)
|
|
|
|
|
|
s.connect((IP, PORT))
|
2025-11-04 15:00:13 +08:00
|
|
|
|
# print(f"连接上料斗变送器 {IP}:{PORT} 成功")
|
2025-11-04 11:40:52 +08:00
|
|
|
|
|
2025-11-04 15:00:13 +08:00
|
|
|
|
# 接收数据(变送器主动推送,recv即可获取数据)
|
|
|
|
|
|
data = s.recv(BUFFER_SIZE)
|
2025-11-04 11:40:52 +08:00
|
|
|
|
if data:
|
|
|
|
|
|
# print(f"收到原始数据:{data}")
|
2025-11-04 15:00:13 +08:00
|
|
|
|
|
|
|
|
|
|
# 提取出完整的一个数据包 (\r\n结尾)
|
|
|
|
|
|
packet = self.get_latest_valid_packet(data)
|
|
|
|
|
|
if not packet:
|
|
|
|
|
|
print("未获取到有效数据包!!")
|
|
|
|
|
|
return None
|
|
|
|
|
|
# 解析重量
|
|
|
|
|
|
weight = self.parse_weight(packet)
|
2025-11-04 11:40:52 +08:00
|
|
|
|
else:
|
2025-11-04 15:00:13 +08:00
|
|
|
|
print("未收到设备数据")
|
2025-11-04 11:40:52 +08:00
|
|
|
|
|
|
|
|
|
|
except ConnectionRefusedError:
|
2025-11-04 16:20:50 +08:00
|
|
|
|
print(f"变送器连接失败:{IP}:{PORT} 拒绝连接(设备离线/端口错误)")
|
2025-11-04 11:40:52 +08:00
|
|
|
|
except socket.timeout:
|
2025-11-04 16:20:50 +08:00
|
|
|
|
print(f"读取变送器数据超时:{TIMEOUT}秒内未收到数据")
|
2025-11-04 11:40:52 +08:00
|
|
|
|
except Exception as e:
|
2025-11-04 15:00:13 +08:00
|
|
|
|
print(f"读取异常:{e}")
|
2025-11-04 11:40:52 +08:00
|
|
|
|
|
|
|
|
|
|
# 成功返回重量(int),失败返回None
|
|
|
|
|
|
return weight
|
2025-11-04 15:00:13 +08:00
|
|
|
|
|
|
|
|
|
|
def get_latest_valid_packet(self, raw_data):
|
|
|
|
|
|
"""
|
|
|
|
|
|
解决TCP粘包:
|
|
|
|
|
|
从原始数据中,筛选所有有效包,返回最新的一个有效包
|
|
|
|
|
|
有效包标准: 1. 能UTF-8解码 2. 按逗号拆分≥3个字段 3. 第三个字段含数字(重量)
|
|
|
|
|
|
"""
|
|
|
|
|
|
DELIMITER = b'\r\n'
|
|
|
|
|
|
# 1. 按分隔符拆分,过滤空包
|
|
|
|
|
|
packets = [p for p in raw_data.split(DELIMITER) if p]
|
|
|
|
|
|
if not packets:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
valid_packets = []
|
|
|
|
|
|
for packet in packets:
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 过滤无效ASCII字符(只保留可见字符)
|
|
|
|
|
|
valid_chars = [c for c in packet if 32 <= c <= 126]
|
|
|
|
|
|
filtered_packet = bytes(valid_chars)
|
|
|
|
|
|
# 2. 验证解码
|
|
|
|
|
|
data_str = filtered_packet.decode('utf-8').strip()
|
|
|
|
|
|
# 3. 验证字段数量
|
|
|
|
|
|
parts = data_str.split(',')
|
|
|
|
|
|
if len(parts) < 3:
|
|
|
|
|
|
continue
|
|
|
|
|
|
# 4. 验证重量字段含数字
|
|
|
|
|
|
weight_part = parts[2].strip()
|
|
|
|
|
|
if not any(char.isdigit() for char in weight_part):
|
|
|
|
|
|
continue
|
|
|
|
|
|
# 满足所有条件,加入有效包列表
|
|
|
|
|
|
valid_packets.append(packet)
|
|
|
|
|
|
except (UnicodeDecodeError, IndexError):
|
|
|
|
|
|
# 解码失败或字段异常,跳过该包
|
|
|
|
|
|
continue
|
2025-11-04 11:40:52 +08:00
|
|
|
|
|
2025-11-04 15:00:13 +08:00
|
|
|
|
# 返回最后一个有效包(最新),无有效包则返回None
|
|
|
|
|
|
return valid_packets[-1] if valid_packets else None
|
|
|
|
|
|
|
|
|
|
|
|
def parse_weight(self, packet_data):
|
|
|
|
|
|
"""解析重量函数:提取重量数值(如从 b'ST,NT,+0000175\r\n' 中提取 175)"""
|
2025-11-04 11:40:52 +08:00
|
|
|
|
try:
|
2025-11-04 15:00:13 +08:00
|
|
|
|
data_str = packet_data.decode('utf-8').strip()
|
2025-11-04 11:40:52 +08:00
|
|
|
|
parts = data_str.split(',')
|
2025-11-04 15:00:13 +08:00
|
|
|
|
# 确保有完整的数据包,三个字段
|
|
|
|
|
|
if len(parts) < 3:
|
|
|
|
|
|
print(f"parse_weight: 包格式错误(字段不足):{data_str}")
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
2025-11-04 11:40:52 +08:00
|
|
|
|
weight_part = parts[2].strip()
|
|
|
|
|
|
return int(''.join(filter(str.isdigit, weight_part)))
|
|
|
|
|
|
except (IndexError, ValueError, UnicodeDecodeError) as e:
|
2025-11-04 15:00:13 +08:00
|
|
|
|
# print(f"数据解析失败:{e},原始数据包:{packet_data}")
|
|
|
|
|
|
return None
|
|
|
|
|
|
|