-
Notifications
You must be signed in to change notification settings - Fork 5
/
circuitpython_serial.py
87 lines (67 loc) · 2.73 KB
/
circuitpython_serial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# SPDX-FileCopyrightText: Copyright (c) 2021 Dan Halbert for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_board_toolkit`
================================================================================
CircuitPython board identification and information
* Author(s): Dan Halbert for Adafruit Industries
"""
# imports
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_Board_Toolkit.git"
from typing import Sequence
import os
import sys
import serial.tools.list_ports
from serial.tools.list_ports_common import ListPortInfo
# Some CircuitPython boards do not have interface names that start with "CircuitPython".
INTERFACE_PREFIXES = ("CircuitPython", "Sol", "StringCarM0Ex")
def comports() -> Sequence[ListPortInfo]:
"""Return all the comports recognized as being associated with a CircuitPython board."""
if sys.platform == "darwin":
# pyserial 3.5 and below have a bug on MacOS that returns an identical
# interface name for a composite USB device with multiple device names.
# For instance, a CircuitPython board with two CDC interfaces
# "CircuitPython CDC control" and "CircuitPython CDC2 control",
# presenting as two /dev/cu.* devices, will only show one of those interface names.
# See https://github.com/pyserial/pyserial/pull/566.
from . import _list_ports_osx # pylint: disable=import-outside-toplevel
ports = _list_ports_osx.comports()
elif os.name == "nt":
# pyserial.tools.list_ports_windows does not currently return the interface.
# This enhanced version does.
from . import _list_ports_windows # pylint: disable=import-outside-toplevel
ports = _list_ports_windows.comports()
else:
ports = serial.tools.list_ports.comports()
return tuple(
port
for port in ports
if port.interface
and port.interface.startswith(
tuple((prefix + " CDC" for prefix in INTERFACE_PREFIXES))
)
)
def repl_comports() -> Sequence[ListPortInfo]:
"""Return all comports presenting a CircuitPython REPL."""
# The trailing space in " CDC " is deliberate.
return tuple(
port
for port in comports()
if port.interface.startswith(
tuple(prefix + " CDC " for prefix in INTERFACE_PREFIXES)
)
)
def data_comports() -> Sequence[ListPortInfo]:
"""Return all comports presenting a CircuitPython serial connection
used for data transfer, not the REPL.
"""
# The trailing space in " CDC2 " is deliberate.
return tuple(
port
for port in comports()
if port.interface.startswith(
tuple(prefix + " CDC2 " for prefix in INTERFACE_PREFIXES)
)
)