temp.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from mcu.anheng_scale import AnhengScale
  2. import time
  3. if __name__ == "__main__":
  4. scale = AnhengScale(baudrate=9600)
  5. # 1.列出所有串口,找到你的COM口
  6. ports = scale.get_all_com_ports()
  7. print("可用串口列表:")
  8. for item in ports:
  9. print("item info ", item)
  10. print(f"device:{item['device']} desc:{item['desc']}")
  11. if not ports:
  12. print("没有找到可用串口")
  13. exit()
  14. # 2.填入你的串口,例如 COM3
  15. com_port = "COM3"
  16. ok = scale.connect(com_port)
  17. if not ok:
  18. print("串口打开失败,请检查接线与COM号")
  19. else:
  20. print(f"已连接 {com_port}")
  21. try:
  22. while True:
  23. data = scale.get_latest()
  24. print(
  25. f"稳定:{data['stable']} |模式:{data['mode']} |原始g:{data['raw_grams']:.2f} |去皮后g:{data['net_grams']:.2f}")
  26. # 示例:按条件软件去皮(需要秤稳定)
  27. # res = scale.software_tare()
  28. # if res:
  29. # print("软件去皮成功")
  30. # scale.hardware_tare() #硬件去皮,秤要设COMMOD=5
  31. # scale.hardware_zero() #硬件归零
  32. time.sleep(0.5)
  33. except KeyboardInterrupt:
  34. print("程序退出")
  35. finally:
  36. scale.disconnect()