Skip to content

Commit dc05698

Browse files
committed
Improved exception and error handling in LibUsbScope, doc update
Signed-off-by: Martin <[email protected]>
1 parent 4cb4edb commit dc05698

26 files changed

+189
-81
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2022-03-01: new sample rates 32k, 640k, doc update, tools update [4cb4edb]
12
2022-02-28: upversion to FW0210, new sample rate 128 kS/s (ID: 113) [f4755d2]
23
2022-02-08: more tool programs in examples [6c9d2d7]
34
2022-02-05: Merge branch 'main' of github.com:Ho-Ro/Hantek6022API fix last commit [078dffd]

PyHT6022/LibUsbScope.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
__author__ = 'Robert Cope', 'Jochen Hoenicke'
22

33
import os
4+
import sys
45
import time
56
import usb1
67
import array
@@ -221,7 +222,11 @@ def open_handle(self):
221222
self.device_handle = self.device.open()
222223
if os.name == 'posix' and self.device_handle.kernelDriverActive(0):
223224
self.device_handle.detachKernelDriver(0)
224-
self.device_handle.claimInterface(0)
225+
try:
226+
self.device_handle.claimInterface(0)
227+
except Exception as e:
228+
print( e, file=sys.stderr )
229+
return False
225230
if self.is_device_firmware_present:
226231
self.set_num_channels(2)
227232
self.set_interface(0)
@@ -236,9 +241,13 @@ def close_handle(self, release_interface=True):
236241
"""
237242
if not self.device_handle:
238243
return True
239-
if release_interface:
240-
self.device_handle.releaseInterface(0)
241-
self.device_handle.close()
244+
try:
245+
if release_interface:
246+
self.device_handle.releaseInterface(0)
247+
self.device_handle.close()
248+
except Exception as e:
249+
print( e, file=sys.stderr )
250+
return False
242251
self.device_handle = None
243252
return True
244253

README.md

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
This repo is based on the excellent work of [Robert](https://github.com/rpcope1/Hantek6022API)
88
and [Jochen](https://github.com/jhoenicke/Hantek6022API)
9-
and focusses mainly on Hantek6022BE/BL under Linux (development system: debian buster).
9+
and focusses mainly on Hantek6022BE/BL under Linux (development system: Debian stable).
1010

1111
## Hantek 6022 Firmware
1212

@@ -18,42 +18,61 @@ and focusses mainly on Hantek6022BE/BL under Linux (development system: debian b
1818

1919
<img alt="Scope Visualisation Example" width="100%" src="plot_from_capture.png">
2020

21-
This is a API for Python for the
22-
ultra-cheap, reasonably usable (and hackable) 6022 DSO, with a libusb implementation via libusb1 for Linux.
21+
This is a API for Python for the ultra-cheap, reasonably usable (and hackable) 6022 DSO,
22+
with a libusb implementation via libusb1 for Linux.
2323

24-
The scope can be accessed by instantiating an oscilloscope object with the correct scopeid (always 0 for one scope
25-
attached). Things like voltage divisions and sampling rates can be set by the appropriate methods.
26-
Please check the provided example programs, the comments will give you more hints for own experiments.
27-
Each method has some documentation as to what it does currently though, and hopefully
28-
variable names are clear enough to give you some idea what they are for.
24+
The scope can be accessed by instantiating an oscilloscope object.
25+
Things like voltage divisions and sampling rates can be set by the appropriate methods.
26+
Please check the provided [example programs](https://github.com/Ho-Ro/Hantek6022API/tree/main/examples),
27+
the comments will give you more hints for own experiments.
28+
Each method has documentation about what it is doing, and hopefully the variable names are clear enough
29+
to give you an idea of what they are for.
2930

30-
## Developed under Linux
31+
## Linux Install
3132

32-
If you're on Linux, you're in luck. Provided are bindings for libusb to operate this
33-
little device. You may wish to first add 60-hantek-6022-usb.rules to your udev rules, via
33+
If you're on Linux, you're in luck.
34+
Provided are bindings for libusb to operate this little device with simple python commands.
35+
If you are a user, you can simply download the latest Debian package from
36+
[releases](https://github.com/Ho-Ro/Hantek6022API/releases) and use the utilities in
37+
[examples](https://github.com/Ho-Ro/Hantek6022API/tree/main/examples),
38+
all tools named `*_6022.py` are copied to `/usr/bin` and are thus globally available.
39+
40+
## Developer Info
41+
42+
If you are a developer, you will definitely clone the repo and work with it more intensively. So please read on...
43+
44+
You may wish to first add `60-hantek-6022-usb.rules` (living in [udev](https://github.com/Ho-Ro/Hantek6022API/tree/main/udev))
45+
to your udev rules, via
3446

3547
sudo cp 60-hantek-6022-usb.rules /etc/udev/rules.d/
3648

3749
After you've done this, the scope should automatically come up with the correct permissions to be accessed
3850
without being root user.
3951

40-
The following instructions are tested with Debian (*stretch* and *buster*)
41-
and are executed also automatically under Ubuntu (*2004*) - have a look
52+
The following instructions are tested with Debian stable versions *stretch*, *buster* and *bullseye*
53+
and are executed also automatically by GitHub under Ubuntu (*2004*) after each push to this repo - have a look
4254
at the [GitHub Action](https://github.com/Ho-Ro/Hantek6022API/actions/workflows/build_check.yml).
43-
On each successful run a debian package is available under *Artifacts*.
55+
On each successful run a Debian package is available under *Artifacts*.
56+
57+
### Build Preparations
4458

4559
To compile the custom firmware you have to install (as root) the *small devices c compiler* `sdcc` and the tool `pkgconf`:
4660

4761
sudo apt install sdcc pkgconf
4862

49-
### Submodule fx2lib
50-
Hantek6022API uses the submodule [fx2lib](https://github.com/Ho-Ro/fx2lib) that I cloned from the [original fx2lib](https://github.com/djmuhlestein/fx2lib) to do minor maintenance updates.
63+
Take care when the SDCC version gets updated. the step from 3.9 to 4.0 introduced a nasty regression due to [less optimal code](https://github.com/Ho-Ro/Hantek6022API/blob/4cb4edbf1e6d2d5df21dbb4dabb8f51c932a0348/PyHT6022/Firmware/DSO6022BE/scope6022.inc#L80)
64+
from the newer version.
65+
66+
Hantek6022API uses the submodule [fx2lib](https://github.com/Ho-Ro/fx2lib) that I cloned from the
67+
[original fx2lib](https://github.com/djmuhlestein/fx2lib) to do minor maintenance updates.
5168

52-
Pull the submodule in:
69+
Pull the submodule in (once):
5370

5471
git submodule init
5572
git submodule update --remote
5673

74+
### Linux Build
75+
5776
To build the custom firmware run `make` in the top level directory:
5877

5978
make
@@ -79,10 +98,44 @@ The installed programs can also be uninstalled cleanly with
7998

8099
sudo dpkg -P hantek6022api
81100

82-
With the device plugged in, run `upload_firmware_6022.py` once to bootstrap the scope for use.
101+
83102
You can then look at the scope traces via `capture_6022.py -t 0.01 | plot_from_capture_6022.py`,
84103
or write your own programs - look at the programs in `examples` as a start.
85104

105+
If you want to make low-level experiments with the python commands you should bootstrap the scope for use:
106+
With the device plugged in, run `upload_6022_firmware.py` once.
107+
The *user tools* `*_6022.py` do this automatically at start.
108+
109+
**Don't Panik!**
110+
The firmware is uploaded into RAM and is lost after switching off the scope or disconnecting
111+
the USB, so the device can never be *bricked*.
112+
113+
This simple program sets the calibration output frequency to 400 Hz
114+
(you can use each even divison of 2 MHz between 32 Hz and 100 kHz).
115+
116+
```python
117+
#!/usr/bin/python3
118+
119+
# get the python package
120+
from PyHT6022.LibUsbScope import Oscilloscope
121+
122+
# create an Osclloscope object
123+
scope = Oscilloscope()
124+
125+
# setup the scope
126+
scope.setup()
127+
128+
# attach to the scope
129+
scope.open_handle()
130+
131+
# upload firmware unless already uploaded
132+
if (not scope.is_device_firmware_present):
133+
scope.flash_firmware()
134+
135+
# and now set the calibration frequency output to 400 Hz
136+
scope.set_calibration_frequency( 400 )
137+
```
138+
86139
## It even works under Windows
87140

88141
[@justlep](https://github.com/justlep) wrote:
@@ -150,10 +203,14 @@ Configure with command line arguments:
150203
-e, --eeprom store calibration values in eeprom
151204
-g, --measure_gain interactively measure gain (as well as offset)
152205

206+
### Fast Offset Calibration
207+
153208
Apply 0 V to both inputs (e.g. connect both probes to the GND calibration connector) and execute:
154209

155210
calibrate_6022.py -e
156211

212+
### Complete Offset and Gain Calibration
213+
157214
If is also possible to measure and create also gain calibration.
158215
To calibrate gain you have to apply a well known voltage (setpoint)
159216
and compare it with the actual value that is read by the scope:
@@ -172,24 +229,26 @@ the program measures and compares them against the expected gain settings:
172229
6. The program option `-e` stores the calibration values in eeprom
173230
7. The program option `-c` creates a config file `modelDSO6022.conf`
174231

175-
This config file can be copied into directory `~/.config/OpenHantek`.
232+
This (optional) config file can be copied into directory `~/.config/OpenHantek`.
176233
On every startup OpenHantek reads this file and applies the calibratipon accordingly.
234+
The config file has higher priority than the eeprom content.
235+
It has also the advantage not to mess with the eeprom.
177236

178237
The calibration voltages do not have to correspond absolutely to the given value,
179-
but the applied voltage should not be much higher than the given value and must be determined exactly -
180-
e.g. by measuring it with a multimeter. Type in the measured voltage at the prompt.
181-
4 AA batteries in a battery holder are a simple and reliable voltage source:
238+
but the applied voltage should not be much higher than the proposed value and must be determined exactly -
239+
e.g. by measuring it with a multimeter. Type in the real measured voltage at the prompt.
240+
4 fresh AA batteries in a battery holder are a simple and reliable voltage source:
182241

183-
Requested Voltage | Applied Voltage | Comment
184-
------------------|-----------------|--------
185-
0.4 V | 0.3 V | 2 x AA with 1/10 probe
186-
0.8 V | 0.6 V | 4 x AA with 1/10 probe
187-
2.0 V | 1.5 V | 1 x AA
188-
4.0 V | 3.0 V or 4.5 V | 2 or 3 x AA
242+
Requested Voltage | Applied Voltage | Comment
243+
------------------|------------------|--------
244+
0.4 V | ~0.3 V | 2 x AA with 1/10 probe
245+
0.8 V | ~0.6 V | 4 x AA with 1/10 probe
246+
2.0 V | ~1.5 V | 1 x AA
247+
4.0 V | ~3.0 V or ~4.5 V | 2 or 3 x AA
189248

190249
[Read more about the eeprom content...](docs/README.md#eeprom)
191250

192-
## Use the device as a data logger ##
251+
## Use the device as a data logger
193252

194253
The program `capture_6022.py` (also in `/usr/bin/`) allows to capture both channels over a long time.
195254

examples/calibrate_6022.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from PyHT6022.LibUsbScope import Oscilloscope
2222

23+
import sys
2324
import time
2425
import binascii
2526

@@ -91,7 +92,8 @@ def read_avg( voltage_range, sample_rate=110, repeat = 1, samples = 12 * 1024 ):
9192

9293
scope = Oscilloscope()
9394
scope.setup()
94-
scope.open_handle()
95+
if not scope.open_handle():
96+
sys.exit( -1 )
9597

9698
if (not scope.is_device_firmware_present):
9799
scope.flash_firmware()

examples/capture_6022.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292

9393
scope = Oscilloscope()
9494
scope.setup()
95-
scope.open_handle()
95+
if not scope.open_handle():
96+
sys.exit( -1 )
9697

9798
# upload correct firmware into device's RAM
9899
if (not scope.is_device_firmware_present):

examples/continous_read.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from PyHT6022.LibUsbScope import Oscilloscope
66
import matplotlib.pyplot as plt
7+
import sys
78
import time
89
import numpy as np
910
from collections import deque
@@ -36,7 +37,8 @@ def build_stability_array(data, threshold=1.0):
3637

3738
scope = Oscilloscope()
3839
scope.setup()
39-
scope.open_handle()
40+
if not scope.open_handle():
41+
sys.exit()
4042
scope.flash_firmware()
4143
scope.set_interface(1) # choose ISO
4244
scope.set_num_channels(1)

examples/get_calibration.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
__author__ = 'Jochen Hoenicke'
44

5+
import sys
56
from PyHT6022.LibUsbScope import Oscilloscope
67

78
scope = Oscilloscope()
89
scope.setup()
9-
scope.open_handle()
10+
if not scope.open_handle():
11+
sys.exit( -1 )
12+
1013
calibration = scope.get_calibration_values( 48 )
1114
scope.close_handle()
12-
1315
# print( calibration )
1416
for x in calibration:
1517
print( hex(x), end=" " )

examples/get_firmware_version.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99
scope = Oscilloscope()
1010
scope.setup()
11-
scope.open_handle()
12-
print( hex( scope.get_fw_version() ) )
13-
scope.close_handle()
11+
version = scope.get_fw_version()
12+
if version:
13+
print( hex( version ) )

examples/read_eeprom.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
__author__ = 'Jochen Hoenicke'
44

5+
import sys
56
from PyHT6022.LibUsbScope import Oscilloscope
67

78
scope = Oscilloscope()
89
scope.setup()
9-
scope.open_handle()
10+
if not scope.open_handle():
11+
sys.exit( -1 )
12+
1013
eeprom = scope.read_eeprom(0, 8)
1114
scope.close_handle()
1215

examples/read_eeprom_256_byte.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#!/usr/bin/python3
22

3+
import sys
34
from PyHT6022.LibUsbScope import Oscilloscope
45

56
scope = Oscilloscope()
67
scope.setup()
7-
scope.open_handle()
8+
if not scope.open_handle():
9+
sys.exit( -1 )
10+
811
eeprom = scope.read_eeprom( 0, 256 )
912
scope.close_handle()
1013

0 commit comments

Comments
 (0)