重构目录结构:调整项目布局
This commit is contained in:
86
hardware/inverter.py
Normal file
86
hardware/inverter.py
Normal file
@ -0,0 +1,86 @@
|
||||
# hardware/inverter.py
|
||||
from pymodbus.exceptions import ModbusException
|
||||
|
||||
|
||||
class InverterController:
|
||||
def __init__(self, relay_controller):
|
||||
self.relay_controller = relay_controller
|
||||
self.max_frequency = 400.0 # 频率最大值
|
||||
|
||||
# 变频器配置
|
||||
self.config = {
|
||||
'slave_id': 1,
|
||||
'frequency_register': 0x01, # 2001H
|
||||
'start_register': 0x00, # 2000H
|
||||
'stop_register': 0x00, # 2000H(用于停机)
|
||||
'start_command': 0x0013, # 正转点动运行
|
||||
'stop_command': 0x0001 # 停机
|
||||
}
|
||||
|
||||
def set_frequency(self, frequency):
|
||||
"""设置变频器频率"""
|
||||
try:
|
||||
if not self.relay_controller.modbus_client.connect():
|
||||
print("无法连接网络继电器Modbus服务")
|
||||
return False
|
||||
|
||||
# 使用最大频率变量计算百分比
|
||||
percentage = frequency / self.max_frequency # 得到 0~1 的比例
|
||||
value = int(percentage * 10000) # 转换为 -10000 ~ 10000 的整数
|
||||
|
||||
# 限制范围
|
||||
value = max(-10000, min(10000, value))
|
||||
|
||||
result = self.relay_controller.modbus_client.write_register(
|
||||
self.config['frequency_register'],
|
||||
value,
|
||||
slave=self.config['slave_id']
|
||||
)
|
||||
|
||||
if isinstance(result, Exception):
|
||||
print(f"设置频率失败: {result}")
|
||||
return False
|
||||
|
||||
print(f"设置变频器频率为 {frequency}Hz")
|
||||
return True
|
||||
except ModbusException as e:
|
||||
print(f"变频器Modbus通信错误: {e}")
|
||||
return False
|
||||
finally:
|
||||
self.relay_controller.modbus_client.close()
|
||||
|
||||
def control(self, action):
|
||||
"""控制变频器启停"""
|
||||
try:
|
||||
if not self.relay_controller.modbus_client.connect():
|
||||
print("无法连接网络继电器Modbus服务")
|
||||
return False
|
||||
|
||||
if action == 'start':
|
||||
result = self.relay_controller.modbus_client.write_register(
|
||||
address=self.config['start_register'],
|
||||
value=self.config['start_command'],
|
||||
slave=self.config['slave_id']
|
||||
)
|
||||
print("启动变频器")
|
||||
elif action == 'stop':
|
||||
result = self.relay_controller.modbus_client.write_register(
|
||||
address=self.config['start_register'],
|
||||
value=self.config['stop_command'],
|
||||
slave=self.config['slave_id']
|
||||
)
|
||||
print("停止变频器")
|
||||
else:
|
||||
print(f"无效操作: {action}")
|
||||
return False
|
||||
|
||||
if isinstance(result, Exception):
|
||||
print(f"控制失败: {result}")
|
||||
return False
|
||||
|
||||
return True
|
||||
except ModbusException as e:
|
||||
print(f"变频器控制错误: {e}")
|
||||
return False
|
||||
finally:
|
||||
self.relay_controller.modbus_client.close()
|
||||
Reference in New Issue
Block a user