Skip to content

Added set voltage and current functionality #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ celerybeat-schedule
.env

# virtualenv
.venv/
venv/
ENV/

Expand All @@ -90,3 +91,6 @@ ENV/

# PyCharm
.idea

# VScode
.vscode/
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Tested with:

Tested on:

+ Windows 7 x64
+ Windows 7 x64
+ Windows 10 x64 Version 1607 (OS Build 14393.2035)
+ Ubuntu 16.04.1 LTS
+ Ubuntu 20.04.1 LTS

## Features of Python-PS2000B
### Supported
Expand Down Expand Up @@ -50,11 +51,15 @@ device = PS2000B.PS2000B("/dev/ttyACM0")
print("Device status: %s" % device.get_device_status_information())

device.enable_remote_control()
device.voltage = 5.1
device.current = 1
device.enable_output()

time.sleep(1)

print("Current output: %0.2f V , %0.2f A" % (device.get_voltage(), device.get_current()))

device.output = False
```

## Documentation
Expand Down
192 changes: 192 additions & 0 deletions eaps_ctrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#!/usr/bin/env python3
'''



'''
import sys
import serial, serial.tools.list_ports
import argparse
from pyps2000b import PS2000B

HWID = [0x232E, 0x0010]
VERSION = '0.5'
VERBOSE = 0
ULim = 16
Ilim = 1.4
Uset = 13
Iset = 1


def print_err(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)

def dprint(debuglevel=1, *args, **kwargs):
global VERBOSE
if VERBOSE >= debuglevel:
print(f'#debug({VERBOSE}): ', *args, **kwargs)

def getPortName(sn=None):
'''
Get port name by HWID and optional serial number
'''
#serial.tools.list_ports.comports(include_links=False)[0].device
dprint(1, str('getPortName({}) {}'.format(sn, str([comport.device for comport in serial.tools.list_ports.comports()]))) )
lPortName = None
for p in list(serial.tools.list_ports.comports(include_links=False)):
# if 'USB' in p.hwid:
if p.vid == HWID[0] and p.pid == HWID[1]:
if sn == None:
lPortName = p.device
break
else:
l_psu = PS2000B.PS2000B(p.device)
if l_psu.get_device_information().device_serial_no.decode() == sn:
lPortName = p.device
break
dprint(2, lPortName)
return lPortName

def findAllPSU():
'''
Show all connected PSU found.
'''
dprint(1, ('findAllPSU() ', str([comport.device for comport in serial.tools.list_ports.comports()])))
for p in list(serial.tools.list_ports.comports(include_links=False)):
if p.vid == HWID[0] and p.pid == HWID[1]:
l_psu = PS2000B.PS2000B(p.device)
print(l_psu.get_device_information())

def check_val(par, value):
'''
Type-check callback for argparse
'''
fvalue = float(value)
if par == 'u':
if fvalue < 0 or fvalue >= ULim:
raise argparse.ArgumentTypeError("%s invalid voltage value" % value)
elif par =='ovp':
if fvalue < 0 or fvalue >= 20:
raise argparse.ArgumentTypeError("%s invalid voltage limit value" % value)
elif par == 'i':
if fvalue < 0 or fvalue > Ilim:
raise argparse.ArgumentTypeError("%s invalid current limit value" % value)
elif par =='ocp':
if fvalue < 0 or fvalue >= 10:
raise argparse.ArgumentTypeError("%s invalid voltage limit value" % value)
return fvalue

def create_parser():
'''
Create parser and arguments
'''
parser = argparse.ArgumentParser(description='EA-PS 2042 B Control'+' \n Version: '+VERSION)
parser.add_argument('-init',action='store_true',help='Initial settings and output off (override with other commands)')
parser.add_argument('-o', '--output', choices=['on','off'], default='off', help='Activate/deactivate output')
parser.add_argument('-r', '--remote', choices=['on','off'], default='on', help='Activate/deactivate remote control mode')
parser.add_argument('-u', type=(lambda x: check_val('u', x)), help='Set output voltage (has to be <U_lim)')
parser.add_argument('-i', type=lambda x: check_val('i', x), help='Set output current for PSU')
parser.add_argument('-v', '--verbose', action='count', help='increase output verbosity')
parser.add_argument('-g', '--get', choices=['voltage','current', 'vlim', 'clim', 'power','output', 'device_info'], default=None, nargs='*', help='Get informations (see README)')
parser.add_argument('-status',action='store_true',help='Get full status of PSU')
parser.add_argument('-find',action='store_true',help='Find all connencted PSU')
parser.add_argument('-sn', type=str, help='Choose device by serial number.')
parser.add_argument('-p','--port', help='Set serial port. Override port detection.')
return parser.parse_args()


def main(args):
global VERBOSE

if args.verbose:
VERBOSE = args.verbose
print(args)
else:
VERBOSE = 0

if args.find:
findAllPSU()
sys.exit()

if args.port == None:
if args.sn:
dprint(1,'# use SN=',args.sn)
portName = getPortName(sn=args.sn)
else:
portName = getPortName()
else:
portName = args.port

if portName == None:
print_err('! EA-PS device not found.')
sys.exit(1)

dprint(1, 'Using device at: {}'.format(portName))

psu = PS2000B.PS2000B(portName)

if psu.is_open():
if args.get:
for arg in args.get:
if arg == 'voltage':
print(psu.voltage)
elif arg == 'current':
print(psu.current)
elif arg == 'power':
print(psu.voltage * psu.current)
elif arg == 'output':
print(psu.output)
elif arg == 'vlim':
print(psu.get_voltage_setpoint())
elif arg == 'clim':
print(psu.get_curent_setpoint())
# elif arg == 'ovp':
# #TODO: implement get_OVP
# elif arg == 'ocp':
# #TODO: implement get_OCP
elif arg == 'device_info':
print('{:<18} {}'.format('Device Type:', psu.get_device_information().device_type.decode()))
print('{:<18} {}'.format('Serial no.:', psu.get_device_information().device_serial_no.decode()))
print('{:<18} {}'.format('Firmware version:', psu.get_device_information().software_version.decode()))
else:
pass
else:
if args.init:
psu.enable_remote_control()
psu.output = False
psu.voltage = Uset
psu.current = Iset
# psu.voltage_limit = ULim
# psu.current_limit = ILim
else:
if args.u:
psu.voltage = args.u
# if args.ovp:
#TODO: implement set_OVP
pass
# if args.ocp:
#TODO: implement set_OCP
pass
if args.i is not None:
psu.current = args.i
if args.output == 'on':
psu.output = True
else:
psu.output = False
if args.remote == 'off':
psu.disable_remote_control()
else:
psu.enable_remote_control()

if args.status:
print(psu.get_device_status_information())

else:
print_err('Error: can not open {}'.format(portName))


if __name__ == '__main__':
args = create_parser()
#try:
main(args)

13 changes: 12 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3
# coding=utf-8
import platform
import time

Expand All @@ -24,10 +26,19 @@
print("...will enable remote control...")
device.enable_remote_control()

print("...set voltage to 12V and max current to 1A...")
device.voltage = 12
device.current = 1
time.sleep(1)
print("...now enabling the power output control...")
device.enable_output()
print("Device status: %s" % device.get_device_status_information())
time.sleep(1)
print("Device status: %s" % device.get_device_status_information())
print("Current output: %0.2f V , %0.2f A" % (device.voltage, device.current))
time.sleep(2)
print("...set voltage to 5.1V...")
device.voltage = 5.1
time.sleep(2)
print("Current output: %0.2f V , %0.2f A" % (device.get_voltage(), device.get_current()))
print("...after 2 seconds power output will be disabled again ...")
time.sleep(2)
Expand Down
Loading