Add support for pySerial 3.0 // Resolve #307

This commit is contained in:
Ivan Kravets
2015-11-06 19:54:15 +02:00
parent 1790c6bd0f
commit 91345c0bdd
5 changed files with 186 additions and 74 deletions

View File

@ -12,6 +12,8 @@ PlatformIO 2.0
(`issue #309 <https://github.com/platformio/platformio/issues/309>`_) (`issue #309 <https://github.com/platformio/platformio/issues/309>`_)
* Added support for Espressif ESP8266 ESP-12E board (NodeMCU) * Added support for Espressif ESP8266 ESP-12E board (NodeMCU)
(`issue #310 <https://github.com/platformio/platformio/issues/310>`_) (`issue #310 <https://github.com/platformio/platformio/issues/310>`_)
* Added support for pySerial 3.0
(`issue #307 <https://github.com/platformio/platformio/issues/307>`_)
* Upload firmware using external programmer via `platformio run --target program <http://docs.platformio.org/en/latest/userguide/cmd_run.html#cmdoption-platformio-run-t>`__ * Upload firmware using external programmer via `platformio run --target program <http://docs.platformio.org/en/latest/userguide/cmd_run.html#cmdoption-platformio-run-t>`__
target target
(`issue #311 <https://github.com/platformio/platformio/issues/311>`_) (`issue #311 <https://github.com/platformio/platformio/issues/311>`_)

View File

@ -140,30 +140,41 @@ Set initial ``RTS`` line state, default ``0``
Set initial ``DTR`` line state, default ``0`` Set initial ``DTR`` line state, default ``0``
.. option:: .. option::
--echo --encoding
Enable local echo, default ``Off`` Set the encoding for the serial port (e.g. ``hexlify``, ``Latin1``, ``UTF-8``),
default ``UTF-8``.
**NEW**: Available in Miniterm/PySerial 3.0
.. option:: .. option::
--cr -f, --filter
Do not send ``CR+LF``, send ``R`` only, default ``Off`` Add text transformation. Available filters:
* ``colorize`` Apply different colors for received and echo
* ``debug`` Print what is sent and received
* ``default`` Remove typical terminal control codes from input
* ``direct`` Do-nothing: forward all data unchanged
* ``nocontrol`` Remove all control codes, incl. CR+LF
* ``printable`` Show decimal code for all non-ASCII characters and replace
most control codes
**NEW**: Available in Miniterm/PySerial 3.0
.. option:: .. option::
--lf --eol
Do not send ``CR+LF``, send ``LF`` only, default ``Off`` End of line mode (``CR``, ``LF`` or ``CRLF``), default ``CRLF``
**NEW**: Available in Miniterm/PySerial 3.0
.. option:: .. option::
-d, --debug --raw
Debug received data (escape non-printable chars). ``--debug`` can be given Do not apply any encodings/transformations
multiple times:
0. just print what is received **NEW**: Available in Miniterm/PySerial 3.0
1. escape non-printable characters, do newlines as unusual
2. escape non-printable characters, newlines too
3. hex dump everything
.. option:: .. option::
--exit-char --exit-char
@ -182,6 +193,43 @@ default ``0x14``
Diagnostics: suppress non-error messages, default ``Off`` Diagnostics: suppress non-error messages, default ``Off``
.. option::
--echo
Enable local echo, default ``Off``
**REMOVED**: Is not available in Miniterm/PySerial 3.0
.. option::
--cr
Do not send ``CR+LF``, send ``R`` only, default ``Off``
**REMOVED**: Is not available in Miniterm/PySerial 3.0
.. option::
--lf
Do not send ``CR+LF``, send ``LF`` only, default ``Off``
**REMOVED**: Is not available in Miniterm/PySerial 3.0
.. option::
-d, --debug
Debug received data (escape non-printable chars). ``--debug`` can be given
multiple times:
0. just print what is received
1. escape non-printable characters, do newlines as unusual
2. escape non-printable characters, newlines too
3. hex dump everything
**REMOVED**: Is not available in Miniterm/PySerial 3.0.
See :option:`platformio serialports monitor --encoding` and
:option:`platformio serialports monitor --filter` options.
Examples Examples
~~~~~~~~ ~~~~~~~~
@ -201,21 +249,17 @@ Examples
--rts [0|1] Set initial RTS line state, default=0 --rts [0|1] Set initial RTS line state, default=0
--dtr [0|1] Set initial DTR line state, default=0 --dtr [0|1] Set initial DTR line state, default=0
--echo Enable local echo, default=Off --echo Enable local echo, default=Off
--cr Do not send CR+LF, send CR only, default=Off --encoding TEXT Set the encoding for the serial port (e.g. hexlify,
--lf Do not send CR+LF, send LF only, default=Off Latin1, UTF-8), default: UTF-8
-d, --debug Debug received data (escape non-printable chars) -f, --filter TEXT Add text transformation
--debug can be given multiple times: --eol [CR|LF|CRLF] End of line mode, default=CRLF
0: just print what is received --raw Do not apply any encodings/transformations
1: escape non-printable characters, do newlines as
unusual
2: escape non-printable characters, newlines too
3: hex dump everything
--exit-char INTEGER ASCII code of special character that is used to exit --exit-char INTEGER ASCII code of special character that is used to exit
the application, default=0x1d the application, default=29 (DEC)
--menu-char INTEGER ASCII code of special character that is used to --menu-char INTEGER ASCII code of special character that is used to
control miniterm (menu), default=0x14 control miniterm (menu), default=20 (DEC)
--quiet Diagnostics: suppress non-error messages, default=Off --quiet Diagnostics: suppress non-error messages, default=Off
--help Show this message and exit. -h, --help Show this message and exit.
2. Communicate with serial device and print help inside terminal 2. Communicate with serial device and print help inside terminal

