Add (configurable) small delay before baudrate detection
Solves issue with controllers that don't like baudrate switches right after connection, e.g. the Einsy board. Fixes #2517
This commit is contained in:
1 parent
6f18495395
commit
28831a68dd
5 files changed
+67
-35
No files matched your search
@@ -140,6 +140,7 @@ def getSettings():
|
||||
"timeoutTemperatureAutoreport": s.getFloat(["serial", "timeout", "temperatureAutoreport"]),
|
||||
"timeoutSdStatus": s.getFloat(["serial", "timeout", "sdStatus"]),
|
||||
"timeoutSdStatusAutoreport": s.getFloat(["serial", "timeout", "sdStatusAutoreport"]),
|
||||
"timeoutBaudrateDetectionPause": s.getFloat(["serial", "timeout", "baudrateDetectionPause"]),
|
||||
"log": s.getBoolean(["serial", "log"]),
|
||||
"additionalPorts": s.get(["serial", "additionalPorts"]),
|
||||
"additionalBaudrates": s.get(["serial", "additionalBaudrates"]),
|
||||
@@ -373,6 +374,7 @@ def _saveSettings(data):
|
||||
if "timeoutTemperatureAutoreport" in data["serial"]: s.setFloat(["serial", "timeout", "temperatureAutoreport"], data["serial"]["timeoutTemperatureAutoreport"])
|
||||
if "timeoutSdStatus" in data["serial"]: s.setFloat(["serial", "timeout", "sdStatus"], data["serial"]["timeoutSdStatus"])
|
||||
if "timeoutSdStatusAutoreport" in data["serial"]: s.setFloat(["serial", "timeout", "sdStatusAutoreport"], data["serial"]["timeoutSdStatusAutoreport"])
|
||||
if "timeoutBaudrateDetectionPause" in data["serial"]: s.setFloat(["serial", "timeout", "baudrateDetectionPause"], data["serial"]["timeoutBaudrateDetectionPause"])
|
||||
if "additionalPorts" in data["serial"] and isinstance(data["serial"]["additionalPorts"], (list, tuple)): s.set(["serial", "additionalPorts"], data["serial"]["additionalPorts"])
|
||||
if "additionalBaudrates" in data["serial"] and isinstance(data["serial"]["additionalBaudrates"], (list, tuple)): s.set(["serial", "additionalBaudrates"], data["serial"]["additionalBaudrates"])
|
||||
if "longRunningCommands" in data["serial"] and isinstance(data["serial"]["longRunningCommands"], (list, tuple)): s.set(["serial", "longRunningCommands"], data["serial"]["longRunningCommands"])
|
||||
|
||||
@@ -91,7 +91,7 @@ default_settings = {
|
||||
"autoconnect": False,
|
||||
"log": False,
|
||||
"timeout": {
|
||||
"detection": 0.5,
|
||||
"detection": 1,
|
||||
"connection": 10,
|
||||
"communication": 30,
|
||||
"communicationBusy": 3,
|
||||
@@ -100,7 +100,8 @@ default_settings = {
|
||||
"temperatureAutoreport": 2,
|
||||
"sdStatus": 1,
|
||||
"sdStatusAutoreport": 1,
|
||||
"resendOk": .5
|
||||
"resendOk": .5,
|
||||
"baudrateDetectionPause": 1.0
|
||||
},
|
||||
"maxCommunicationTimeouts": {
|
||||
"idle": 2,
|
||||
|
||||
@@ -159,6 +159,7 @@ $(function() {
|
||||
self.serial_timeoutTemperatureAutoreport = ko.observable(undefined);
|
||||
self.serial_timeoutSdStatus = ko.observable(undefined);
|
||||
self.serial_timeoutSdStatusAutoreport = ko.observable(undefined);
|
||||
self.serial_timeoutBaudrateDetectionPause = ko.observable(undefined);
|
||||
self.serial_log = ko.observable(undefined);
|
||||
self.serial_additionalPorts = ko.observable(undefined);
|
||||
self.serial_additionalBaudrates = ko.observable(undefined);
|
||||
|
||||
@@ -146,6 +146,16 @@
|
||||
<div>
|
||||
<div><small><a href="#" class="muted" data-bind="toggleContent: { class: 'fa-caret-right fa-caret-down', container: '#settings_serial_timeouts_timeouts .hide' }"><i class="fa fa-caret-right"></i> {{ _('Advanced options') }}</a></small></div>
|
||||
<div class="hide">
|
||||
<div class="control-group" title="{{ _('Time to wait after opening the serial connection before starting baudrate detection') }}">
|
||||
<label class="control-label" for="settings-serialTimeoutBaudrateDetectionPause">{{ _('Initial baudrate detection pause') }}</label>
|
||||
<div class="controls">
|
||||
<div class="input-append">
|
||||
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: serial_timeoutBaudrateDetectionPause" id="settings-serialTimeoutBaudrateDetectionPause">
|
||||
<span class="add-on">s</span>
|
||||
</div>
|
||||
<span class="help-block">{{ _('Some controllers need a bit of a breather before baudrate detection after initial connect. If baudrate detection fails due to a hanging controller, try increasing this value.') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group" title="{{ _('Maximum consecutive communication timeouts while idle. More than this and the printer will be considered to be gone. Set to 0 to disable.') }}">
|
||||
<label class="control-label" for="settings-serialMaxTimeoutsIdle">{{ _('Max. consecutive timeouts while idle') }}</label>
|
||||
<div class="controls">
|
||||
|
||||
+51
-33
@@ -417,7 +417,7 @@ class MachineCom(object):
|
||||
self._printerProfileManager = printerProfileManager
|
||||
self._state = self.STATE_NONE
|
||||
self._serial = None
|
||||
self._baudrateDetectList = baudrateList()
|
||||
self._baudrateDetectList = []
|
||||
self._baudrateDetectRetry = 0
|
||||
self._temperatureTargetSetThreshold = 25
|
||||
self._tempOffsets = dict()
|
||||
@@ -1413,10 +1413,15 @@ class MachineCom(object):
|
||||
|
||||
self._log("Connected to: %s, starting monitor" % self._serial)
|
||||
if self._baudrate == 0:
|
||||
self._serial.timeout = 0.01
|
||||
try_hello = False
|
||||
self._log("Starting baud rate detection")
|
||||
self._log("Starting baud rate detection...")
|
||||
self._changeState(self.STATE_DETECT_BAUDRATE)
|
||||
|
||||
# Some controllers (e.g. Original Prusa) don't appear to like too fast serial interaction after
|
||||
# open, so let's wait a bit before the first baudrate detection step
|
||||
time.sleep(settings().getFloat(["serial", "timeout", "baudrateDetectionPause"]))
|
||||
|
||||
self._perform_baudrate_detection_step(init=True)
|
||||
else:
|
||||
self._changeState(self.STATE_CONNECTING)
|
||||
|
||||
@@ -1428,9 +1433,6 @@ class MachineCom(object):
|
||||
supportRepetierTargetTemp = settings().getBoolean(["serial", "repetierTargetTemp"])
|
||||
supportWait = settings().getBoolean(["serial", "supportWait"])
|
||||
|
||||
connection_timeout = settings().getFloat(["serial", "timeout", "connection"])
|
||||
detection_timeout = settings().getFloat(["serial", "timeout", "detection"])
|
||||
|
||||
# enqueue the "hello command" first thing
|
||||
if try_hello:
|
||||
self.sayHello()
|
||||
@@ -1873,30 +1875,8 @@ class MachineCom(object):
|
||||
### Baudrate detection
|
||||
if self._state == self.STATE_DETECT_BAUDRATE:
|
||||
if line == '' or time.time() > self._timeout:
|
||||
if self._baudrateDetectRetry > 0:
|
||||
self._serial.timeout = detection_timeout
|
||||
self._baudrateDetectRetry -= 1
|
||||
self._serial.write('\n')
|
||||
self._log("Baudrate test retry: %d" % (self._baudrateDetectRetry))
|
||||
self.sayHello(tags={"trigger:baudrate_detection",})
|
||||
elif len(self._baudrateDetectList) > 0:
|
||||
baudrate = self._baudrateDetectList.pop(0)
|
||||
try:
|
||||
self._serial.baudrate = baudrate
|
||||
if self._serial.timeout != connection_timeout:
|
||||
self._serial.timeout = connection_timeout
|
||||
self._log("Trying baudrate: %d" % (baudrate))
|
||||
self._baudrateDetectRetry = 5
|
||||
self._timeout = get_new_timeout("communicationBusy" if self._busy_protocol_detected else "communication", self._timeout_intervals)
|
||||
self._serial.write('\n')
|
||||
self.sayHello(tags={"trigger:baudrate_detection",})
|
||||
except:
|
||||
self._log("Unexpected error while setting baudrate {}: {}".format(baudrate, get_exception_string()))
|
||||
self._logger.exception("Unexpceted error while setting baudrate {}".format(baudrate))
|
||||
else:
|
||||
error_text = "No more baudrates to test, and no suitable baudrate found."
|
||||
self._trigger_error(error_text, "autodetect_baudrate")
|
||||
elif 'start' in line or 'ok' in line:
|
||||
self._perform_baudrate_detection_step()
|
||||
elif 'start' in line or line.startswith('ok'):
|
||||
self._onConnected()
|
||||
if 'start' in line:
|
||||
self._clear_to_send.set()
|
||||
@@ -2042,6 +2022,43 @@ class MachineCom(object):
|
||||
self._log(message + " " + general_message)
|
||||
self._clear_to_send.set()
|
||||
|
||||
def _perform_baudrate_detection_step(self, init=False):
|
||||
if init:
|
||||
timeout = settings().getFloat(["serial", "timeout", "connection"])
|
||||
self._baudrateDetectList = baudrateList()
|
||||
else:
|
||||
timeout = settings().getFloat(["serial", "timeout", "detection"])
|
||||
|
||||
if self._baudrateDetectRetry > 0:
|
||||
if self._serial.timeout != timeout:
|
||||
self._serial.timeout = timeout
|
||||
self._timeout = time.time() + timeout
|
||||
|
||||
self._log("Baudrate test retry #{}".format(5 - self._baudrateDetectRetry))
|
||||
self._baudrateDetectRetry -= 1
|
||||
self._do_send_without_checksum("", log=False) # new line to reset things
|
||||
self.sayHello(tags={"trigger:baudrate_detection", })
|
||||
|
||||
elif len(self._baudrateDetectList) > 0:
|
||||
baudrate = self._baudrateDetectList.pop(0)
|
||||
try:
|
||||
self._serial.baudrate = baudrate
|
||||
if self._serial.timeout != timeout:
|
||||
self._serial.timeout = timeout
|
||||
self._timeout = time.time() + timeout
|
||||
|
||||
self._log("Trying baudrate: {}".format(baudrate))
|
||||
self._baudrateDetectRetry = 4
|
||||
self._do_send_without_checksum("", log=False) # new line to reset things
|
||||
self.sayHello(tags={"trigger:baudrate_detection", })
|
||||
except:
|
||||
self._log("Unexpected error while setting baudrate {}: {}".format(baudrate, get_exception_string()))
|
||||
self._logger.exception("Unexpected error while setting baudrate {}".format(baudrate))
|
||||
|
||||
else:
|
||||
error_text = "No more baudrates to test, and no suitable baudrate found."
|
||||
self._trigger_error(error_text, "autodetect_baudrate")
|
||||
|
||||
def _finish_heatup(self):
|
||||
if self._heatupWaitStartTime:
|
||||
self._heatupWaitTimeLost = self._heatupWaitTimeLost + (time.time() - self._heatupWaitStartTime)
|
||||
@@ -2284,7 +2301,7 @@ class MachineCom(object):
|
||||
if baudrate == 0:
|
||||
baudrates = baudrateList()
|
||||
serial_obj = serial.Serial(str(port),
|
||||
115200 if 115200 in baudrates else baudrates[0],
|
||||
baudrates[0],
|
||||
timeout=read_timeout,
|
||||
writeTimeout=10000, # TODO 1.3.8 rename to write_timeout for pyserial >= 3.x
|
||||
parity=serial.PARITY_ODD)
|
||||
@@ -2984,11 +3001,12 @@ class MachineCom(object):
|
||||
command_to_send = command_to_send + "*" + str(checksum)
|
||||
self._do_send_without_checksum(command_to_send)
|
||||
|
||||
def _do_send_without_checksum(self, cmd):
|
||||
def _do_send_without_checksum(self, cmd, log=True):
|
||||
if self._serial is None:
|
||||
return
|
||||
|
||||
self._log("Send: " + str(cmd))
|
||||
if log:
|
||||
self._log("Send: " + str(cmd))
|
||||
|
||||
cmd += "\n"
|
||||
written = 0
|
||||
|
||||
Reference in new issue
Block a user