Python.Net cannot import dll - python

I am using python 2.7 to control a Newport 1936-R power meter which uses custom .dll.
It is not VISA (Virtual Instrument Software Architecture) compatible so I can't use pyvisa.
I have installed the USB driver which contains the dll named usbdll.dll. I copied the dll to a system folder so that it is visible. When using Python.Net I can AddReference but when I import the dll I get the error No module named usbdll.
My Python.Net version is 2.3.0

The correct dll to use is UsbDllWrap.dll located in Samples folder of your installation

I found a work around; by using NI Visa Wizard, I generated a driver and installed it. I can communicate with Newport Power Meter 1936-R with a 'raw' connection. I suggest trying NI VISA Wizard before going to Python.Net. The instrument connected can be seen in NI MAX, but not in Device Manager and pyvisa's resource manager. If anybody needs anything I am happy to help.

Related

DLL load failed while `import dolphindb`

I'm trying DolphinDB python API to subscribe a stream table from our DolphinDB cluster. But as I run import dolphindb it reports
ImportError: DLL load failed: The specified module could not be found.
As I read the readme for a second time, I found that only Anaconda python is supported, because of some issue of DLL loading.
But I cannot use Anaconda for some reason, so is there any workaround?
The c++ version python api is based on DolphinDB's c++ api. On windows platform, c++ api is compiled by mingw so that the runtime requires mingw's core dll. The non-Anaconda doesn't come with mingw runtime and hence failed to import the DophinDB python api.
The new version of python api has included mingw's required libraries. Please check it out.

Using libarchive in python on Windows

I'm trying to get libarchive module working in python 3.4 on windows.
I've installed libarchive-c with pip and all went ok but whenever I try to import it into my code or even to run it alone I'me getting error:
OSError: [WinError 126] The specified module could not be found
This is coming from ffi.py from the code below:
libarchive_path = os.environ.get('LIBARCHIVE') or find_library('archive')
libarchive = ctypes.cdll.LoadLibrary(libarchive_path)
I've never used ctypes before but if I understand correctly it is looking for external DLL. So found and installed http://gnuwin32.sourceforge.net/packages/libarchive.htm also I've added C:\Program Files (x86)\GnuWin32\bin to my %PATH% in environmental variables but it still cannot load the module. As it does not give me the name I'm not sure what module it is looking for.
What am I missing?
(disclaimer) I contribute to https://github.com/Changaco/python-libarchive-c and I maintain https://github.com/nexB/scancode-toolkit
Both contain a ctypes binding for libarchive, though ScanCode is for extraction only.
My answer here is for python-libarchive-c, but ScanCode contains some of the DLL you are looking for so I am lacing in a bit of both.
To get python-libarchive-c going on Windows you need a libarchive DLL and its deps that can then be loaded.
There are no pre-built DLLs bundled in python-libarchive-c but I have prebuilt Windows binaries for another project here:
https://github.com/nexB/scancode-toolkit/tree/develop/src/extractcode/bin/win-32/bin
The corresponding source code is there: https://github.com/nexB/scancode-thirdparty-src
And you have MinGW32 build instructions there if you want to rebuild from source yourself: https://github.com/nexB/scancode-thirdparty-src/blob/master/libarchive/build.sh#L47
In general to load a DLL from a path -- assuming that the var libarchive contains the full path to that DLL -- use this:
lib = ctypes.CDLL(libarchive)
Now this is for Scancode. For python-libarchive-c, you could try to set the LIBARCHIVE variable to point the path of your DLL with:
set LIBARCHIVE="C:\.....\libarchive.dll"
Then start Python, import the library and use it.
NB: I did not test this (yet) , but this should work. IF not please file a bug.
I did not run any test on Python 3.4 either. I use primarily Python 2.7.
But the DLL and the code is not Python 2.7-specific at all.
FWIW, the way scancode loads the library is a tad more engaged since it can from the same code load DLLs Win/Linux/Mac for specific 32 or 64 bits archs using conventional locations. You can see the code in action there:
https://github.com/nexB/scancode-toolkit/blob/develop/src/extractcode/libarchive2.py#L64
ScanCode is NOT using python-libarchive-c ATM yet but a different/custom ctypes binding focused on a more specific use case of extraction only. At least it gives you access to a Win DLL and its deps (or instruction a build them) and an example on how to load it correctly.
/HTH

Python pyVISA GPIB connection issue

