2009-06-08
PyUSBで、機器名称の取得
ちょこっとした理由でPythonからPCに接続されているUSBの機器名を取得したくなった。
ググっても、なかなか、出てこなかったので、備忘録がわりに。
#!/usr/local/bin/python
import usb
class UsbDevice(object):
def __init__(self, vendor_id=None, product_id=None):
busses = usb.busses()
for bus in busses:
devices = bus.devices
for device in devices:
if (device.idVendor, device.idProduct) == (vendor_id, product_id):
self.device = device
self.configuration = self.device.configurations[0]
self.interface = self.configuration.interfaces[0][0]
self.endpoints = []
self.pipes = []
for endpoint in self.interface.endpoints:
self.endpoints.append(endpoint)
self.pipes.append(endpoint.address)
return
raise RuntimeError, 'Device not found'
def open(self):
if hasattr(self, 'handle'):
raise RuntimeError, 'Device already opened'
self.handle = self.device.open()
product = self.handle.getString(self.device.iProduct, 20)
manufacturer = self.handle.getString(self.device.iManufacturer, 20)
print product, manufacturer
def close(self):
if hasattr(self, 'handle'):
self.handle.releaseInterface()
del self.handle
else:
raise RuntimeError, 'Device not opened'
def main():
dev = UsbDevice(0x04fe, 0x0008)
dev.open()
# dev.close()
if __name__ == '__main__':
main()
getStringの第2引数に文字数を指定してるけど、これは、libusbがCで書かれているなごりかな。ソースを読んでいないのでわかりませんが。
プラネックス
購入: 73人 クリック: 264回
購入: 73人 クリック: 264回
トラックバック - http://d.hatena.ne.jp/kawa1128/20090608/1244439834
リンク元
- 15 http://www.google.co.jp/search?q=PyUSB&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:official&client=firefox-a
- 9 http://www.google.com/search?client=ubuntu&channel=fs&q=pyusb&ie=utf-8&oe=utf-8
- 8 http://www.google.co.jp/search?client=safari&rls=en&q=PyUSB&ie=UTF-8&oe=UTF-8&redir_esc=&ei=s84ZTIH2DsO5cc6isJMK
- 8 http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=pyusb
- 7 http://search.yahoo.co.jp/search?p=pyusb&search.x=1&fr=top_ga1_sa&tid=top_ga1_sa&ei=UTF-8&aq=0&oq=py
- 7 http://www.google.co.jp/search?aq=f&sourceid=chrome&ie=UTF-8&q=pyUSB
- 7 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&hs=jsu&q=Could+not+find+android+apk&btnG=検索&lr=lang_ja
- 7 http://www.google.co.jp/search?q=pyUSB&lr=lang_ja&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja:official&client=firefox-a
- 7 http://www.google.co.jp/url?sa=t&rct=j&q=pyusb&source=web&cd=6&ved=0CEwQFjAF&url=http://d.hatena.ne.jp/kawa1128/20090608/1244439834&ei=1W0QT-jbPKeSiAeTv4UL&usg=AFQjCNFpBGv2cPqEzXRoGUF_u-fQFPZL7g&cad=rja
- 6 http://reader.livedoor.com/reader/


