1
0
mirror of https://github.com/myhdl/myhdl.git synced 2024-12-14 07:44:38 +08:00
myhdl/example/rs232/rs232_util.py
2003-01-30 22:17:15 +00:00

40 lines
750 B
Python

import operator
sec = 1e9
ODD, EVEN, MARK, SPACE = range(4)
class Error(Exception):
pass
class ParityError(Error):
pass
class StopBitError(Error):
pass
class Config(object):
__slots__ = ("baud_rate", "n_bits", "n_stops", "parity")
def __init__(self, baud_rate=9600, n_bits=8, n_stops=1, parity=None):
self.baud_rate = baud_rate
self.n_bits = n_bits
self.n_stops = n_stops
self.parity = parity
def parity(data, cfg):
if cfg == ODD:
return not reduceXor(data[8:])
elif cfg== EVEN:
return reduceXor(data[8:])
elif cfg == MARK:
return 1
elif cfg == SPACE:
return 0
def reduceXor(bv):
return reduce(operator.xor, [b for b in bv])