Package: pybk8500
Python library for the BK Precision 8500 SERIES DC ELECTRONIC LOADSModels 8500, 8502, 8510, 8512, 8514, 8518, 8520,
8522, 8524 & 8526Protocol found athttps://bkpmedia.s3.amazonaws.com/downloads/manuals/en-us/85xx_manual.pdfin the “Command details” section.Command Line InterfaceI added some command line interface utilities that use python’s “-m” module switch.Installpipinstallpybk8500[all]Requirementspyserial>=3.4continuous-threading>=2.0.0Send CommandSend a command to a serial port and wait for a response.python-mpybk8500.send_cmd"COM1"9600"RemoteOn"--address1# will send the command down a serial port and wait for a responseThe command ID can be the string NAME or integer ID.# cmd_id as strpython-mpybk8500.send_cmd"COM1"9600"SetRemote"--value1--address1# cmd_id as hexpython-mpybk8500.send_cmd"COM1"96000x20--value1--address1# cmd_id as decimalpython-mpybk8500.send_cmd"COM1"960032--value1--address1ParserThe parser is used to parse incoming messages. It can be used with to parse single messages,
parse messages with an iterator, or parse using a callback function.Parse Iterator ExampleUse a for loop to iterate through parsed messages.importpybk8500withopen('saved_messages.txt','rb')asf:p=pybk8500.Parser()formsginp.parse_iter(f.read()):print(msg.NAME)forfield,valueinmsg.fields().items():print('\t{}={}'.format(field,value))Parse Callback ExampleUse a callback function to handle parsed messages. This is useful for GUI’s with a serial port.importsysimportpybk8500defprint_error(error):print('{}:{}'.format(type(error).__name__,str(error)),file=sys.stderr)defprint_msg(msg):print(msg.NAME)forfield,valueinmsg.fields().items():print('\t{}={}'.format(field,value))p=pybk8500.Parser()p.error=print_errorwithopen('saved_messages.txt','rb')asf:p.parse(f.read(),print_msg)Parse Single Message ExampleParse a single message at a time.importsysimportpybk8500defprint_error(error):print('{}:{}'.format(type(error).__name__,str(error)),file=sys.stderr)withopen('saved_messages.txt','rb')asf:p=pybk8500.Parser()byts=f.read()whileTrue:msg,error,byts=self.parse_msg(byts)ifmsgisnotNone:print(msg.NAME)forfield,valueinmsg.fields().items():print('\t{}={}'.format(field,value))eliferrorisnotNone:print_error(error)else:breakCommandsGenerate commands and send them down a serial portRequirementspyserialpip install pyserialUse CommandsCreate and use command easily.Commands can be found athttps://bkpmedia.s3.amazonaws.com/downloads/manuals/en-us/85xx_manual.pdfin the “Command details” section.importpybk8500importserialser=serial.Serial('COM1',9600)cmd=pybk8500.SetRemoteOperation(address=1,operation='Remote')ser.write(bytes(cmd))# Most commands have a value aliascmd=pybk8500.SetRemoteOperation(address=1,value='Remote')ser.write(bytes(cmd))Commands are bytearrays which can be used as bytes. When you change a value a flag is set to indicate that the
checksum must be recalculated. Callingbytes(cmd)will recalculate the checksum before converting to bytes.ProfileCustom profile CSV runner.CSVThe CSV of profile commands is defined by “Command”, “Value”, “Run Time (s)”.Command - Name of the command you want to send.Runs any command registered in “pybk8500.Parser.lookup” as well as predefined custom internal commands.Commands can be a name “SetRemote”, hex value “0x20”, or decimal value 32.Value - Value to pass into the command.The Command needs the “value” alias or custom internal command.Accepts units! “1 mW” will be parsed and converted to “0.001 W”Run Time (s) - Turn on the load and run for the given amount of time.Accepts Units “1 h” or “1:00:00” will be parsed and converted to “3600 s”CommandsComment line by starting the line with “#” or “;”Internal Commands“SetupRemote,,” - Turn On Remote, Turn Off Load“TeardownRemote,,” - Turn Off Load, Turn Off Remote“Run,,10 s” - Run the current mode by turning on the load and reading the input for the run time.“Connect,,” - Connect the serial port.“SampleRate,40,” - Set the read input time sample rate (1/value delay after each read).“SampleTime,0.1,” - Set the read input time (value delay after each read).“BaudRate,38400,” - Set the serial port baud rate.“Com,COM1,” - Set the serial com port.“Port,COM1,” - Set the serial com port.“Output,my_file.csv,” - Set the output file for any subsequent runs.“Output,,” - Print the results for subsequent runs.“Print,===== Print =====,” - Print the value (“===== Print =====”) to stdout.“Sleep,0.1 s,” - Sleep the given amount of time“Sleep,,0.1 s” - Sleep with timeout also works“Stop,,” - Stop running the program here. This is a hard stop that can be useful for debugging.All defined commands in “pybk8500.commands.py” can be used“CC,3 A,100 ms” - Set Constant Current of value (3 A).If “Run Time (s)” is given run this mode for the given amount of time.“CV,12 V,1” - Set Constant Voltage of value (12 V).If “Run Time (s)” is given run this mode for the given amount of time.“CW,1,08:00” - Set Constant Power of value (1 W).If “Run Time (s)” is given run this mode for the given amount of time.“CR,1,1” - Set Constant Resistance of value (1 Ohm).If “Run Time (s)” is given run this mode for the given amount of time.Example# profile.txt
Command,Value,Run Time (s)
Print,===== Setup Coms =====
SampleTime,0.1,
BaudRate,38400,
Com,COM1,

SetupRemote,,
# "SetupRemote,," does the following
# RemoteOn,,
# LoadOff,,

Print,========== Setup Max Values ==========,
SetMaxCurrent,4.600 A,0
SetMaxVoltage,25.000,0
SetMaxPower,30.000,0

Print,========== CC ==========,
Output,CC_test1.csv,
CC,1mA,0.100
# Continue saving output for next run
CC,3 W,100 ms

Output,CC_test2.csv,
CC,1.600,19.8

# Stop output. Print results
Output,,
SampleRate,1.000,0
CW,20.000,60.000

Print,===== Finished =====,
TeardownRemote,,
# "TeardownRemote,," does the following
# LoadOff,,
# RemoteOff,,Run with the command linepython-mpybk8500.run_profile"./profile.txt"Run with python scriptfrompybk8500.run_profileimportmain# python -m pybk8500.run_profile "./profile.txt"main('./profile.txt')Plot the resultsfrompybk8500importparse_csv,plot_csv_file,plot_csv_files# python -m pybk8500.plot_csv "./profile_results.csv"plot_csv_files('./profile_results.csv','./profile_results2.csv')Combine csv files.frompybk8500importcombine_csv_files# python -m pybk8500.combine_csv "./profile_results.csv" "./profile_results2.csv"combine_csv_files('./profile_results.csv','./profile_results2.csv')