This commit is contained in:
Jaime van Kessel 2015-03-31 09:59:24 +02:00
parent b4df277cc3
commit 220562aa05
3 changed files with 18 additions and 15 deletions

View file

@ -1,5 +1,6 @@
from UM.Logger import Logger
from .avr_isp import stk500v2
import threading
class PrinterConnection():
def __init__(self, serial_port):
@ -19,6 +20,8 @@ class PrinterConnection():
self._listen_thread = threading.Thread(target=self._listen)
self._listen_thread.daemon = True
#self._listen_thread.start()
def getSerialPort(self):
return self._serial_port
## Try to connect the serial. This simply starts the thread.
def connect(self):
@ -32,8 +35,7 @@ class PrinterConnection():
# Create new printer connection
self.active_printer_connection = PrinterConnection(temp_serial)
Logger.log('i', "Established connection on port %s" % serial_port)
break
except ispBase.IspError as (e):
except ispBase.IspError as e:
Logger.log('i', "Could not establish connection on %s: %s. Device is not arduino based." %(serial_port,str(e)))
except:
Logger.log('i', "Could not establish connection on %s, unknown reasons. Device is not arduino based." % serial_port)
@ -42,14 +44,14 @@ class PrinterConnection():
#Device is not arduino based, so we need to cycle the baud rates.
for baud_rate in self._getBaudrateList():
timeout_time = time.time() + 5
if self._serial = None:
if self._serial is None:
self._serial = serial.Serial(str(self._port), baud_rate, timeout=5, writeTimeout=10000)
else:
if not self.setBaudRate(baud_rate):
continue #Could not set the baud rate, go to the next
sucesfull_responses = 0
while timeout_time > time.time():
line = self._readline():
line = self._readline()
if "T:" in line:
self._serial.timeout = 0.5
self._sendCommand("M105") # Request temperature, as this should (if baudrate is correct) result in a command with 'T:' in it
@ -63,7 +65,6 @@ class PrinterConnection():
return #Stop trying to connect, we are connected.
def _listen(self):
time.sleep(5)
pass
def setBaudRate(self, baud_rate):
@ -82,9 +83,11 @@ class PrinterConnection():
if self._is_connected:
self._listen_thread.start() #Start listening
def close(self):
pass #TODO: handle
def isConnected(self):
return self._is_connected = True
return self._is_connected
def _listen(self):
while True:
@ -118,7 +121,7 @@ class PrinterConnection():
def hasError(self):
return self._error_state == None ? False : True
return False
def _readline(self):
@ -127,7 +130,7 @@ class PrinterConnection():
try:
ret = self._serial.readline()
except:
self._log("Unexpected error while reading serial port."))
self._log("Unexpected error while reading serial port.")
self._errorValue = getExceptionString()
self.close(True)
return None

View file

@ -1,10 +1,12 @@
from UM.Signal import Signal, SignalEmitter
from UM.PluginObject import PluginObject
from . import PrinterConnection
import threading
import platform
import glob
import time
import os
class USBPrinterManager(SignalEmitter,PluginObject):
def __init__(self):
@ -24,17 +26,16 @@ class USBPrinterManager(SignalEmitter,PluginObject):
temp_serial_port_list = self.getSerialPortList(only_list_usb = True)
if temp_serial_port_list != self._serial_port_list: # Something changed about the list since we last changed something.
disconnected_ports = [port for port in self._serial_port_list if port not in temp_serial_port_list ]
self._serial_port_list = temp_serial.port_list
self._serial_port_list = temp_serial_port_list
for serial_port in self._serial_port_list:
if self.getConnectionByPort(serial_port) is None: #If it doesn't already exist, add it
self._printer_connections.append(PrinterConnection(serial_port))
if not os.path.islink(serial_port): #Only add the connection if it's a non symbolic link
self._printer_connections.append(PrinterConnection.PrinterConnection(serial_port))
for serial_port in disconnected_ports: # Close connections and remove them from list.
connection = self.getConnectionByPort(serial_port)
connection.close()
self._printer_connections.remove(connection)
time.sleep(5) #Throttle, as we don't need this information to be updated every single second.
def connectAllConnections(self):

View file

@ -1,4 +1,3 @@
from . import USBPrintDevice
from . import USBPrinterManager
def getMetaData():
return {