尝试获取示波器显示时的UnicodeDecodeError

时间:2017-02-17 07:13:51

标签: python ascii decode encode

我在Python 2.7中尝试使用SCPI命令查询设备(示波器)以获取屏幕截图时收到此错误。

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

以下是相关的代码行以及回溯:

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)

我已经查看了其他论坛并且已经看到编码/解码是错误的根源,但是只是刚开始用Python编程而且我仍然对如何编码或解码成正确的数据类型(我甚至不知道应该是什么。)

1 个答案:

答案 0 :(得分:0)

我找到的修复程序位于here的更新手册中。

它改变了

的代码
    final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
    final int index = jvmName.indexOf('@');
    if(index > 1) {
        try {
            String processId = Long.toString(Long.parseLong(jvmName.substring(0, index)));
            Scanner scan = new Scanner(Runtime.getRuntime().exec("wmic process where (ParentProcessId="+ processId +") get Caption,ProcessId").getInputStream());
            scan.useDelimiter("\\A");
            String childProcessIds =  scan.hasNext() ? scan.next() : "";
            List<String> chromeDrivers = new ArrayList<String>();
            String[] splited = childProcessIds.split("\\s+");
            for(int i =0 ; i<splited.length; i = i+2){
                if("chromedriver.exe".equalsIgnoreCase(splited[i])){
                    chromeDrivers.add(splited[i+1]);
                }
            }
            /*              
            *
            *Do whatever you want to do with the chromedriver's PID here    
            *
            * */        
            scan.close();
        } catch (Exception e) {

        }
    }

更新

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

设法修复编码错误。