我正在尝试从横河电机示波器读取一些值。我使用pyvisa建立了连接,并且可以很好地发送和接收来自osci的数据。当我尝试读取857个以上的值时,出现此问题。如果设置结束点857,则可以接收全部数据并将其打印/写入文件,但是如果将其设置为858,则会出现下一个错误:
Traceback (most recent call last):
File "osci_connect.py", line 16, in <module>
values1 = (my_osci.query_ascii_values(':WAVEFORM:SEND? 0'))
File "C:\Python37\lib\site-packages\pyvisa\resources\messagebased.py", line 629, in query_ascii_values
delay)
File "C:\Python37\lib\site-packages\pyvisa\resources\messagebased.py", line 447, in read_ascii_values
block = self.read()
File "C:\Python37\lib\site-packages\pyvisa\resources\messagebased.py", line 413, in read
message = self._read_raw().decode(enco)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position 8156: ordinal not in range(128)
这是我使用pydev进行调试时数据的最后一个字节的样子:
b'.04E+00,-0.04E+00,-0.02E+00,0.03E+00,-0.01E+00,-0.01E+00,0.00E+00,-0.02E+00,'
b'-0.01E+00,-0.03E+00,-0.03E+00,0.01E+00,0.04E+00,-0.01E+00,-0.02E+00,-0.06E+0'
b'0,0.02E+00,0.03E+00,0.03E+00,0.01E+00,0.04E+00,0.00E+00,0.01E+00,0.04E+00,0.'
b'03E+00,-0.03E+00,-0.\x00\x005\xc4\x1c\xbf~')
我猜测错误是由“ \ x00 \ x005 ...”字符生成的,但我不明白为什么其他错误会按预期方式返回,并且在857之后我得到了此错误。
以下是我的代码:
import visa
rm = visa.ResourceManager()
#rm = visa.ResourceManager('C:\WINDOWS\system32\visa32.dll')
my_osci = rm.open_resource("TCPIP::172.20.113.189::INSTR",write_termination='\n',read_termination='\n')
print("Hello, I am:" + my_osci.query("*IDN?"))
print("Send ':WAVEFORM:END 1' ")
my_osci.write(":WAVEFORM:FORMAT ASCII")
my_osci.write(":WAVEFORM:START 0")
my_osci.write(":WAVEFORM:END 858")
#values = my_osci.query_ascii_values(':WAVEFORM:END 20')
values1 = (my_osci.query_ascii_values(':WAVEFORM:SEND? 0'))
print("Received values:")
print(values1)