<div>Hi,</div>
<div>I've got a lego mindstorm RCX 1.0 (but firmware is 2.0) that uses one of those old serial IR towers to control the microcontroller. I've had a look around at python's serial documentation as well as the RCX's documentation and I'm trying to write something to control the RCX directly using python. Several examples from of doing this in python include using lnp (i think) and that doesn't quite run well in windows. I've had a look at the C++ code and some protocol documentation here: <a href="http://www.generation5.org/content/2001/rob08.asp">http://www.generation5.org/content/2001/rob08.asp</a> and converted it to python. I've attached it at the end of the email. So now I've figured out how to check for the battery level and it seems to work (I've tested it on my RCX) but I'm confused with the other documentation (e.g. <a href="http://graphics.stanford.edu/~kekoa/rcx/">http://graphics.stanford.edu/~kekoa/rcx/</a> ) about how to do this in python or what this all means. I was wondering if anyone can help me complete these? or maybe help me do it step-by-step?</div>
<div>Thanks for any help.</div>
<div> </div>
<div>Below is the python code I've been working on:</div>
<div> </div>
<div>import time<br>import serial<br>import struct<br>import binascii</div>
<div> </div>
<div>def tcbin(x, y=8): <br> """<br> This function returns the padded, two's complement representation of x, in y-bits.<br> It is conventional for y to be 8, 16, 32 or 64, though y can have any non-zero positive value. <br>
"""<br> if x >= 0:<br> binstr = bin(x)<br> # pad with leading zeros<br> while len(binstr) < y + 2:<br> binstr = "0b0" + binstr[2:]<br> return binstr<br>
return bin((2**y) + x) # x is negative</div>
<div> </div>
<div>def bitcompliment(hex_code): <br> return hex(int(tcbin(~(ord(hex_code))),2))<br> <br>def processOutput(raw_data,output):<br> outputStatus = True<br> pos = 0<br> for i in range(3):<br> if raw_data[i] != output[i]:<br>
outputStatus = False<br> pos+=1<br> if outputStatus:<br> print "output OK"<br> else:<br> print "problem with output"<br> outputCompliment = True<br> while outputCompliment:<br>
if hex(ord(output[pos])) == bitcompliment(output[pos+1]):<br> print "output compliment OK"<br> else:<br> print "problem with output compliment"<br> pos+=2<br>
if hex(ord(output[pos])) == '0x55' and hex(ord(output[pos+1])) == '0xff' and hex(ord(output[pos+2])) == '0x0':<br> pos+=3 <br> outputCompliment = False<br> if hex(ord(output[pos])) == '0xcf' or hex(ord(output[pos])) == '0xc7':<br>
#battery checker<br> pos+=2<br> if hex(ord(output[pos])) == bitcompliment(output[pos+1]) and hex(ord(output[pos+2])) == bitcompliment(output[pos+3]):<br> s = ((ord(output[pos+2]) * 256) + ord(output[pos])) / 1000.0<br>
print "Battery is at " + str(s) + " Volts"<br> else: <br> for i in range(len(output[pos:]),len(output),2):<br> print hex(ord(output[i]))<br> if i+1 < len(output):<br>
if hex(ord(output[i])) == bitcompliment(output[i+1]):<br> print "message OK. contents: " + hex(ord(output[i])) <br> <br># configure the serial connections (the parameters differs on the device you are connecting to)<br>
ser = serial.Serial(<br> port='COM1',<br> baudrate=2400,<br> parity=serial.PARITY_ODD,<br> stopbits=serial.STOPBITS_ONE,<br> bytesize=serial.EIGHTBITS <br>)</div>
<div>raw_data = '\x55\xff\x00\x38\xc7\x38\xc7'<br></div>
<div>result = ser.write(raw_data)</div>
<div>out = ''<br>time.sleep(1) #pause for a second<br>while ser.inWaiting() > 0:<br> out+=ser.read(1)</div>
<div>processOutput(raw_data,out)</div>
<div> </div>