Usage of pySerial from the VNATools scripting environment

52 views
Skip to first unread message

Luca Oberto

unread,
May 24, 2023, 11:53:43 AM5/24/23
to VNA Tools
Hi,
I need to automate VNAToola measurements by means of a python script. In between measurements, I need to command another instrument (a switch) via serial connection, but the "import serial" line in the code does not work. It says that there is no serial module.

Pyserial is installed in the PC so that a separate python script (outside VNATools) is able to properly communicate with the instrument.

Is it possible to do this inside the scripting environment of VNATools?

Thank you,
Luca

Michael Wollensack METAS

unread,
May 25, 2023, 2:21:27 AM5/25/23
to VNA Tools
Hi,

the script engine inside VNA Tools uses IronPython. If you use PySerial outside of VNA Tools, then you use it from CPython and not from IronPython.

You can automate as well VNA Tools from CPython, see for example https://groups.google.com/g/vnatools/c/vekhXXA1ksQ/m/Xeuo24nXCgAJ

Hope this helps.

Regards
Michael

Luca Oberto

unread,
May 25, 2023, 5:12:51 AM5/25/23
to VNA Tools
Hi Michael,
is it possible to import pyserial from IronPyton (inside VNATools)?

Regards,
Luca

Michael Wollensack METAS

unread,
May 25, 2023, 6:59:14 AM5/25/23
to VNA Tools
Hi,

> is it possible to import pyserial from IronPyton (inside VNATools)?

In principle pyserial should be usable from IronPython, see https://pypi.org/project/pyserial/
If it's possible to use it from the built in IronPython script engine in VNA Tools I'm not sure.

I would not use pyserial. I would rather use IVI.VISA, see the following example:

import clr
clr.AddReference('Ivi.Visa')
clr.AddReference('System.Windows.Forms')
clr.AddReference('Metas.Vna.Tools')
from Ivi.Visa import ISerialSession, GlobalResourceManager, SerialParity, SerialStopBitsMode
from System.Threading import Thread
from System.Windows.Forms import MessageBox
from Metas.Vna.Tools import Script

s = Script(RootPath)

vi = GlobalResourceManager.Open('ASRL3::INSTR')
vi.BaudRate = 9600;
vi.DataBits = 8;
vi.Parity = SerialParity.None;
vi.StopBits = SerialStopBitsMode.One;
vi.TerminationCharacter = 10;
vi.TerminationCharacterEnabled = True;


Hope this helps.

Regards
Michael

Luca Oberto

unread,
May 25, 2023, 9:07:01 AM5/25/23
to VNA Tools
Hi Michael,
I copied your code into a new VNATools script changing only  'ASRL3::INSTR' with 'COM7' and setting the proper baudrate. I do not have any error, so it seems to work.
Anyway I do not understand how to write commands or read an answer to and from the serial port.

I tried with vi.Write("reset") and  vi.WriteLine("reset") (the standard pyvisa commands) but they do not work. I cannot find any documentation online searching for ivi.visa and ironpython...

My serial instrument is controlled by and Arduino device, so this is the device I want to talk to.
With pyserial it is very easy, but I am not able to do the same inside the VNATools environment...

I am sorry but I am a so inexperienced programmer!
Could you please point me to the right direction?

Thank you,
Luca

Michael Wollensack METAS

unread,
May 25, 2023, 9:57:21 AM5/25/23
to VNA Tools
Hi,

this is all described in the following document VPP-4.3.6: VISA Implementation Specification for .NET, see https://www.ivifoundation.org/downloads/Architecture%20Specifications/vpp436_2022-12-19.pdf

vi.RawIO.Read()
vi.RawIO.Write("reset")


Regards
Michael
Reply all
Reply to author
Forward
0 new messages