<commit_before><commit_msg>Adding initial serial comm with Arduino.<commit_after>import serial
import serial.tools.list_ports
import traceback

class SerialComm:

    def __init__(self):
        self.port=None
        self.open() #Auto-detects the serial port on which the arduino responds and opens it into self.port

    def openPort(self):
        for path,comment,HWID in serial.tools.list_ports.comports():
            try:
                p=serial.Serial(path,9600,timeout=3)
                #Got an open port, listen
                print "Trying", path
                l=p.readline()
                if "webcambot waiting" in l:
                    #FOUND!
                    break
            except serial.SerialException:
                pass
        else:
            raise RuntimeError("No port found, bailing out.")
        #if I made it to here, I have my port
        print >> p, "howdy;",
        p.flush()
        l=p.readline()
        if "webcambot ready" in l:
            p.flushInput()
            p.flushOutput()
            return p
        else:
            raise RuntimeError("Handshake failed")
    
    def open(self):
        self.port=self.openPort()
        print "Handshake successful. Using:",self.port.portstr

    def sendMsg(self,msg):
        print "comm:",msg+";"
        self.port.write(msg)
        self.port.timeout=10
        l=self.port.readline()
        self.port.flushInput()
        self.port.flushOutput()
        if "OK" in l:
            return "OK"
        else:
            return "Failure: >>>"+l+"<<<"

if __name__=="__main__":
    s=SerialComm()
    s.open()
    print >> s.port, "h +360;h -360;"
{'fraction_non_alphanumeric': 0.1088929219600726, 'fraction_numerical': 0.007864488808227465, 'mean_word_length': 1.7203947368421053, 'pattern_counts': {'":': 1, '<': 6, '<?xml version=': 0, '>': 10, '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}}
{'dir': 'git-commits-cleaned', 'id': '4489696', 'n_tokens_mistral': 498, 'n_tokens_neox': 474, 'n_words': 128}