Skip to content

Commit fa75c3f

Browse files
committed
Added bytesize,parity,stopbits parameters
1 parent 696ea3b commit fa75c3f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ stream_modules:
189189
module: streamserial
190190
device: /dev/ttyS0
191191
baud: 115200
192+
bytesize: 8 # Number of data bits in word. Can be: 5,6,7,8
193+
parity: none # Parity can be one of none,odd,even,mark,space
194+
stopbits: 1 # Number of stop bits. Can be: 1,1.5,2
192195
cleanup: no # This optional boolean value sets whether the module's `cleanup()` function will be called when the software exits.
193196

194197
stream_reads:

pi_mqtt_gpio/modules/streamserial.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
from __future__ import print_function
22

33
from pi_mqtt_gpio.modules import GenericStream
4+
import serial
45
from serial import Serial
56

67
REQUIREMENTS = ("serial",)
78
CONFIG_SCHEMA = {
89
"device": {"type": "string", "required": True, "empty": False},
910
"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},
1014
}
1115

1216
PORTS_USED = {}
1317

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+
1439
class Stream(GenericStream):
1540
"""
1641
Implementation of stream class for outputting to STDIO.
@@ -20,7 +45,19 @@ def __init__(self, config):
2045
global PORTS_USED
2146
#print("__init__(config=%r)" % config)
2247
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)
2461
self.ser.flushInput()
2562
PORTS_USED[config['device']] = self.ser
2663
else:

0 commit comments

Comments
 (0)