-
Notifications
You must be signed in to change notification settings - Fork 179
New feature: Serial Terminal (revised) #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
f6c29a7
4aeb3be
6c8cf25
2753d3a
05d2216
c4c4c01
dd6cb5d
90a548b
15d50d7
4c30306
3c9dbf4
dbb455f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -514,6 +514,32 @@ $ mbed export -i uvision -m K64F | |
|
||
Mbed CLI creates a `.uvprojx` file in the projectfiles/uvision folder. You can open the project file with uVision. | ||
|
||
### Serial terminal | ||
|
||
You can open a serial terminal to the COM port of a connected Mbed target (usually board) using the `mbed sterm` command. If no COM port is specified, Mbed CLI will attempt to detect the connected Mbed targets and their COM ports. | ||
|
||
There are various options to `mbed sterm`: | ||
* `--port <COM port>` to specify system COM port to connect to. | ||
* `--baudrate <numeric>` to select the communication baudrate, where the default value is 9600. | ||
* `--echo <on|off>` to switch local echo (default is `on`). | ||
* `--reset` to reset the connected target by sending Break before opening the serial terminal. | ||
|
||
You can also set default port, baudrate and echo mode using the `TERM_PORT`, `TERM_BAUDRATE` and `TERM_ECHO` Mbed CLI configuration options. | ||
|
||
The following shortcuts are available within the serial terminal: | ||
- Ctrl+b - Send Break (reset target) | ||
- Ctrl+c - Exit terminal | ||
- Ctrl+e - Toggle local echo | ||
- Ctrl+h - Help | ||
- Ctrl+t - Menu escape key | ||
- _Even more shortcuts are available through the Menu shortcut. Check the help within the serial terminal (Ctrl+h).__ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @screamerbg Why not list all of the shortcut commands within the README? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yennster Primarily because it's a long list of commands that are very specific to the console experience. Unless you have the console running, you won't care about these options. Does this make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the last bullet should say:
|
||
|
||
To automate things, you can also add the `--sterm` option to `mbed compile -f` to compile a new program, flash the program/firmware image to the connected target and then open serial terminal to it's COM port: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @screamerbg I think this should say:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's great. Thanks! |
||
|
||
``` | ||
$ mbed compile -t GCC_ARM -m K64F -f --sterm | ||
``` | ||
|
||
## Testing | ||
|
||
Use the `mbed test` command to compile and run tests. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
|
||
#!/usr/bin/env python2 | ||
|
||
# Copyright (c) 2016 ARM Limited, All Rights Reserved | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
|
||
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
# either express or implied. | ||
|
||
|
||
# pylint: disable=too-many-arguments, too-many-locals, too-many-branches, too-many-lines, line-too-long, | ||
# pylint: disable=too-many-nested-blocks, too-many-public-methods, too-many-instance-attributes, too-many-statements | ||
# pylint: disable=invalid-name, missing-docstring, bad-continuation | ||
|
||
|
||
# Global class used for global config | ||
class MbedTerminal(object): | ||
serial = None # Serial() object | ||
port = None | ||
baudrate = None | ||
echo = None | ||
|
||
def __init__(self, port, baudrate=9600, echo=True, timeout=10): | ||
self.port = port | ||
self.baudrate = int(baudrate) | ||
self.timeout = int(timeout) | ||
self.echo = False if str(echo).lower() == 'off' else True | ||
|
||
try: | ||
from serial import Serial, SerialException | ||
except (IOError, ImportError, OSError): | ||
return False | ||
|
||
try: | ||
self.serial = Serial(self.port, baudrate=self.baudrate, timeout=self.timeout) | ||
self.serial.flush() | ||
self.serial.reset_input_buffer() | ||
except Exception as e: | ||
print 'error' | ||
self.serial = None | ||
return False | ||
|
||
def terminal(self, print_header=True): | ||
try: | ||
import serial.tools.miniterm as miniterm | ||
except (IOError, ImportError, OSError): | ||
return False | ||
|
||
term = miniterm.Miniterm(self.serial, echo=self.echo) | ||
term.exit_character = '\x03' | ||
term.menu_character = '\x14' | ||
term.set_rx_encoding('UTF-8') | ||
term.set_tx_encoding('UTF-8') | ||
|
||
def console_print(text): | ||
term.console.write('--- %s ---\n' % text) | ||
|
||
def get_print_help(): | ||
return """ | ||
--- Mbed Serial Terminal (0.3a) | ||
--- Based on miniterm from pySerial | ||
--- | ||
--- CTRL+B Send Break (reset target) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @screamerbg Are these just comments within the file? Can they be changed to match the lowercase commands in the README? (L67-L85) |
||
--- CTRL+C Exit terminal | ||
--- CTRL+E Toggle local echo | ||
--- CTRL+H Help | ||
--- CTRL+T Menu escape key, followed by: | ||
--- P Change COM port | ||
--- B Change baudrate | ||
--- TAB Show detailed terminal info | ||
--- CTRL+A Change encoding (default UTF-8) | ||
--- CTRL+F Edit filters | ||
--- CTRL+L Toggle EOL | ||
--- CTRL+R Toggle RTS | ||
--- CTRL+D Toggle DTR | ||
--- CTRL+C Send control character to remote | ||
--- CTRL+T Send control character to remote | ||
""" | ||
|
||
def print_help(): | ||
term.console.write(get_print_help()) | ||
|
||
|
||
def input_handler(): | ||
menu_active = False | ||
while term.alive: | ||
try: | ||
c = term.console.getkey() | ||
except KeyboardInterrupt: | ||
c = '\x03' | ||
if not term.alive: | ||
break | ||
if menu_active and c in ['p', 'b', '\t', '\x01', '\x03', '\x04', '\x05', '\x06', '\x0c', '\x14']: | ||
term.handle_menu_key(c) | ||
menu_active = False | ||
elif c == term.menu_character: | ||
console_print('[MENU]') | ||
menu_active = True # next char will be for menu | ||
elif c == '\x02': # ctrl+b sendbreak | ||
console_print('[RESET]') | ||
self.reset() | ||
elif c == '\x03': # ctrl+c | ||
console_print('[QUIT]') | ||
term.stop() | ||
term.alive = False | ||
break | ||
elif c == '\x05': # ctrl+e | ||
console_print('[ECHO %s]' % ('OFF' if term.echo else 'ON')) | ||
term.echo = not term.echo | ||
elif c == '\x08': # ctrl+h | ||
print_help() | ||
# elif c == '\t': # tab/ctrl+i | ||
# term.dump_port_settings() | ||
else: | ||
text = c | ||
for transformation in term.tx_transformations: | ||
text = transformation.tx(text) | ||
term.serial.write(term.tx_encoder.encode(text)) | ||
if term.echo: | ||
echo_text = c | ||
for transformation in term.tx_transformations: | ||
echo_text = transformation.echo(echo_text) | ||
term.console.write(echo_text) | ||
term.writer = input_handler | ||
|
||
if print_header: | ||
console_print("Terminal on {p.name} - {p.baudrate},{p.bytesize},{p.parity},{p.stopbits}".format(p=term.serial)) | ||
|
||
term.start() | ||
|
||
try: | ||
term.join(True) | ||
except KeyboardInterrupt: | ||
pass | ||
term.join() | ||
term.close() | ||
|
||
return True | ||
|
||
def reset(self): | ||
try: | ||
self.serial.sendBreak() | ||
except: | ||
try: | ||
self.serial.setBreak(False) # For Linux the following setBreak() is needed to release the reset signal on the target mcu. | ||
except: | ||
return False | ||
return True |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean you can use the
mbed sterm
command with multiple Mbed boards plugged into the computer at the same time?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It would detect all and iterate through them (one by one, not simultaneously).