Posted Sat, 02 Sep 2023 01:04:57 GMT by Kish, Alex
Having a random timeout problem with Keithley 2400 that is driving me nuts and became a challenge.

In the experiment the constant voltage is sourced, and I only read current. I need to do it for as long as I can continuously.

Does not matter fast or slow and how many timeouts and error catchers are implemented.

This is my code. Managed to capture the visa timeout error (by 'try' first and re-establish connection after an exception was raised) but now a new problem.

The firmware is C32 from October 14, 2010. I have three of these Keithleys and they behave the same.

It looks to me like sometimes the instrument throws a strange message that cannot be interpreted.

Using serial RS232 protocol and 'pymeasure' that is based on 'pyvisa'.
Tried GPIB but it does not work on a Mac.

Re-starting the script solves the problem, sometimes with an error about header.


        for stepZ in range(scan_points_Z):
            new_position_Z = scan_start_steps_Z + stepZ*scan_step_Z
            new_position_Z_mm = new_position_Z * microstep_size_T
            #print('position Z = ', new_position_Z)
            reply = detector_Z.move_abs(int(new_position_Z))
            #time.sleep(scan_delay)
   
            if (index % 100) == 0:
                print('index = ', index)

            #time.sleep(0.1)
            try:
                keithley.measure_current(nplc=0.01, current=10e-6, auto_range=False)
                time.sleep(0.1)
                PDcurrent = keithley.current*(-1)
            except:
                print('Crashed, restarting')
                keithley.clear()
                keithley.reset()
                time.sleep(10.0)
                # CONNECT TO KEITHLEY
                from pymeasure.instruments.keithley import Keithley2400
                from pymeasure.instruments import list_resources
                keithley = Keithley2400("ASRL1::INSTR", timeout=200000, baud_rate=57600)
               

                keithley.read_termination = "\n"
                keithley.write_termination = "\n"

                # IDENTIFY KEITHLEY INSTRUMENT
                keithley.write("*IDN?")
                time.sleep(0.1)
                keithley.read()
                
                #keithley.shutdown()
                time.sleep(2.0)

                keithley.reset()
                keithley.use_front_terminals()


 
index =  0
index =  100
index =  200
index =  300
index =  400
index =  500
index =  600
index =  700
index =  800
index =  900
index =  1000
index =  1100
Crashed, restarting
---------------------------------------------------------------------------
VisaIOError                               Traceback (most recent call last)
Cell In [9], line 64
     63 try:
---> 64     keithley.measure_current(nplc=0.01, current=10e-6, auto_range=False)
     65     time.sleep(0.1)

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/keithley/keithley2400.py:434, in Keithley2400.measure_current(self, nplc, current, auto_range)
    433     self.current_range = current
--> 434 self.check_errors()

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/keithley/keithley2400.py:524, in Keithley2400.check_errors(self)
    522 """ Logs any system errors reported by the instrument.
    523 """
--> 524 code, message = self.error
    525 while code != 0:

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/instrument.py:211, in Instrument.__getattribute__(self, name)
    209         raise AttributeError(
    210             f"{name} is a reserved variable name and it cannot be read")
--> 211 return super().__getattribute__(name)

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/keithley/keithley2400.py:514, in Keithley2400.error(self)
    512 """ Returns a tuple of an error code and message from a
    513 single error. """
--> 514 err = self.values(":system:error?")
    515 if len(err) < 2:

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/instrument.py:317, in Instrument.values(self, command, separator, cast, preprocess_reply)
    306 """ Writes a command to the instrument and returns a list of formatted
    307 values from the result.
    308 
   (...)
    315 :returns: A list of the desired type, or strings where the casting fails
    316 """
--> 317 results = str(self.ask(command)).strip()
    318 if callable(preprocess_reply):

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/instrument.py:303, in Instrument.ask(self, command, query_delay)
    302 self.wait_for(query_delay)
--> 303 return self.read()

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/instrument.py:263, in Instrument.read(self, **kwargs)
    262 """Read up to (excluding) `read_termination` or the whole read buffer."""
