1
1
from __future__ import print_function
2
2
3
3
from pi_mqtt_gpio .modules import GenericStream
4
+ import serial
4
5
from serial import Serial
5
6
6
7
REQUIREMENTS = ("serial" ,)
7
8
CONFIG_SCHEMA = {
8
9
"device" : {"type" : "string" , "required" : True , "empty" : False },
9
10
"baud" : {"type" : "integer" , "required" : True , "empty" : False },
11
+ "bytesize" : {"type" : "integer" , "required" : True , "empty" : False },
12
+ "parity" : {"type" : "string" , "required" : True , "empty" : False },
13
+ "stopbits" : {"type" : "float" , "required" : True , "empty" : False },
10
14
}
11
15
12
16
PORTS_USED = {}
13
17
18
+ BYTESIZE = {
19
+ 5 : serial .FIVEBITS ,
20
+ 6 : serial .SIXBITS ,
21
+ 7 : serial .SEVENBITS ,
22
+ 8 : serial .EIGHTBITS
23
+ }
24
+
25
+ PARITY = {
26
+ "none" : serial .PARITY_NONE ,
27
+ "odd" : serial .PARITY_ODD ,
28
+ "even" : serial .PARITY_EVEN ,
29
+ "mark" : serial .PARITY_MARK ,
30
+ "space" : serial .PARITY_SPACE
31
+ }
32
+
33
+ STOPBITS = {
34
+ 1 : serial .STOPBITS_ONE ,
35
+ 1.5 : serial .STOPBITS_ONE_POINT_FIVE ,
36
+ 2 : serial .STOPBITS_TWO
37
+ }
38
+
14
39
class Stream (GenericStream ):
15
40
"""
16
41
Implementation of stream class for outputting to STDIO.
@@ -20,7 +45,19 @@ def __init__(self, config):
20
45
global PORTS_USED
21
46
#print("__init__(config=%r)" % config)
22
47
if not config ['device' ] in PORTS_USED :
23
- self .ser = Serial (config ['device' ], config ['baud' ], timeout = 20 )
48
+
49
+ if not config ['bytesize' ] in BYTESIZE .keys ():
50
+ raise Exception ("bytesize not one of: " + str (BYTESIZE .keys ()))
51
+ if not config ['parity' ] in PARITY .keys ():
52
+ raise Exception ("parity not one of:" + str (PARITY .keys ()))
53
+ if not config ['stopbits' ] in STOPBITS .keys ():
54
+ raise Exception ("stopbits not one of: " + str (STOPBITS .keys ()))
55
+
56
+ bytesize = BYTESIZE [config ['bytesize' ]]
57
+ parity = PARITY [config ['parity' ]]
58
+ stopbits = STOPBITS [config ['stopbits' ]]
59
+
60
+ self .ser = Serial (port = config ['device' ], baudrate = config ['baud' ], bytesize = bytesize , parity = parity , stopbits = stopbits , timeout = 20 )
24
61
self .ser .flushInput ()
25
62
PORTS_USED [config ['device' ]] = self .ser
26
63
else :
0 commit comments