Search code examples
pythonunicodevisa

Importing VISA waveform from an oscilloscope into Python


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.


Solution

  • 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.