Files
Feeding_control_system/tests/Investor485test.py
2025-11-17 00:05:40 +08:00

259 lines
8.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import serial
import time
import struct
class InovanceMD520:
def __init__(self, port='COM4', baudrate=9600, timeout=1):
"""
初始化汇川MD520变频器通信
:param port: 串口名称Windows为COMxLinux为/dev/ttyUSBx
:param baudrate: 波特率默认9600
:param timeout: 超时时间,秒
"""
self.port = port
self.baudrate = baudrate
self.timeout = timeout
self.ser = None
def connect(self):
"""连接串口"""
try:
self.ser = serial.Serial(
port=self.port,
baudrate=self.baudrate,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=self.timeout
)
print(f"成功连接到串口 {self.port}")
return True
except serial.SerialException as e:
print(f"连接串口失败: {e}")
return False
def disconnect(self):
"""断开串口连接"""
if self.ser and self.ser.is_open:
self.ser.close()
print("串口连接已关闭")
def calculate_crc(self, data):
"""
计算Modbus CRC16校验码
:param data: 字节数据
:return: CRC校验码低位在前高位在后
"""
crc = 0xFFFF
for byte in data:
crc ^= byte
for _ in range(8):
if crc & 0x0001:
crc = (crc >> 1) ^ 0xA001
else:
crc = crc >> 1
return struct.pack('<H', crc) # 小端序,低位在前
def query_frequency(self, slave_addr=0x01):
"""
查询变频器运行频率
:param slave_addr: 从站地址默认1
:return: 频率值Hz如查询失败返回None
"""
if not self.ser or not self.ser.is_open:
print("串口未连接")
return None
# 构建查询指令读取运行频率地址1001H
# 01 03 10 01 00 01
cmd_data = bytes([
slave_addr, # 从站地址
0x03, # 功能码:读取保持寄存器
0x10, 0x01, # 寄存器地址1001H运行频率
0x00, 0x01 # 读取1个寄存器
])
# 计算CRC
crc = self.calculate_crc(cmd_data)
# 完整指令
full_cmd = cmd_data + crc
print(f"发送查询指令: {full_cmd.hex().upper()}")
try:
# 清空输入缓冲区
self.ser.reset_input_buffer()
# 发送指令
self.ser.write(full_cmd)
# 等待响应根据文档需要等待3.5个字节时间)
time.sleep(0.01) # 约10ms延迟
# 读取响应
# 正常响应格式:地址(1) + 功能码(1) + 字节数(1) + 数据(2) + CRC(2) = 7字节
response = self.ser.read(7)
if len(response) < 7:
print(f"响应数据长度不足: {len(response)} 字节")
return None
print(f"收到响应: {response.hex().upper()}")
# 验证响应
if response[0] != slave_addr:
print(f"从站地址不匹配: 期望{slave_addr:02X}, 收到{response[0]:02X}")
return None
if response[1] != 0x03:
print(f"功能码错误: 期望03, 收到{response[1]:02X}")
return None
if response[2] != 0x02:
print(f"数据长度错误: 期望02, 收到{response[2]:02X}")
return None
# 验证CRC
received_crc = response[-2:]
calculated_crc = self.calculate_crc(response[:-2])
if received_crc != calculated_crc:
print("CRC校验失败")
return None
# 解析频率数据(高字节在前)
frequency_raw = (response[3] << 8) | response[4]
frequency_hz = frequency_raw / 100.0 # 转换为Hz单位0.01Hz
print(f"原始频率值: {frequency_raw} (0x{frequency_raw:04X})")
print(f"实际频率: {frequency_hz:.2f} Hz")
return frequency_hz
except Exception as e:
print(f"通信错误: {e}")
return None
def read_register(self, slave_addr, register_addr, register_count=1):
"""
通用读取寄存器方法
:param slave_addr: 从站地址
:param register_addr: 寄存器地址16位
:param register_count: 读取的寄存器数量
:return: 读取的数据列表
"""
if not self.ser or not self.ser.is_open:
print("串口未连接")
return None
# 构建读取指令
cmd_data = bytes([
slave_addr, # 从站地址
0x03, # 功能码:读取保持寄存器
(register_addr >> 8) & 0xFF, # 寄存器地址高字节
register_addr & 0xFF, # 寄存器地址低字节
(register_count >> 8) & 0xFF, # 寄存器数量高字节
register_count & 0xFF # 寄存器数量低字节
])
# 计算CRC
crc = self.calculate_crc(cmd_data)
full_cmd = cmd_data + crc
print(f"发送读取指令: {full_cmd.hex().upper()}")
try:
self.ser.reset_input_buffer()
self.ser.write(full_cmd)
time.sleep(0.01)
# 计算预期响应长度
expected_length = 5 + 2 * register_count # 地址1 + 功能码1 + 字节数1 + 数据2*N + CRC2
response = self.ser.read(expected_length)
if len(response) < expected_length:
print(f"响应数据长度不足: {len(response)} 字节,期望 {expected_length} 字节")
return None
print(f"收到响应: {response.hex().upper()}")
# 验证CRC
received_crc = response[-2:]
calculated_crc = self.calculate_crc(response[:-2])
if received_crc != calculated_crc:
print("CRC校验失败")
return None
# 解析数据
data_length = response[2]
data_bytes = response[3:3 + data_length]
results = []
for i in range(0, len(data_bytes), 2):
value = (data_bytes[i] << 8) | data_bytes[i + 1]
results.append(value)
return results
except Exception as e:
print(f"通信错误: {e}")
return None
def main():
# 创建变频器对象
inverter = InovanceMD520(port='COM4', baudrate=9600)
# 连接串口
if not inverter.connect():
return
try:
while True:
print("\n" + "=" * 50)
print("汇川MD520变频器频率查询")
print("=" * 50)
# 查询运行频率
frequency = inverter.query_frequency(slave_addr=0x01)
if frequency is not None:
print(f"✅ 当前运行频率: {frequency:.2f} Hz")
else:
print("❌ 频率查询失败")
# 可选:读取其他监控参数
print("\n--- 其他监控参数 ---")
# 读取母线电压 (地址1002H)
voltage_data = inverter.read_register(0x01, 0x1002)
if voltage_data:
voltage = voltage_data[0] / 10.0 # 单位0.1V
print(f"母线电压: {voltage:.1f} V")
# 读取输出电压 (地址1003H)
output_voltage_data = inverter.read_register(0x01, 0x1003)
if output_voltage_data:
output_voltage = output_voltage_data[0] # 单位1V
print(f"输出电压: {output_voltage} V")
# 读取输出电流 (地址1004H)
current_data = inverter.read_register(0x01, 0x1004)
if current_data:
current = current_data[0] / 100.0 # 单位0.01A
print(f"输出电流: {current:.2f} A")
# 等待5秒后再次查询
print("\n等待5秒后继续查询...")
time.sleep(5)
except KeyboardInterrupt:
print("\n用户中断查询")
finally:
# 断开连接
inverter.disconnect()
if __name__ == "__main__":
main()