B&K Precision 1785B, 1786B, 1787B, 1788 Power supply Python Library Table of Contents Introduction Prerequisites Why a Library is Useful 2 2 3 Using the Library from Python Conventions Return values Example TimeNow() TurnPSOn() TurnPSOff() Remote or local control 6 6 6 6 7 7 7 7 SetRemoteControl() SetLocalControl() Maximum 7 SetMaxVoltage(voltage) Output SetOutVoltage(voltage) SetOutCurrent(voltage) SetCommunicationAddress(address=0) B&K 1785, 1786B, 1787B, 1788 Python Library 7 Reading 7 GetR
Introduction We provide a python library, ps178x.py, that can provide programming access to B&K Precision's power supply 1785-1788 series. The library is only supported for use on Windows computers. There are two ways you can use this library: 1. You can use it to access 1785B-1788 power supply from your own python programs. A PS178x object is provided whose interface allows access to various features of the power supply.
Why a Library is Useful The native programming interface to the power supplies is fairly low-level. It involves sending 26 byte commands and receiving 26 byte responses from the instrument. We'll demonstrate this interface in this section. First, it will demonstrate that your computer can talk to the instrument. Secondly, you'll likely see the need for a "higher-level" interface. The following material assumes you're at a Windows command line.
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.
The first byte of a command is always 0xaa and the second byte is the address of the power supply. The address should be set to 0. The third byte identifies the command "set to remote" and the fourth byte is a 1, which means enable remote mode. If the fourth byte was 0, this command would set the power supply to local mode. The third byte of the response string is 0x12, which means this is a packet that gives the status of the last command sent.
Using the Library from Python The pyserial library abstracts the serial port. Thus, you may be able to use the ps178x.py module for python programming on other platforms, such as Linux or Mac. However, B&K only supports use of the ps178x.py module on Windows 2000 and later platforms. At any time while using the library, you can execute ps.debug = 1 (where ps is a PS178x object defined in the module ps178x.py) and you will turn on debugging printout.
Output Mode: Off Over heat protection: Normal Output State: OFF Output current set to: 5.0 Maximum output voltage: 5.0 Output voltage set to: 2.5 Product info: Model 6821 Serial no. 6369001100 Firmware version 1.58 Set to local control TimeNow() Returns a string with the current date and time. This is useful for data logging. Example: Fri Dec 19 12:01:32 2008 The facilities in python's time module allow you to customize this string. TurnPSOn() Turns the power supply on.
Reading GetReading() Gets the reading for the current displayed output current and output voltage, the state of the power supply ( output state, over heat protection, output mode, fan speed, and operation state), set current value, set voltage value, and maximum voltage value. EnableLocalControl() Enables the local control of the front panel. Doing this will allow changing from remote to local mode by pressing shift + 7 on the front panel.
Using the COM Server Using the COM server will only work on Windows computers. You'll need to install python, pyserial, and pywin32; please refer to the appendices for instructions. Here's a summary of the things we'll do in this section: 1. Connect a power supply to the computer. 2. Register the COM server. 3. Run a python script to access the power supply via the COM server. 4.
Set output voltage Set output current Input values: Current output current: 0.0 A Current output voltage: 0.02 V Power supply state: Operation Mode: Remote Control Fan Speed: 1 Output Mode: Off Over heat protection: Normal Output State: OFF Output current set to: 5.0 Maximum output voltage: 5.0 Output voltage set to: 2.5 Product info: Model 6821 Serial no. 6369001100 Firmware version 1.
Troubleshooting All my commands are failing If you get the message Command cannot be carried out (i.e., the 0xB0 status byte), it's possible that you haven't set the instrument to remote mode. Execute SetRemoteControl() before trying to execute your commands. Make sure you're using the correct COM port number on your computer. Suspect this if the LEDs on the IT-E131 or IT-E132 interface boxes do not light up when you're sending commands.
Python References If you're an experienced programmer, you can learn python by reading the tutorial that comes with python (it's in the Doc directory of your python installation). You will likely be writing useful python programs in an hour or two. There are numerous introductory python books available. For a beginning programmer, Lutz's Learning Python might be appropriate. Other references are: Martelli, Python in a Nutshell, published by O'Reilly. Lutz, Programming Python, published by O'Reilly.
Appendices Introduction: For convenience, included in the “python.zip” file that can be downloaded from www.bkprecision.com are Python 2.5.2, pyserial 2.4, and pywin32 2.12. These are the 3 executable files required to use the python library and the COM server. If user decides to install using the provided files, please skip Step 1 in Appendix 1, Appendix 2, and Appendix 3.
If you see no error message after the import serial line, your pyserial installation was successful. Press ctrl-z and return to exit the python interpreter. Appendix 3: Getting and Installing pywin32 Step 1: Go to http://wiki.python.org/moin/Win32All and follow the links to the SourceForge repository. Step 2: Select the executable with the python version that matches the version of python you installed. Run the executable to install pywin32.