Skip to content
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

Small addition to allow context managers #23

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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ $ pip install git+https://github.com/OE-FET/keithley2600
## Usage

Connect to the Keithley 2600 and perform some base commands:

```python
from keithley2600 import Keithley2600

Expand All @@ -65,6 +66,25 @@ i = k.smua.measure.i() # measures current at smuA
k.smua.measure.v(k.smua.nvbuffer1) # measures the voltage, stores the result in buffer
k.smua.nvbuffer1.clear() # clears nvbuffer1 of SMUA
```

Alternatively, the device can be used in a context:

```python
from keithley2600 import Keithley2600

with Keithley2600('TCPIP0::192.168.2.121::INSTR') as kth:
kth.reset()
smu = kth.smua
smu.sense = smu.SENSE_REMOTE
smu.source.limiti = 0.01 # mA
smu.source.autorangev = smu.AUTORANGE_ON
smu.source.func = smu.OUTPUT_DCVOLTS
smu.measure.autozero = smu.AUTOZERO_AUTO
smu.measure.autorangei = smu.AUTORANGE_ON
smu.measure.nplc = 16
smu.source.levelv = 3.3
smu.source.output = smu.OUTPUT_ON
```
Higher level commands defined in the driver:

```python
Expand Down
15 changes: 15 additions & 0 deletions keithley2600/keithley_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,21 @@ def __init__(

def __repr__(self) -> str:
return f"<{self.__class__.__name__}({self.visa_address})>"

def __enter__(self):
"""
Allows usage in contexts ("with Keithley2600() as kth:")

- cleaning the error-queue helps with faulty scripts that leave the SMU in a
weird state
- restarting the script often fails at basic tasks (like accessing keithley.smua)
because the TSP-Command-Set wasn't read successfully (or other weird behavior).
"""
self.connection.write("errorqueue.clear()")
orgua marked this conversation as resolved.
Show resolved Hide resolved
return self

def __exit__(self, *args):
self.disconnect()

# =============================================================================
# Connect to keithley
Expand Down