Created
April 9, 2015 08:47
-
-
Save dpslwk/ebfd59edd84f99b0c4c8 to your computer and use it in GitHub Desktop.
This is a snippet of python used to read incoming LLAP messages from a serial port, is called as part of a while loop in a dedicated serial thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# class level varables for check incoming characters are valid LLAP | |
_validID = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-#@?\\*" | |
_validData = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !\"#$%&'()*+,-.:;<=>?@[\\\/]^_`{|}~" | |
def _SerialReadIncomingLLap(self): | |
char = self._serial.read() # should not time out but we should check anyway | |
self.logger.debug("tSerial: RX:{}".format(char)) | |
if char == 'a': | |
# this should be the start of a llap message | |
# read 11 more or time out | |
llapMsg = "a" | |
count = 0 | |
while count < 11: | |
char = self._serial.read() | |
if not char: | |
self.logger.debug("tSerial: RX:{}".format(char)) | |
return | |
if char == 'a': | |
# start again and | |
count = 0 | |
llapMsg = "a" | |
self.logger.debug("tSerial: RX:{}".format(char)) | |
elif (count == 0 or count == 1) and char in self._validID: | |
# we have a vlaid ID | |
llapMsg += char | |
count += 1 | |
elif count >= 2 and char in self._validData: | |
# we have a valid data | |
llapMsg += char | |
count +=1 | |
else: | |
self.logger.debug("tSerial: RX:{}".format(llapMsg[1:] + char)) | |
return | |
self.logger.debug("tSerial: RX:{}".format(llapMsg[1:])) | |
if len(llapMsg) == 12: # just double check length | |
if llapMsg[1:3] == "??": | |
self._SerialProcessQQ(llapMsg[3:].strip("-")) | |
else: | |
# not a configme llap so send out via UDP LLAP | |
try: | |
self.qUDPSend.put_nowait(self.encodeLLAPJson(llapMsg, self.config.get('Serial', 'network'))) | |
except Queue.Full: | |
self.logger.warn("tSerial: Failed to put {} on qUDPSend as it's full".format(llapMsg)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment