重构目录结构:调整项目布局
This commit is contained in:
10
hardware/__init__.py
Normal file
10
hardware/__init__.py
Normal file
@ -0,0 +1,10 @@
|
||||
# hardware/__init__.py
|
||||
"""
|
||||
硬件控制模块
|
||||
包含所有硬件设备的控制接口
|
||||
"""
|
||||
from .relay import RelayController
|
||||
from .inverter import InverterController
|
||||
from .transmitter import TransmitterController
|
||||
|
||||
__all__ = ['RelayController', 'InverterController', 'TransmitterController']
|
||||
BIN
hardware/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
hardware/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
hardware/__pycache__/inverter.cpython-39.pyc
Normal file
BIN
hardware/__pycache__/inverter.cpython-39.pyc
Normal file
Binary file not shown.
BIN
hardware/__pycache__/relay.cpython-39.pyc
Normal file
BIN
hardware/__pycache__/relay.cpython-39.pyc
Normal file
Binary file not shown.
BIN
hardware/__pycache__/transmitter.cpython-39.pyc
Normal file
BIN
hardware/__pycache__/transmitter.cpython-39.pyc
Normal file
Binary file not shown.
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()
|
||||
77
hardware/relay.py
Normal file
77
hardware/relay.py
Normal file
@ -0,0 +1,77 @@
|
||||
# hardware/relay.py
|
||||
import socket
|
||||
import binascii
|
||||
from pymodbus.client import ModbusTcpClient
|
||||
from pymodbus.exceptions import ModbusException
|
||||
|
||||
|
||||
class RelayController:
|
||||
# 继电器映射
|
||||
DOOR_UPPER = 'door_upper' # DO0 - 上料斗滑动
|
||||
DOOR_LOWER_1 = 'door_lower_1' # DO1 - 上料斗出砼门
|
||||
DOOR_LOWER_2 = 'door_lower_2' # DO2 - 下料斗出砼门
|
||||
BREAK_ARCH_UPPER = 'break_arch_upper' # DO3 - 上料斗破拱
|
||||
BREAK_ARCH_LOWER = 'break_arch_lower' # DO4 - 下料斗破拱
|
||||
|
||||
def __init__(self, host='192.168.0.18', port=50000):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.modbus_client = ModbusTcpClient(host, port=port)
|
||||
|
||||
# 继电器命令(原始Socket)
|
||||
self.relay_commands = {
|
||||
self.DOOR_UPPER: {'open': '00000000000601050000FF00', 'close': '000000000006010500000000'},
|
||||
self.DOOR_LOWER_1: {'open': '00000000000601050001FF00', 'close': '000000000006010500010000'},
|
||||
self.DOOR_LOWER_2: {'open': '00000000000601050002FF00', 'close': '000000000006010500020000'},
|
||||
self.BREAK_ARCH_UPPER: {'open': '00000000000601050003FF00', 'close': '000000000006010500030000'},
|
||||
self.BREAK_ARCH_LOWER: {'open': '00000000000601050004FF00', 'close': '000000000006010500040000'}
|
||||
}
|
||||
|
||||
# 读取状态命令
|
||||
self.read_status_command = '000000000006010100000008'
|
||||
|
||||
# 设备位映射
|
||||
self.device_bit_map = {
|
||||
self.DOOR_UPPER: 0,
|
||||
self.DOOR_LOWER_1: 1,
|
||||
self.DOOR_LOWER_2: 2,
|
||||
self.BREAK_ARCH_UPPER: 3,
|
||||
self.BREAK_ARCH_LOWER: 4
|
||||
}
|
||||
|
||||
def send_command(self, command_hex):
|
||||
"""发送原始Socket命令"""
|
||||
try:
|
||||
byte_data = binascii.unhexlify(command_hex)
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
sock.connect((self.host, self.port))
|
||||
sock.send(byte_data)
|
||||
response = sock.recv(1024)
|
||||
print(f"收到继电器响应: {binascii.hexlify(response)}")
|
||||
return response
|
||||
except Exception as e:
|
||||
print(f"继电器通信错误: {e}")
|
||||
return None
|
||||
|
||||
def get_status(self):
|
||||
"""获取继电器状态"""
|
||||
response = self.send_command(self.read_status_command)
|
||||
status_dict = {}
|
||||
|
||||
if response and len(response) >= 10:
|
||||
status_byte = response[9]
|
||||
status_bin = f"{status_byte:08b}"[::-1]
|
||||
for key, bit_index in self.device_bit_map.items():
|
||||
status_dict[key] = status_bin[bit_index] == '1'
|
||||
else:
|
||||
print("读取继电器状态失败")
|
||||
|
||||
return status_dict
|
||||
|
||||
def control(self, device, action):
|
||||
"""控制继电器"""
|
||||
if device in self.relay_commands and action in self.relay_commands[device]:
|
||||
print(f"控制继电器 {device} {action}")
|
||||
self.send_command(self.relay_commands[device][action])
|
||||
else:
|
||||
print(f"无效设备或动作: {device}, {action}")
|
||||
69
hardware/transmitter.py
Normal file
69
hardware/transmitter.py
Normal file
@ -0,0 +1,69 @@
|
||||
# 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
|
||||
}
|
||||
}
|
||||
|
||||
def read_data(self, transmitter_id):
|
||||
"""读取变送器数据"""
|
||||
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()
|
||||
Reference in New Issue
Block a user