Skip to content

How to properly release a serial port in Instrumental? #174

@pmcculey

Description

@pmcculey

I'm making a driver for a Mayuno M8811 SMU. This is a relatively cheap unit & only has a serial interface. I need to clean up & release the serial port after the program exits but can't find a way to do it. Most of my other stuff have ethernet, gpib or usb interfaces so this issue does not arise for those I think.

my conf file entry for the instrument is:
MaynuoSMU = {'visa_address': 'ASRL4::INSTR', 'baud_rate':38400, 'module': 'sourcemeters.M8811', 'classname': 'MayunoSourcemeter'}

I fudged things by opening the instrument with the following line:
SMU=instrument('MaynuoSMU', reopen_policy='new')
and closing it with : SMU.close() at the end of the code

This worked for single measurements but I'd prefer to be using 'reuse' rather than 'new' when opening, as recommended in the documents.
I then tried to make an animated plot as follows:

All imports here.............

def update(frame, SMU, data, plt_samples, line, fig):
SMU.initiate()
value = SMU.measure_current().magnitude
if value is not None:
data.append(value)
del data[:plt_samples*-1:] # Keep only the last n values as defined by the plt_samples variable
line.set_ydata(data)
plt.xlim(0, len(data))
if min(data) != max(data):
plt.ylim(min(data), max(data))
fig.canvas.draw()
return line,

if name == 'main':
SMU=instrument('MaynuoSMU', reopen_policy='new') # have to use 'new' for the moment as otherwise the com port does not close on exit
SMU.setVoltage('5V')
SMU.setMaxCurrent('0.2A')
SMU.sense = True
print('Sense: ',SMU.sense)
SMU.output_on=True
print ('DVM: ', SMU.measure_dvm() )
#The above works fine
if plot is true:
#setup an animated plot
y_label = 'Amps'
plt.style.use("dark_background")
fig, ax = plt.subplots(figsize=(8,4.5))
plt.suptitle( 'M8811' )
plt.xlabel('Sample#')
plt.ylabel(y_label)
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
ax.ticklabel_format(style='sci', axis='x')
data = [0] * plt_samples # Initialize with zeros
line, = ax.plot(data, 'r-')
plt.autoscale(enable=True, axis='y', tight=None)
ani = animation.FuncAnimation(fig, update, fargs=(SMU, data, plt_samples, line, fig), interval=1, blit=True, cache_frame_data=False)
plt.show()
SMU.close()

The SMU.close() line executes meaning the resource closes. This means that the animation callback fails when trying to plot. If I comment out the last line, and open the instrument with 'strict' or 'reuse' all works fine until I exit (by closing the animated plot). At that point, the port is unfortunately not released. Ideally the code would automatically release the port when the code exits.

In the driver the close function is:
def close(self):
self._rsrc.write('syst:loc') # local panel control
self._rsrc.close()

I suspect this is something I'm not doing right rather than a bug in Instrumental, but I have no clue where to start looking. Any ideas appreciated.

Thanks,

Joe

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions