Skip to content
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

Update consutil for picocom v1.7 #300

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@
FLOW_KEY = "flow_control"
DEFAULT_BAUD = "9600"

# QUIET == True => picocom will not output any messages, and pexpect will wait for console
# switch login or command line to let user interact with shell
# Downside: if console switch output ever does not match DEV_READY_MSG, program will think connection failed
# QUIET == False => picocom will output messages - welcome message is caught by pexpect, so successful
# connection will always lead to user interacting with shell
# Downside: at end of session, picocom will print exit message, exposing picocom to user
QUIET = False
DEV_READY_MSG = r"([Ll]ogin:|[Pp]assword:|[$>#])" # login prompt or command line prompt
TIMEOUT_SEC = 0.2

# runs command, exit if stderr is written to, returns stdout otherwise
Expand Down
19 changes: 6 additions & 13 deletions consutil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def clear(linenum):
cmd = "sudo kill -SIGTERM " + pid
click.echo("Sending SIGTERM to process " + pid)
run_command(cmd)
cmd = "sudo rm /var/lock/LCK..ttyUSB" + linenum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to add -f flag to rm command to help ensure file is deleted.

run_command(cmd)
else:
click.echo("No process is connected to line " + linenum)

Expand All @@ -78,27 +80,18 @@ def connect(target, devicename):
# build and start picocom command
actBaud, _, flowBool = getConnectionInfo(lineNumber)
flowCmd = "h" if flowBool else "n"
quietCmd = "-q" if QUIET else ""
cmd = "sudo picocom -b {} -f {} {} {}{}".format(actBaud, flowCmd, quietCmd, DEVICE_PREFIX, lineNumber)
cmd = "sudo picocom -b {} -f {} {}{}".format(actBaud, flowCmd, DEVICE_PREFIX, lineNumber)
proc = pexpect.spawn(cmd)
proc.send("\n")

if QUIET:
readyMsg = DEV_READY_MSG
else:
readyMsg = "Terminal ready" # picocom ready message
busyMsg = "Resource temporarily unavailable" # picocom busy message
readyMsg = "Terminal ready" # picocom ready message
busyMsg = "FATAL" # picocom busy message

# interact with picocom or print error message, depending on pexpect output
index = proc.expect([readyMsg, busyMsg, pexpect.EOF, pexpect.TIMEOUT], timeout=TIMEOUT_SEC)
if index == 0: # terminal ready
click.echo("Successful connection to line {}\nPress ^A ^X to disconnect".format(lineNumber))
if QUIET:
# prints picocom output up to and including readyMsg
click.echo(proc.before + proc.match.group(0), nl=False)
proc.interact()
if QUIET:
click.echo("\nTerminating...")
click.echo("\nTerminating")
elif index == 1: # resource is busy
click.echo("Cannot connect: line {} is busy".format(lineNumber))
else: # process reached EOF or timed out
Expand Down