import os import configparser # 模拟 Real_Position 类 class Real_Position: def __init__(self): self.X = 0 self.Y = 0 self.Z = 0 self.A = 0 self.B = 0 self.C = 0 def init_position(self, x, y, z, a, b, c): self.X = x self.Y = y self.Z = z self.A = a self.B = b self.C = c def get_position(self): return self def __str__(self): return f"X:{self.X:.3f}, Y:{self.Y:.3f}, Z:{self.Z:.3f}, A:{self.A:.3f}, B:{self.B:.3f}, C:{self.C:.3f}" # 模拟 DetectStatus class DetectStatus: DOk = "DOk" # 实际要测试的类 class Detect: def __init__(self): self.detection = "" self.detect_status = DetectStatus.DOk self.detect_position = None self.position_index = 0 # 默认读取索引为 0 的点位 def run(self): if self.detect_status == DetectStatus.DOk: self.detect_position = None config = configparser.ConfigParser() config_file = os.path.join(os.path.dirname(__file__), 'list.ini') # 配置文件地址 if not os.path.exists(config_file): print("配置文件 list.ini 不存在") return False config.read(config_file) if not config.has_section('positions'): print("配置文件中没有 [positions] 段") return False if not config.has_option('positions', str(self.position_index)): print(f"没有索引为 {self.position_index} 的点位") return False try: # 读取配置项 data = config.get('positions', str(self.position_index)).strip().split(',') if len(data) != 6: raise ValueError(f"点位数据格式错误(应为6个值): {data}") x, y, z, a, b, c = map(float, data) # 初始化坐标 self.detect_position = Real_Position() self.detect_position.init_position(x, y, z, a, b, c) return True except Exception as e: print(f"读取点位时出错: {e}") return False return False # ======================== # 测试逻辑 # ======================== if __name__ == "__main__": detect = Detect() print("🔧 开始测试 Detect.run() 是否能正确加载点位...\n") for index in range(12): # 测试索引 0~11 detect.position_index = index print(f"\n🔄 测试索引: {index}") success = detect.run() if success: print(f"✅ 加载成功: {detect.detect_position}") else: print(f"❌ 加载失败(可能是越界或配置错误)")