print " ",
s = "%02x" % ord(bytes[i])
if s == "00":
s = chr(250)*2
print s,
print
def CalculateChecksum(cmd):
assert((len(cmd) == length_packet - 1) or (len(cmd) ==
length_packet))
checksum = 0
for i in xrange(length_packet - 1):
checksum += ord(cmd[i])
checksum %= 256
return checksum
def main():
port = 3 # COM4 for my computer
baudrate = 38400
sp = serial.Serial(port, baudrate) # Open a serial connection
# Construct a set to remote command
cmd = chr(0xaa) + chr(0x00) + chr(0x20) # First three bytes
cmd += chr(0x01) + chr(0x00)*(length_packet - 1 - 4)
cmd += chr(CalculateChecksum(cmd))
assert(len(cmd) == length_packet)
# Send command to DC load
sp.write(cmd)
print "Set to remote command:"
DumpCommand(cmd)
# Get response from DC load
response = sp.read(length_packet)
assert(len(response) == length_packet)
print "Response:"
DumpCommand(response)
main()
The first three lines of the main() function set up a serial port to talk to. The next five lines construct
the string that we will send to the DC load. The chr() function creates a single character that has
the ASCII value of the argument. The + symbols allow strings to be concatenated. The expression
chr(0)*a_number creates a string of ASCII 0x00 characters whose length is a_number. . The last
character is the checksum of the previous 25 characters, calculated for us by the
CalculateChecksum() function.
When a command is sent to the instrument, you must always request for return data, which will
always be another 26 bytes. This is also dumped to the screen.
You can download a complete python program along with detailed documentation from our website
at www.bkprecision.com
8500 DC Load Series Version: February 4, 2009 Page 47 of 76