<jupyter_start><jupyter_text>Sensors> reads sensor data over UART<jupyter_code>#export
import time
import serial # pip install pyserial
import numpy
#export

class Sensors(object):
    
    def __init__(self,baudrate=19200,port="/dev/ttyTHS0"):
        self.ser = serial.Serial(port=port,
                                baudrate=baudrate,
                                bytesize=serial.EIGHTBITS,
                                parity=serial.PARITY_NONE,
                                stopbits=serial.STOPBITS_ONE,
                                )
        self.ser.flushInput()
        time.sleep(1) # Wait a second to let the port initialize

    def readSerial(self):
        
        if ser.inWaiting() > 0:
            
            self.data = ser.readline().decode()
            #print(data.decode())
            
            #if data == "\r".encode():
            #    ser.write("\n".encode())
                
    def loopReadSerial(self):
        try: 
            print("starting transmission")
            ser.write("UART Demonstration Program\r\n".encode())
            
            while True:
                self.readSerial()
                
                print(self.data)
                break
                
        except KeyboardInterrupt:
            print("Exiting Program")

        except Exception as exception_error:

            print("Error occurred. Exiting Program")
            print("Error: " + str(exception_error))

        finally:
            self.ser.close()


peripherals = Sensors(baudrate=19200,port="/dev/ttyTHS0")
peripherals.loopReadSerial()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0973505853357979, 'fraction_numerical': 0.00862600123228589, 'mean_word_length': 1.4831804281345566, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '345151', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 451, 'n_tokens_neox': 429, 'n_words': 80, 'path': '06_sensors.ipynb', 'repo_name': 'openhsi/openhsi_old', 'size': '1623'}