Commit d9018826 authored by Guillaume Seguin's avatar Guillaume Seguin

Factorize connection handling to pronsole

parent d95bd881
......@@ -28,6 +28,8 @@ import logging
import traceback
import re
from serial import SerialException
from . import printcore
from .utils import install_locale, run_command, get_command_output, \
format_time, format_duration, RemainingTimeEstimator, \
......@@ -1006,6 +1008,28 @@ class pronsole(cmd.Cmd):
# 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):
a = l.split()
p = self.scanserial()
......@@ -1031,7 +1055,7 @@ class pronsole(cmd.Cmd):
if baud != self.settings.baudrate:
self.settings.baudrate = 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):
self.log("Connect to printer")
......
......@@ -47,7 +47,6 @@ except:
from printrun.gui.widgets import SpecialButton, MacroEditor, \
PronterOptions, ButtonEdit
from serial import SerialException
winsize = (800, 500)
layerindex = 0
......@@ -1017,24 +1016,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>."""
self.paused = 0
if self.sdprinting:
self.p.send_now("M26 S0")
try:
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())
if not self.connect_to_printer(port, baud):
return
self.statuscheck = True
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