Skip to content

Instantly share code, notes, and snippets.

@crystalline
Last active June 16, 2018 18:11
Show Gist options
  • Save crystalline/74c48ed85cc02b762d7cdd26d3b6edce to your computer and use it in GitHub Desktop.
Save crystalline/74c48ed85cc02b762d7cdd26d3b6edce to your computer and use it in GitHub Desktop.
A tool to run G-CODE file on marlin-compatible (repetier, etc) 3d-printers
#!/usr/bin/env python2.7
import serial
import time
import sys
from datetime import datetime
import os
import argparse
def heuristicallyFindSerPort():
ports = filter(lambda s: s.startswith('ttyUSB'), os.listdir('/dev'))
if (len(ports) > 0):
return '/dev/'+ports[0]
return '/dev/ttyUSB0'
parser = argparse.ArgumentParser(description='Execute G-CODE file on a 3d-printer')
defport = heuristicallyFindSerPort()
parser.add_argument('--serport', metavar='P', type=str, default=defport,
help='path to serial device')
parser.add_argument('--baudrate', metavar='B', type=int, default=250000,
help='baudrate')
parser.add_argument('--debug', metavar='D', type=bool, default=False,
help='debug mode')
parser.add_argument('--file', metavar='F', type=str, default='',
help='path to G-CODE file')
args = parser.parse_args()
DBG=args.debug
def debug(s):
if DBG:
print 'debug:',s
tty = args.serport
baudrate = args.baudrate
codefile = args.file
print 'Opening printer connection via '+tty+' @ '+str(baudrate)+' baud'
com = serial.Serial()
com.port = tty
com.baudrate = baudrate
com.timeout = 1
com.setDTR(False)
com.open()
print 'Waiting 2 seconds...'
time.sleep(2)
def execCMD(gcode, ser):
com.write(gcode+'\n')
com.flush()
while True:
r = com.readline()
debug(r)
if r.startswith('ok'):
return 0
print 'Executing ',codefile
code = filter(lambda expr: not expr.startswith(';'), map(lambda s: s.strip(), open(codefile).readlines()));
debug(code)
t1 = datetime.now()
i = 0
for expr in code:
execCMD(expr, com)
i += 1
debug('cmd '+str(i)+'/'+str(len(code)))
delta = datetime.now() - t1
print "%8.1f %s" % (int(delta.total_seconds()), expr)
t2 = datetime.now()
delta = t2 - t1
print 'Done in '+str(delta.total_seconds())+ 's'
com.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment