Commit 64137be9 authored by kliment's avatar kliment

Initial commit, basic functionality (send gcode to printer, pause, resume,...

Initial commit, basic functionality (send gcode to printer, pause, resume, send intermediate command)
parents
This is the beginning of a python-based host software for reprap printers.
Currently contains printcore.py, which requires pyserial to be installed.
Using printcore:
p=printcore('/dev/ttyUSB0',115200)
p.startprint(data) # data is an array of gcode lines
p.send_now("M105") # sends M105 as soon as possible
p.pause()
p.resume()
p.disconnect()
from serial import Serial
from threading import Thread
import time
class printcore():
def __init__(self,port=None,baud=None):
self.baud=None
self.port=None
self.printer=None
self.clear=0
self.mainqueue=[]
self.priqueue=[]
if port is not None and baud is not None:
print port, baud
self.connect(port, baud)
print "connected\n"
self.readthread=None
self.queueindex=0
self.lineno=0
self.resendfrom=-1
self.online=False
self.printing=False
self.sentlines={}
def disconnect(self):
if(self.printer):
self.printer.close()
self.printer=None
self.online=False
self.printing=False
def connect(self,port=None,baud=None):
if(self.printer):
self.disconnect()
if port is not None:
self.port=port
if baud is not None:
self.baud=baud
if self.port is not None and self.baud is not None:
self.printer=Serial(self.port,self.baud,timeout=5)
Thread(target=self._listen).start()
def _listen(self):
"""This function acts on messages from the firmware
"""
time.sleep(1)
print "listening:"
while(True):
if(not self.printer or not self.printer.isOpen):
print "port not open"
break
line=self.printer.readline()
print "RECV:",line
if(line.startswith('start')):
self.clear=True
self.online=True
elif(line.startswith('ok')):
self.clear=True
self.resendfrom=-1
#put temp handling here
elif(line.startswith('Error')):
pass
if "Resend" in line or "rs" in line:
toresend=int(line.split(':')[1])
self.resendfrom=toresend
self.clear=True
print "done listening"
def checksum(self,command):
return reduce(lambda x,y:x^y, map(ord,command))
def startprint(self,data):
if(self.printing):
return False
self.printing=True
self.mainqueue=[]+data
self._send("M110",-1, True)
Thread(target=self._print).start()
return True
def pause(self):
self.printing=False
def resume(self):
self.printing=True
threading.Thread(target=self._print).start()
def send_now(self,command):
if(self.printing):
self.priqueue+=[command]
else:
self._send(command)
def _print(self):
while(self.printing and self.printer):
self.sendnext()
def sendnext(self):
if(not self.printer):
return
if(self.resendfrom>-1):
while(self.resendfrom<self.lineno):
while not self.clear:
time.sleep(0.001)
self.clear=False
self._send(self.sentlines[self.resendfrom],self.resendfrom,False)
self.resendfrom+=1
self.resendfrom=-1
for i in self.priqueue[:]:
while not self.clear:
time.sleep(0.001)
self.clear=False
self._send(i)
del(self.preque[0])
if(self.printing and self.queueindex<len(self.mainqueue)):
tline=self.mainqueue[self.queueindex]
if(not tline.startswith(';') and len(tline)>0):
while not self.clear:
time.sleep(0.001)
self.clear=False
self._send(tline,self.lineno,True)
self.lineno+=1
self.queueindex+=1
else:
self.printing=False
self.queueindex=0
self.lineno=0
self._send("M110",-1, True)
def _send(self, command, lineno=0, calcchecksum=False):
if(calcchecksum):
prefix="N"+str(lineno)+" "+command
command=prefix+"*"+str(self.checksum(prefix))
self.sentlines[lineno]=command
if(self.printer):
print "sending: "+command+"\n"
self.printer.write(command+"\n")
if __name__ == '__main__':
p=printcore('/dev/ttyUSB0',115200)
time.sleep(5)
testdata="""G28
G1 X0 Y0
G1 X10 Y10
G1 X0 Y0
;
"""
p.startprint(testdata.split('\n'))
time.sleep(10)
p.disconnect()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment