Thu, 23 Aug 2018 12:32:26 +0200
SVG gcode generator: interpret color style if present
15 | 1 | # Serial wrapper around pyserial that adds support for custom baudrates (250000) |
2 | # on linux, when pyserial is < 2.7 | |
3 | ||
4 | # This code was copied from the pyserial 2.7 base code. | |
5 | # Therefore, it follows the license used by pyserial which is the '3-clause BSD license' | |
6 | ||
7 | from serial import * | |
8 | import sys | |
9 | ||
10 | if sys.platform.startswith('linux'): | |
11 | import serial.serialposix | |
12 | ||
13 | try: | |
14 | import pkg_resources | |
15 | ||
16 | old_version = float(pkg_resources.get_distribution("pyserial").version) < 2.7 | |
17 | except: | |
18 | old_version = True | |
19 | ||
20 | if old_version and not hasattr(serial.serialposix, "TCGETS2") and \ | |
21 | hasattr(serial.serialposix, "set_special_baudrate"): | |
22 | # Detected pyserial < 2.7 which doesn't support custom baudrates | |
23 | # Replacing set_special_baudrate with updated function from pyserial 2.7 | |
24 | ||
25 | TCGETS2 = 0x802C542A | |
26 | TCSETS2 = 0x402C542B | |
27 | BOTHER = 0o010000 | |
28 | ||
29 | def set_special_baudrate(port, baudrate): | |
30 | # right size is 44 on x86_64, allow for some growth | |
31 | import array | |
32 | buf = array.array('i', [0] * 64) | |
33 | ||
34 | try: | |
35 | # get serial_struct | |
36 | FCNTL.ioctl(port.fd, TCGETS2, buf) | |
37 | # set custom speed | |
38 | buf[2] &= ~TERMIOS.CBAUD | |
39 | buf[2] |= BOTHER | |
40 | buf[9] = buf[10] = baudrate | |
41 | ||
42 | # set serial_struct | |
43 | res = FCNTL.ioctl(port.fd, TCSETS2, buf) | |
44 | except IOError, e: | |
45 | raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e)) | |
46 | ||
47 | # We need to change the function inside the serialposix module otherwise, it won't | |
48 | # be called by the code within that module | |
49 | serial.serialposix.set_special_baudrate = set_special_baudrate |