chiark / gitweb /
Look for CuraEngine in ..../CuraEngine/build/CuraEngine[.exe]
[cura.git] / Cura / util / serialWrapper.py
1 # Serial wrapper around pyserial that adds support for custom baudrates (250000)
2 # on linux, when pyserial is < 2.7
3
4 from serial import *
5 import os
6 if os.name == 'posix':
7         import serial.serialposix
8
9         if not hasattr(serial.serialposix, "TCGETS2") and \
10            hasattr(serial.serialposix, "set_special_baudrate"):
11                 # Detected pyserial < 2.7 which doesn't support custom baudrates
12                 # Replacing set_special_baudrate with updated function from pyserial 2.7
13
14                 TCGETS2 = 0x802C542A
15                 TCSETS2 = 0x402C542B
16                 BOTHER = 0o010000
17
18                 def set_special_baudrate(port, baudrate):
19                         # right size is 44 on x86_64, allow for some growth
20                         import array
21                         buf = array.array('i', [0] * 64)
22
23                         try:
24                                 # get serial_struct
25                                 FCNTL.ioctl(port.fd, TCGETS2, buf)
26                                 # set custom speed
27                                 buf[2] &= ~TERMIOS.CBAUD
28                                 buf[2] |= BOTHER
29                                 buf[9] = buf[10] = baudrate
30
31                                 # set serial_struct
32                                 res = FCNTL.ioctl(port.fd, TCSETS2, buf)
33                         except IOError, e:
34                                 raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e))
35
36                 # We need to change the function inside the serialposix module otherwise, it won't
37                 # be called by the code within that module
38                 serial.serialposix.set_special_baudrate = set_special_baudrate