Skip to content

Instantly share code, notes, and snippets.

@foosel foosel/serial_double_open.py Secret
Created Jan 22, 2018

Embed
What would you like to do?
Minimal OctoPrint plugin that performs a double open on the serial port to potentially work around issues with Malyan printers. Put file into ~/.octoprint/plugins and restart the server. Based on https://github.com/foosel/OctoPrint/issues/2271#issuecomment-352662485
# coding=utf-8
from __future__ import absolute_import
import serial
import logging
import sys
import octoprint.plugin
from octoprint.events import Events, eventManager
def serial_factory(comm_instance, port, baudrate, read_timeout, *args, **kwargs):
"""
This is basically the default serial factory, just with a weird open/open/close sequence applied
Performs
OPEN Parity.ODD
OPEN Parity.NONE
CLOSE Parity.ODD
USE Parity.NONE
See https://github.com/foosel/OctoPrint/issues/2271#issuecomment-352662485
"""
if sys.platform == "win32":
# Windows doesn't allow double open, log and bail
logging.getLogger("octoprint.plugins.serial_double_open").error("Cannot run serial double open under windows")
return None
if port is None or port == 'AUTO':
# no known port, try auto detection
comm_instance._changeState(comm_instance.STATE_DETECT_SERIAL)
port = comm_instance._detect_port()
if port is None:
comm_instance._errorValue = 'Failed to autodetect serial port, please set it manually.'
comm_instance._changeState(comm_instance.STATE_ERROR)
eventManager().fire(Events.ERROR, {"error": comm_instance.getErrorString()})
comm_instance._log("Failed to autodetect serial port, please set it manually.")
return None
# connect to regular serial port
comm_instance._log("Connecting to: %s" % port)
if baudrate == 0:
from octoprint.util.comm import baudrateList
baudrates = baudrateList()
baudrate = 115200 if 115200 in baudrates else baudrates[0]
serial_obj1 = serial.Serial(str(port), baudrate, timeout=read_timeout, writeTimeout=10000,
parity=serial.PARITY_ODD)
serial_obj2 = serial.Serial(str(port), baudrate, timeout=read_timeout, writeTimeout=10000,
parity=serial.PARITY_NONE)
serial_obj1.close() # close the first instance, we don't actually need that
return serial_obj2 # return the second instance
__plugin_name__ = "Serial Double Open Plugin"
__plugin_description__ = "Performs a double open on the serial port to potentially work around issues with Malyan printers"
__plugin_author__ = "Gina Häußge"
__plugin_homepage__ = "https://github.com/foosel/OctoPrint/issues/2271#issuecomment-352662485"
__plugin_hooks__ = {
"octoprint.comm.transport.serial.factory": serial_factory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.