<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 5.50.4134.600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face="Franklin Gothic Book" color=#0000ff>Hi All</FONT></DIV>
<DIV><FONT face="Franklin Gothic Book" color=#0000ff></FONT> </DIV>
<DIV><FONT face="Franklin Gothic Book" color=#0000ff>I am new to this language, 
and in using it to drive a serial port, I need to calculate classic block check 
characters - for the veterans amongst you - think "Burroughs Poll Select" or 
"Uniscope"...</FONT></DIV>
<DIV><FONT face="Franklin Gothic Book" color=#0000ff></FONT> </DIV>
<DIV><FONT face="Franklin Gothic Book" color=#0000ff>This is a program fragment 
that puts the string in an array of Bytes - is there a better way to do this 
job?  - the annotated action starts after the ascii definitions of the 
first 32 control characters...</FONT></DIV>
<DIV><FONT face="Franklin Gothic Book" color=#0000ff></FONT> </DIV>
<DIV><FONT face="Franklin Gothic Book" color=#0000ff><FONT face="Courier New" 
size=2>
<P># some ascii control character values defined</P>
<P>nul = '\x00' # null char</P>
<P>soh = '\x01' # start of header</P>
<P>stx = '\x02' # start of text</P>
<P>etx = '\x03' # end of text</P>
<P>eot = '\x04' # end of transmission</P>
<P>enq = '\x05' # enquiry</P>
<P>ack = '\x06' # ack</P>
<P>bel = '\x07' # bell char</P>
<P>bs = '\x08' # backspace</P>
<P>ht = '\x09' # horizontal tab</P>
<P>lf = '\x0a' # line feed - linux newline</P>
<P>vt = '\x0b' # vertical tab</P>
<P>ff = '\x0c' # form feed</P>
<P>cr = '\x0d' # carriage return</P>
<P>so = '\x0e' # shift out</P>
<P>si = '\x0f' # shift in</P>
<P>dle = '\x10' # data link escape</P>
<P>dc1 = '\x11' # data control 1 - aka xon</P>
<P>dc2 = '\x12' # data control 2 </P>
<P>dc3 = '\x13' # data control 3 - aka xoff</P>
<P>dc4 = '\x14' # data control 4</P>
<P>nak = '\x15' # negative acknowledgement</P>
<P>syn = '\x16' # sync char</P>
<P>etb = '\x17' # end of transmission block</P>
<P>can = '\x18' # cancel</P>
<P>em = '\x19' # end of message</P>
<P>sub = '\x1a' # sub</P>
<P>esc = '\x1b' # escape</P>
<P>fs = '\x1c' # field separator</P>
<P>gs = '\x1d' # group separator</P>
<P>rs = '\x1e' # record separator</P>
<P>us = '\x1f' # unit separator</P>
<P></P>
<P></P>
<P># some strings defined</P>
<P></P>
<P>poll = soh + 'P' + 'A' + stx + etx # rudimentary poll string </P>
<P>bcc = nul</P>
<P># here starts the trouble:</P>
<P></P>
<P>ar = array.array('B', poll)</P>
<P></P>
<P>bccint = ar[0] # this is prolly dicey - what type and how big is this?</P>
<P></P>
<P>for x in ar[1:]:</P>
<P>    bccint = bccint ^ x # this works, but is it the best 
way?</P>
<P></P>
<P>print repr(bccint)</P>
<P></P>
<P>ar.append(bccint)</P>
<P></P>
<P># this seems so left-handed - we make a string, then we put it in an 
array,</P>
<P># then we iterate over the array elements to calculate the</P>
<P># bcc (block check character), then we add it to the array - then we 
should</P>
<P># really make it all a string again to write it to the port...</P>
<P># it would be nice to be able to write:</P>
<P># nul = '\x00'</P>
<P># bcc = nul</P>
<P># poll = "some arbitrary string"</P>
<P># bcc = bcc ^ (x for x in poll[0:]) # calculate and</P>
<P># poll = poll + bcc # add the bcc to the string</P>
<P># port.write(poll) # and write the lot to the port</P>
<P># port.flush()</P>
<P># and on the way in:</P>
<P># numchars = 5 # some magic number depending on what we are expecting 
back</P>
<P># response = port.read(numchars) # or a more complicated char at a time 
function</P>
<P># bcc = nul</P>
<P># bcc = bcc ^ (x for x in response[0:])</P>
<P># if bcc != nul:</P>
<P>#     error_routine(response)</P>
<P># so we would stay working with strings - the stuff from the port</P>
<P># coming in is a string, and we need a string to write again...</P>
<P># *grin* but of course the xor and strong typing won't allow the</P>
<P># string - to say nothing of the iterator...</P>
<P><FONT face="Franklin Gothic Book" size=3></FONT> </P>
<P># here is a loop, demonstrating getting an arbitrary lf terminated string 
in,</P>
<P># and writing the fixed one with a bcc tagged on out as a response, along</P>
<P># with some debug printing.</P>
<P>s = esc + '@' + esc + 'v' + lf # the line we are searching for</P>
<P></P>
<P>while True:</P>
<P>    try:</P>
<P>        buffer = port.readline() # read in as a 
string, (unblocked)</P>
<P>    except KeyboardInterrupt:</P>
<P>        print "\n^C - Returning to Shell - 
Error is:", KeyboardInterrupt</P>
<P>        ret_val = 1 # ^C while 
waiting for input</P>
<P>        return buffer, ret_val # go 
out on error</P>
<P>    except IOError:</P>
<P>        continue # IOError on input - no 
record available</P>
<P>    if buffer == '': # empty string is rubbish - ignore it</P>
<P>        continue</P>
<P>    rawfile.write(buffer) # Keep the record as read</P>
<P>    print repr(buffer) # show how python sees it</P>
<P>    print repr(s) # show what our target looks like</P>
<P>    if s == buffer:</P>
<P>        print 'Bingo !!!'</P>
<P>        ar.tofile(port) # luckily there is this 
method,</P>
<P>        port.flush() # so we don't have to 
convert back to string...</P>
<P><FONT face="Franklin Gothic Book" size=3></FONT> </P>
<P><FONT face="Franklin Gothic Book" size=3>This lot was cut and pasted from the 
little test program I am using to debug the port stuff - there may be 
tab issues - the question I suppose has to do with the types that the xor 
supports, and I suppose I am saying that it would be nice if a single 
byte string (also from an iterator) would be supported by the xor operator... ( 
maybe a single char string...there's a difference between ascii and 
unicode...)</FONT></P>
<P><FONT face="Franklin Gothic Book" size=3>As my program stands it 
works, but it seems long - winded and ugly....</FONT></P>
<P><FONT face="Franklin Gothic Book" size=3>- Hendrik van 
Rooyen</FONT></P></FONT></FONT></DIV></BODY></HTML>