Instruction manual

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 power supply
sp.write(cmd)
print "Set to remote command:"
DumpCommand(cmd)
# Get response from power supply
response = sp.read(length_packet)
assert(len(response) == length_packet)
print "Response:"
DumpCommand(response)
main()
The last line in the script calls the function main(). The first three lines of the main() function set
up a serial port to talk to. This serial object is created for us by the pyserial module we installed.
Note that in pyserial, the first COM port on your computer is numbered 0; thus, if you're using
COM1 on your PC, you'll set the port variable to 0.
The next five lines construct the string that we will send to the power supply. The chr() function
creates a single character that has the ASCII value of the argument. The + symbols allows 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.
We use the write method of the sp object (the connection to the serial port) to send the command
to the instrument. The DumpCommand() function prints the contents of the command to the screen in
a hex format.
When a command has been sent to the instrument, you must always request the return data, which
will always be another 26 bytes. This is also dumped to the screen.
Here are the results printed when this script is run:
Set to remote command:
aa ·· 20 01 ·· ·· ·· ·· ·· ··
·· ·· ·· ·· ·· ·· ·· ·· ·· ··
·· ·· ·· ·· ·· cb
Response:
aa ·· 12 80 ·· ·· ·· ·· ·· ··
·· ·· ·· ·· ·· ·· ·· ·· ·· ··
·· ·· ·· ·· ·· 3c
The · characters represent the bytes with a value of 0x00. This makes it easier to see the nonzero
bytes in the string.
B&K 1785, 1786B, 1787B, 1788 Python Library 15 January 2009 Page 4 of 14