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. Windowsパソコンの買い替え/アップデート

  2. JqueryMobileにホレボレ

  3. やっぱりドットインストールは勉強にいい

  4. Linux Kernel Map

  5. 多くのテキストファイルをWordPressの投稿に書き込む

  6. wxPython (PythonのGUI)

  7. 静的HTMLに他HTMLを読み込む方法

  8. 思想をあらわすプログラミング

記事をプリント