Commit d9018826 authored by Guillaume Seguin's avatar Guillaume Seguin

Factorize connection handling to pronsole

parent d95bd881
...@@ -28,6 +28,8 @@ import logging ...@@ -28,6 +28,8 @@ import logging
import traceback import traceback
import re import re
from serial import SerialException
from . import printcore from . import printcore
from .utils import install_locale, run_command, get_command_output, \ from .utils import install_locale, run_command, get_command_output, \
format_time, format_duration, RemainingTimeEstimator, \ format_time, format_duration, RemainingTimeEstimator, \
...@@ -1006,6 +1008,28 @@ class pronsole(cmd.Cmd): ...@@ -1006,6 +1008,28 @@ class pronsole(cmd.Cmd):
# Printer connection handling # Printer connection handling
# -------------------------------------------------------------- # --------------------------------------------------------------
def connect_to_printer(self, port, baud):
try:
self.p.connect(port, baud)
return True
except SerialException as e:
# Currently, there is no errno, but it should be there in the future
if e.errno == 2:
self.logError(_("Error: You are trying to connect to a non-existing port."))
elif e.errno == 8:
self.logError(_("Error: You don't have permission to open %s.") % port)
self.logError(_("You might need to add yourself to the dialout group."))
else:
self.logError(traceback.format_exc())
# Kill the scope anyway
return False
except OSError as e:
if e.errno == 2:
self.logError(_("Error: You are trying to connect to a non-existing port."))
else:
self.logError(traceback.format_exc())
return False
def do_connect(self, l): def do_connect(self, l):
a = l.split() a = l.split()
p = self.scanserial() p = self.scanserial()
...@@ -1031,7 +1055,7 @@ class pronsole(cmd.Cmd): ...@@ -1031,7 +1055,7 @@ class pronsole(cmd.Cmd):
if baud != self.settings.baudrate: if baud != self.settings.baudrate:
self.settings.baudrate = baud self.settings.baudrate = baud
self.save_in_rc("set baudrate", "set baudrate %d" % baud) self.save_in_rc("set baudrate", "set baudrate %d" % baud)
self.p.connect(port, baud) self.connect_to_printer(port, baud)
def help_connect(self): def help_connect(self):
self.log("Connect to printer") self.log("Connect to printer")
......
...@@ -47,7 +47,6 @@ except: ...@@ -47,7 +47,6 @@ except:
from printrun.gui.widgets import SpecialButton, MacroEditor, \ from printrun.gui.widgets import SpecialButton, MacroEditor, \
PronterOptions, ButtonEdit PronterOptions, ButtonEdit
from serial import SerialException
winsize = (800, 500) winsize = (800, 500)
layerindex = 0 layerindex = 0
...@@ -1017,24 +1016,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>.""" ...@@ -1017,24 +1016,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>."""
self.paused = 0 self.paused = 0
if self.sdprinting: if self.sdprinting:
self.p.send_now("M26 S0") self.p.send_now("M26 S0")
try: if not self.connect_to_printer(port, baud):
self.p.connect(port, baud)
except SerialException as e:
# Currently, there is no errno, but it should be there in the future
if e.errno == 2:
self.logError(_("Error: You are trying to connect to a non-existing port."))
elif e.errno == 8:
self.logError(_("Error: You don't have permission to open %s.") % port)
self.logError(_("You might need to add yourself to the dialout group."))
else:
self.logError(traceback.format_exc())
# Kill the scope anyway
return
except OSError as e:
if e.errno == 2:
self.logError(_("Error: You are trying to connect to a non-existing port."))
else:
self.logError(traceback.format_exc())
return return
self.statuscheck = True self.statuscheck = True
if port != self.settings.port: if port != self.settings.port:
......
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