Skip to content

Commit adcbc1a

Browse files
committed
Version 0.4.0
CDC ACM driver is updated. Now it supports Nordic nRF devices. Tested with BBC micro:bit V1(nRF51).
1 parent b78f6ca commit adcbc1a

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
# usbserial4a
2-
[![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)
2+
[![PayPal Donate][paypal_img]][paypal_link]
3+
[![PyPI version][pypi_img]][pypi_link]
4+
[![Downloads][downloads_img]][downloads_link]
5+
6+
[paypal_img]: https://github.com/jacklinquan/images/blob/master/paypal_donate_badge.svg
7+
[paypal_link]: https://www.paypal.me/jacklinquan
8+
[pypi_img]: https://badge.fury.io/py/usbserial4a.svg
9+
[pypi_link]: https://badge.fury.io/py/usbserial4a
10+
[downloads_img]: https://pepy.tech/badge/usbserial4a
11+
[downloads_link]: https://pepy.tech/project/usbserial4a
312

413
Python package for Kivy Android USB serial port.
514

6-
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).
15+
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).
716

817
Implemented drivers are listed below:
918
* FTDI serial driver - done and tested with FT230X.
10-
* CDC ACM serial driver - done and tested with MCP2200.
19+
* CDC ACM serial driver - done and tested with MCP2200, BBC micro:bit V1(nRF51).
1120
* CP210x serial driver - done and tested with CP2102.
1221
* CH34x serial driver - done and tested with CH340.
1322
* PL2303 serial driver - done and tested with PL2303.
1423

15-
Please consider [![Paypal Donate](https://github.com/jacklinquan/images/blob/master/paypal_donate_button_200x80.png)](https://www.paypal.me/jacklinquan) to support me.
16-
1724
## How to use it:
1825
**To make quick prototype or to test and debug the script before building an App:**
1926

@@ -27,6 +34,8 @@ In Pydroid, go to `Menu->Pip` and install `usbserial4a`.
2734

2835
Or go to `Menu->Terminal` and enter `pip install usbserial4a`.
2936

37+
Save `example.py` in the storage of the Android device.
38+
3039
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.
3140

3241
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
3544

3645
**To build dedicated Apps with buildozer:**
3746

47+
(*These instructions are outdated and need update.*)
48+
3849
It works on Android 4.0+.
3950

4051
In `buildozer.spec` add `termios.so` to the whitelist.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="usbserial4a",
5-
version="0.3.0",
5+
version="0.4.0",
66
description="Python package for Kivy Android USB serial port.",
77
long_description="https://github.com/jacklinquan/usbserial4a",
88
long_description_content_type="text/markdown",

usbserial4a/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
"""
77

88
# Project version
9-
__version__ = "0.3.0"
9+
__version__ = "0.4.0"

usbserial4a/cdcacmserial4a.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class CdcAcmSerial(SerialBase):
4242
def __init__(self, *args, **kwargs):
4343
self._device = None
4444
self._connection = None
45-
self._interface = None
45+
self._control_index = None
46+
self._control_interface = None
47+
self._data_interface = None
4648
self._control_endpoint = None
4749
self._read_endpoint = None
4850
self._write_endpoint = None
@@ -91,6 +93,7 @@ def _open_single_interface(self):
9193
device = self._device
9294

9395
# Claiming control/data interface.
96+
self._control_index = 0
9497
self._control_interface = device.getInterface(0)
9598
self._data_interface = device.getInterface(0)
9699
if not self._connection.claimInterface(self._control_interface, True):
@@ -131,20 +134,41 @@ def _open_interface(self):
131134
"""Open default interface device."""
132135
device = self._device
133136

134-
# Claiming control interface.
135-
self._control_interface = device.getInterface(0)
137+
for i in range(device.getInterfaceCount()):
138+
interface = device.getInterface(i)
139+
if interface.getInterfaceClass() == usb.UsbConstants.USB_CLASS_COMM:
140+
self._control_index = i
141+
self._control_interface = interface
142+
if interface.getInterfaceClass() == usb.UsbConstants.USB_CLASS_CDC_DATA:
143+
self._data_interface = interface
144+
145+
if self._control_interface is None:
146+
raise SerialException("Could not find control interface.")
136147
if not self._connection.claimInterface(self._control_interface, True):
137148
raise SerialException("Could not claim control interface.")
138149

139150
self._control_endpoint = self._control_interface.getEndpoint(0)
140151

141-
# Claiming data interface.
142-
self._data_interface = device.getInterface(1)
152+
if self._data_interface is None:
153+
raise SerialException("Could not find data interface.")
143154
if not self._connection.claimInterface(self._data_interface, True):
144155
raise SerialException("Could not claim data interface.")
145156

146-
self._read_endpoint = self._data_interface.getEndpoint(1)
147-
self._write_endpoint = self._data_interface.getEndpoint(0)
157+
for i in range(self._data_interface.getEndpointCount()):
158+
ep = self._data_interface.getEndpoint(i)
159+
if (
160+
ep.getDirection() == usb.UsbConstants.USB_DIR_IN
161+
and ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK
162+
):
163+
self._read_endpoint = ep
164+
if (
165+
ep.getDirection() == usb.UsbConstants.USB_DIR_OUT
166+
and ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK
167+
):
168+
self._write_endpoint = ep
169+
170+
if None in (self._read_endpoint, self._write_endpoint):
171+
raise SerialException("Could not find read/write endpoint.")
148172

149173
def _reconfigure_port(self):
150174
"""Reconfigure serial port parameters."""
@@ -318,7 +342,7 @@ def _ctrl_transfer_out(self, request, value, buf=None):
318342
self.REQTYPE_HOST2DEVICE,
319343
request,
320344
value,
321-
0,
345+
self._control_index,
322346
buf,
323347
(0 if buf is None else len(buf)),
324348
self.USB_WRITE_TIMEOUT_MILLIS,
@@ -335,7 +359,7 @@ def _ctrl_transfer_in(self, request, value, buf):
335359
self.REQTYPE_DEVICE2HOST,
336360
request,
337361
value,
338-
0,
362+
self._control_index,
339363
buf,
340364
(0 if buf is None else len(buf)),
341365
self.USB_READ_TIMEOUT_MILLIS,

0 commit comments

Comments
 (0)