Difference between revisions of "Python Read Telegraph"

From ThePlaz.com

Jump to: navigation, search
(conner's base code)
 
(modified)
 
Line 1: Line 1:
 
<pre>
 
<pre>
 
import serial
 
import serial
 
+
 
 
timeout=250
 
timeout=250
 
reading=True
 
reading=True
 
print "\n\n\n\n\n\n\n\n\n\n\n\n"
 
print "\n\n\n\n\n\n\n\n\n\n\n\n"
 
zerocount=0
 
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/tty.usbserial-A6007z03', 9600)
+
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):
 
def analyze(s):
s=s[s.find("1"):]
+
        s=s[s.find("1"):]
s=s.replace("10", "1 0")
+
        s=s.replace("10", "1 0")
s=s.replace("01", "0 1")
+
        s=s.replace("01", "0 1")
s=s.replace("010", "000")
+
        s=s.replace("010", "000")
s=s.replace("101", "111")
+
        s=s.replace("101", "111")
s=s.replace("0110", "0000")
+
        s=s.replace("0110", "0000")
while (s.find("00")!=-1):
+
        while (s.find("00")!=-1):
s=s.replace("00", "0")
+
                s=s.replace("00", "0")
s=s.replace("0", " ")
+
        s=s.replace("0", " ")
#print s
+
        #print s
blocks=s.split()
+
        blocks=s.split()
#print blocks
+
        #print blocks
total=0
+
     
count=0
+
        total=0
min=100000000000
+
        count=0
for i in range(0, len(blocks)):
+
        min=100000000000
if(len(blocks[i])<min):
+
     
min=len(blocks[i])
+
        for i in range(0, len(blocks)):
mess=""
+
                if(len(blocks[i])<min):
for i in range(0, len(blocks)):
+
                        min=len(blocks[i])
if(len(blocks[i])>=2*min):
+
     
mess+="_"
+
        mess=""
else:
+
        for i in range(0, len(blocks)):
mess+="."
+
                if(len(blocks[i])>=2*min):
if(mess!=""):
+
                        mess+="_"
if(mess.find("_")==-1):
+
                else:
if(min>=75):
+
                        mess+="."
mess=mess.replace(".", "_")
+
     
print mess
+
        if(mess!=""):
 
+
                if(mess.find("_")==-1):
for i in range(0, len(codes)):
+
                        if(min>=75):
if(mess==codes[i]):
+
                                mess=mess.replace(".", "_")
#print min
+
                print mess
print keys[i]+"\n"
+
 
 +
        for i in range(0, len(codes)):
 +
                if(mess==codes[i]):
 +
                        #print min
 +
                        print keys[i]+"\n"
 +
                     
 
s=""
 
s=""
 
temp=0
 
temp=0
 
count=0
 
count=0
 
while 1:
 
while 1:
rl=ser.readline()
+
        rl=ser.readline()
try:
+
     
rl = int((rl.strip()).replace("?", ""))
+
        try:
except:
+
                rl = int((rl.strip()).replace("?", ""))
#print "whoops"
+
        except:
1
+
                #print "whoops"
if(rl>800):
+
                1
rl="1"
+
        if(rl>800):
else:
+
                rl="1"
rl="0"
+
        else:
s+=rl  
+
                rl="0"
 
+
        s+=rl
if(rl=="0"):
+
 
zerocount+=1
+
        if(rl=="0"):
if(zerocount>=timeout):
+
                zerocount+=1
analyze(s)
+
                if(zerocount>=timeout):
s=""
+
                        analyze(s)
zerocount=0
+
                        s=""
else:
+
                        zerocount=0
zerocount=0
+
        else:
 +
                zerocount=0
 
</pre>
 
</pre>

Latest revision as of 20:14, 9 December 2009

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