Reading the query buffer
Introduction
Whenever a query (write command sending a string with a trailing "?") is sent to the SCPI server, the reply is stored in a buffer and can be retrieved using a read command. Being a buffer, the first unread reply will be fetched first (FIFO queue).
visaSession.write(":SYSTem:ERRor:ALL?")
allErrors = visaSession.read()
If a reply doesn´t get retrieved and a further query is sent, followed by a read-command, this will fetch the answer of the first query. This can easily lead to some unexpected behavior and thus it´s important to keep queries and read commands synchronized.
In the following example a query is sent using the write-command and terminating the string with a question mark. Then, before fetching the first query´s reply, a further query is fired and eventually a read-command is executed to get a value from the buffer. the lockOK variable will receive the reply of the first query:
visaSession.write(":SYSTem:ERRor:ALL?")
visaSession.write(":SYST:LOCK:REQ?")
lockOK = visaSession.read()
print("The value of lockOK is: " + lockOK)
This flaw can be corrected adding a read-command after the first query:
visaSession.write(":SYSTem:ERRor:ALL?")
allErrors = visaSession.read() # added read command
print("The value of allErrors is: " + allErrors)
visaSession.write(":SYST:LOCK:REQ?")
lockOK = visaSession.read()
print("The value of lockOK is: " + lockOK)
The Python pyvisa package offers a "query" method which combines write and read.
relOK = visaSession.query(':SYSTem:LOCK:RELease?')
In this case the described issue cannot take place even if the returned value is not stored in any variable. The "query" method is used in most examples included in this user guide. Not every language offers such method and the user should be aware of the SCPI behavior.