View File

@ -1,7 +1,7 @@
# Copyright (C) Ivan Kravets <me@ikravets.com> # Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details. # See LICENSE for details.
VERSION = (2, 4, "0.dev1") VERSION = (2, 4, "0.dev2")
__version__ = ".".join([str(s) for s in VERSION]) __version__ = ".".join([str(s) for s in VERSION])
__title__ = "platformio" __title__ = "platformio"

View File

@ -5,6 +5,7 @@ import json
import sys import sys
import click import click
from serial import VERSION as PYSERIAL_VERSION
from serial.tools import miniterm from serial.tools import miniterm
from platformio import app from platformio import app
@ -33,52 +34,117 @@ def serialports_list(json_output):
click.echo("") click.echo("")
@cli.command("monitor", short_help="Monitor Serial port") if int(PYSERIAL_VERSION[0]) == 3:
@click.option("--port", "-p", help="Port, a number or a device name") @cli.command("monitor", short_help="Monitor Serial port")
@click.option("--baud", "-b", type=int, default=9600, @click.option("--port", "-p", help="Port, a number or a device name")
help="Set baud rate, default=9600") @click.option("--baud", "-b", type=int, default=9600,
@click.option("--parity", default="N", help="Set baud rate, default=9600")
type=click.Choice(["N", "E", "O", "S", "M"]), @click.option("--parity", default="N",
help="Set parity, default=N") type=click.Choice(["N", "E", "O", "S", "M"]),
@click.option("--rtscts", is_flag=True, help="Set parity, default=N")
help="Enable RTS/CTS flow control, default=Off") @click.option("--rtscts", is_flag=True,
@click.option("--xonxoff", is_flag=True, help="Enable RTS/CTS flow control, default=Off")
help="Enable software flow control, default=Off") @click.option("--xonxoff", is_flag=True,
@click.option("--rts", default="0", type=click.Choice(["0", "1"]), help="Enable software flow control, default=Off")
help="Set initial RTS line state, default=0") @click.option("--rts", default="0", type=click.Choice(["0", "1"]),
@click.option("--dtr", default="0", type=click.Choice(["0", "1"]), help="Set initial RTS line state, default=0")
help="Set initial DTR line state, default=0") @click.option("--dtr", default="0", type=click.Choice(["0", "1"]),
@click.option("--echo", is_flag=True, help="Set initial DTR line state, default=0")
help="Enable local echo, default=Off") @click.option("--encoding", default="UTF-8",
@click.option("--cr", is_flag=True, help="Set the encoding for the serial port (e.g. hexlify, "
help="Do not send CR+LF, send CR only, default=Off") "Latin1, UTF-8), default: UTF-8")
@click.option("--lf", is_flag=True, @click.option("--filter", "-f", multiple=True,
help="Do not send CR+LF, send LF only, default=Off") help="Add text transformation")
@click.option("--debug", "-d", count=True, @click.option("--eol", default="CRLF",
help="""Debug received data (escape non-printable chars) type=click.Choice(["CR", "LF", "CRLF"]),
# --debug can be given multiple times: help="End of line mode, default=CRLF")
# 0: just print what is received @click.option("--raw", is_flag=True,
# 1: escape non-printable characters, do newlines as unusual help="Do not apply any encodings/transformations")
# 2: escape non-printable characters, newlines too @click.option("--exit-char", type=int, default=29,
# 3: hex dump everything""") help="ASCII code of special character that is used to exit "
@click.option("--exit-char", type=int, default=29, "the application, default=29 (DEC)")
help="ASCII code of special character that is used to exit the " @click.option("--menu-char", type=int, default=20,
"application, default=19 (DEC)") help="ASCII code of special character that is used to "
@click.option("--menu-char", type=int, default=20, "control miniterm (menu), default=20 (DEC)")
help="ASCII code of special character that is used to control " @click.option("--quiet", is_flag=True,
"miniterm (menu), default=20 (DEC)") help="Diagnostics: suppress non-error messages, default=Off")
@click.option("--quiet", is_flag=True, def serialports_monitor(**kwargs):
help="Diagnostics: suppress non-error messages, default=Off") if not kwargs['port']:
def serialports_monitor(**kwargs): for item in get_serialports():
sys.argv = app.get_session_var("command_ctx").args[1:] if "VID:PID" in item['hwid']:
kwargs['port'] = item['port']
break
if not kwargs['port']: sys.argv = ["monitor"]
for item in get_serialports(): for k, v in kwargs.iteritems():
if "VID:PID" in item['hwid']: if k in ("port", "baud", "rts", "dtr"):
sys.argv += ["--port", item['port']] continue
break k = "--" + k.replace("_", "-")
if isinstance(v, bool):
if v:
sys.argv.append(k)
elif isinstance(v, tuple):
for i in v:
sys.argv.extend([k, i])
else:
sys.argv.extend([k, str(v)])
try: try:
miniterm.main() miniterm.main( # pylint: disable=E1123
except Exception as e: # pylint: disable=W0702 default_port=kwargs['port'],
raise MinitermException(e) default_baudrate=kwargs['baud'],
default_rts=kwargs['rts'],
default_dtr=kwargs['dtr']
)
except Exception as e: # pylint: disable=W0702
raise MinitermException(e)
else:
@cli.command("monitor", short_help="Monitor Serial port")
@click.option("--port", "-p", help="Port, a number or a device name")
@click.option("--baud", "-b", type=int, default=9600,
help="Set baud rate, default=9600")
@click.option("--parity", default="N",
type=click.Choice(["N", "E", "O", "S", "M"]),
help="Set parity, default=N")
@click.option("--rtscts", is_flag=True,
help="Enable RTS/CTS flow control, default=Off")
@click.option("--xonxoff", is_flag=True,
help="Enable software flow control, default=Off")
@click.option("--rts", default="0", type=click.Choice(["0", "1"]),
help="Set initial RTS line state, default=0")
@click.option("--dtr", default="0", type=click.Choice(["0", "1"]),
help="Set initial DTR line state, default=0")
@click.option("--echo", is_flag=True,
help="Enable local echo, default=Off")
@click.option("--cr", is_flag=True,
help="Do not send CR+LF, send CR only, default=Off")
@click.option("--lf", is_flag=True,
help="Do not send CR+LF, send LF only, default=Off")
@click.option("--debug", "-d", count=True,
help="""Debug received data (escape non-printable chars)
# --debug can be given multiple times:
# 0: just print what is received
# 1: escape non-printable characters, do newlines as unusual
# 2: escape non-printable characters, newlines too
# 3: hex dump everything""")
@click.option("--exit-char", type=int, default=29,
help="ASCII code of special character that is used to exit "
"the application, default=29 (DEC)")
@click.option("--menu-char", type=int, default=20,
help="ASCII code of special character that is used to "
"control miniterm (menu), default=20 (DEC)")
@click.option("--quiet", is_flag=True,
help="Diagnostics: suppress non-error messages, default=Off")
def serialports_monitor(**kwargs):
sys.argv = app.get_session_var("command_ctx").args[1:]
if not kwargs['port']:
for item in get_serialports():
if "VID:PID" in item['hwid']:
sys.argv += ["--port", item['port']]
break
try:
miniterm.main()
except Exception as e: # pylint: disable=W0702
raise MinitermException(e)

View File

@ -12,7 +12,7 @@ install_requires = [
"bottle", "bottle",
"click>=3.2", "click>=3.2",
"lockfile>=0.9.1", "lockfile>=0.9.1",
"pyserial<3", "pyserial",
"requests>=2.4.0" "requests>=2.4.0"
] ]