Files
archery/cslaser.py
2025-11-24 16:52:53 +08:00

61 lines
1.9 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.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
激光模块开关测试脚本
平台MaixPy (Sipeed MAIX)
功能每2秒循环开启/关闭激光,验证硬件是否正常响应
作者ZZH
"""
from maix import uart, pinmap, time
# === 配置 ===
UART_PORT = "/dev/ttyS1" # 激光模块连接的串口(通常是 UART1
BAUDRATE = 9600 # 波特率(根据你的模块调整)
# 引脚映射(根据你硬件连接修改)
pinmap.set_pin_function("A18", "UART1_RX") # RX
pinmap.set_pin_function("A19", "UART1_TX") # TX
# 激光控制指令(根据你的模块协议)
MODULE_ADDR = 0x00
LASER_ON_CMD = bytes([0xAA, MODULE_ADDR, 0x01, 0xBE, 0x00, 0x01, 0x00, 0x01, 0xC1])
LASER_OFF_CMD = bytes([0xAA, MODULE_ADDR, 0x01, 0xBE, 0x00, 0x01, 0x00, 0x00, 0xC0])
# === 初始化串口 ===
print("🔧 正在初始化激光串口...")
laser_uart = uart.UART(UART_PORT, BAUDRATE)
# === 辅助函数 ===
def send_laser_cmd(cmd, name):
"""发送激光指令并尝试读取回包"""
print(f"➡️ 发送指令: {name}")
laser_uart.write(cmd)
time.sleep_ms(50) # 等待模块处理
# 尝试读取回包(非必须,部分模块无返回)
resp = laser_uart.read(20)
if resp:
print(f"✅ 收到回包 ({len(resp)}字节): {resp.hex()}")
else:
print("🔇 无回包(正常或模块不支持)")
# === 主测试循环 ===
print("\n🚀 开始激光开关测试(按 Ctrl+C 停止)")
print("周期开1秒 → 关1秒\n")
try:
while True:
# 开启激光
send_laser_cmd(LASER_ON_CMD, "LASER ON")
time.sleep(1.0) # 持续开启 1 秒
# 关闭激光
send_laser_cmd(LASER_OFF_CMD, "LASER OFF")
time.sleep(1.0) # 关闭 1 秒
except KeyboardInterrupt:
print("\n🛑 测试被用户中断")
# 最终确保激光关闭
laser_uart.write(LASER_OFF_CMD)
print("✅ 已发送最终关闭指令")