Rohde-Schwarz / Examples

Remote-control examples for our instruments. Here you find all ready-to-use example code scripts and projects.
MIT License
105 stars 32 forks source link

Code refactoring (HMC8012 example) #6

Closed Vincent-Stragier closed 1 year ago

Vincent-Stragier commented 1 year ago

Hi,

I've just got my hands on a HMC8012 for a small project and I wanted to use Python to collect the data. I was happy to find this example, however, I had/have some troubles and questions.

First, the example was not running directly, I had an uncaught TimeoutException in the try, except block. The block can catch IOError which for me seems a bit odd (I do not grasp how that kind of error can occur).

Second, the default path on the computer for the log file does not exist on every computer. Thus, the script crashes because of the missing path. So I implemented a way to automatically create the path.

Third, I don't know if it makes sense to use write_str() instead of write_str_with_opc(). Isn't write_str_with_opc() better to ensure that the commands are well received? Or did I misunderstand something about the OPC?

Fourth, I've added parameters to the function. I generally prefer to avoid the usage of global variable in the code. Well, I've left the instrument global variable, but looking at the example, I wonder if creating an instrument class wouldn't be cleaner. It's mostly an aesthetic consideration.

Fifth, the script only works one or two times in a row, then, the HMC8012 behave weirdly, either, it cannot create the file internally, or it doesn't start the sample (the counter stays stuck at 0). Well, for the second behaviour, I wonder if it did not occur when trying to measure a current with an erroneous range of 400 mA instead of 200 mA.

Best, Vincent

P.S.: I need to retest the code, since I modified it Friday with the tool in front of me, then today, only with Copilot (so the code, might need some debug).

Vincent-Stragier commented 1 year ago

Hello,

I can confirm that this code is working. However, I still have crashes on the HMC8012 when launching the script more than once without restarting the multimeter manually.

PS C:\Users\Vincent\OneDrive - UMONS\Documents\GitHub\R-S_Examples> & C:/Users/Vincent/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/Vincent/OneDrive - UMONS/Documents/GitHub/R-S_Examples/Misc/Python/RsInstrument/RsInstrument_HMC8012_measurement_logging_filetransfer.py"
Program started.
VISA Manufacturer: Rohde & Schwarz GmbH
Hello, I am HAMEG,HMC8012,019030712,01.400

Content of the directory at the start is ['', 'TEST.CSV', 'TEST0001.CSV', 'TEST0002.CSV', 'TEST0003.CSV']
Hello, I am HAMEG,HMC8012,019030712,01.400

Content of the directory at the start is ['', 'TEST0001.CSV', 'TEST0002.CSV', 'TEST0003.CSV']
Log File Name now is "/INT/DATA/TEST.CSV"
Logging runs now for 5 seconds - please wait!

Now transferring the log file...
File saved locally to c:\Tempdata\logdata.csvProgram successfully ended.
PS C:\Users\Vincent\OneDrive - UMONS\Documents\GitHub\R-S_Examples> & C:/Users/Vincent/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/Vincent/OneDrive - UMONS/Documents/GitHub/R-S_Examples/Misc/Python/RsInstrument/RsInstrument_HMC8012_measurement_logging_filetransfer.py"
Program started.
VISA Manufacturer: Rohde & Schwarz GmbH
Hello, I am HAMEG,HMC8012,019030712,01.400

Content of the directory at the start is ['No Logging Files found']
Log File Name now is "/INT/DATA/TEST.CSV"
Logging runs now for 5 seconds - please wait!

Now transferring the log file...

Traceback (most recent call last):
  File "c:\Users\Vincent\OneDrive - UMONS\Documents\GitHub\R-S_Examples\Misc\Python\RsInstrument\RsInstrument_HMC8012_measurement_logging_filetransfer.py", line 176, in <module>
    file_get(log_file_name=log_file_name, pc_log_file_name=pc_log_file_name)
  File "c:\Users\Vincent\OneDrive - UMONS\Documents\GitHub\R-S_Examples\Misc\Python\RsInstrument\RsInstrument_HMC8012_measurement_logging_filetransfer.py", line 149, in file_get
    log_data = hmc8012.query_str_with_opc(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python311\Lib\site-packages\RsInstrument\RsInstrument.py", line 461, in query_str_with_opc
    return self._core.io.query_str_with_opc(query, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python311\Lib\site-packages\RsInstrument\Internal\Instrument.py", line 712, in query_str_with_opc
    response = self._session.query_str_with_opc(query, timeout, log_info)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python311\Lib\site-packages\RsInstrument\Internal\VisaSession.py", line 885, in query_str_with_opc
    response = self._read_str()
               ^^^^^^^^^^^^^^^^
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python311\Lib\site-packages\RsInstrument\Internal\VisaSession.py", line 831, in _read_str
    self._read_unknown_len(stream, True)
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python311\Lib\site-packages\RsInstrument\Internal\VisaSession.py", line 745, in _read_unknown_len
    chunk, self.last_status = self._session.visalib.read(self._session.session, chunk_size)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyvisa\ctwrapper\functions.py", line 2337, in read 
    ret = library.viRead(session, buffer, count, byref(return_count))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 226, in _return_handler
    return self.handle_return_value(session, ret_value)  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Vincent\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
    raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.

Regards, Vincent

Miloslav-RS commented 1 year ago

Hi Vincent, thx for the report and the contribution. I'll merge your version, and we will investigate the HMC8012 behavior. HMC8012 is quite an old instrument, there might be some issues with the remote controlling it. Keep in mind, the examples are for demonstration purposes, they do not consider all the possible eventualities.