Skip to content

Commit b341beb

Browse files
author
Benji
committed
Fixing imports and adding support for hex streams
1 parent fa75c3f commit b341beb

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

pi_mqtt_gpio/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@
331331
type: boolean
332332
required: no
333333
default: no
334+
encoding:
335+
type: string
336+
required: no
334337
interval:
335338
type: float
336339
required: no

pi_mqtt_gpio/modules/streamserial.py

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,54 @@
11
from __future__ import print_function
22

33
from pi_mqtt_gpio.modules import GenericStream
4-
import serial
5-
from serial import Serial
64

7-
REQUIREMENTS = ("serial",)
5+
REQUIREMENTS = ("pyserial",)
86
CONFIG_SCHEMA = {
97
"device": {"type": "string", "required": True, "empty": False},
108
"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},
1412
}
1513

1614
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
3818

3919
class Stream(GenericStream):
4020
"""
4121
Implementation of stream class for outputting to STDIO.
4222
"""
4323

4424
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+
4752
if not config['device'] in PORTS_USED:
4853

4954
if not config['bytesize'] in BYTESIZE.keys():
@@ -57,25 +62,27 @@ def __init__(self, config):
5762
parity = PARITY[config['parity']]
5863
stopbits = STOPBITS[config['stopbits']]
5964

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)
6166
self.ser.flushInput()
6267
PORTS_USED[config['device']] = self.ser
6368
else:
6469
self.ser = PORTS_USED[config['device']]
6570

6671
def setup_stream(self, config):
67-
#print("setup_stream(config=%r)" % config)
72+
print("setup_stream(config=%r)" % config)
6873
pass
6974

7075
def read(self, config):
7176
if (self.ser.inWaiting() <= 0):
7277
return None
7378
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))
7582
return data
7683

7784
def write(self, config, data):
78-
#print("write(config=%r, data=%s)" % (config,data))
85+
print("write(config=%r, data=%s)" % (config,data))
7986
self.ser.write(data)
8087
pass
8188

0 commit comments

Comments
 (0)