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. ポケットオシロスコープ DSO Touchの説明書を翻訳したよ

  2. htmltemplate for PHP

  3. Mac環境でPHP使ってウェブアプリの開発

  4. Windowsパソコンの買い替え/アップデート

  5. wxPython (PythonのGUI)

  6. Raspberry Piに本当の乱数発生器が追加されていた

  7. web APIサーバーで語られないこと

  8. 回線遅延シミュレーション

記事をプリント