通过USB和BLE与GoDirect设备接口的库
godirect的Python项目详细描述
直接
一个python模块,用于使用usb或ble从Vernier Go Direct® Sensors读取数据。
看看godirect-examples repository中的想法和一些有用的例子。
要求
godirect
需要以下python模块。它们将被安装
通过pip安装godirect
时自动作为依赖项。
- pexpect
以下python模块推荐用于godirect
。只有在通过pip安装godirect
时指定为extras
时,才会安装它们。见下文。
- VernierPygatt(Pygatt项目的分支,在Windows上修复了BGAPI)
- HIDAPI(USB HID设备支持)
安装
自动安装USB和BLE的extras
支持依赖项。
pip install godirect[usb,ble]
示例使用
连接到默认阈值内的第一个可用USB设备或最近的BLE设备,并从默认传感器采集10个样本。
fromgodirectimportGoDirectgodirect=GoDirect()device=godirect.get_device()ifdevice!=Noneanddevice.open(auto_start=True):sensors=device.get_enabled_sensors()print("Connected to "+device.name)print("Reading 10 measurements")foriinrange(0,10):ifdevice.read():forsensorinsensors:print(sensor.sensor_description+": "+str(sensor.value))device.stop()device.close()godirect.quit()
请注意,您可以选择启用USB、BLE或两者都启用。默认情况下,两者都将启用。
godirect=GoDirect(use_ble=True,use_usb=True)
下面是如何从ble和/或usb后端获取godirectdevice对象的列表。
# returns a list of GoDirectDevice objectsdevices=godirect.list_devices()
或者你可以让图书馆自动为你找到最近的设备。
# returns a GoDirectDevice on success or None on failuremydevice=godirect.get_device()# to adjust the BLE threshold pass in a minimum dB valuemydevice=godirect.get_device(threshold=-200)
找到或选择设备后,必须将其打开。默认情况下,只有信息 集合在一起。自动启用默认传感器并开始测量发送 auto_start=true并跳到获取已启用传感器的列表。
# returns True on success or False on failuremydevice.open()
打开设备后,您可以在 装置。
# returns a list of Sensor objectssensors=mydevice.list_sensors()
也可以选择要从中收集的传感器,否则 将使用默认传感器。
# pass a list of sensor numbers to enablemydevice.enable_sensors([2,3,4])
# start measurements at the typical rate for the enabled sensorsmydevice.start()# returns True on success# start measurements at 1000ms per samplemydevice.start(period=1000)# get a list of the GoDirectSensor objects that are enabledmysensors=mydevice.get_enabled_sensors()
read()
方法将阻塞,直到返回数据,以便可以读取
一个紧密的循环。
foriinrange(0,10):# read() will append the measurement received to the values list in the Sensor objectifmydevice.read():forsensorinmysensors:print(sensor.value)
stop()
方法将停止设备上的数据收集。close()
方法
将断开USB或BLE设备的连接。quit()
方法将优雅地停止USB或BLE后端。
调试
godirect使用标准的python日志模块。您可以设置日志记录的详细程度 以获取信息或调试以查看更多通信详细信息。
importlogginglogging.basicConfig()logging.getLogger('godirect').setLevel(logging.DEBUG)logging.getLogger('pygatt').setLevel(logging.DEBUG)
窗口
pygatt模块使用bgapi后端与ble加密狗通信。 如果自动检测失败,您可能必须指定分配给ble加密狗的com端口。
python
godirect = GoDirect(ble_com_port='COM9')
Linux
为了在linux系统上通过usb与go-direct设备通信,您需要提供udev规则来授予设备适当的权限。您可以使用以下命令在正确的目录中创建这样的规则:
sudo su -c 'cat <<EOT >/etc/udev/rules.d/vstlibusb.rules
SUBSYSTEM=="usb", ATTRS{idVendor}=="08f7", MODE="0666"
SUBSYSTEM=="usb_device", ATTRS{idVendor}=="08f7", MODE="0666"
EOT'
许可证
GNU通用公共许可v3(GPLV3)
游标产品是为教育用途而设计的。我们的产品不是设计的,也不推荐用于任何工业、医疗或商业过程,如生命支持、患者诊断、制造过程控制或任何类型的工业测试。