--> 263 return self.adapter.read(**kwargs)

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/adapters/adapter.py:107, in Adapter.read(self, **kwargs)
    100 """Read up to (excluding) `read_termination` or the whole read buffer.
    101 
    102 Do not override in a subclass!
   (...)
    105 :returns str: ASCII response of the instrument (excluding read_termination).
    106 """
--> 107 read = self._read(**kwargs)
    108 self.log.debug("READ:%s", read)

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/adapters/visa.py:150, in VISAAdapter._read(self, **kwargs)
    145 """Read up to (excluding) `read_termination` or the whole read buffer.
    146 
    147 :param kwargs: Keyword arguments for the connection itself.
    148 :returns str: ASCII response of the instrument (excluding read_termination).
    149 """
--> 150 return self.connection.read(**kwargs)

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pyvisa/resources/messagebased.py:486, in MessageBasedResource.read(self, termination, encoding)
    485     termination = self._read_termination
--> 486     message = self._read_raw().decode(enco)
    487 else:

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pyvisa/resources/messagebased.py:442, in MessageBasedResource._read_raw(self, size)
    436 logger.debug(
    437     "%s - reading %d bytes (last status %r)",
    438     self._resource_name,
    439     size,
    440     status,
    441 )
--> 442 chunk, status = self.visalib.read(self.session, size)
    443 ret.extend(chunk)

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pyvisa/ctwrapper/functions.py:2337, in read(library, session, count)
   2336 return_count = ViUInt32()
-> 2337 ret = library.viRead(session, buffer, count, byref(return_count))
   2338 return buffer.raw[: return_count.value], ret

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pyvisa/ctwrapper/highlevel.py:226, in IVIVisaLibrary._return_handler(self, ret_value, func, arguments)
    224         session = None
--> 226 return self.handle_return_value(session, ret_value)

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pyvisa/highlevel.py:251, in VisaLibraryBase.handle_return_value(self, session, status_code)
    250 if rv < 0:
--> 251     raise errors.VisaIOError(rv)
    253 if rv in self.issue_warning_on:

VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In [9], line 105
    102 keithley.write_timeout = None
    103 del keithley.timeout
--> 105 keithley.measure_current(nplc=0.01, current=10e-6, auto_range=False)
    106 time.sleep(0.1)
    108 keithley.stop_buffer()

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/keithley/keithley2400.py:434, in Keithley2400.measure_current(self, nplc, current, auto_range)
    432 else:
    433     self.current_range = current
--> 434 self.check_errors()

File /Applications/xapps/miniconda3/envs/scipy/lib/python3.9/site-packages/pymeasure/instruments/keithley/keithley2400.py:527, in Keithley2400.check_errors(self)
    525 while code != 0:
    526     t = time.time()
--> 527     log.info("Keithley 2400 reported error: %d, %s" % (code, message))
    528     code, message = self.error
    529     if (time.time() - t) > 10:

TypeError: %d format: a number is required, not str

Posted Sat, 02 Sep 2023 01:07:28 GMT by Kish, Alex

I attach the full script. What it does it move some motors from Zaber Technologies, and record signal from Keithley at specific positions/data points.

Posted Wed, 06 Sep 2023 22:56:00 GMT by C, Andrea
Not sure about pyMeasure and what actual SCPI commands it is sending to the 2400.
Looks like a loss of synchronization between the code and the instrument state since the error handler is itself throwing an error!

But since it is working mostly, I am wondering if baud rate of the RS-232 is playing a role.
On our old forum, there are some posts about dodgy RS-232 behavior at the fastest baud rate.  
Try slowing down to 19K baud rate.

If trouble persists, can you reduce the code into smaller bit that shows the issue?
Alternately, try using pySerial or pyVISA and directly send the SCPI commands understood by the 2400.
Posted Thu, 07 Sep 2023 00:35:31 GMT by Kish, Alex
Thank you Andrea.

I managed to fix the error from the error handler by replacing %d with %s in the pymeasure code, I think there was a bug.

However, it worked for a while, and now error handler randomly times out as well.

I will follow the advice and reduce the baud rate. Even the lowest baud rate gave me time out error, but I did not use error handler back then.
If it crashes again, will re-write the code for pySerial or pyVisa and bypass possible issues with high level interpreter in pymeasure.

You must be signed in to post in this forum.