0

I am receiving this error in Python 2.7 when trying to query my device (oscilloscope) using SCPI commands to obtain a screen capture.

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

Here is the relevant line of code as well as the traceback:

InfiniiVision.query(":DISPlay:DATA? PNG, COLor")

#Traceback

  File "C:/Users/William/Desktop/example3.py", line 334, in <module>
    InfiniiVision.query(":DISPlay:DATA? PNG, COLor")

  File "C:\ProgramData\Anaconda2\lib\site-packages\pyvisa\resources\messagebased.py", line 407, in query
    return self.read()

  File "C:\ProgramData\Anaconda2\lib\site-packages\pyvisa\resources\messagebased.py", line 332, in read
    message = self.read_raw().decode(enco)

I have looked at other forums and have seen that the encoding/decoding is the root of the error, however have only just started programming in Python and am still lost about how to encode or decode into the correct data type (which I don't even know what it is supposed to be).

  • The data type is string, but you have to know the encoding that the source produces in order to correctly decode it into a valid string. Python is picky about this, and it can be frustrating, but it's for a good reason -- too many programs are "garbage in, garbage out" because the programmer didn't understand the issues with character encodings. – tripleee Feb 17 '17 at 07:22
  • Python 3 has the concept of a byte string, but as a quick and dirty fix for Python 2, you can specify `'latin-1'` as the encoding for stuff which should just be accepted verbatim. – tripleee Feb 17 '17 at 07:23
  • Possible duplicate of [Convert bytes to a Python string](http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string) – tripleee Feb 17 '17 at 07:24
  • What is `InfiniiVision`? Something from some specific library? – Bakuriu Feb 17 '17 at 07:37
  • I have tried to change the encoding to to latin-1 by changing the first line of the file from # -*- coding: utf-8 -*- to # -*- coding: latin-1 -*- however am still receiving the same error. – discombobulator23 Feb 17 '17 at 07:40
  • @Bakuriu InfiniiVision is just the name of the variable for myScope (i.e. my oscilloscope). The scope I am using is the DSO-X-2024A. They have a programmers manual which I am also using however seems to be significantly out of date. – discombobulator23 Feb 17 '17 at 07:42
  • What is a "scope"? What is DSO-X-2024A? I believe there is a *significant* amount of information about the environment in which you are running the code that is missing from the question. Also: you misunderstood the meaning of the `coding: xxx` comment. It tells the compiler how to read *the source code*. But your problem is about encoding/decoding **data**, hence it does not matter in the slightest what that comment is... the issue is that you should pass the encoding to the functions you are calling, but to do this we have to know what are they since it depends on their API. – Bakuriu Feb 17 '17 at 07:56
  • The encoding of the source code of your script is immaterial, you can keep that in utf-8 or any other encoding which is convenient to you. When you open a file handle (or whatever you are doing) you need to mention in the code which encoding you expect the input stream to contain. – tripleee Feb 17 '17 at 08:26

1 Answers1

0

The fix I found was in an updated manual found here.

It changed the code from

sDisplay = do_query_string(":DISPlay:DATA? PNG, COLor")
sDisplay = get_definite_length_block_data(sDisplay)

to the updated

sDisplay = do_query_ieee_block(":DISPlay:DATA? PNG, COLor")

which managed to fix the encoding error.