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. AIを使ったプログラミング(2)

  2. JavaScriptでオブジェクト指向をするには

  3. ロリポップでPHPエラーが出た時

  4. WooCommerceからB2Webへ(2)

  5. Asgaros Forum ユーザーズ ガイド

  6. Grbl-Panel日本語

  7. 製品を作るおじさんの独り言

  8. PHPバッチ