chiark / gitweb /
Add a wrapper around serial to monkey patch support for custom baudrates
authorYouness Alaoui <kakaroto@kakaroto.homelinux.net>
Mon, 30 Nov 2015 21:17:55 +0000 (16:17 -0500)
committernickthetait <tait@alephobjects.com>
Mon, 11 Jan 2016 15:10:18 +0000 (08:10 -0700)
This fixes T316.
The monkey patch (https://en.wikipedia.org/wiki/Monkey_patch) replaces
the set_special_baudrate from 2.6 by the function from 2.7 which fixes
setting 250000 baudrate on linux.
See T316 for more details on the actual patch

Cura/gui/firmwareInstall.py
Cura/util/machineCom.py
Cura/util/serialWrapper.py [new file with mode: 0644]

index 722a293be798b78fd98f04d7eb1d443eba229d7c..da325dd81e190a5a9b136c2c1d7f719b7a7fd6a4 100644 (file)
@@ -5,7 +5,8 @@ import wx
 import threading
 import sys
 import time
-import serial
+
+from Cura.util import serialWrapper as serial
 
 from Cura.avr_isp import stk500v2
 from Cura.avr_isp import ispBase
index 1245c53aabf4e878ca472d52c4782b0182208fd2..1a52911e2cd63892b30a056aafcbd192e510d9d4 100644 (file)
@@ -15,7 +15,7 @@ import threading
 import platform
 import Queue as queue
 
-import serial
+from Cura.util import serialWrapper as serial
 
 from Cura.avr_isp import stk500v2
 from Cura.avr_isp import ispBase
diff --git a/Cura/util/serialWrapper.py b/Cura/util/serialWrapper.py
new file mode 100644 (file)
index 0000000..f41c686
--- /dev/null
@@ -0,0 +1,38 @@
+# Serial wrapper around pyserial that adds support for custom baudrates (250000)
+# on linux, when pyserial is < 2.7
+
+from serial import *
+import os
+if os.name == 'posix':
+       import serial.serialposix
+
+       if not hasattr(serial.serialposix, "TCGETS2") and \
+          hasattr(serial.serialposix, "set_special_baudrate"):
+               # Detected pyserial < 2.7 which doesn't support custom baudrates
+               # Replacing set_special_baudrate with updated function from pyserial 2.7
+
+               TCGETS2 = 0x802C542A
+               TCSETS2 = 0x402C542B
+               BOTHER = 0o010000
+
+               def set_special_baudrate(port, baudrate):
+                       # right size is 44 on x86_64, allow for some growth
+                       import array
+                       buf = array.array('i', [0] * 64)
+
+                       try:
+                               # get serial_struct
+                               FCNTL.ioctl(port.fd, TCGETS2, buf)
+                               # set custom speed
+                               buf[2] &= ~TERMIOS.CBAUD
+                               buf[2] |= BOTHER
+                               buf[9] = buf[10] = baudrate
+
+                               # set serial_struct
+                               res = FCNTL.ioctl(port.fd, TCSETS2, buf)
+                       except IOError, e:
+                               raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e))
+
+               # We need to change the function inside the serialposix module otherwise, it won't
+               # be called by the code within that module
+               serial.serialposix.set_special_baudrate = set_special_baudrate