| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- from mcu.anheng_scale import AnhengScale
- import time
- if __name__ == "__main__":
- scale = AnhengScale(baudrate=9600)
- # 1.列出所有串口,找到你的COM口
- ports = scale.get_all_com_ports()
- print("可用串口列表:")
- for item in ports:
- print("item info ", item)
- print(f"device:{item['device']} desc:{item['desc']}")
- if not ports:
- print("没有找到可用串口")
- exit()
- # 2.填入你的串口,例如 COM3
- com_port = "COM3"
- ok = scale.connect(com_port)
- if not ok:
- print("串口打开失败,请检查接线与COM号")
- else:
- print(f"已连接 {com_port}")
- try:
- while True:
- data = scale.get_latest()
- print(
- f"稳定:{data['stable']} |模式:{data['mode']} |原始g:{data['raw_grams']:.2f} |去皮后g:{data['net_grams']:.2f}")
- # 示例:按条件软件去皮(需要秤稳定)
- # res = scale.software_tare()
- # if res:
- # print("软件去皮成功")
- # scale.hardware_tare() #硬件去皮,秤要设COMMOD=5
- # scale.hardware_zero() #硬件归零
- time.sleep(0.5)
- except KeyboardInterrupt:
- print("程序退出")
- finally:
- scale.disconnect()
|