stage_one
This commit is contained in:
339
tests/485test.py
Normal file
339
tests/485test.py
Normal file
@ -0,0 +1,339 @@
|
||||
import serial
|
||||
import time
|
||||
import struct
|
||||
|
||||
|
||||
class InovanceMD520:
|
||||
def __init__(self, port='COM4', baudrate=9600, timeout=1):
|
||||
"""
|
||||
初始化汇川MD520变频器通信
|
||||
:param port: 串口名称,Windows为COMx,Linux为/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 set_frequency(self, slave_addr=0x01,frequency=210):
|
||||
"""
|
||||
变频器运行频率
|
||||
: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, # 从站地址
|
||||
0x06, # 功能码:读取保持寄存器
|
||||
0x10, 0x00, # 寄存器地址:1001H(运行频率)
|
||||
0x1B, 0x58 # 读取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] != 0x06:
|
||||
print(f"功能码错误: 期望03, 收到{response[1]:02X}")
|
||||
return None
|
||||
|
||||
# 验证CRC
|
||||
received_crc = response[-2:]
|
||||
calculated_crc = self.calculate_crc(response[:-2])
|
||||
if received_crc != calculated_crc:
|
||||
print("CRC校验失败")
|
||||
return None
|
||||
|
||||
return True
|
||||
|
||||
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='COM3', baudrate=9600)
|
||||
|
||||
# 连接串口
|
||||
if not inverter.connect():
|
||||
return
|
||||
|
||||
try:
|
||||
while True:
|
||||
print("\n" + "=" * 50)
|
||||
print("汇川MD520变频器频率查询")
|
||||
print("=" * 50)
|
||||
|
||||
# 设置允许频率
|
||||
frequency = inverter.set_frequency(slave_addr=0x01, frequency=210.0)
|
||||
if frequency is not None:
|
||||
print(f"✅ 设置成功")
|
||||
else:
|
||||
print("❌ 频率设置失败")
|
||||
|
||||
# 查询运行频率
|
||||
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()
|
||||
@ -203,7 +203,7 @@ class InovanceMD520:
|
||||
|
||||
def main():
|
||||
# 创建变频器对象
|
||||
inverter = InovanceMD520(port='COM4', baudrate=9600)
|
||||
inverter = InovanceMD520(port='COM3', baudrate=9600)
|
||||
|
||||
# 连接串口
|
||||
if not inverter.connect():
|
||||
|
||||
@ -3,15 +3,20 @@ RFID
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
import time
|
||||
from hardware.RFID.rfid_service import rfid_service
|
||||
|
||||
|
||||
def test_data_callback(raw_data):
|
||||
|
||||
def test_data_callback(status,raw_data):
|
||||
"""
|
||||
测试用的数据接收回调函数
|
||||
"""
|
||||
print(f"[回调] 收到RFID数据: {raw_data}")
|
||||
if status:
|
||||
print(f"[回调] 收到RFID数据: {raw_data}")
|
||||
else:
|
||||
print(f"[回调] 读取RFID数据为空或失败")
|
||||
|
||||
|
||||
def test_rfid_functions():
|
||||
@ -19,7 +24,7 @@ def test_rfid_functions():
|
||||
测试RFIDHardware的主要功能
|
||||
"""
|
||||
# 初始化RFID控制器
|
||||
rfid = rfid_service(host='192.168.250.67', port=6000)
|
||||
rfid = rfid_service(host='192.168.250.77', port=6000)
|
||||
|
||||
# print("=== RFID硬件测试开始 ===")
|
||||
|
||||
@ -35,7 +40,7 @@ def test_rfid_functions():
|
||||
# mode_data = rfid.read_working_mode(address=0x00)
|
||||
# if mode_data:
|
||||
# print("读取到工作模式参数:")
|
||||
# for key, value in mode_data.items():
|
||||
# for key, value in mode_data.items():.
|
||||
# print(f" {key}: {value:02X} ({value})")
|
||||
|
||||
# rfid.set_working_mode(address=0x00, mode_params={
|
||||
@ -59,8 +64,8 @@ def test_rfid_functions():
|
||||
# print("读取读写器信息失败")
|
||||
|
||||
# 测试设置功率 (仅演示,实际使用时根据需要调整)
|
||||
# print("\n3. 测试设置功率 (演示,不实际执行):")
|
||||
# power_success = rfid.set_power(address=0x00, power_value=16)
|
||||
# print("\n3. 测试设置功率:")
|
||||
# power_success = rfid.set_power(address=0x00, power_value=26)
|
||||
# print(f"功率设置{'成功' if power_success else '失败'}")
|
||||
|
||||
# # 测试设置读卡间隔 (仅演示)
|
||||
|
||||
Reference in New Issue
Block a user