I'm trying to connect to a GPIB instrument using pyVISA but I get an error. I'm following the simple example on the pyVISA website: http://pyvisa.readthedocs.org/en/master/
I type the following code into the IPython console:
import visa
rm = visa.ResourceManager()
rm.list_resources()
Out[4]:
(u'USB0::0x0957::0x0D0B::MY49431510::INSTR',
u'USB0::0x0957::0x17BC::MY52400338::INSTR',
u'ASRL1::INSTR',
u'ASRL2::INSTR',
u'ASRL5::INSTR',
u'ASRL6::INSTR',
u'ASRL7::INSTR',
u'ASRL8::INSTR',
u'ASRL9::INSTR',
u'ASRL10::INSTR')
However, my GPIB instruments do not show up, just the serial ports and my USB instruments. When I launch the Agilent Connection Expert (or NI MAX) I can see all my GPIB instruments and can communicate with them. I also am able to communicate with the instruments without issue with Matlab.
So when I try and connect to the instrument with the line:
inst = rm.open_resource('GPIB0::19::INSTR')
It throws an error:
VisaIOError: VI_ERROR_INTF_NUM_NCONFIG (-1073807195): The interface type is valid but the specified interface number is not configured.
I've also tried to connect with the USB instruments that show up when I list the resources and they do not throw this error. However, when I try and do a simple query:
inst = rm.open_resource('USB0::0x0957::0x0D0B::MY49431510::INSTR')
inst.query("*IDN?")
Nothing gets returned and no errors get thrown.
I'm using Python(X,Y) 2.7.9.0 with Python version 2.7.9 32 bit
pyVISA version is 1.6.3
NI bitness is 32
Any guidance is appreciated as I'm new to Python.
Thanks
You probably need to start the resource manager with:
rm = ResourceManager('Path to library')
where Path to library is the location of the NI Vista Library.
Also try:
python -m visa info
to ensure that you have it installed with the same word length as the python version that you are running.
I was having the same issue, and did manage to fix it by setting the path address to: 'C:/Program Files/IVI Foundation/VISA/WinNT/agvisa/agbin/visa32.dll'. If you haven't found the solution, I hope it works for you as well.
I had exactly the same problem - USB resources showed up, GPIB resources didn't. I tried to open the resource exactly as you did, inst = rm.open_resource('GPIB0::22::INSTR') (with the GPIB address changed to that of one of my instruments) with exactly the same result. Some inspired random guessing led me to
inst = rm.open_resource('GPIB1::22::INSTR')
which worked perfectly! Once the instrument is open it shows up in the list of resources, as expected. I have no idea why the interface is seen as GPIB1.
I am using the Keysight/Agilent/HP IO libraries suite as my VISA library (the NI library is not installed), and an Agilent 82357B USB to GPIB interface. I did not have to do any customisation of my pyVisa installation to get this working. Which is nice.
Hope this helps!
I had the same problem (running 32-bit python 2.7 on a 64-bit win7), I got it fixed using
rm = ResourceManager('C:/Program Files (x86)/IVI Foundation/VISA/WinNT/agvisa/agbin/visa32.dll')
which is the 32-bit VISA path given in the "About Agilent IO Control" Information.
I have ni-visa and Agilent visa installed on my computer. However, pyvisa cannot access the Agilent 82357B device due to pointing to the wrong library.
I used this command to specify the right library, it worked for me. Couples of notes:
Find the right library for the device which is visa32.dll located in different installation directories depends on each visa packet.
Use forward slash for delimiting characters even it is on windows.
Specify visa packet in the command: rm = visa.ResourceManager('c:/....')
I had the same issue. The problem was that the automatically installed Windows driver was wrong and my device showed up as USB0::0x3923::0x702A::010A65AB::RAW in NI MAX.
It was solved by an update of the driver (device manager-> properties) from C:\Program Files\National Instruments\NI-488.2 (or where your driver is installed).
Now GPIB works also from python.
I observed same error report when running on a PC with visa library from Keysight. Replacing with visa dll from National Instruments solved the problem.

Extract GUID from ActiveX COM DLL using Python

How would you extract the GUID of an ActiveX COM DLL using only Python in a cross-platform manner?
I ran across pythoncom, which comes with pywin32. It has a LoadTypeLib function which can do just that:
import pythoncom
dll = pythoncom.LoadTypeLib("C:\\path\to\\my.dll")
print str(dll.GetLibAttr()[0])
This is better than my current solution which relies on an external CLI application, however a cross-platform solution would be even better as I don't believe pywin32 can be installed on a Linux CI easily.
I ended up using the wonderful comtypes package:
from comtypes.typeinfo import LoadTypeLibEx
lib = LoadTypeLibEx("C:\\path\to\\my.dll")
print lib.GetLibAttr().guid
The code is effectively the same; it literally calls the same LoadTypeLib from OleAut32 so it is Windows-only, however comtypes is installable through pip which makes the distribution easier.

python com component does not register when using libxml2 on win7

i've built a com component which utilizes libxml2 python bindings , the build is successfull but when i try to register the component i get "specified module could not be found , unable to load python dll" this is the error i get when the component is built using the bundle files option set as 1
if i build the component with bundle files set as 3 ,then i get a different error saying that the component was loaded but the call to DllRegisterServer Failed with error code 80040201
if the libxml2 import is removed all works fine.
any help wold be simply great.
thanks
Most likely, regsvr32.exe which registers you COM component couldn't find a DLL that your COM component needs.
I'm not familiar with Python COM components but is there some way you can run depends.exe on it? This is the usual way to track down binary dependency problems.

Categories

Resources