ããã¯ã PyUSB 1.0ããã¥ã¢ã«ã䜿çšããããã°ã©ãã³ã°ã®ç¿»èš³ã§ã
ãã®ã¬ã€ãã¯PyUSBéçºè ã«ãã£ãŠæžãããŸããããã³ãããããã°ããå®è¡ããŠãç§ã¯walacãäž»ãªèè ã§ãããšä¿¡ããŠããŸãã
èªå·±çŽ¹ä»ãããŠ
PyUSB 1.0ã¯ã USBãžã®ç°¡åãªã¢ã¯ã»ã¹ãæäŸããPythonã©ã€ãã©ãªã§ãã PyUSBã¯ããŸããŸãªæ©èœãæäŸããŸãã
- Pythonã§æžããã100ïŒ
ïŒ
Cã§äœæããã0.xããŒãžã§ã³ãšã¯ç°ãªãã1.0ããŒãžã§ã³ã¯Pythonã§äœæãããŠããŸãã ããã«ãããCã®çµéšã®ãªãPythonããã°ã©ããŒã¯ãPyUSBã®åäœãããããç解ã§ããŸãã - ãã©ãããã©ãŒã ã®äžç«æ§ïŒ
ããŒãžã§ã³1.0ã«ã¯ãããã³ããšã³ãããã¯ãšã³ãã¹ããŒã ãå«ãŸããŠããŸãã APIãã·ã¹ãã åºæã®å®è£ ã®è©³çŽ°ããåé¢ããŸãã IBackendã€ã³ã¿ãŒãã§ã€ã¹ã¯ããããã®2ã€ã®ã¬ã€ã€ãŒãæ¥ç¶ããŸãã PyUSBã«ã¯ãlibusb 0.1ãlibusb 1.0ãããã³OpenUSBã®çµã¿èŸŒã¿ããã¯ãšã³ããä»å±ããŠããŸãã å¿ èŠã«å¿ããŠãèªåã§ããã¯ãšã³ããäœæã§ããŸãã - 移æ€æ§ïŒ
PyUSBã¯ãPython> = 2.4ã ctypes ãããã³ãµããŒããããŠããçµã¿èŸŒã¿ããã¯ãšã³ãã®å°ãªããšã1ã€ãåããä»»æã®ãã©ãããã©ãŒã ã§å®è¡ããå¿ èŠããããŸãã - ã·ã³ãã«ãïŒ
USBããã€ã¹ãšã®ããåãããããŸã§ã«ãªãç°¡åã«ãªããŸããïŒ USBã¯è€éãªãããã³ã«ã§ãããPyUSBã«ã¯æãäžè¬çãªæ§æã«é©ããããªã»ããããããŸãã - çææ§æ¯è»ã®ãµããŒãïŒ
PyUSBã¯ãåºã«ãªãããã¯ãšã³ããã¢ã€ãœã¯ããã¹è»¢éããµããŒãããŠããå Žåã«ãµããŒãããŸãã
PyUSBã¯USBããã°ã©ãã³ã°ã®èŠçã軜æžããŸããããã®ãã¥ãŒããªã¢ã«ã§ã¯ãUSBãããã³ã«ã«é¢ããæäœéã®ç¥èãããããšãåæãšããŠããŸãã USBã«ã€ããŠäœãç¥ããªãå Žåã¯ãJan Axelson ã®åªããUSB Completeããã¯ããå§ãããŸãã
ååã«è©±ããŠãã³ãŒããæžããŸãããïŒ
誰ã誰
å§ããããã«ãPyUSBã¢ãžã¥ãŒã«ã®èª¬æãããŸãããã ãã¹ãŠã®PyUSBã¢ãžã¥ãŒã«ã¯usbã®äžã«ããã次ã®ã¢ãžã¥ãŒã«ããããŸãã
ã¢ãžã¥ãŒã« | 説æ |
---|---|
ã³ã¢ | ã¡ã€ã³USBã¢ãžã¥ãŒã«ã |
util | è£å©æ©èœã |
å¶åŸ¡ | æšæºç®¡çãªã¯ãšã¹ãã |
ã¬ã¬ã·ãŒ | ããŒãžã§ã³0.xäºææ§ã¬ã€ã€ãŒã |
ããã¯ãšã³ã | çµã¿èŸŒã¿ã®ããã¯ãšã³ããå«ããµãããã±ãŒãžã |
ããšãã°ã ã³ã¢ã¢ãžã¥ãŒã«ãã€ã³ããŒãããã«ã¯ã次ã®ããã«å ¥åããŸãã
>>> import usb.core >>> dev = usb.core.find()
ãããå§ããŸããã
以äžã¯ãæåã«èŠã€ãã£ãããŒã¿ãœãŒã¹ïŒãšã³ããã€ã³ãOUTïŒã«æååãtestããéä¿¡ããç°¡åãªããã°ã©ã ã§ãã
import usb.core import usb.util # dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001) # ? if dev is None: raise ValueError('Device not found') # . , # dev.set_configuration() # cfg = dev.get_active_configuration() intf = cfg[(0,0)] ep = usb.util.find_descriptor( intf, # custom_match = \ lambda e: \ usb.util.endpoint_direction(e.bEndpointAddress) == \ usb.util.ENDPOINT_OUT) assert ep is not None # ep.write('test')
æåã®2è¡ã¯PyUSBããã±ãŒãžã¢ãžã¥ãŒã«ãã€ã³ããŒãããŸãã usb.coreã¯ã¡ã€ã³ã¢ãžã¥ãŒã«ã§ããã usb.utilã«ã¯ãã«ããŒé¢æ°ãå«ãŸããŠããŸãã 次ã®ã³ãã³ãã¯ãããã€ã¹ãæ€çŽ¢ããèŠã€ãã£ãå Žåã¯ãªããžã§ã¯ãã®ã€ã³ã¹ã¿ã³ã¹ãè¿ããŸãã ããã§ãªãå Žåã¯ã Noneãè¿ããŸãã 次ã«ã䜿çšããæ§æãèšå®ããŸãã 泚ïŒåŒæ°ããªãå Žåãç®çã®æ§æãããã©ã«ãã§èšå®ãããããšãæå³ããŸãã ã芧ã®ãšãããå€ãã®PyUSBæ©èœã«ã¯ãã»ãšãã©ã®äžè¬çãªããã€ã¹ã®ããã©ã«ãèšå®ããããŸãã ãã®å Žåãæåã«èŠã€ãã£ãæ§æãèšå®ãããŸãã
次ã«ãé¢å¿ã®ãããšã³ããã€ã³ããæ¢ããŸãã ç§ãã¡ãæã£ãŠããæåã®ã€ã³ã¿ãŒãã§ãŒã¹å ã§ãããæ¢ããŠããŸãã ãã®ãã€ã³ããèŠã€ããããããã«ããŒã¿ãéä¿¡ããŸãã
ãšã³ããã€ã³ãã¢ãã¬ã¹ãäºåã«ããã£ãŠããå Žåã¯ãããã€ã¹ãªããžã§ã¯ãã®æžã蟌ã¿é¢æ°ãåŒã³åºãã ãã§ãã
dev.write(1, 'test')
ããã§ã¯ãã¢ãã¬ã¹1ã®ãã¬ãŒã¯ãã€ã³ãã«æååãtestããæžã蟌ã¿ãŸãã ãããã®æ©èœã¯ãã¹ãŠã次ã®ã»ã¯ã·ã§ã³ã§è©³ãã説æããŸãã
äœãæªãã®ïŒ
PyUSBã®åé¢æ°ã¯ããšã©ãŒãçºçããå Žåã«äŸå€ãã¹ããŒããŸãã æšæºã®PythonäŸå€ã«å ããŠãPyUSBã¯USBé¢é£ãšã©ãŒã®usb.core.USBErrorãå®çŸ©ããŸãã
PyUSBãã°æ©èœã䜿çšããããšãã§ããŸãã ãã®ã³ã°ã¢ãžã¥ãŒã«ã䜿çšããŸãã ããã䜿çšããã«ã¯ã critical ã error ã warning ã infoãŸãã¯debugã®ããããã®ãã°ã¬ãã«ã§PYUSB_DEBUGç°å¢å€æ°ãå®çŸ©ããŸãã
ããã©ã«ãã§ã¯ãã¡ãã»ãŒãžã¯sys.stderrã«éä¿¡ãããŸã ã å¿ èŠã«å¿ããŠãç°å¢å€æ°PYUSB_LOG_FILENAMEãå®çŸ©ããããšã«ããããã°ã¡ãã»ãŒãžããã¡ã€ã«ã«ãªãã€ã¬ã¯ãã§ããŸãã ãã®å€ããã¡ã€ã«ãžã®æ£ãããã¹ã§ããå Žåãã¡ãã»ãŒãžãããã«æžã蟌ãŸããŸã ãããã§ãªãå Žåãã¡ãã»ãŒãžã¯sys.stderrã«éä¿¡ãããŸã ã
ã©ãã«ããŸãã
ã³ã¢ã¢ãžã¥ãŒã«ã®findïŒïŒé¢æ°ã¯ãã·ã¹ãã ã«æ¥ç¶ãããŠããããã€ã¹ã®æ€çŽ¢ãšçªå·ä»ãã«äœ¿çšãããŸãã ããšãã°ãããã€ã¹ã®ãã³ããŒIDã®å€ã0xfffeã§ã補åIDã0x0001ã§ãããšããŸãã ãã®ããã€ã¹ãèŠã€ããå¿ èŠãããå Žåããããè¡ããŸãã
import usb.core dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001) if dev is None: raise ValueError('Our device is not connected')
ããã ãã§ããé¢æ°ã¯ãããã€ã¹ãè¡šãusb.core.Deviceãªããžã§ã¯ããè¿ããŸãã ããã€ã¹ãèŠã€ãããªãå Žåã Noneãè¿ããŸãã å®éãDevice Descriptorã¯ã©ã¹ã®ä»»æã®ãã£ãŒã«ãã䜿çšã§ããŸãã ããšãã°ãã·ã¹ãã ã«USBããªã³ã¿ãŒãæ¥ç¶ãããŠãããã©ãããç¥ãããå Žåã¯ã©ãã§ããããïŒ ãšãŠãç°¡åã§ãïŒ
# , if usb.core.find(bDeviceClass=7) is None: raise ValueError('No printer found')
7ã¯ãUSBä»æ§ã«åŸã£ãããªã³ã¿ãŒã¯ã©ã¹ã®ã³ãŒãã§ãã ãããåŸ ã£ãŠãå©çšå¯èœãªãã¹ãŠã®ããªã³ã¿ãŒã«çªå·ãä»ãããå Žåã¯ã©ãããã°ããã§ããïŒ åé¡ãããŸããïŒ
# ... printers = usb.core.find(find_all=True, bDeviceClass=7) # Python 2, Python 3, import sys sys.stdout.write('There are ' + len(printers) + ' in the system\n.')
ã©ããã ããŠãå°ã説æããããã®æé... findã«ã¯find_allãšãããã©ã¡ãŒã¿ãŒããã ãããã©ã«ãã¯Falseã§ãã åœã®ãšã [1] ã findã¯æå®ãããåºæºã«äžèŽããæåã®ããã€ã¹ãè¿ããŸãïŒããã«ã€ããŠã¯ããã«èª¬æããŸãïŒã ãã©ã¡ãŒã¿ã«çã®å€ãæž¡ããšã findã¯ä»£ããã«æ¡ä»¶ã«äžèŽãããã¹ãŠã®ããã€ã¹ã®ãªã¹ããè¿ããŸãã 以äžã§ãïŒ ã·ã³ãã«ã§ããïŒ
çµãã£ãïŒ ããïŒ ãŸã ãã¹ãŠã説æããŠããŸãããå€ãã®ããã€ã¹ã¯ãå®éã«ã¯ã©ã¹æ å ±ãDevice Descriptorã§ã¯ãªãInterface Descriptorã«å ¥ããŠããŸãã ãããã£ãŠãã·ã¹ãã ã«æ¥ç¶ãããŠãããã¹ãŠã®ããªã³ã¿ãŒãæ¬åœã«èŠã€ããã«ã¯ããã¹ãŠã®æ§æãšãã¹ãŠã®ã€ã³ã¿ãŒãã§ã€ã¹ã調ã¹ãã€ã³ã¿ãŒãã§ã€ã¹ã®1ã€ãbInterfaceClass 7ã«èšå®ãããŠãããã©ããã確èªããå¿ èŠããããŸãããããå®è£ ã§ããç°¡åãªæ¹æ³ã¯ãããŸããã çãïŒã¯ããããã§ãã ãŸããæ¥ç¶ãããŠãããã¹ãŠã®ããªã³ã¿ãŒãèŠã€ããããã®æ¢è£œã®ã³ãŒããèŠãŠã¿ãŸãããã
import usb.core import usb.util import sys class find_class(object): def __init__(self, class_): self._class = class_ def __call__(self, device): # , if device.bDeviceClass == self._class: return True # , , # , for cfg in device: # find_descriptor: ? intf = usb.util.find_descriptor( cfg, bInterfaceClass=self._class ) if intf is not None: return True return False printers = usb.core.find(find_all=1, custom_match=find_class(7))
custom_matchãã©ã¡ãŒã¿ãŒã¯ãããã€ã¹ãªããžã§ã¯ããåãåããåŒã³åºããããªããžã§ã¯ããåãå ¥ããŸãã é©åãªããã€ã¹ã®å Žåã¯trueãäžé©åãªããã€ã¹ã®å Žåã¯falseãè¿ãå¿ èŠããããŸãã å¿ èŠã«å¿ããŠã custom_matchãšããã€ã¹ãã£ãŒã«ããçµã¿åãããããšãã§ããŸãã
# , : printers = usb.core.find(find_all=1, custom_match=find_class(7), idVendor=0xfffe)
ããã§ã¯ããµãã©ã€ã€0xfffeã®ããªã³ã¿ã«èå³ããããŸãã
èªåã説æããŠãã ãã
ããŠãããã€ã¹ãèŠã€ããŸããããæäœããåã«ãããã€ã¹ã«ã€ããŠè©³ããç¥ããããšæããŸãã æ§æãã€ã³ã¿ãŒãã§ã€ã¹ããšã³ããã€ã³ããããŒã¿ã¹ããªãŒã ã®çš®é¡...
ããã€ã¹ãããå Žåã¯ããªããžã§ã¯ãããããã£ãšããŠããã€ã¹èšè¿°åã®ä»»æã®ãã£ãŒã«ãã«ã¢ã¯ã»ã¹ã§ããŸãã
>>> dev.bLength >>> dev.bNumConfigurations >>> dev.bDeviceClass >>> # ...
ããã€ã¹ã§äœ¿çšå¯èœãªæ§æã«ã¢ã¯ã»ã¹ããã«ã¯ãããã€ã¹ãå埩ã§ããŸãã
for cfg in dev: sys.stdout.write(str(cfg.bConfigurationValue) + '\n')
åæ§ã«ãèšå®ãç¹°ãè¿ããŠã€ã³ã¿ãŒãã§ã€ã¹ã«ã¢ã¯ã»ã¹ããããã€ã³ã¿ãŒãã§ã€ã¹ãç¹°ãè¿ããŠã³ã³ãããŒã«ãã€ã³ãã«ã¢ã¯ã»ã¹ãããã§ããŸãã ãªããžã§ã¯ãã®åã¿ã€ãã«ã¯ãå±æ§ãšããŠå¯Ÿå¿ããèšè¿°åã®ãã£ãŒã«ãããããŸãã äŸãèŠãŠã¿ãŸãããïŒ
for cfg in dev: sys.stdout.write(str(cfg.bConfigurationValue) + '\n') for intf in cfg: sys.stdout.write('\t' + \ str(intf.bInterfaceNumber) + \ ',' + \ str(intf.bAlternateSetting) + \ '\n') for ep in intf: sys.stdout.write('\t\t' + \ str(ep.bEndpointAddress) + \ '\n')
次ã®ããã«ãèšè¿°åãžã®ã©ã³ãã ã¢ã¯ã»ã¹ã«ã€ã³ããã¯ã¹ã䜿çšããããšãã§ããŸãã
>>> # >>> cfg = dev[1] >>> # >>> intf = cfg[(0,0)] >>> # >>> ep = intf[2]
ã芧ã®ãšãããã€ã³ããã¯ã¹ã¯0ããã«ãŠã³ããããŸããããããåŸ ã£ãŠãã ããïŒ ã€ã³ã¿ãŒãã§ãŒã¹ã«ã¢ã¯ã»ã¹ããæ¹æ³ã«å¥åŠãªç¹ããããŸã...ã¯ããããã§ããConfigurationã®ã€ã³ããã¯ã¹ã¯äžé£ã®2ã€ã®å€ãåããŸããæåã®å€ã¯Interfaceã€ã³ããã¯ã¹ã§ã2çªç®ã¯ä»£æ¿èšå®ã§ãã äžè¬ã«ãæåã®ã€ã³ã¿ãŒãã§ã€ã¹ã«ã¢ã¯ã»ã¹ããããã«ã2çªç®ã®èšå®ã§ã¯ã cfg [ïŒ0,1ïŒ]ãèšè¿°ããŸãã
ããã§ãèšè¿°åãæ€çŽ¢ããããã®åŒ·åãªæ¹æ³-䟿å©ãªfind_descriptoré¢æ°ãåŠç¿ããŸãã ããªã³ã¿æ€çŽ¢ã®äŸã§ãã§ã«èŠãŸããã find_descriptorã¯ã findãšã»ãŒåãããã«æ©èœããŸãã ã2ã€ã®äŸå€ããããŸãã
- find_descriptorã¯ãæ€çŽ¢ãããœãŒã¹èšè¿°åãæåã®ãã©ã¡ãŒã¿ãŒãšããŠåãåããŸãã
- ããã¯ãšã³ããã©ã¡ãŒã¿ã¯ãããŸãã [2] ã
ããšãã°ã cfgæ§æèšè¿°åããããã€ã³ã¿ãŒãã§ã€ã¹1ã®ãã¹ãŠã®ä»£æ¿èšå®ãæ€çŽ¢ããå Žåã次ã®ããã«ããŸãã
import usb.util alt = usb.util.find_descriptor(cfg, find_all=True, bInterfaceNumber=1)
find_descriptorã¯usb.utilã¢ãžã¥ãŒã«ã«ããããšã«æ³šæããŠãã ãã ã ãŸãã åè¿°ã®custom_matchãã©ã¡ãŒã¿ãŒãåãå ¥ããŸãã
è€æ°ã®åäžã®ããã€ã¹ãæ±ã£ãŠããŸã
2ã€ã®åäžã®ããã€ã¹ãã³ã³ãã¥ãŒã¿ãŒã«æ¥ç¶ã§ããå ŽåããããŸãã ã©ãããã°ããããåºå¥ã§ããŸããïŒ ããã€ã¹ãªããžã§ã¯ãã«ã¯ãUSBä»æ§ã®äžéšã§ã¯ãªãããéåžžã«äŸ¿å©ãª2ã€ã®è¿œå å±æ§ããããŸãã ãã¹å±æ§ãšã¢ãã¬ã¹å±æ§ã§ãã ãŸã第äžã«ããããã®å±æ§ã¯ããã¯ãšã³ãããã®ãã®ã§ãããããã¯ãšã³ãã¯ãããããµããŒãããŠããªãå¯èœæ§ããããŸãããã®å Žåã¯Noneã«èšå®ãããŠããŸãã ãã ãããããã®å±æ§ã¯ããã€ã¹ãã¹ã®çªå·ãšã¢ãã¬ã¹ãè¡šãããæ³åã®ãšãã ãåãidVendorå±æ§å€ãšidProductå±æ§å€ãæã€2ã€ã®ããã€ã¹ãåºå¥ããããã«äœ¿çšã§ããŸãã
ã©ãããã°ããã§ããïŒ
æ¥ç¶åŸãããã€ãã®æšæºã¯ãšãªã䜿çšããŠUSBããã€ã¹ãæ§æããå¿ èŠããããŸãã USBä»æ§ã®æ€èšãå§ãããšããèšè¿°åãæ§æãã€ã³ã¿ãŒãã§ã€ã¹ã代æ¿èšå®ã転éã¿ã€ãããã®ä»ãã¹ãŠã«ãã£ããããŸãã...ãããŠææªãªã®ã¯ãããããç¡èŠããããšã¯ã§ããŸããïŒããã€ã¹ã¯ãæ§æãèšå®ãããŠããªããŠãæ©èœããªãããšã§ãïŒ PyUSBã¯ããªãã®äººçãã§ããã ãã·ã³ãã«ã«ããããšããŠããŸãã ããšãã°ãããã€ã¹ãªããžã§ã¯ããåãåã£ãåŸããŸãæäœããåã«ã set_configurationãªã¯ãšã¹ããéä¿¡ããå¿ èŠããããŸãã é¢å¿ã®ãããã®ã¯ãšãªã®æ§æãã©ã¡ãŒã¿ãŒã¯bConfigurationValueã§ãã ã»ãšãã©ã®ããã€ã¹ã«ã¯è€æ°ã®æ§æãããã䜿çšããæ§æå€ã远跡ããã®ã¯é¢åã§ãïŒãã ããç§ãèŠãã³ãŒãã®ã»ãšãã©ã¯ãããããŒãã³ãŒãã£ã³ã°ããã ãã§ãïŒã ãããã£ãŠãPyUSBã§ã¯ãåŒæ°ãªãã§set_configurationãªã¯ãšã¹ããéä¿¡ããã ãã§æžã¿ãŸãã ãã®å Žåã圌ã¯æåã«èŠã€ãã£ãæ§æãã€ã³ã¹ããŒã«ããŸãïŒããã€ã¹ã«1ã€ãããªãå Žåãæ§æå€ãå¿é ããå¿ èŠã¯ãŸã£ãããããŸããïŒã ããšãã°ã1ã€ã®æ§æèšè¿°åãæã€ããã€ã¹ãããããã®bConfigurationValueãã£ãŒã«ãã5ã§ãããšããŸã [3] ãåŸç¶ã®ã¯ãšãªã¯åãããã«æ©èœããŸãã
>>> dev.set_configuration(5) # >>> dev.set_configuration() # , 5 - # >>> cfg = util.find_descriptor(dev, bConfigurationValue=5) >>> cfg.set() # >>> cfg = util.find_descriptor(dev, bConfigurationValue=5) >>> dev.set_configuration(cfg)
ããïŒ Configurationãªããžã§ã¯ããset_configurationã®ãã©ã¡ãŒã¿ãŒãšããŠäœ¿çšã§ããŸãïŒ ã¯ãã圌ã¯çŸåšã®èšå®ã§èªåèªèº«ãèšå®ããããã®èšå®ã¡ãœãããæã£ãŠããŸãã
æ§æããå¿ èŠãããããŸãã¯æ§æããå¿ èŠããªãå¥ã®ãªãã·ã§ã³ã¯ãã€ã³ã¿ãŒãã§ãŒã¹ãå€æŽãããªãã·ã§ã³ã§ãã åããã€ã¹ã¯äžåºŠã«1ã€ã®ã¢ã¯ãã£ãåãããæ§æã®ã¿ãæã€ããšãã§ããåæ§æã¯è€æ°ã®ã€ã³ã¿ãŒãã§ãŒã¹ãæã€ããšãã§ãããã¹ãŠã®ã€ã³ã¿ãŒãã§ãŒã¹ãåæã«äœ¿çšã§ããŸãã ã€ã³ã¿ãŒãã§ã€ã¹ãè«çããã€ã¹ãšèãããšããã®æŠå¿µãããããç解ã§ããŸãã äŸãã°ãå€æ©èœããªã³ã¿ãŒãæ³åããŠã¿ãŸããããããã¯åæã«ããªã³ã¿ãŒãšã¹ãã£ããŒã®äž¡æ¹ã§ãã è€éã«ãªããªãããã«ïŒãŸãã¯ãå°ãªããšãã§ããã ãåçŽã«ããããã«ïŒãæ§æã1ã€ãããªããšä»®å®ããŸãã ãªããªã ããªã³ã¿ãŒãšã¹ãã£ããŒããããæ§æã«ã¯ããªã³ã¿ãŒçšãšã¹ãã£ããŒçšã®2ã€ã®ã€ã³ã¿ãŒãã§ã€ã¹ããããŸãã è€æ°ã®ã€ã³ã¿ãŒãã§ã€ã¹ãæã€ããã€ã¹ã¯ãè€åããã€ã¹ãšåŒã°ããŸãã å€æ©èœããªã³ã¿ãŒãã³ã³ãã¥ãŒã¿ãŒã«æ¥ç¶ãããšããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯2ã€ã®ç°ãªããã©ã€ããŒãããŒãããŸãã [4] ã
代æ¿ã€ã³ã¿ãŒãã§ãŒã¹èšå®ã¯ã©ãã§ããïŒ ããªããå°ããããšã¯è¯ãããšã§ãã ã€ã³ã¿ãŒãã§ã€ã¹ã«ã¯ã1ã€ä»¥äžã®ä»£æ¿èšå®ããããŸãã 代æ¿èšå®ã1ã€ã ãã®ã€ã³ã¿ãŒãã§ãŒã¹ã«ã¯ã代æ¿èšå®ããªããšèŠãªãããŸã [5] ã ããã€ã¹ã®æ§æãšããŠã®ã€ã³ã¿ãŒãã§ãŒã¹ã®ä»£æ¿èšå®ãã€ãŸããåã€ã³ã¿ãŒãã§ãŒã¹ã«å¯ŸããŠãã¢ã¯ãã£ããªä»£æ¿èšå®ã1ã€ã ãæã€ããšãã§ããŸãã ããšãã°ãUSBä»æ§ã§ã¯ãããã€ã¹ã®äž»èŠãªä»£æ¿æ§æã«ã¢ã€ãœã¯ããã¹ãã§ãã¯ãã€ã³ããèšå®ã§ããªãããšãææ¡ãããŠããŸã [6] ããããã£ãŠãã¹ããªãŒãã³ã°ããã€ã¹ã«ã¯å°ãªããšã2ã€ã®ä»£æ¿èšå®ãå¿ èŠã§ããã2çªç®ã®èšå®ã«ã¯ã¢ã€ãœã¯ããã¹ãã§ãã¯ãã€ã³ããå¿ èŠã§ãã ãã ããæ§æãšã¯ç°ãªãã代æ¿æ§æã1ã€ã ãã®ã€ã³ã¿ãŒãã§ãŒã¹ãæ§æããå¿ èŠã¯ãããŸããã [7] ã set_interface_altsettingé¢æ°ã䜿çšããŠã代æ¿ã€ã³ã¿ãŒãã§ãŒã¹èšå®ãéžæããŸãã
>>> dev.set_interface_altsetting(interface = 0, alternate_setting = 0)
èŠå
USBã®ä»æ§ã§ã¯ãè¿œå ã®ä»£æ¿èšå®ããªãã€ã³ã¿ãŒãã§ã€ã¹ã«å¯ŸããSET_INTERFACEèŠæ±ãåä¿¡ããå Žåãããã€ã¹ã¯ãšã©ãŒãè¿ãããšãèš±å¯ãããŠããŸãã ãã®ãããã€ã³ã¿ãŒãã§ã€ã¹ã«è€æ°ã®ä»£æ¿èšå®ãããããšããŸãã¯SET_INTERFACEèŠæ±ãåãå ¥ããããšã確å®ã§ãªãå Žåãæãå®å šãªæ¹æ³ã¯ã 次ã®ããã«try-exceptãããã¯å ã§set_interface_altsettingãåŒã³åºãããšã§ãã
try: dev.set_interface_altsetting(...) except USBError: pass
Interfaceãªããžã§ã¯ããé¢æ°ãã©ã¡ãŒã¿ãŒãšããŠäœ¿çšããããšãã§ããŸããinterfaceããã³alternate_settingãã©ã¡ãŒã¿ãŒã¯ã bInterfaceNumberããã³bAlternateSettingãã£ãŒã«ãããèªåçã«ç¶æ¿ãããŸã ã äŸïŒ
>>> intf = find_descriptor(...) >>> dev.set_interface_altsetting(intf) >>> intf.set_altsetting() # !
èŠå
ã€ã³ã¿ãŒãã§ã€ã¹ãªããžã§ã¯ãã¯ãã¢ã¯ãã£ããªæ§æèšè¿°åã«å±ããŠããå¿ èŠããããŸãã
ãããŒãç§ã«è©±ããŠ
ãããŠãUSBããã€ã¹ãšå¯Ÿè©±ããæ¹æ³ãç解ããæãæ¥ãŸããã USBã«ã¯ããã«ã¯è»¢éãå²ã蟌ã¿è»¢éãã¢ã€ãœã¯ããã¹è»¢éãã³ã³ãããŒã«è»¢éã®4çš®é¡ã®ããŒã¿ã¹ããªãŒã ããããŸãã åã¹ã¬ããã®ç®çãšãããã®éãã説æããã€ããã¯ãããŸããã ãããã£ãŠãå°ãªããšãUSBããŒã¿ã¹ããªãŒã ã®åºæ¬çãªç¥èãããããšãåæãšããŠããŸãã
å¶åŸ¡ããŒã¿ã¹ããªãŒã ã¯æ§é ãä»æ§ã§èª¬æãããŠããå¯äžã®ã¹ããªãŒã ã§ãããæ®ãã¯åçŽã«USBã®èŠ³ç¹ããçããŒã¿ãéåä¿¡ããŸãã ãããã£ãŠãå¶åŸ¡ãããŒãæäœããããã®ããŸããŸãªæ©èœããããæ®ãã®ãããŒã¯åãæ©èœã«ãã£ãŠåŠçãããŸãã
ctrl_transferã¡ãœããã䜿çšããŠãå¶åŸ¡ããŒã¿ã¹ããªãŒã ã«ã¢ã¯ã»ã¹ã§ããŸãã çºä¿¡ïŒOUTïŒã¹ããªãŒã ãšçä¿¡ïŒINïŒã¹ããªãŒã ã®äž¡æ¹ã«äœ¿çšãããŸãã ãããŒã®æ¹åã¯ã bmRequestTypeãã©ã¡ãŒã¿ãŒã«ãã£ãŠæ±ºå®ãããŸãã
ãã©ã¡ãŒã¿ãŒctrl_transferã¯ãå¶åŸ¡èŠæ±ã®æ§é ãšã»ãŒäžèŽããŠããŸãã 以äžã¯ãå¶åŸ¡ããŒã¿ã¹ããªãŒã ãæŽçããæ¹æ³ã®äŸã§ãã [8] ïŒ
>>> msg = 'test' >>> assert dev.ctrl_transfer(0x40, CTRL_LOOPBACK_WRITE, 0, 0, msg) == len(msg) >>> ret = dev.ctrl_transfer(0xC0, CTRL_LOOPBACK_READ, 0, 0, len(msg)) >>> sret = ''.join([chr(x) for x in ret]) >>> assert sret == msg
ãã®äŸã§ã¯ãããã€ã¹ã«ã«ãŒãããã¯ãã€ãã®ããã«åäœãã2ã€ã®ãŠãŒã¶ãŒå¶åŸ¡èŠæ±ãå«ãŸããŠããããšãåæãšããŠããŸãã ã¡ãã»ãŒãžCTRL_LOOPBACK_WRITEã§æžãããã®ã¯ãã¡ãã»ãŒãžCTRL_LOOPBACK_READã§èªãããšãã§ããŸãã
æåã®4ã€ã®ãã©ã¡ãŒã¿ãŒ-bmRequestType ã bmRequest ã wValueããã³wIndexã¯ãå¶åŸ¡ã¹ããªãŒã ã®æšæºæ§é ã®ãã£ãŒã«ãã§ãã 5çªç®ã®ãã©ã¡ãŒã¿ãŒã¯ãçºä¿¡ããŒã¿ã¹ããªãŒã çšã«è»¢éãããããŒã¿ããŸãã¯çä¿¡ã¹ããªãŒã ã§èªã¿åãããããŒã¿ã®æ°ã§ãã éä¿¡ãããããŒã¿ã¯ãé åã®__init__ã¡ãœããã®å ¥åã«ãã©ã¡ãŒã¿ãŒãšããŠäŸçµŠããããšãã§ããä»»æã®ã¿ã€ãã®ã·ãŒã±ã³ã¹ã«ããããšãã§ããŸãã 転éãããããŒã¿ããªãå Žåããã©ã¡ãŒã¿ãŒã¯None ïŒçä¿¡ããŒã¿ã¹ããªãŒã ã®å Žåã¯0ïŒã«èšå®ããå¿ èŠããããŸãã æäœã®ã¿ã€ã ã¢ãŠãã瀺ãå¥ã®ãªãã·ã§ã³ã®ãã©ã¡ãŒã¿ãŒããããŸãã æž¡ããªãå Žåãããã©ã«ãã®ã¿ã€ã ã¢ãŠãã䜿çšãããŸãïŒããã«ã€ããŠã¯åŸã§èª¬æããŸãïŒã çºä¿¡ããŒã¿ã¹ããªãŒã ã§ã¯ãæ»ãå€ã¯å®éã«ããã€ã¹ã«éä¿¡ããããã€ãæ°ã§ãã çä¿¡ã¹ããªãŒã ã§ã¯ãæ»ãå€ã¯ããŒã¿ãèªã¿åãããé åã§ãã
ä»ã®ã¹ããªãŒã ã®å Žåã writeã¡ãœãããšreadã¡ãœããããããã䜿çšã㊠ã ããŒã¿ã®æžã蟌ã¿ãšèªã¿åããè¡ãããšãã§ããŸãã ã¹ããªãŒã ã®ã¿ã€ããå¿é ããå¿ èŠã¯ãããŸããããã§ãã¯ãã€ã³ãã®ã¢ãã¬ã¹ã«ãã£ãŠèªåçã«æ€åºãããŸãã ãã¬ãŒã¯ãã€ã³ã1ã«ã«ãŒãããã¯ãã€ããããå Žåãã«ãŒãããã¯ã®äŸã次ã«ç€ºããŸãã
>>> msg = 'test' >>> assert len(dev.write(1, msg, 100)) == len(msg) >>> ret = dev.read(0x81, len(msg), 100) >>> sret = ''.join([chr(x) for x in ret]) >>> assert sret == msg
æåãš3çªç®ã®ãã©ã¡ãŒã¿ãŒã¯äž¡æ¹ã®ã¡ãœããã§åãã§ã-ããã¯ãããããã§ãã¯ãã€ã³ãã¢ãã¬ã¹ãšã¿ã€ã ã¢ãŠãã§ãã 2çªç®ã®ãã©ã¡ãŒã¿ãŒã¯ãéä¿¡ãããããŒã¿ïŒæžã蟌ã¿ïŒãŸãã¯èªã¿åããã€ãæ°ïŒèªã¿åãïŒã§ãã è¿ãããããŒã¿ã¯ã èªã¿åãã¡ãœããã®é åãªããžã§ã¯ãã®ã€ã³ã¹ã¿ã³ã¹ããŸãã¯æžã蟌ã¿ã¡ãœããã®æžã蟌ã¿ãã€ãæ°ã®ããããã§ãã
ããŒã¿2ããŒãžã§ã³ã§ã¯ããã€ãæ°ã®ä»£ããã«ã èªã¿åãçšã«è»¢éããããããŒã¿ãèªã¿åãããé åãªããžã§ã¯ããctrl_transferã§ããŸãã ãã®å Žåãèªã¿èŸŒããã€ãæ°ã¯ãé åã®é·ãã«array.itemsizeã®å€ãæãããã®ã«ãªããŸãã
ctrl_transferã§ã¯ã ã¿ã€ã ã¢ãŠããã©ã¡ãŒã¿ã¯ãªãã·ã§ã³ã§ãã ã¿ã€ã ã¢ãŠããçç¥ãããšã Device.default_timeoutããããã£ãæäœã¿ã€ã ã¢ãŠããšããŠäœ¿çšãããŸãã
èªåãå¶åŸ¡ãã
ããŒã¿ã¹ããªãŒã é¢æ°ã«å ããŠã usb.controlã¢ãžã¥ãŒã«ã¯æšæºã®USBå¶åŸ¡èŠæ±ãå«ãé¢æ°ãæäŸãã usb.utilã¢ãžã¥ãŒã«ã«ã¯ã©ã€ã³èšè¿°åãå ·äœçã«è¡šç€ºãã䟿å©ãªget_stringé¢æ°ããããŸãã
è¿œå ã®ãããã¯
ãã¹ãŠã®å倧ãªæœè±¡åã®èåŸã«ããçŽ æŽãããå®çŸ
以åã¯ã libusbã®ã¿ããããŸããã 次ã«libusb 1.0ãç»å Žããlibusb 0.1ãš1.0ãã§ããŸããã ãã®åŸã OpenUSBãäœæãã USBã©ã€ãã©ãªã®ããã«ã®å¡ã«äœãã§ããŸã [9] ã PyUSBã¯ãããã©ã®ããã«åŠçããŸããïŒ ããŠãPyUSBã¯æ°äž»çãªã©ã€ãã©ãªã§ãããå¿ èŠãªã©ã€ãã©ãªãéžæã§ããŸãã å®éãç¬èªã®USBã©ã€ãã©ãªããŒãããäœæããããã䜿çšããããPyUSBã«æ瀺ããããšãã§ããŸãã
findé¢æ°ã«ã¯å¥ã®ãã©ã¡ãŒã¿ãŒããããŸãããããã«ã€ããŠã¯èª¬æããŸããã§ããã ããã¯ããã¯ãšã³ããã©ã¡ãŒã¿ã§ãã 転éããªãå Žåãçµã¿èŸŒã¿ã®ããã¯ãšã³ãã®ããããã䜿çšãããŸãã ããã¯ãšã³ãã¯usb.backend.IBackendããç¶æ¿ããããªããžã§ã¯ãã§ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã åºæã®USBãžã£ã³ã¯ã®å°å ¥ãæ åœããŸãã ãæ³åã®ãšãããçµã¿èŸŒã¿ã®libusb 0.1ãlibusb 1.0ãããã³OpenUSBã¯ããã¯ãšã³ãã§ãã
ç¬èªã®ããã¯ãšã³ããäœæããŠäœ¿çšã§ããŸãã IBackendããç¶æ¿ããå¿ èŠãªã¡ãœãããæå¹ã«ããŸãã ãããã©ã®ããã«è¡ãããããç解ããã«ã¯ã usb.backendã®ããã¥ã¡ã³ããèŠãå¿ èŠããããããããŸããã
ãããŸãŸã«ãªããªãã§
Pythonã«ã¯ã èªåã¡ã¢ãªç®¡çãšåŒã°ãããã®ããããŸã ã ããã¯ãä»®æ³ãã·ã³ãã¡ã¢ãªãããªããžã§ã¯ãããã€ã¢ã³ããŒããããã決å®ããããšãæå³ããŸãã å éšã§ã¯ãPyUSBã¯äœæ¥ããå¿ èŠããããã¹ãŠã®äœã¬ãã«ã®ãªãœãŒã¹ïŒã€ã³ã¿ãŒãã§ã€ã¹ã®æ¿èªãããã€ã¹ã®èª¿æŽãªã©ïŒã管çããã»ãšãã©ã®ãŠãŒã¶ãŒã¯å¿é ããå¿ èŠã¯ãããŸããã ããããPythonã«ãããªããžã§ã¯ãã®èªåç Žæ£ã®æªå®çŸ©ã®æ§è³ªã«ããããŠãŒã¶ãŒã¯å²ãåœãŠããããªãœãŒã¹ããã€ãªãªãŒã¹ãããããäºæž¬ã§ããŸããã äžéšã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ããªãœãŒã¹ã決å®çã«å²ãåœãŠãŠè§£æŸããå¿ èŠããããŸãã ãã®ãããªã¢ããªã±ãŒã·ã§ã³ã®å Žåã usb.utilã¢ãžã¥ãŒã«ã¯ãªãœãŒã¹ç®¡çãšå¯Ÿè©±ããããã®æ©èœãæäŸããŸãã
ã€ã³ã¿ãŒãã§ã€ã¹ãæåã§èŠæ±ããã³ãªãªãŒã¹ããå Žåã¯ã claim_interfaceããã³release_interfaceé¢æ°ã䜿çšã§ããŸããããã€ã¹ããŸã èŠæ±ããŠããªãå Žåãclaim_interfaceé¢æ°ã¯æå®ãããã€ã³ã¿ãŒãã§ã€ã¹ãèŠæ±ããŸããããã€ã¹ããã§ã«ã€ã³ã¿ãŒãã§ã€ã¹ãèŠæ±ããŠããå ŽåãäœãããŸããããŸããrelease_interfaceã¯ãèŠæ±ãããå Žåãæå®ãããã€ã³ã¿ãŒãã§ã€ã¹ã解æŸããŸããã€ã³ã¿ãŒãã§ã€ã¹ãèŠæ±ãããªãå ŽåãäœãããŸãããæåã®ã€ã³ã¿ãŒãã§ã€ã¹ã¯ãšãªã䜿çšããŠãlibusbã® ããã¥ã¡ã³ãã«èšèŒãããŠããæ§æéžæã®åé¡ã解決ã§ããŸãã
ããã€ã¹ãªããžã§ã¯ãã«ãã£ãŠå²ãåœãŠããããã¹ãŠã®ãªãœãŒã¹ïŒèŠæ±ãããã€ã³ã¿ãŒãã§ã€ã¹ãå«ãïŒã解æŸããå Žåã¯ãdispose_resourcesé¢æ°ã䜿çšã§ããŸãããå²ãåœãŠããããã¹ãŠã®ãªãœãŒã¹ã解æŸããããã€ã¹ãªããžã§ã¯ããïŒããã€ã¹èªäœã®ããŒããŠã§ã¢ã§ã¯ãªãïŒãfindé¢æ°ã䜿çšããåŸã«è¿ãããç¶æ ã«è»¢éããŸãã
æåã©ã€ãã©ãªãŒå®çŸ©
äžè¬ã«ãããã¯ãšã³ãã¯ãUSBã«ã¢ã¯ã»ã¹ããããã®APIãå®è£ ããå ±æã©ã€ãã©ãªã®ã©ãããŒã§ããããã©ã«ãã§ã¯ãããã¯ãšã³ãã¯ctypesé¢æ°find_libraryïŒïŒã䜿çšããŸãã Linuxããã³ãã®ä»ã®Unixã©ã€ã¯ãªãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ã¯ãfind_libraryã¯ã©ã€ãã©ãªãã¡ã€ã«ãèŠã€ããããã«å€éšããã°ã©ã ïŒ/ sbin / ldconfigãgccããã³objdumpãªã©ïŒãå®è¡ããããšããŸãã
ãããã®ããã°ã©ã ãååšããªãã·ã¹ãã ããã³/ãŸãã¯ã©ã€ãã©ãªãã£ãã·ã¥ãç¡å¹ã«ãªã£ãŠããã·ã¹ãã ã§ã¯ããã®æ©èœã¯äœ¿çšã§ããŸãããå¶éãå æããããã«ãPyUSBã§ã¯ãfind_libraryïŒïŒã«ã¹ã¿ã é¢æ°ãããã¯ãšã³ãã«éä¿¡ã§ããŸãã
ãã®ãããªã·ããªãªã®äŸã¯æ¬¡ã®ãšããã§ãã
>>> import usb.core >>> import usb.backend.libusb1 >>> >>> backend = usb.backend.libusb1.get_backend(find_library=lambda x: "/usr/lib/libusb-1.0.so") >>> dev = usb.core.find(..., backend=backend)
find_libraryã¯get_backendïŒïŒé¢æ°ã®åŒæ°ã§ããããšã«æ³šæããŠãã ããããã®é¢æ°ã§ã¯ãããã¯ãšã³ãã«é©ããã©ã€ãã©ãªãèŠã€ããããã®é¢æ°ãæäŸããŸãã
å€ãåŠæ ¡ã®ã«ãŒã«
å€ãPyUSB APIïŒ0.äœããããïŒã䜿çšããŠã¢ããªã±ãŒã·ã§ã³ãäœæããŠããå Žåãæ°ããAPIã䜿çšããããã«ã³ãŒããæŽæ°ããå¿ èŠããããã©ãããèªåããããšãã§ããŸãããŸããããªãã¯ãããè¡ãã¹ãã§ãããããã¯å¿ èŠã§ã¯ãããŸãããPyUSB 1.0ã«ã¯usb.legacyäºææ§ã¢ãžã¥ãŒã«ãä»å±ããŠããŸããæ°ããAPIã«åºã¥ããå€ãAPIãå«ãŸããŠããŸããããŸããã¢ããªã±ãŒã·ã§ã³usbãæ©èœãããããã«ãusbãšããŠimport usbã®è¡ãimport usb.legacyã«çœ®ãæããã ãã§ããïŒãçãã¯ã€ãšã¹ã§ããæ©èœããŸãããå¿ ãããå¿ èŠã§ã¯ãããŸãããã¢ããªã±ãŒã·ã§ã³ãå€æŽããã«å®è¡ããå Žåãimport usbè¡ã¯usb.legacyãããã¹ãŠã®ãããªãã¯ãã£ã©ã¯ã¿ãŒãã€ã³ããŒããããããåäœããŸãããåé¡ãçºçããå Žå-ãã°ãçºèŠããå¯èœæ§ãé«ãã§ãã
å©ããŠãã ãã
ããªããå©ããå¿ èŠãšãããªãã°ãç§ã«é»åã¡ãŒã«ãæžããªãã§ãã ãããããã®ããã®ã¡ãŒãªã³ã°ãªã¹ãããããŸãããµãã¹ã¯ãªãã·ã§ã³ã®æé ã¯ãPyUSB Webãµã€ãã§èŠã€ããããšãã§ããŸãã
[1] TrueãŸãã¯FalseïŒå€§æåïŒãèšè¿°ãããšããPythonèšèªã®å¯Ÿå¿ããå€ãæå³ããŸãããããŠãtrueïŒtrueïŒãŸãã¯falseïŒfalseïŒãšèšããšããtrueãŸãã¯falseãšèŠãªãããPythonåŒãæå³ããŸããïŒãã®é¡äŒŒæ§ã¯ãªãªãžãã«ã§çºçãã翻蚳ã«ãããçãšåœã®æŠå¿µãç解ããã®ã«åœ¹ç«ã¡ãŸãã- 泚ãïŒïŒ
[2]ç¹å®ã®ããã¯ãšã³ãã®ããã¥ã¡ã³ããåç §ããŠãã ããã
[3] USBä»æ§ã¯ãæ§æå€ã«ç¹å®ã®å€ã匷å¶ããŸãããã€ã³ã¿ãŒãã§ã€ã¹çªå·ãšä»£æ¿èšå®ã«ã€ããŠãåæ§ã§ãã
[4]å®éã«ã¯ããã¹ãŠãããå°ãè€éã§ãããããã¯ç§ãã¡ã®åãªã説æã§ãã
[5]ããã¯å¥åŠã«èãããŸãã
[6]ããã¯ãããã€ã¹ã®æ§æäžã«ã¢ã€ãœã¯ããã¹ããŒã¿ã¹ããªãŒã ã®åž¯åå¹ ããªãå Žåãæ£åžžã«çªå·ä»ãã§ããããã§ãã
[7]ããã¯ãããã€ã¹ãæªæ§æç¶æ ã«ãªãããšãèš±å¯ãããŠãããããæ§æã§ã¯çºçããŸããã
[8] PyUSBã§ã¯ãå¶åŸ¡ããŒã¿ã¹ããªãŒã ãå¶åŸ¡ãã€ã³ã0ã«ã¢ã¯ã»ã¹ããŸãããããŸãã«ãããã€ã¹ã«ä»£æ¿å¶åŸ¡ã³ã³ãããŒã«ãã€ã³ãããããŸãïŒãã®ãããªããã€ã¹ã«åºäŒã£ãããšã¯ãããŸããïŒã
[9]ããã¯åè«ã§ããçå£ã«åãæ¢ããªãã§ãã ãããçŽ æŽãããéžæè¢ã¯éžæè¢ãªããããåªããŠããŸãã