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

Optionally log and replay serial commands sent to EBB #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions plotink/ebb_replay_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

HELP = '''
This simple script replays the commands logged by
the EggBot Inkscape extension.

Usage: python3 ebb_replay_log.py <logfile> <serial port>
'''

from serial import Serial
import sys

if len(sys.argv) < 3:
print(HELP)
sys.exit(-1)

filename = sys.argv[1]
port = sys.argv[2]

ser = Serial(port, 115200)
commands = open(filename).readlines()

for command in commands:
ser.write(command.encode('ascii'))
ret = ser.readline()
print(command, ret)
19 changes: 19 additions & 0 deletions plotink/ebb_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

logger = logging.getLogger(__name__)

command_log = None

def version():
'''Version number for this document'''
return "0.19" # Dated 2022-10-05
Expand Down Expand Up @@ -299,6 +301,21 @@ def testPort(port_name):
return None


def startLogging(logfile):
global command_log
if logfile and not command_log:
try:
command_log = open(logfile, 'wb')
except PermissionError:
command_log = None # be explicit


def stopLogging():
global command_log
if command_log:
command_log.close()


def openPort():
'''
Find and open a port to a single attached EiBotBoard.
Expand Down Expand Up @@ -376,6 +393,8 @@ def command(port_name, cmd, verbose=True):
'''General command to send a command to the EiBotBoard'''
if port_name is not None and cmd is not None:
try:
if command_log:
command_log.write(cmd.encode('ascii'))
port_name.write(cmd.encode('ascii'))
response = port_name.readline().decode('ascii')
n_retry_count = 0
Expand Down