pfroud / JVisa

Java library for VISA (Virtual Instrument Software Architecture) instruments
Apache License 2.0
38 stars 13 forks source link

Does this library work on MacOSX? #3

Closed Amaruk2365 closed 2 years ago

Amaruk2365 commented 2 years ago

Thanks for sharing this fantastic library!!! I am trying out the SimpleExample you have here but I get run-time errors (see below). I am using this on MacOSX but I did not do any changes to the code (it seems to point to a VISA .dll). Any help is appreciated! Thank you again! Details: MacOS Catalina version 10.15.7 The computer has LabVIEW 2021 installed and I can control instruments just fine with VISA from within LabVIEW.

pfroud commented 2 years ago

I appreciate your appreciation!

It looks like JVisa should be compatible with macOS. The first step is to fix the NoClassDefFoundError, which means Java can't find JNA. JNA is the library which lets JVisa talk to NI-VISA.

I suggest using NetBeans IDE for testing this. The JVisa git reposority contains files which make it a Netbeans project. If you clone the git reposority then go to Netbeans and click File > Open Project, then Netbeans should recognize the JVisa folder as a Netbeans project. The git reposority also contains a compiled JNA jar file, and the Netbeans project is already configured up to use it.

You are right that JVisa currently points to a dll file, here's the line: https://github.com/pfroud/JVisa/blob/a018392f831c9b4eb2ade86e2c39417d46294f5d/src/jvisa/JVisaResourceManager.java#L58

On a Mac, it looks like the file extension will be .dylib (dynamic library) or maybe .so (shared object). The file name could be libvisa or libnivisa. You can read the section called "Installed Files and Paths" in the NI-VISA 21.0 for macOS Readme to see where to look for it.

If you get it working, we can update the Java code to check the OS type.

Amaruk2365 commented 2 years ago

Thank you for your quick reply!!! Ahhh... I had not imported JNA into this project. I thought it was part of JVisa... I have tested my JNA library before in a separate project and I got that example to work (but it had no VISA functions).

After also adding JNA to my project I got the error that I was expecting (.dll file) as shown below:

Couldn't load nivisa.dll
java.lang.UnsatisfiedLinkError: Unable to load library 'nivisa64.dll':
dlopen(libnivisa64.dll.dylib, 9): image not found
dlopen(libnivisa64.dll.dylib, 9): image not found
pfroud commented 2 years ago

OK good progress.

There's nothing special about these .java files, you should be able to edit them. How did you get the JVisa code on your computer? (Cloned using command-line git? Downloaded a ZIP from github.com?)

Amaruk2365 commented 2 years ago

Hi Peter, I downloaded the .jar file directly from GitHub (JVisa-2022-01-04.jar).

pfroud commented 2 years ago

For this you'll need to get the JVisa source code, which are .java files.

A jar file is a package of compiled java classes. A jar lets you use the contents, but not edit them. Confusingly, your IDE tries to be helpful by automatically decompiling the jar file to show you source code -- but you can't edit it.

To get the JVisa source code you can click this link: https://github.com/pfroud/JVisa/archive/refs/heads/master.zip It will download a file called JVisa-master.zip. Extract the zip file to a folder called JVisa-master.

NetBeans IDE will recognize the folder JVisa-master as a Netbeans project. It looks like IntelliJ will also be able to open the folder.

Amaruk2365 commented 2 years ago

Hi again Peter,

I was able to download the zip file, unzip it and have IntelliJ recognize the project folder! Great step forward!! I am now able to edit the source code but I have not done any changes yet.

Below you can see the code as it shows up (key section) and the error messages I get. What do you suggest I change the VISA_LIBRARY value to? It seems your code adds the extension automatically now. Perhaps I need to specify where I have my VISA libraries installed on my Mac? Let me know what to look for and I can see where they reside on my Mac.

pfroud commented 2 years ago

Now you need to go looking for a dylib file that looks like belongs to Visa.

According to this page https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/UsingDynamicLibraries.html

The standard locations for dynamic libraries are ~/lib, /usr/local/lib, and /usr/lib.

According to our friends at PyVISA, it will be called one of these:

"visa", "visa32", "visa32.dll", "visa64", "visa64.dll"

See https://github.com/pyvisa/pyvisa/blob/53a1d410f727e8d16212ceae3229e6219443f9aa/pyvisa/ctwrapper/highlevel.py#L95

And it looks like JVisa should load one of those libraries, not the NI-specific one

Amaruk2365 commented 2 years ago

Hi Peter, I don't know much about these libraries but it seems that NI is using the frameworks concept mentioned here: https://www.ni.com/pdf/manuals/375354h.html I did however find the VISA.framework folder so it is on my system. Any ideas of what to look for and where to look - I am all ears. Thanks again! Best regards, Mikael

pfroud commented 2 years ago

In JVisaResourceManager.java, try setting String nativeLibraryName to visa, visa32, and visa64 then try running SimpleExample.java each time

Amaruk2365 commented 2 years ago

We are getting closer... When I set it to just "visa" I don't get the same error message which seems encouraging!

