Importing VISA waveform from an oscilloscope into Python

4k views Asked by At

I having problems with the return result from this VISA acquisition call:

    ribData = []
    ribData = inst.query('CURVe?')

I am using this call to grab a waveform from an oscilloscope. I am developing this program in Python.

If the values are positive, the call returns the binary values and I can graph them, but if I drop the waveform on the scope below the halfway point I receive the error:

Traceback (most recent call last):

    File "C:/_Python/TDS3054/mainTds.py", line 107, in cButton
       ribData = inst.query('CURVe?')
    File "C:\Python34\lib\site-packages\pyvisa\resources\messagebased.py", line 384, in query
       return self.read()
    File "C:\Python34\lib\site-packages\pyvisa\resources\messagebased.py", line 309, in read
       message = self.read_raw().decode(enco)
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 401: ordinal not in range(128)

I use the following VISA calls to set up the oscilloscope prior to the acquisition.

    inst.write('DATa:SOUrce CH2')
    inst.write('DATa:WIDth 1')
    inst.write('DATA:START 10')
    inst.write('DATA:STOP 800')
    inst.write('DATa:ENCdg RIBinary')  # RIB -128 to +127

Because, the failure occurs on the acquisition call (CURVe), I was wondering is there a VISA library call available to solve this problem. Perhaps I need to set the Unicode to UTF-8 or perhaps VISA doesn't deal with Unicodes or perhaps this is not my problem.

2

There are 2 answers

1
Rick On BEST ANSWER

Most likely it was a Unicode problem, but I found the answer in the pyVisa interface document. In this document I found the function query_binary_values(), and I replaced the inst.Query('CURVe") with it. This is how I used it.

tdsData = inst.query_binary_values('CURVe?', datatype='b', is_big_endian=True)

The data that was returned did not have the UnicodeDecodeError, and I was able to plot all the lines with no problem.

0
Logic1 On

Notice in that error it states that it's using 'ascii' encoding. This is not preferred as I'm pretty sure your instrument is outputting a wider range of value than that encoding method can handle:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 401: ordinal not in range(128)

So to fix (at least in my case) you can specify a different encoding method. First reference your instrument in python then set its encoding to 'latin-1'

eg:

inst = rm.open_resource('USB0::0x1AB1::0x0588::DS1ET152915193::INSTR')
inst.encoding =  "latin-1"

Then to read in the values and convert them to an array of scalars just do something like:

data = map(ord, inst.query(":WAVeform:DATA? FFT")) #UTF string to int array conversion

data should now contain all "full-ranged" values.