Commit 99ec219e authored by Guillaume Seguin's avatar Guillaume Seguin

Improvements to connect and disconnect

parent b4cc74f8
...@@ -49,6 +49,8 @@ class printcore(): ...@@ -49,6 +49,8 @@ class printcore():
self.loud=False#emit sent and received lines to terminal self.loud=False#emit sent and received lines to terminal
self.greetings=['start','Grbl '] self.greetings=['start','Grbl ']
self.wait=0# default wait period for send(), send_now() self.wait=0# default wait period for send(), send_now()
self.read_thread=None
self.stop_read_thread=False
if port is not None and baud is not None: if port is not None and baud is not None:
#print port, baud #print port, baud
self.connect(port, baud) self.connect(port, baud)
...@@ -58,7 +60,11 @@ class printcore(): ...@@ -58,7 +60,11 @@ class printcore():
def disconnect(self): def disconnect(self):
"""Disconnects from printer and pauses the print """Disconnects from printer and pauses the print
""" """
if(self.printer): if self.printer:
if self.read_thread:
self.stop_read_thread = True
self.read_thread.join()
self.read_thread = None
self.printer.close() self.printer.close()
self.printer=None self.printer=None
self.online=False self.online=False
...@@ -74,13 +80,15 @@ class printcore(): ...@@ -74,13 +80,15 @@ class printcore():
if baud is not None: if baud is not None:
self.baud=baud self.baud=baud
if self.port is not None and self.baud is not None: if self.port is not None and self.baud is not None:
self.printer=Serial(port = self.port, baudrate = self.baud, timeout = 5) self.printer=Serial(port = self.port, baudrate = self.baud, timeout = 1, dsrdtr = True)
Thread(target=self._listen).start() self.stop_read_thread = False
self.read_thread = Thread(target=self._listen)
self.read_thread.start()
def reset(self): def reset(self):
"""Reset the printer """Reset the printer
""" """
if(self.printer): if self.printer:
self.printer.setDTR(1) self.printer.setDTR(1)
self.printer.setDTR(0) self.printer.setDTR(0)
...@@ -96,6 +104,8 @@ class printcore(): ...@@ -96,6 +104,8 @@ class printcore():
if (not self.online and not self.printing): # send M105 to initiate connection if (not self.online and not self.printing): # send M105 to initiate connection
self._send("M105") self._send("M105")
while(True): while(True):
if self.stop_read_thread:
return
if(not self.printer or not self.printer.isOpen): if(not self.printer or not self.printer.isOpen):
break break
try: try:
......
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