pfroud commented 2 years ago

Excellent news!

I hard-coded JVisa to only search for USB instruments which was dumb.

Please open JVisaResourceManager.java and edit line 159:

 final ByteBuffer filterExpression = stringToByteBuffer("USB?*");

Change the string from "USB?*" to "?*", so the line is

 final ByteBuffer filterExpression = stringToByteBuffer("?*");

And then run Simple Example again.

If everything works I will publish another release on Github.

Amaruk2365 commented 2 years ago

Hi Peter,

We are almost there!!

I changed the line to "?*" as you suggested and ran two experiments. See details below.

Thanks again!!

Best regards, Mikael

Experiment 1. Instruments connected:

This is the output I get:

Found 6 VISA instrument(s): jvisa.JVisaException: in viWrite(): Unable to start operation because setup is invalid (due to attributes being set to an inconsistent state). (0xBFFF003A) 2 / 6: "NSDCV+0.0000E+00,B0000" 3 / 6: "NSDCV+0.0000E+00,B0000" 4 / 6: "Siglent Technologies,SDM3045X,"

Experiment 2. Instruments connected:

This is the output I get:

Found 3 VISA instrument(s): 1 / 3: "Siglent Technologies,SDM3045X" jvisa.JVisaException: in viOpen(): Insufficient location information or the device or resource is not present in the system. (0xBFFF0011)

pfroud commented 2 years ago

Could you clarify about Experiment number 2: only the Siglent multimeter is connected, but JVisa finds three instruments?

Have you ever used git? If you clone this repository, I can add debug printouts and push them to github so you can pull them to your computer.

Amaruk2365 commented 2 years ago

No, never used Git. What is it? Does it run on Mac?

pfroud commented 2 years ago

Sounds like we're trying to open random USB stuff which won't work.

Please replace SimpleExample.java with this file which has some debug printouts: SimpleExample.java.txt

Then run experiment number 2 only (one LAN instrument only) and reply with the output.

Git is software to keep track of changes to files. It is not easy to use so I'll just attach file to this issue. It does run on Mac (and Linux).

Amaruk2365 commented 2 years ago

Hi Peter,

I have now replaced the code with the text file you sent me. It seems to work just fine. The extra ghost instruments are probably due to a static setting I have for them.

pfroud commented 2 years ago

Hi Mikael, thanks for running the experiments and sharing more details about your setup.

I think you are right about the ghost instruments, and I think that means JVisa is working correctly on your computer now.

The SimpleExample file tries to open everything it can find, including static ones. When you do have instruments connected, JVisa can open them and the *IDN? query works.

I will publish a new release on GitHub this week.

Amaruk2365 commented 2 years ago

Excellent! Let me know when it is up and I will download the new jar file for my project. I am glad you figured it out!!! Thanks again!

pfroud commented 2 years ago

I released JVisa-2022-03-02, please test it...

https://github.com/pfroud/JVisa/releases/tag/JVisa-2022-03-02

Amaruk2365 commented 2 years ago

Hi Peter, It works like a charm! Many thanks!!

pfroud commented 2 years ago

Great news! I will close this GitHub issue, thanks for helping me add that feature to the project.

If you can please try JVisa on Linux too and then post a comment in this issue: https://github.com/pfroud/JVisa/issues/2

Amaruk2365 commented 2 years ago

Hi again Peter, I could not wait so I got started installing things on my Linux. IntelliJ installed just fine. But it turns out LabVIEW (has VISA) is not supported on my Linux version and installing the VISA by itself seems complicated (might work but not sure). Too bad, I was hoping to test it for you but I need to solve the issue with the VISA installation first. I might ask on the NI forum but there is not guarantee it will work on my Linux version.

Best regards, Mikael

On Thu, Mar 3, 2022 at 4:08 PM Garcia Family @.***> wrote:

Hi Peter,

Yes, I will try it on Linux on the weekend and let you know how it goes (I will have to install an IDE first). No problem!

I have now also successfully written my own test program using JVisa and talked to all my instruments (Siglent, Keysight, and Keithley) over both GPIB and LAN. Works flawlessly!

Just a fun side-note: My Linux computer is actually a Macbook Pro that I wiped clean and installed Linux on. Works great so I look forward to running this experiment on it. I will also give you the details about the Linux version I am using for your record.

Best regards, Mikael

On Thu, Mar 3, 2022 at 3:44 PM Peter Froud @.***> wrote:

Closed #3 https://github.com/pfroud/JVisa/issues/3.

— Reply to this email directly, view it on GitHub https://github.com/pfroud/JVisa/issues/3#event-6180897352, or unsubscribe https://github.com/notifications/unsubscribe-auth/AX6T7MLOAKQP6DIQJGQNJXTU6EQB3ANCNFSM5PNGBJTA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

pfroud commented 2 years ago

Bummer, thanks for trying though.

I also looked into installing NI-VISA on Linux. Their instructions are not great. If you have Ubuntu version 18.04 or 20.04 I think I know how.

Here's the link to documentation for reference:

Download and Install NI Driver Software on Linux Desktop