Files
Feeding_control_system/tests/485test.py

79 lines
2.7 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 minimalmodbus
import time
from serial import SerialException
# 设置频率为55Hz并启动变频器
def start_inverter_55hz():
try:
# 1. 连接到变频器
# COM3: 串口地址
# 1: 从站地址
inverter = minimalmodbus.Instrument('COM3', 1)
# 2. 配置串口参数
inverter.serial.baudrate = 9600 # 波特率
inverter.serial.bytesize = 8 # 数据位
inverter.serial.parity = 'N' # 无校验
inverter.serial.stopbits = 1 # 停止位
inverter.serial.timeout = 1.0 # 超时时间
inverter.mode = minimalmodbus.MODE_RTU # RTU模式
print("✅ 已连接到变频器")
# 3. 设置频率为55Hz
# 假设最大频率为50Hz55Hz对应110% = 11000
# # 如果您的最大频率不同,请调整下面的计算
# frequency_value = int(70 * 100)
# inverter.write_register(0x1000, frequency_value)
# print(f"✅ 频率设置为200Hz")
frequency_value = int(210 * 100)
inverter.write_register(0x7310, frequency_value)
# freq = inverter.read_register(0x7310)
# print(f" 读取到的频率值: {freq}")
# max_freq = 300
# target_freq = 230
# frequency_value = int(target_freq / max_freq * 100 * 100) # = 2333
# inverter.write_register(0x1000, frequency_value)
# time.sleep(0.5) # 等待一下
# 4. 启动变频器(正转运行)
inverter.write_register(0x2000, 1) # 1=正转运行
print("✅ 变频器已启动(正转运行)")
# time.sleep()
# inverter.write_register(0x2000, 6) # 1=正转运行
# print("✅ 变频器已停止(正转运行)")
# 5. 检查状态
time.sleep(1)
#读取3000H可直接读取变频器的当前状态0001正转运行0002反转运行0003停机0004电机参数辨识0005故障
status = inverter.read_register(0x3000)
if status == 1:
print("✅ 变频器正在正转运行")
elif status == 5:
print("⚠️ 变频器故障")
else:
print(f"📊 变频器状态码: {status}")
except SerialException:
print('无法连接或打开串口')
except Exception as e:
print(f"❌ 错误: {e}")
print("请检查:")
print("1. COM3端口是否正确")
print("2. 变频器电源和通信线是否连接正常?")
print("3. 变频器地址和波特率设置是否正确?")
finally:
print("脚本执行完成")
# 运行
if __name__ == "__main__":
start_inverter_55hz()