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 UM.Logger import Logger
from .avr_isp import stk500v2 from .avr_isp import stk500v2
import threading
class PrinterConnection(): class PrinterConnection():
def __init__(self, serial_port): def __init__(self, serial_port):
@ -19,12 +20,14 @@ class PrinterConnection():
self._listen_thread = threading.Thread(target=self._listen) self._listen_thread = threading.Thread(target=self._listen)
self._listen_thread.daemon = True self._listen_thread.daemon = True
#self._listen_thread.start() #self._listen_thread.start()
def getSerialPort(self):
return self._serial_port
## Try to connect the serial. This simply starts the thread. ## Try to connect the serial. This simply starts the thread.
def connect(self): def connect(self):
self._connect_thread.start() self._connect_thread.start()
def _connect(self): def _connect(self):
self._is_connecting = True self._is_connecting = True
programmer.connect(serial_port) #Connect with the serial, if this succeeds, it's an arduino based usb device. programmer.connect(serial_port) #Connect with the serial, if this succeeds, it's an arduino based usb device.
try: try:
@ -32,8 +35,7 @@ class PrinterConnection():
# Create new printer connection # Create new printer connection
self.active_printer_connection = PrinterConnection(temp_serial) self.active_printer_connection = PrinterConnection(temp_serial)
Logger.log('i', "Established connection on port %s" % serial_port) 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))) Logger.log('i', "Could not establish connection on %s: %s. Device is not arduino based." %(serial_port,str(e)))
except: except:
Logger.log('i', "Could not establish connection on %s, unknown reasons. Device is not arduino based." % serial_port) 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. #Device is not arduino based, so we need to cycle the baud rates.
for baud_rate in self._getBaudrateList(): for baud_rate in self._getBaudrateList():
timeout_time = time.time() + 5 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) self._serial = serial.Serial(str(self._port), baud_rate, timeout=5, writeTimeout=10000)
else: else:
if not self.setBaudRate(baud_rate): if not self.setBaudRate(baud_rate):
continue #Could not set the baud rate, go to the next continue #Could not set the baud rate, go to the next
sucesfull_responses = 0 sucesfull_responses = 0
while timeout_time > time.time(): while timeout_time > time.time():
line = self._readline(): line = self._readline()
if "T:" in line: if "T:" in line:
self._serial.timeout = 0.5 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 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. return #Stop trying to connect, we are connected.
def _listen(self): def _listen(self):
time.sleep(5)
pass pass
def setBaudRate(self, baud_rate): def setBaudRate(self, baud_rate):
@ -82,10 +83,12 @@ class PrinterConnection():
if self._is_connected: if self._is_connected:
self._listen_thread.start() #Start listening self._listen_thread.start() #Start listening
def close(self):
pass #TODO: handle
def isConnected(self): def isConnected(self):
return self._is_connected = True return self._is_connected
def _listen(self): def _listen(self):
while True: while True:
line = self._readline() line = self._readline()
@ -118,7 +121,7 @@ class PrinterConnection():
def hasError(self): def hasError(self):
return self._error_state == None ? False : True return False
def _readline(self): def _readline(self):
@ -127,7 +130,7 @@ class PrinterConnection():
try: try:
ret = self._serial.readline() ret = self._serial.readline()
except: except:
self._log("Unexpected error while reading serial port.")) self._log("Unexpected error while reading serial port.")
self._errorValue = getExceptionString() self._errorValue = getExceptionString()
self.close(True) self.close(True)
return None return None

View file

@ -1,10 +1,12 @@
from UM.Signal import Signal, SignalEmitter from UM.Signal import Signal, SignalEmitter
from UM.PluginObject import PluginObject from UM.PluginObject import PluginObject
from . import PrinterConnection
import threading import threading
import platform import platform
import glob import glob
import time import time
import os
class USBPrinterManager(SignalEmitter,PluginObject): class USBPrinterManager(SignalEmitter,PluginObject):
def __init__(self): def __init__(self):
@ -24,17 +26,16 @@ class USBPrinterManager(SignalEmitter,PluginObject):
temp_serial_port_list = self.getSerialPortList(only_list_usb = True) 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. 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 ] 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: for serial_port in self._serial_port_list:
if self.getConnectionByPort(serial_port) is None: #If it doesn't already exist, add it 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. for serial_port in disconnected_ports: # Close connections and remove them from list.
connection = self.getConnectionByPort(serial_port) connection = self.getConnectionByPort(serial_port)
connection.close() connection.close()
self._printer_connections.remove(connection) self._printer_connections.remove(connection)
time.sleep(5) #Throttle, as we don't need this information to be updated every single second. time.sleep(5) #Throttle, as we don't need this information to be updated every single second.
def connectAllConnections(self): def connectAllConnections(self):

View file

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