Re: Python bindings and a suggestion for improvement
Tomislav Ivek <[email protected]>
| Newsgroups | gmane.linux.hardware.gpib.general |
|---|---|
| Message-ID | <CAAgNE5n=A1nQ1ir08jjHxCWg+XkKPMVU6+7w00POJMwsgWweMw@mail.gmail.com> |
It turns out Python bindings do not expose the iblines() functionality. This would be most useful for pyvisa-py to test if REN is asserted. I've been using the attached pach to call iblines() from Python. It adds one new function, gpib.lines(), and line-related constants to the gpib module. It also introduces the corresponding method Gpib.lines() to the object-oriented Gpib module. Cheers, Tomislav On Wed, Nov 28, 2018 at 3:49 PM Tomislav Ivek <[email protected]> wrote: > Great, thank you! > > Tomislav > > > On Wed, Nov 28, 2018, 15:37 dave penkler <[email protected] wrote: > >> Patch applied. Thanks. >> >> On Wed, Nov 28, 2018 at 1:52 PM Tomislav Ivek <[email protected]> >> wrote: >> >>> Hi Frank, thanks for the reply. Attached is the patch that introduces a >>> safe Gpib.Gpib.close() which will ensure proper cleanup. Please take a look. >>> >>> Cheers, >>> Tomislav >>> >>> >>> On Wed, Nov 28, 2018 at 11:15 AM Frank Mori Hess <[email protected]> >>> wrote: >>> >>>> On Tue, Nov 27, 2018 at 4:19 PM Tomislav Ivek <[email protected]> >>>> wrote: >>>> > >>>> > The pure-Python VISA backend pyvisa-py has recently stumbled upon a >>>> double-close situation with linux-gpib's Python class Gpib.Gpib: >>>> https://github.com/pyvisa/pyvisa-py/pull/171 This bug is related to a >>>> design point in Gpib.Gpib which cleans up its GPIB handle only on Python's >>>> GC cycle. This is not guaranteed to run when the programmer expects it to >>>> and in extreme cases might even leak resources. It appears Gpib.Gpib has no >>>> other user-facing method to close its GPIB handle. >>>> > >>>> > This can be resolved by adding a Gpib.close() method for >>>> user-controlled cleanup. I hav ealready tested the fix in gpib_ctypes, a >>>> cross-platform GPIB binding that aims to be compatible with linux-gpib ( >>>> https://pypi.org/project/gpib-ctypes/). I would be happy to send a >>>> patch for linux-gpib's Python lib. >>>> > >>>> > Please let me know what you think about that. >>>> >>>> >>>> I'm no python expert, but your change sounds reasonable to me. >>>> >>> _______________________________________________ >>> Linux-gpib-general mailing list >>> [email protected] >>> https://lists.sourceforge.net/lists/listinfo/linux-gpib-general >>> >> _______________________________________________ Linux-gpib-general mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-gpib-general
python_gpib_lines.patch
(text/x-patch, 2.9 KB)
Index: linux-gpib-user/language/python/gpibinter.c
===================================================================
--- linux-gpib-user/language/python/gpibinter.c (revision 1771)
+++ linux-gpib-user/language/python/gpibinter.c (working copy)
@@ -210,6 +210,31 @@
return PyBool_FromLong(found_listener);
}
+static char gpib_lines__doc__[] =
+ "lines -- get status of the control and handshaking bus lines (board)\n"
+ "lines(handle) -> line_status_int";
+
+static PyObject* gpib_lines(PyObject *self, PyObject *args)
+{
+ int board;
+ int sta;
+ short line_status;
+
+ if(!PyArg_ParseTuple(args, "i:lines", &board))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ sta = iblines(board, &line_status);
+ Py_END_ALLOW_THREADS
+
+ if(sta & ERR){
+ _SetGpibError("lines");
+ return NULL;
+ }
+
+ return PyInt_FromLong(line_status);
+}
+
static char gpib_read__doc__[] =
"read -- read data bytes (board or device)\n"
"read(handle, num_bytes) -> string";
@@ -617,6 +642,7 @@
{"dev", gpib_dev, METH_VARARGS, gpib_dev__doc__},
{"config", gpib_config, METH_VARARGS, gpib_config__doc__},
{"listener", gpib_listener, METH_VARARGS, gpib_listener__doc__},
+ {"lines", gpib_lines, METH_VARARGS, gpib_lines__doc__},
{"read", gpib_read, METH_VARARGS, gpib_read__doc__},
{"write", gpib_write, METH_VARARGS, gpib_write__doc__},
{"write_async", gpib_write_async, METH_VARARGS, gpib_write_async__doc__},
@@ -772,6 +798,24 @@
PyModule_AddIntConstant(m, "IbStbRQS", IbStbRQS);
PyModule_AddIntConstant(m, "IbStbESB", IbStbESB);
PyModule_AddIntConstant(m, "IbStbMAV", IbStbMAV);
+
+ /* line status bits */
+ PyModule_AddIntConstant(m, "ValidDAV", ValidDAV);
+ PyModule_AddIntConstant(m, "ValidNDAC", ValidNDAC);
+ PyModule_AddIntConstant(m, "ValidNRFD", ValidNRFD);
+ PyModule_AddIntConstant(m, "ValidIFC", ValidIFC);
+ PyModule_AddIntConstant(m, "ValidREN", ValidREN);
+ PyModule_AddIntConstant(m, "ValidSRQ", ValidSRQ);
+ PyModule_AddIntConstant(m, "ValidATN", ValidATN);
+ PyModule_AddIntConstant(m, "ValidEOI", ValidEOI);
+ PyModule_AddIntConstant(m, "BusDAV", BusDAV);
+ PyModule_AddIntConstant(m, "BusNDAC", BusNDAC);
+ PyModule_AddIntConstant(m, "BusNRFD", BusNRFD);
+ PyModule_AddIntConstant(m, "BusIFC", BusIFC);
+ PyModule_AddIntConstant(m, "BusREN", BusREN);
+ PyModule_AddIntConstant(m, "BusSRQ", BusSRQ);
+ PyModule_AddIntConstant(m, "BusATN", BusATN);
+ PyModule_AddIntConstant(m, "BusEOI", BusEOI);
/* Check for errors */
if (PyErr_Occurred())
Py_FatalError("can't initialize module gpib");
Index: linux-gpib-user/language/python/Gpib.py
===================================================================
--- linux-gpib-user/language/python/Gpib.py (revision 1771)
+++ linux-gpib-user/language/python/Gpib.py (working copy)
@@ -63,6 +63,10 @@
self.res = gpib.listener(self.id,pad,sad)
return self.res
+ def lines(self):
+ self.res = gpib.lines(self.id)
+ return self.res
+
def ask(self,option):
self.res = gpib.ask(self.id,option)
return self.res