♻ Turn lines read from serial into unicode
Backported from devel branch. Should also fix #3091
This commit is contained in:
1 parent
d1914824c6
commit
c879ad27f4
2 files changed
+16
-14
No files matched your search
@@ -18,7 +18,7 @@ from serial import SerialTimeoutException
|
||||
|
||||
from octoprint.settings import settings
|
||||
from octoprint.plugin import plugin_manager
|
||||
from octoprint.util import RepeatedTimer, monotonic_time
|
||||
from octoprint.util import RepeatedTimer, monotonic_time, to_bytes
|
||||
|
||||
class VirtualPrinter(object):
|
||||
command_regex = re.compile("^([GMTF])(\d+)")
|
||||
@@ -45,7 +45,7 @@ class VirtualPrinter(object):
|
||||
self._seriallog.addHandler(seriallog_handler)
|
||||
self._seriallog.setLevel(logging.INFO)
|
||||
|
||||
self._seriallog.info("-"*78)
|
||||
self._seriallog.info(u"-"*78)
|
||||
|
||||
self._read_timeout = read_timeout
|
||||
self._write_timeout = write_timeout
|
||||
@@ -1379,13 +1379,13 @@ class VirtualPrinter(object):
|
||||
return 0
|
||||
|
||||
if "M112" in data and self._supportM112:
|
||||
self._seriallog.info("<<< {}".format(data.strip()))
|
||||
self._seriallog.info(u"<<< {}".format(data.strip()))
|
||||
self._kill()
|
||||
return len(data)
|
||||
|
||||
try:
|
||||
written = self.incoming.put(data, timeout=self._write_timeout, partial=True)
|
||||
self._seriallog.info("<<< {}".format(data.strip()))
|
||||
self._seriallog.info(u"<<< {}".format(data.strip()))
|
||||
return written
|
||||
except queue.Full:
|
||||
self._logger.info("Incoming queue is full, raising SerialTimeoutException")
|
||||
@@ -1421,9 +1421,9 @@ class VirtualPrinter(object):
|
||||
try:
|
||||
# fetch a line from the queue, wait no longer than timeout
|
||||
line = self.outgoing.get(timeout=timeout)
|
||||
self._seriallog.info(">>> {}".format(line.strip()))
|
||||
self._seriallog.info(u">>> {}".format(line.strip()))
|
||||
self.outgoing.task_done()
|
||||
return line
|
||||
return to_bytes(line, errors="replace")
|
||||
except queue.Empty:
|
||||
# queue empty? return empty line
|
||||
return ""
|
||||
|
||||
@@ -636,6 +636,8 @@ class MachineCom(object):
|
||||
self._log(message)
|
||||
|
||||
def _log(self, message):
|
||||
message = to_unicode(message)
|
||||
|
||||
self._terminal_log.append(message)
|
||||
self._callback.on_comm_log(message)
|
||||
self._serialLogger.debug(message)
|
||||
@@ -2737,17 +2739,17 @@ class MachineCom(object):
|
||||
self.close(is_error=True)
|
||||
return None
|
||||
|
||||
if ret != "":
|
||||
try:
|
||||
self._log("Recv: " + sanitize_ascii(ret))
|
||||
except ValueError as e:
|
||||
self._log("WARN: While reading last line: %s" % e)
|
||||
self._log("Recv: " + repr(ret))
|
||||
try:
|
||||
ret = ret.decode('utf-8')
|
||||
except UnicodeDecodeError:
|
||||
ret = ret.decode('latin1')
|
||||
|
||||
self._log(u"Recv: {}".format(ret))
|
||||
|
||||
for name, hook in self._received_message_hooks.items():
|
||||
try:
|
||||
ret = hook(self, ret)
|
||||
except:
|
||||
except Exception:
|
||||
self._logger.exception("Error while processing hook {name}:".format(**locals()))
|
||||
else:
|
||||
if ret is None:
|
||||
@@ -3338,7 +3340,7 @@ class MachineCom(object):
|
||||
return
|
||||
|
||||
if log:
|
||||
self._log("Send: " + str(cmd))
|
||||
self._log(u"Send: {}".format(to_unicode(cmd, errors="replace")))
|
||||
|
||||
cmd += "\n"
|
||||
written = 0
|
||||
|
||||
Reference in new issue
Block a user