Cleaned up control flow
This commit is contained in:
parent
699424ed7c
commit
bf59b55ef6
|
@ -12,11 +12,6 @@ CLEARCHANNEL = 48
|
||||||
|
|
||||||
|
|
||||||
class SSS7Bus(object):
|
class SSS7Bus(object):
|
||||||
ERR_SUCCESS = 0
|
|
||||||
ERR_TIMEOUT_ON_LENGTH = -1
|
|
||||||
ERR_TIMEOUT_ON_PAYLOAD = -2
|
|
||||||
ERR_CRC_ERRROR = -3
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, port, baudrate=9600):
|
def __init__(self, port, baudrate=9600):
|
||||||
self._bit_time = 1.0 / baudrate
|
self._bit_time = 1.0 / baudrate
|
||||||
|
@ -25,74 +20,82 @@ class SSS7Bus(object):
|
||||||
|
|
||||||
self._buffer = []
|
self._buffer = []
|
||||||
|
|
||||||
|
|
||||||
def _debug(self, msg):
|
def _debug(self, msg):
|
||||||
print "[SSS7Bus] %s" % msg
|
print "[SSS7Bus] %s" % msg
|
||||||
|
|
||||||
|
|
||||||
def _read_frame(self, clear_channel=False):
|
|
||||||
if clear_channel :
|
|
||||||
self._serial.timeout = self._bit_time * CLEARCHANNEL
|
|
||||||
self._debug("Checking if bus is idle for %f seconds" % (self._bit_time * CLEARCHANNEL))
|
|
||||||
else:
|
|
||||||
self._debug("Trying to read a frame from the bus")
|
|
||||||
self._serial.timeout = timeout=self._bit_time * TIMEOUT
|
|
||||||
|
|
||||||
length_byte = self._serial.read(1)
|
|
||||||
if len(length_byte) == 0:
|
|
||||||
if clear_channel:
|
|
||||||
self._debug("Bus was idle")
|
|
||||||
else:
|
|
||||||
self._debug("Timeout reading frame length")
|
|
||||||
return self.ERR_TIMEOUT_ON_LENGTH
|
|
||||||
|
|
||||||
if clear_channel:
|
|
||||||
self._debug("Bus was not idle, trying to read frame")
|
|
||||||
|
|
||||||
length = ord(length_byte) - 1 # we read the size length byte already
|
|
||||||
|
|
||||||
self._serial.timeout = timeout=self._bit_time * TIMEOUT
|
|
||||||
|
|
||||||
self._debug("Trying to read remaing %d bytes of frame" % length)
|
|
||||||
data = self._serial.read(length)
|
|
||||||
|
|
||||||
# if read returns less then length, no new byte has been received for timeout seconds
|
|
||||||
if len(data) != length:
|
|
||||||
self._debug("Timeout reading frame payload")
|
|
||||||
return self.ERR_TIMEOUT_ON_PAYLOAD
|
|
||||||
|
|
||||||
crc = data[-2:]
|
|
||||||
payload = data[:-2]
|
|
||||||
|
|
||||||
crc16 = PredefinedCrc('crc16')
|
|
||||||
crc16.update(payload)
|
|
||||||
real_crc = crc16.digest()
|
|
||||||
|
|
||||||
if real_crc != crc:
|
|
||||||
self._debug("Wrong crc for frame payload")
|
|
||||||
return self.ERR_CRC_ERRROR
|
|
||||||
|
|
||||||
self._debug("Sucessfully read rest of frame")
|
|
||||||
self._buffer.insert(0,data)
|
|
||||||
|
|
||||||
return self.ERR_SUCCESS
|
|
||||||
|
|
||||||
|
|
||||||
def _flush_input_buffer(self):
|
def _flush_input_buffer(self):
|
||||||
while self._serial.inWaiting() > 0:
|
while self._serial.inWaiting() > 0:
|
||||||
self._debug("Flushing input buffer")
|
self._debug("Flushing input buffer")
|
||||||
self._read_frame()
|
self._read_frame()
|
||||||
|
|
||||||
|
|
||||||
def _can_send(self):
|
def _can_send(self):
|
||||||
self._flush_input_buffer()
|
self._flush_input_buffer()
|
||||||
return self._read_frame(True) == self.ERR_TIMEOUT_ON_LENGTH
|
|
||||||
|
|
||||||
|
self._serial.timeout = self._bit_time * CLEARCHANNEL
|
||||||
|
self._debug("Checking if bus is idle for %f seconds" % (self._bit_time * CLEARCHANNEL))
|
||||||
|
|
||||||
|
length_byte = self._serial.read(1)
|
||||||
|
if len(length_byte) == 0:
|
||||||
|
self._debug("Bus seems idle")
|
||||||
|
return True
|
||||||
|
|
||||||
|
self._debug("Bus is not idle, reading frames")
|
||||||
|
self._read_frame_rest(length_byte)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _read_frame(self):
|
||||||
|
self._debug("Trying to read a frame from the bus")
|
||||||
|
self._serial.timeout = timeout=self._bit_time * TIMEOUT
|
||||||
|
|
||||||
|
length_byte = self._serial.read(1)
|
||||||
|
if len(length_byte) == 0:
|
||||||
|
self._debug("Timeout reading frame length")
|
||||||
|
return False
|
||||||
|
|
||||||
|
return self._read_frame_rest(length_byte)
|
||||||
|
|
||||||
|
|
||||||
|
def _read_frame_rest(self, length_byte):
|
||||||
|
length = ord(length_byte) - 1 # we read the size length byte already
|
||||||
|
|
||||||
|
self._serial.timeout = timeout=self._bit_time * TIMEOUT
|
||||||
|
|
||||||
|
self._debug("Trying to read remaing %d bytes of frame" % length)
|
||||||
|
data = self._serial.read(length)
|
||||||
|
|
||||||
|
# if read returns less then length, no new byte has been received for timeout seconds
|
||||||
|
if len(data) != length:
|
||||||
|
self._debug("Timeout reading frame payload")
|
||||||
|
return False
|
||||||
|
|
||||||
|
crc = data[-2:]
|
||||||
|
payload = data[:-2]
|
||||||
|
|
||||||
|
crc16 = PredefinedCrc('crc16')
|
||||||
|
crc16.update(payload)
|
||||||
|
real_crc = crc16.digest()
|
||||||
|
|
||||||
|
if real_crc != crc:
|
||||||
|
self._debug("Wrong crc for frame payload")
|
||||||
|
return True
|
||||||
|
|
||||||
|
self._debug("Sucessfully read rest of frame")
|
||||||
|
self._buffer.insert(0,data)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _send_byte(self, byte):
|
def _send_byte(self, byte):
|
||||||
self._serial.write(byte)
|
self._serial.write(byte)
|
||||||
return byte == self._serial.read(1)
|
return byte == self._serial.read(1)
|
||||||
|
|
||||||
|
|
||||||
def _send_frame(self, frame):
|
def _send_frame(self, frame):
|
||||||
while not self._can_send():
|
while not self._can_send():
|
||||||
pass
|
pass
|
||||||
|
@ -102,12 +105,15 @@ class SSS7Bus(object):
|
||||||
if not result:
|
if not result:
|
||||||
self._debug("Sending frame failed with collision")
|
self._debug("Sending frame failed with collision")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self._debug("Successfully send frame")
|
self._debug("Successfully send frame")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def send_message(self, msg, priority=5):
|
def send_message(self, msg, priority=5):
|
||||||
|
if len(msg) > 253:
|
||||||
|
raise ValueError("Message length can not exceed 253 byte")
|
||||||
|
|
||||||
crc16 = PredefinedCrc('crc16')
|
crc16 = PredefinedCrc('crc16')
|
||||||
crc16.update(msg)
|
crc16.update(msg)
|
||||||
msg_crc = crc16.digest()
|
msg_crc = crc16.digest()
|
||||||
|
|
Loading…
Reference in New Issue