1
1
from __future__ import print_function
2
2
3
3
from pi_mqtt_gpio .modules import GenericStream
4
- import serial
5
- from serial import Serial
6
4
7
- REQUIREMENTS = ("serial " ,)
5
+ REQUIREMENTS = ("pyserial " ,)
8
6
CONFIG_SCHEMA = {
9
7
"device" : {"type" : "string" , "required" : True , "empty" : False },
10
8
"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 },
9
+ "bytesize" : {"type" : "integer" , "required" : False , "default" : 8 , "empty" : False },
10
+ "parity" : {"type" : "string" , "required" : False , "default" : "none" , "empty" : False },
11
+ "stopbits" : {"type" : "float" , "required" : False , "default" : 1 , "empty" : False },
14
12
}
15
13
16
14
PORTS_USED = {}
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
- }
15
+ BYTESIZE = None
16
+ PARITY = None
17
+ STOPBITS = None
38
18
39
19
class Stream (GenericStream ):
40
20
"""
41
21
Implementation of stream class for outputting to STDIO.
42
22
"""
43
23
44
24
def __init__ (self , config ):
45
- global PORTS_USED
46
- #print("__init__(config=%r)" % config)
25
+ global PORTS_USED , BYTESIZE , PARITY , STOPBITS
26
+
27
+ import serial
28
+
29
+ print ("__init__(config=%r)" % config )
30
+
31
+ BYTESIZE = {
32
+ 5 : serial .FIVEBITS ,
33
+ 6 : serial .SIXBITS ,
34
+ 7 : serial .SEVENBITS ,
35
+ 8 : serial .EIGHTBITS
36
+ }
37
+
38
+ PARITY = {
39
+ "none" : serial .PARITY_NONE ,
40
+ "odd" : serial .PARITY_ODD ,
41
+ "even" : serial .PARITY_EVEN ,
42
+ "mark" : serial .PARITY_MARK ,
43
+ "space" : serial .PARITY_SPACE
44
+ }
45
+
46
+ STOPBITS = {
47
+ 1 : serial .STOPBITS_ONE ,
48
+ 1.5 : serial .STOPBITS_ONE_POINT_FIVE ,
49
+ 2 : serial .STOPBITS_TWO
50
+ }
51
+
47
52
if not config ['device' ] in PORTS_USED :
48
53
49
54
if not config ['bytesize' ] in BYTESIZE .keys ():
@@ -57,25 +62,27 @@ def __init__(self, config):
57
62
parity = PARITY [config ['parity' ]]
58
63
stopbits = STOPBITS [config ['stopbits' ]]
59
64
60
- self .ser = Serial (port = config ['device' ], baudrate = config ['baud' ], bytesize = bytesize , parity = parity , stopbits = stopbits , timeout = 20 )
65
+ self .ser = serial . Serial (port = config ['device' ], baudrate = config ['baud' ], bytesize = bytesize , parity = parity , stopbits = stopbits , timeout = 20 )
61
66
self .ser .flushInput ()
62
67
PORTS_USED [config ['device' ]] = self .ser
63
68
else :
64
69
self .ser = PORTS_USED [config ['device' ]]
65
70
66
71
def setup_stream (self , config ):
67
- # print("setup_stream(config=%r)" % config)
72
+ print ("setup_stream(config=%r)" % config )
68
73
pass
69
74
70
75
def read (self , config ):
71
76
if (self .ser .inWaiting () <= 0 ):
72
77
return None
73
78
data = self .ser .read (self .ser .inWaiting ()).decode ('string_escape' )
74
- #print("read(config=%r) = %s" % (config, data))
79
+ if (config .get ('encoding' )):
80
+ data = data .encode (config ['encoding' ])
81
+ print ("read(config=%r) = %s" % (config , data ))
75
82
return data
76
83
77
84
def write (self , config , data ):
78
- # print("write(config=%r, data=%s)" % (config,data))
85
+ print ("write(config=%r, data=%s)" % (config ,data ))
79
86
self .ser .write (data )
80
87
pass
81
88
0 commit comments