Skip to content
Snippets Groups Projects
Commit fa75c3f2 authored by ChrisEAlfred's avatar ChrisEAlfred
Browse files

Added bytesize,parity,stopbits parameters

parent 696ea3bd
No related branches found
No related tags found
No related merge requests found
......@@ -189,6 +189,9 @@ stream_modules:
module: streamserial
device: /dev/ttyS0
baud: 115200
bytesize: 8 # Number of data bits in word. Can be: 5,6,7,8
parity: none # Parity can be one of none,odd,even,mark,space
stopbits: 1 # Number of stop bits. Can be: 1,1.5,2
cleanup: no # This optional boolean value sets whether the module's `cleanup()` function will be called when the software exits.
stream_reads:
......
from __future__ import print_function
from pi_mqtt_gpio.modules import GenericStream
import serial
from serial import Serial
REQUIREMENTS = ("serial",)
CONFIG_SCHEMA = {
"device": {"type": "string", "required": True, "empty": False},
"baud": {"type": "integer", "required": True, "empty": False},
"bytesize": {"type": "integer", "required": True, "empty": False},
"parity": {"type": "string", "required": True, "empty": False},
"stopbits": {"type": "float", "required": True, "empty": False},
}
PORTS_USED = {}
BYTESIZE = {
5: serial.FIVEBITS,
6: serial.SIXBITS,
7: serial.SEVENBITS,
8: serial.EIGHTBITS
}
PARITY = {
"none": serial.PARITY_NONE,
"odd": serial.PARITY_ODD,
"even": serial.PARITY_EVEN,
"mark": serial.PARITY_MARK,
"space": serial.PARITY_SPACE
}
STOPBITS = {
1: serial.STOPBITS_ONE,
1.5: serial.STOPBITS_ONE_POINT_FIVE,
2: serial.STOPBITS_TWO
}
class Stream(GenericStream):
"""
Implementation of stream class for outputting to STDIO.
......@@ -20,7 +45,19 @@ class Stream(GenericStream):
global PORTS_USED
#print("__init__(config=%r)" % config)
if not config['device'] in PORTS_USED:
self.ser = Serial(config['device'], config['baud'], timeout=20)
if not config['bytesize'] in BYTESIZE.keys():
raise Exception("bytesize not one of: " + str(BYTESIZE.keys()))
if not config['parity'] in PARITY.keys():
raise Exception("parity not one of:" + str(PARITY.keys()))
if not config['stopbits'] in STOPBITS.keys():
raise Exception("stopbits not one of: " + str(STOPBITS.keys()))
bytesize = BYTESIZE[config['bytesize']]
parity = PARITY[config['parity']]
stopbits = STOPBITS[config['stopbits']]
self.ser = Serial(port=config['device'], baudrate=config['baud'], bytesize=bytesize, parity=parity, stopbits=stopbits, timeout=20)
self.ser.flushInput()
PORTS_USED[config['device']] = self.ser
else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment