From adcbc1abaedffe96c44e0f65755325258d424c74 Mon Sep 17 00:00:00 2001 From: Quan Lin Date: Thu, 13 Jan 2022 11:29:25 +1100 Subject: [PATCH] Version 0.4.0 CDC ACM driver is updated. Now it supports Nordic nRF devices. Tested with BBC micro:bit V1(nRF51). --- README.md | 21 +++++++++++++----- setup.py | 2 +- usbserial4a/__init__.py | 2 +- usbserial4a/cdcacmserial4a.py | 42 +++++++++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 131bbb6..e9e80f1 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,26 @@ # usbserial4a -[![PyPI version](https://badge.fury.io/py/usbserial4a.svg)](https://badge.fury.io/py/usbserial4a) [![Downloads](https://pepy.tech/badge/usbserial4a)](https://pepy.tech/project/usbserial4a) +[![PayPal Donate][paypal_img]][paypal_link] +[![PyPI version][pypi_img]][pypi_link] +[![Downloads][downloads_img]][downloads_link] + + [paypal_img]: https://github.com/jacklinquan/images/blob/master/paypal_donate_badge.svg + [paypal_link]: https://www.paypal.me/jacklinquan + [pypi_img]: https://badge.fury.io/py/usbserial4a.svg + [pypi_link]: https://badge.fury.io/py/usbserial4a + [downloads_img]: https://pepy.tech/badge/usbserial4a + [downloads_link]: https://pepy.tech/project/usbserial4a Python package for Kivy Android USB serial port. -Please try the Android App built with usbserial4a on Google Play: [PyTool USB Serial Free](https://play.google.com/store/apps/details?id=com.quanlin.pytoolusbserialfree), [PyTool Modbus Free](https://play.google.com/store/apps/details?id=com.quanlin.pytoolmodbusfree). +Please try the Android App built with usbserial4a on Google Play: [PyTool USB Serial F](https://play.google.com/store/apps/details?id=com.quanlin.pytoolusbserialfree), [PyTool Modbus Free](https://play.google.com/store/apps/details?id=com.quanlin.pytoolmodbusfree). Implemented drivers are listed below: * FTDI serial driver - done and tested with FT230X. -* CDC ACM serial driver - done and tested with MCP2200. +* CDC ACM serial driver - done and tested with MCP2200, BBC micro:bit V1(nRF51). * CP210x serial driver - done and tested with CP2102. * CH34x serial driver - done and tested with CH340. * PL2303 serial driver - done and tested with PL2303. -Please consider [![Paypal Donate](https://github.com/jacklinquan/images/blob/master/paypal_donate_button_200x80.png)](https://www.paypal.me/jacklinquan) to support me. - ## How to use it: **To make quick prototype or to test and debug the script before building an App:** @@ -27,6 +34,8 @@ In Pydroid, go to `Menu->Pip` and install `usbserial4a`. Or go to `Menu->Terminal` and enter `pip install usbserial4a`. +Save `example.py` in the storage of the Android device. + Open `example.py` and run it. When it runs for the first time, it might prompt you for permission to access the USB device. Accept the permission and run this script again, then it should send the data `b'Hello world!'` as expected. Go to `Menu->Graphical program output`. @@ -35,6 +44,8 @@ Scroll to the last line, it should list all the USB devices connected to the And **To build dedicated Apps with buildozer:** +(*These instructions are outdated and need update.*) + It works on Android 4.0+. In `buildozer.spec` add `termios.so` to the whitelist. diff --git a/setup.py b/setup.py index 07a60e0..bc08442 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name="usbserial4a", - version="0.3.0", + version="0.4.0", description="Python package for Kivy Android USB serial port.", long_description="https://github.com/jacklinquan/usbserial4a", long_description_content_type="text/markdown", diff --git a/usbserial4a/__init__.py b/usbserial4a/__init__.py index 6f3b743..2394d17 100644 --- a/usbserial4a/__init__.py +++ b/usbserial4a/__init__.py @@ -6,4 +6,4 @@ Requires: kivy, pyjnius, usb4a """ # Project version -__version__ = "0.3.0" +__version__ = "0.4.0" diff --git a/usbserial4a/cdcacmserial4a.py b/usbserial4a/cdcacmserial4a.py index dda1a09..27ea929 100644 --- a/usbserial4a/cdcacmserial4a.py +++ b/usbserial4a/cdcacmserial4a.py @@ -42,7 +42,9 @@ class CdcAcmSerial(SerialBase): def __init__(self, *args, **kwargs): self._device = None self._connection = None - self._interface = None + self._control_index = None + self._control_interface = None + self._data_interface = None self._control_endpoint = None self._read_endpoint = None self._write_endpoint = None @@ -91,6 +93,7 @@ class CdcAcmSerial(SerialBase): device = self._device # Claiming control/data interface. + self._control_index = 0 self._control_interface = device.getInterface(0) self._data_interface = device.getInterface(0) if not self._connection.claimInterface(self._control_interface, True): @@ -131,20 +134,41 @@ class CdcAcmSerial(SerialBase): """Open default interface device.""" device = self._device - # Claiming control interface. - self._control_interface = device.getInterface(0) + for i in range(device.getInterfaceCount()): + interface = device.getInterface(i) + if interface.getInterfaceClass() == usb.UsbConstants.USB_CLASS_COMM: + self._control_index = i + self._control_interface = interface + if interface.getInterfaceClass() == usb.UsbConstants.USB_CLASS_CDC_DATA: + self._data_interface = interface + + if self._control_interface is None: + raise SerialException("Could not find control interface.") if not self._connection.claimInterface(self._control_interface, True): raise SerialException("Could not claim control interface.") self._control_endpoint = self._control_interface.getEndpoint(0) - # Claiming data interface. - self._data_interface = device.getInterface(1) + if self._data_interface is None: + raise SerialException("Could not find data interface.") if not self._connection.claimInterface(self._data_interface, True): raise SerialException("Could not claim data interface.") - self._read_endpoint = self._data_interface.getEndpoint(1) - self._write_endpoint = self._data_interface.getEndpoint(0) + for i in range(self._data_interface.getEndpointCount()): + ep = self._data_interface.getEndpoint(i) + if ( + ep.getDirection() == usb.UsbConstants.USB_DIR_IN + and ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK + ): + self._read_endpoint = ep + if ( + ep.getDirection() == usb.UsbConstants.USB_DIR_OUT + and ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK + ): + self._write_endpoint = ep + + if None in (self._read_endpoint, self._write_endpoint): + raise SerialException("Could not find read/write endpoint.") def _reconfigure_port(self): """Reconfigure serial port parameters.""" @@ -318,7 +342,7 @@ class CdcAcmSerial(SerialBase): self.REQTYPE_HOST2DEVICE, request, value, - 0, + self._control_index, buf, (0 if buf is None else len(buf)), self.USB_WRITE_TIMEOUT_MILLIS, @@ -335,7 +359,7 @@ class CdcAcmSerial(SerialBase): self.REQTYPE_DEVICE2HOST, request, value, - 0, + self._control_index, buf, (0 if buf is None else len(buf)), self.USB_READ_TIMEOUT_MILLIS,