ITで遊ぶ

汎用プログラムPythonとUSB(2)

LibUSBとの接続をするPyUSB
パッケージをダウンロードし、コマンドラインでそのフォルダーに移動します。
そこにはsetup.pyがあるはずです。確認してください。

python setup.py install

でインストールします。
USBと通信するためには、DeviceID, ProductIDが必要です。
次のプログラムを用意します。(名前を仮にfindusb.pyとします。)

#!/usr/bin/python
import sys
import usb.core
# find USB devices
dev = usb.core.find(find_all=True)
# loop through devices, printing vendor and product ids in decimal and hex
for cfg in dev:
  sys.stdout.write('Decimal VendorID=' + str(cfg.idVendor) + ' & ProductID=' + str(cfg.idProduct) + '\n')
  sys.stdout.write('Hexadecimal VendorID=' + hex(cfg.idVendor) + ' & ProductID=' + hex(cfg.idProduct) + '\n\n')
  1. USBデバイスを差し込んで、findusb.pyを走らせます。
  2. 次にデバイスを抜いて、またfindusb.pyを走らせます。
  3. なくなっているものが、対象のUSBデバイスのデータということになります。

 

>>> import usb.core
>>> dev = usb.core.find()

PyUSB記事へ続く。。。

関連記事

  1. CodeIgniter 別のViewの書き方

  2. Google Map APIでとりあえずマーカーを立てる

  3. ハッカソンって騒ぐだけ?

  4. Windowsスクリプト

  5. JqueryMobileにホレボレ

  6. CakePHP(1)

  7. カラーミー 出荷日のリスト

  8. micro:bit + BLE + ChromeブラウザーそしてPro…