[Tutor] Looping problem
vicki at thepenguin.org
vicki at thepenguin.org
Thu Aug 12 19:21:17 CEST 2004
Okay, I am trying to parse some data which looks something like this:
ab<TAB><TAB>11<CR><ACK>
<STX>04142069<ETX><ACK>
<STX>08000000006E<ETX><ACK>
<STX>0414106A<ETX><ACK>
<STX>10169100000000000061<ETX><ACK>
<STX>10169000000000000060<ETX><ACK>
<STX>1016934129CD4CCB201E<ETX><ACK>
<STX>10169200000000000062<ETX><ACK>
<STX>10169500000000000065<ETX><ACK>
<STX>10169400000000000064<ETX><ACK>
<STX>20170000000281082A07D4021A0000000815<EOT><ACK><ACK>
What I need to do is move to the point just after the ab command, and then
send an '\x02' for each <STX>, send an '\x03' for each <ETX>, read in each
<ACK> after sending the <ETX>, and recognize when I reach the end of data
and send an '\x04' for the <EOT> and receive the two <ACK>s. I wrote the
following:
LTRD_START = False
LTRD_END = False
for readString in infile.readlines():
word = ""
#Find our place in the input file
if re.search("Load Time Resolve Data",readString):
LTRD_START = True
else:
if re.search("ab<TAB><TAB>11<CR><ACK>",readString):
print "Do Nothing"
elif LTRD_START:
#Strip off beginning whitespace
readString.lstrip()
#Strip off <STX> and send [02] on port
s = string.lstrip(readString)
readString = string.lstrip(s,'<STX>')
port.write('\x02')
print readString
#Reset loop variable
INSIDE = False
#While not at the end of the data
while not LTRD_END:
for byte in readString:
#Check to see if it is a comm word like <STX>
if byte == '<':
INSIDE = True
word = byte
elif INSIDE and byte != '>':
word += byte
elif INSIDE and byte == '>':
word += byte
#Check to see if this is the end of the data
if word == '<ETX>':
port.write('\x03')
result = port.read()
elif word == '<EOT>':
LTRD_END = True
result = port.read()
print result
result = port.read()
print result
break
#Since this is the end of a comm word,
#reset the loop variable
INSIDE = False
elif INSIDE == False:
port.write(byte)
But I've got something wrong, because it loops infinitely and doesn't kick
out when it hits the <EOT>. Can anyone else see it?
Vicki
"A pessimist sees the difficulty in every opportunity; an optimist sees
the opportunity in every difficulty."
-- Winston Churchill
More information about the Tutor
mailing list