printrun-src/printrun/serialWrapper.py

Fri, 03 Jun 2016 10:06:09 +0200

author
mbayer
date
Fri, 03 Jun 2016 10:06:09 +0200
changeset 18
11f6d97f83b0
parent 15
0bbb006204fc
permissions
-rw-r--r--

forgot svg preamble & postamble

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

mercurial