Bk Precision BK-8500 Electronic Load 1 mA - 30 A 0.1 - 120 Vdc 0 - 300 W BK-8500 Datenbogen

Produktcode
BK-8500
Seite von 76
        if i % 10 == 0 and i != 0:
            print
            print header,
        if i % 5 == 0:
            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 
Calcu la t eChecksum( )
 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.
8500 DC Load Series                    
Version:  September 2, 2009 
Page 47 of 76