-
Notifications
You must be signed in to change notification settings - Fork 179
New feature: Serial Terminal #650
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
22e3ec7
Add initial support for PySerial Miniterm
screamerbg f8c2170
Allow reset and sterm switches for 'mbed detect' even if mbed OS tool…
screamerbg 653eb85
Fix help messages
screamerbg a5e0d75
Move CDC code to separate python file
screamerbg c4e98ff
Catch missing serial module (but don't depend on it)
screamerbg e9d77ea
Address comments in PR #650
screamerbg 5c65097
Reimplement Serial Terminal feature and documentation based on PR fee…
screamerbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = bool(echo) | ||
|
||
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) | ||
--- 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is not strictly true. The
-f/--flash
option requiresmbed-host-tests
.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.
I'd rather encourage the user to install
mbed-greentea
which will pull in all the needed modules behind it. In this case, the product ismbed-greentea
, not host-tests.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.
Sure, that works. If
mbed-host-tests
is not something the user should ever install standalone, I'll merge it with greentea.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.
In case this was not clean: this is resolved.