Recent

Author Topic: visasession  (Read 7259 times)

Stefano58

  • New member
  • *
  • Posts: 9
visasession
« on: July 22, 2016, 06:13:13 pm »
please, Can anyone give me a small application for FP-VISA with a USB meter?

thank you

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: visasession
« Reply #1 on: July 22, 2016, 06:57:32 pm »
It all depends on what meter you are using. The different vendors use different commands to setup and retrieve data.

Here's a quick and dirty example for a Keysight 34411A where the dmm is a TVisaSession component from here(https://github.com/Laksen/FP-VISA):
The address has to be configured to match the USB visa address provided by whatever VISA stack you have installed.
Code: Pascal  [Select][+][-]
  1. begin
  2.   // Setup for autoranging DC measurements
  3.   dmm.Command('CONFigure:VOLTage:DC');
  4.   dmm.Command('TRIGger:SOURce BUS');
  5.   dmm.Command('INITiate');
  6.  
  7.   // Do a single measurement
  8.   dmm.Command('*TRG');
  9.   dmm.Query('*OPC?'); // Wait for trigger
  10.   memo1.Lines.Add('Read: "'+trim(dmm.Query('FETCh?')));
  11. end;
  12.  

Stefano58

  • New member
  • *
  • Posts: 9
Re: visasession
« Reply #2 on: July 25, 2016, 06:11:01 pm »
I also need to install a VISA stack, can you help me?
Thank you for your patience.

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: visasession
« Reply #3 on: July 26, 2016, 12:10:49 am »
At work I use Keysight IO Libraries (http://www.keysight.com/main/software.jspx?cc=DK&lc=dan&ckey=2175637&nid=-33330.977662&id=2175637), but that's Windows only. For Linux/Mac NI VISA might work, but it's apparently not easy to get working; at least not on Linux.

Stefano58

  • New member
  • *
  • Posts: 9
Re: visasession
« Reply #4 on: July 29, 2016, 07:29:41 pm »
I have already installed VISA library.
The Python script, as reported below, runs with no problem.
--------------------------------------------------------------
>>> import visa
>>> rm = visa.ResourceManager() # create a ResourceManager object
>>> print(rm.list_resources())  # list the available resources
('USB0::0x0699::0x0368::C012494::INSTR', 'ASRL10::INSTR', 'ASRL33::INSTR', 'USB::0x0699::0x0368::C012494::INSTR')
>>> my_inst = rm.open_resource('USB0::0x0699::0x0368::C012494::INSTR')
>>> print(my_inst.query("*IDN?"))
TEKTRONIX,TBS 1102B,C012494,CF:91.1CT FV:v4.0
----------------------------------------------------------------
I installed also the FP-VISA package for Lazarus.
Can anyone translate the Python script into Lazarus?

Thank you

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: visasession
« Reply #5 on: July 29, 2016, 10:23:46 pm »
Try this:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   session;
  5.  
  6. var
  7.   my_inst: TVisaSession;
  8. begin
  9.   my_inst:=TVisaSession.Create(nil);
  10.   my_inst.Address:='USB0::0x0699::0x0368::C012494::INSTR';
  11.  
  12.   my_inst.Active:=true;
  13.   writeln(my_inst.Query('*IDN?'#10));
  14.  
  15.   my_inst.Free;
  16. end.
  17.  

CM630

  • Hero Member
  • *****
  • Posts: 1296
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: visasession
« Reply #6 on: February 03, 2025, 02:39:20 pm »
Any idea how to get the list of the available VISA devices?
I tried to use viFindRsrc, but I did not succeed.
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: visasession
« Reply #7 on: February 03, 2025, 04:40:21 pm »
Code: Pascal  [Select][+][-]
  1. //Searches for all connected devices, that matches 'filter'.
  2. {
  3. Filter examples:
  4.  ?* -All devices
  5.  ?*INSTR -All INSTR capable devices
  6.  USB?* -All USB connected devices
  7. https://www.ni.com/docs/en-US/bundle/ni-visa/page/ni-visa/vifindrsrc.html
  8. }
  9. function SearchDevices(const filter: string; devlist: TStringList): longint;
  10. var
  11.     handle: ViSession;
  12.     res: ViStatus;
  13.     list: ViFindList;
  14.     cnt, i: ViUInt32;
  15.     dsc: array[0..255] of ViChar;
  16. begin
  17.    res := viOpenDefaultRM(@handle);
  18.    try
  19.       if (res < VI_SUCCESS) then begin
  20.          exit(res);
  21.       end;
  22.  
  23.       res := viFindRsrc(handle, pchar(filter), @list, @cnt, dsc);
  24.       if (res < VI_SUCCESS) then begin
  25.          exit(res);
  26.       end;
  27.  
  28.       if (cnt > 0) then begin
  29.          devlist.Clear;
  30.          devlist.Add(dsc);
  31.          for i:=1 to cnt - 1 do begin
  32.             res := viFindNext(list, dsc);
  33.             if (res < VI_SUCCESS) then begin
  34.                exit(res);
  35.             end;
  36.             devlist.Add(dsc);
  37.          end;
  38.       end;
  39.       result := res;
  40.    finally
  41.       viClose(handle);
  42.    end;
  43. end;
  44.  

It should work like that (it works on Keysight IOLibs), but discovery depends on the stack (VXI-11 is discovered with UDP broadcast, HiSLIP is discovered through LXI(mDNS/UDP), USBTMC needs to match driver, ASRL needs to be preconfigured) or whether you have stored any preconfigured devices in the VISA resource manager manually. Basically for network connected devices you need them to be on the same subnet as the host machine

CM630

  • Hero Member
  • *****
  • Posts: 1296
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: visasession
« Reply #8 on: February 04, 2025, 09:36:33 am »
Thanks, in Windows it seems to work fine with NI VISA - I tried it with a Tektronix and a Rigol.
I expanded your example, maybe it could do without a session, I but that is what I had as an example from the forum.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   visa, visatype, session;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31. //Searches for all connected devices, that matches 'filter'.
  32. {
  33. Filter examples:
  34.  ?* -All devices
  35.  ?*INSTR -All INSTR capable devices
  36.  USB?* -All USB connected devices
  37. https://www.ni.com/docs/en-US/bundle/ni-visa/page/ni-visa/vifindrsrc.html
  38. }
  39. function SearchDevices(const filter: string; devlist: TStringList): longint;
  40. var
  41.     handle: ViSession;
  42.     res: ViStatus;
  43.     list: ViFindList;
  44.     cnt, i: ViUInt32;
  45.     dsc: array[0..255] of ViChar;
  46. begin
  47.    res := viOpenDefaultRM(@handle);
  48.    try
  49.       if (res < VI_SUCCESS) then begin
  50.          exit(res);
  51.       end;
  52.  
  53.       res := viFindRsrc(handle, pchar(filter), @list, @cnt, dsc);
  54.       if (res < VI_SUCCESS) then begin
  55.          exit(res);
  56.       end;
  57.  
  58.       if (cnt > 0) then begin
  59.          devlist.Clear;
  60.          devlist.Add(dsc);
  61.          for i:=1 to cnt - 1 do begin
  62.             res := viFindNext(list, dsc);
  63.             if (res < VI_SUCCESS) then begin
  64.                exit(res);
  65.             end;
  66.             devlist.Add(dsc);
  67.          end;
  68.       end;
  69.       result := res;
  70.    finally
  71.       viClose(handle);
  72.    end;
  73. end;
  74.  
  75. { TForm1 }
  76.  
  77. procedure TForm1.Button1Click(Sender: TObject);
  78. var
  79.   mystl: TStringList;
  80.   i: integer = 0;
  81.   VisaSession1 : TVisaSession;
  82.   DeviceInfo: string = '';
  83. begin
  84.    memo1.Clear;
  85.    Application.ProcessMessages; //Needed by Memo1.Clear
  86.    mystl := TStringList.Create;
  87.    SearchDevices('?*',mystl);
  88.    if (mystl.Count < 1) then exit; ;
  89.    for i:=0 to (mystl.Count - 1) do
  90.    begin
  91.      DeviceInfo := mystl.Strings[i];
  92.      VisaSession1 := TVisaSession.Create(nil);
  93.      VisaSession1.Address := mystl.Strings[i];
  94.      //Usaually COM ports do not respond
  95.      try
  96.        VisaSession1.Active := True;
  97.        DeviceInfo := DeviceInfo + ' ➤ ' + VisaSession1.Query('*IDN?');
  98.      except
  99.        if DeviceInfo.StartsWith('ASRL')
  100.        then DeviceInfo := DeviceInfo + ' ➤ Unknown COM port'
  101.        else DeviceInfo := DeviceInfo + ' ➤ Unknown';
  102.      end;
  103.      Memo1.Append (DeviceInfo);
  104.      VisaSession1.Active := False;
  105.    end;
  106. end;
  107.  
  108. end.
  109.  
  110.  

But what startled me is that the dynamic-link library is linked ... statically  :o
Do you have an implementation for dynamic linking? If not, I shall (try to) make.
« Last Edit: February 04, 2025, 09:47:35 am by CM630 »
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: visasession
« Reply #9 on: February 04, 2025, 01:16:37 pm »
Do you have an implementation for dynamic linking? If not, I shall (try to) make.

No, but I was thinking about making one and overhauling it a bit since I've needed to use it on Linux a couple of times where the library discovery process is quite odd

MarkMLl

  • Hero Member
  • *****
  • Posts: 8320
Re: visasession
« Reply #10 on: February 04, 2025, 02:22:06 pm »
No, but I was thinking about making one and overhauling it a bit since I've needed to use it on Linux a couple of times where the library discovery process is quite odd

I've just been struggling with that one myself, in relation to extending a battery tester program to handle different types of physical device using a shim generator originally written to aid access to system libraries such as libusb.

When considering a backend (etc.) library written in Pascal, placement of a .so for at-startup loading is picky: it's actually easier to use on-demand loading since that allows you to specify the directory explicitly, i.e. "same as executable", "in a backend directory", or whatever.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

CM630

  • Hero Member
  • *****
  • Posts: 1296
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: visasession
« Reply #11 on: February 05, 2025, 05:43:20 pm »
An update is attached.
It contains:
1. visa_dyn.pas - dynamic bindings for Visa.
2. An example app for listing the attached Visa devices
3. A list of the visa routines, I used a notepad and a sheets app to create the dynamic bindings.

In the attachment I have replaced visa with visa_dyn in session.pas.
I have not thought on how to make it switchable, maybe it is possible.

The example may work, but it may not work. I have found multiple *visa32*.dll files on one of the PCs.
I have tried 6 or 7 of them.
Most load and the app gets the data, with some DLLs the COM ports are not detected (only the Visa ones), one DLL did not load, one loaded, but crashed on unloading, one loaded, but the app retrieved no data.
I tried Tektronix, Rigol and R&S. The last one did not respond to VisaSession1.Query('*IDN?'), the others did.

Edit: I forgot to take care for 64 bit Lazarus. I guess *visa64.dll shall be used, I will try it next days.

Edit2: I updated the visa_dyn and the example. The example app works when built with Lazarus64 even with visa32.dll but differently: generates exceptions with some COM devices; a single VISA device is found 3 times (twice with the same address, once with USB instead of USB0). Again, I found a *visa64.dll which seems to work normally.
« Last Edit: February 06, 2025, 08:39:56 am by CM630 »
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: visasession
« Reply #12 on: February 06, 2025, 12:52:07 pm »
Looks like a good start. I think it makes sense to only use dynamic linking. The old approach was taken due to laziness

CM630

  • Hero Member
  • *****
  • Posts: 1296
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: visasession
« Reply #13 on: February 06, 2025, 02:31:43 pm »
Conversion from static to dynamic is fast and easy, once you do it.
Maybe this function deserves mentioning:

Code: Pascal  [Select][+][-]
  1. function GetManufacturer (Address: string): String;
  2. var
  3.   ManufID : string = '';
  4. begin
  5.   if (Address = '') then exit ('');
  6.   if (Length(Address.Split(['::']))<2) then exit ('');
  7.   ManufID:= uppercase(Address.Split(['::'])[1]);
  8.  
  9.   case ManufID of
  10.     '0X0699': Result := 'Tektronix';
  11.     '0X1AB1': Result := 'Rigol';
  12.     '0X0AAD': Result := 'Rohde & Schwarz';
  13.     else Result := ManufID;
  14.   end;
  15. end;
It seems odd, but I cannot find a list of manufacturer IDs  :o, maybe you can extend the list with devices that you have available.
And maybe these constants might be added in visatype.pas or somewhere else.
And maybe lowercase is better than uppercase, Visa seems to return a lowercase “x”.
« Last Edit: February 06, 2025, 03:04:31 pm by CM630 »
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: visasession
« Reply #14 on: February 07, 2025, 12:03:45 am »
Those are just USB vendor IDs: https://the-sz.com/products/usbid/index.php?v=0X0699&p=&n=
But I wouldn't trust that, but instead rely on the *IDN? result

 

TinyPortal © 2005-2018