Search code examples
python-3.xunicodedata-streamoscilloscopeerrorcontrolsystem

Streaming data from the R&S rto oscilloscope - UnicodeDecodeError python3.6


I'm trying to get the signal data for a specific channel on the Rhode and Schwarz RTO oscilloscope . I'm using the vxi11 python(3.6) library to communicate with the scope.

On my first try, I was able to extract all the data of the scope channel I was querying without any errors(using this query command CHAN1:WAV1:DATA?) but soon after I started getting this error message.

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 10: invalid continuation byte

The wierd thing is that I'm still able to get the head of the data without any issues. It's only when I request the entire data to be sent over that I see this error.

I've tried to change the format of the data between REAL(binary) and ASCii, but to no avail.

Another weird thing is that when I switch the data encoding of the received data to 'latin-1', it works fine for a moment(giving me a strange character string, that I'm assuming is the data I want - just in another format) and then crashes.

The entire output looks as follows:

****IDN : Rohde&Schwarz,RTO,1329.7002k04/100938,4.20.1.0
FORM[:DATA]ASCii : None
CHAN1:WAV1:DATA:HEAD? : -0.2008,0.1992,10000000,1
'utf-8' codec can't decode byte 0xc7 in position 10: invalid continuation byte
'utf-8' codec can't decode byte 0xc7 in position 10: invalid continuation byte
Traceback (most recent call last):
  File "testing_rtodto.py", line 21, in ask_query
    logger.debug(print(query+" :",str(conn._ask(query))))
  File "../lib_maxiv_rtodto/client.py", line 187, in _ask
    response = self.instrument.ask(data)#, encoding="latin-1")
  File "/usr/lib/python3.6/site-packages/vxi11/vxi11.py", line 743, in ask
    return self.read(num, encoding)
  File "/usr/lib/python3.6/site-packages/vxi11/vxi11.py", line 731, in read
    return self.read_raw(num).decode(encoding).rstrip('\r\n')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 10: invalid continuation byte

Solution

  • Alrighty, I found a fix. Thanks mostly to this thread https://github.com/pyvisa/pyvisa/issues/306

    Though I'm not using the same communication library as they are, the problem seemed to be the way I was querying the data not how the library was reading it. Turns out you have to follow R&S's instrument instructions very very VERY closely(although their documentation is extremely confusing and hard to find - not to mention the lack of example query strings for important query functions)

    Essentially, the query command that worked was FORM ASC;:CHAN1:DATA?. This explicitly converts the data to ASCii format before returning it to the communicating library.

    I also found some sample python scripts that R&S have provided (https://cdn.rohde-schwarz.com/pws/service_support/driver_pagedq/files_1/directscpi/DirectSCPI_PyCharm_Python_Examples.zip).