Python Read Telegraph

From ThePlaz.com

Jump to: navigation, search
import serial

timeout=250
reading=True
print "\n\n\n\n\n\n\n\n\n\n\n\n"
zerocount=0
codes=["._", "_...", "_._.", "_..", ".", ".._.", "__.", "....", "..", 
".___", "_._", "._..", "__", "_.", "___", ".__.", "__._", "._.", 
"...", "_", ".._", "..._", ".__", "_.._", "_.__", "__.."]
keys=["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
ser = serial.Serial('/dev/ttyUSB0', 9600)


def analyze(s):
        s=s[s.find("1"):]
        s=s.replace("10", "1 0")
        s=s.replace("01", "0 1")
        s=s.replace("010", "000")
        s=s.replace("101", "111")
        s=s.replace("0110", "0000")
        while (s.find("00")!=-1):
                s=s.replace("00", "0")
        s=s.replace("0", " ")
        #print s
        blocks=s.split()
        #print blocks
       
        total=0
        count=0
        min=100000000000
       
        for i in range(0, len(blocks)):
                if(len(blocks[i])<min):
                        min=len(blocks[i])
       
        mess=""
        for i in range(0, len(blocks)):
                if(len(blocks[i])>=2*min):
                        mess+="_"
                else:
                        mess+="."
       
        if(mess!=""):
                if(mess.find("_")==-1):
                        if(min>=75):
                                mess=mess.replace(".", "_")
                print mess

        for i in range(0, len(codes)):
                if(mess==codes[i]):
                        #print min
                        print keys[i]+"\n"
                       
s=""
temp=0
count=0
while 1:
        rl=ser.readline()
       
        try:
                rl = int((rl.strip()).replace("?", ""))
        except:
                #print "whoops"
                1
        if(rl>800):
                rl="1"
        else:
                rl="0"
        s+=rl  

        if(rl=="0"):
                zerocount+=1
                if(zerocount>=timeout):
                        analyze(s)
                        s=""
                        zerocount=0
        else:
                zerocount=0