Agilent 34401A Excel macro - Page 1
Forum section: electronics / testgear

author: ad78
author_rank: Newbie
timestamp: August 23, 2025, 04:57:07 pm
content: Hello!Please help! I have a problem. I'm a beginner in programming... also, I'm doing a project (Agilent 34401A) where the point is to use long-term sampling. For example, on DC Voltage with 100 NPLC... and the next one is AC Current.I would like to replace the fixed delay time with some better solution that monitors the end of the Read? command and then returns the result. I control it via GPIB.Thank you in advance for your help.Ad.The example:Parancs = "CONF:CURR:AC " & Range & ""stat = viWrite(DMM, Parancs, Len(Parancs), retCount)Parancs = "DET:BAND 3"stat = viWrite(DMM, Parancs, Len(Parancs), retCount)Parancs = "READ?"stat = viWrite(DMM, Parancs, Len(Parancs), retCount)Sleep 8000stat = viRead(DMM, Buffer, 200, retCount)ertek = Val(Buffer)

author: coromonadalix
author_rank: Super Contributor
timestamp: August 24, 2025, 06:31:47 am
content: nowadays excel by default will lockout macros .... HP Agilent now Keysight had some excel examples programsyou should go with python or some already "test controller sofware", its free and can do logging, search the threads here

author: ad78
author_rank: Newbie
timestamp: August 24, 2025, 07:57:28 am
content: Due to the background environment (so much data and calculations) only Excel is suitable for me, which is why I asked for help on this topic. I can't figure out how to time the multimeter and use its commands.

author: Kean
author_rank: Unknown
timestamp: August 24, 2025, 03:33:11 pm
content: Have a look at the MAV bit of the Status byte in the 34401A user guide. MAV = Message Available.If I recall correctly, the sequence is:send INIT to start a measurementloopshort delay - e.g. 100ms or similarread status byte using *STB?if MAV (bit 4) of status byte is set then exit loopsend FETCH? to get measurement valueThere may be some extra initialisation needed as well (trigger settings, *CLS, etc), but hopefully this will help you find the solution.

author: ad78
author_rank: Newbie
timestamp: August 24, 2025, 04:07:37 pm
content: That would be good....Someone who knows, please help!THX Kean

author: coromonadalix
author_rank: Super Contributor
timestamp: August 24, 2025, 04:17:29 pm
content: be patient ... you want something who's considered as old, and maybe non practical in a senseand try to find documentation(s) on commands as other(s) have given you ....you have keysight i/o librairies with other tools who may help understand your needs over gpib, you can send commands and see how it works ... and after try to implement them in a excel sheet

author: Tony_G
author_rank: Super Contributor
timestamp: August 24, 2025, 04:33:07 pm
content: Up front notice, I haven't used the VISA API directly, only through .NET Framework or PyVISA.That said, I think you might want to look at the documentation for viWaitOnEvent and the 34401A *OPC command.I used the VSIA.NET version in this C# code:Code:private static StatisticalValues GetData(string stage){// Take the measurementSendCommand(":INIT");SendCommand("*OPC"); // 34401A// Wait for the data to be availablesrqWait.Wait();//result = QueryString(":TRACe:DATA?"); // 2015THDvar result = QueryString(":FETCh?"); // 34401A// Convert the string to a list of doublesList<double> doubleList = ConvertStringToDoubleList(result);// Print the results//PrintMeasurementResults("Zero Carryover - " + stage, doubleList);return new StatisticalValues(doubleList.Min(), doubleList.Max(), doubleList.Average(), StdDev(doubleList));}That might give you enough insight to use it in an Excel macro.TonyG

author: Kean
author_rank: Unknown
timestamp: August 24, 2025, 05:07:12 pm
content: It has been a long time since I used NI-VISA (and I never want to again), but I recall there was an API called viReadSTB.So maybe look at the documentation for that, and maybe search around for some example close to what you want.https://www.ni.com/docs/en-US/bundle/ni-visa-api-ref/page/ni-visa-api-ref/vireadstb.htmlI think using SRQ is quite a bit more complex. Not sure if you can even do it in an Excel macro, but it probably requires viEnableEvent and viWaitOnEvent.https://www.ni.com/docs/en-US/bundle/ni-visa-api-ref/page/ni-visa-api-ref/vienableevent.htmlhttps://www.ni.com/docs/en-US/bundle/ni-visa-api-ref/page/ni-visa-api-ref/viwaitonevent.html

author: Tony_G
author_rank: Super Contributor
timestamp: August 24, 2025, 08:04:26 pm
content: As Kean said it's a bit more complex, from the NI VISA docs but it looks like handling SRQ shouldn't be that hard:Call viEnableEvent(sesn, VI_EVENT_SERVICE_REQ, VI_QUEUE, VI_NULL)Perform actionWait for the SRQ viWaitOnEvent(sesn, VI_EVENT_SERVICE_REQ, 20000, etype, event)Ad78 YMMV - Doing this in C#/VISA.NET is easy, as it is in PyVISA, but should work in Excel.Doc code:Code:Private Sub vbMain()Const MAX_CNT = 1024Dim statDim dfltRMDim sesnDim bufferHandleDim retCountDim etypeDim eventDim stb As ViStatusAs ViSessionAs ViSessionAs StringAs LongAs ViEventTypeAs ViEventAs IntegerRem Begin by initializing the systemRem NOTE: For simplicity, we will not show error checkingstat = viOpenDefaultRM(dfltRM)If (stat < VI_SUCCESS) ThenRem Error initializing VISA...exitingExit SubEnd IfRem Open communication with GPIB device at primary address 2stat = viOpen(dfltRM, "GPIB0::2::INSTR", VI_NULL, VI_NULL, sesn)Rem Allocate memory for bufferRem In addition, allocate space for the ASCII NULL characterbufferHandler = Space$(MAX_CNT + 1)Rem Enable the driver to detect eventsstat = viEnableEvent(sesn, VI_EVENT_SERVICE_REQ, VI_QUEUE, VI_NULL)Rem Tell the device to begin acquiring a waveformstat = viWrite(sesn, "E0x51; W1", 9, retCount)Rem The device asserts SRQ when the waveform is readystat = viWaitOnEvent(sesn, VI_EVENT_SERVICE_REQ, 20000, etype, event)If (stat < VI_SUCCESS) ThenRem Waveform not received...exitingstat = viClose (dfltRM)Exit SubEnd Ifstat = viReadSTB (sesn, stb)Rem Read the datastat = viRead(sesn, bufferHandle, MAX_CNT, retCount)Rem Your code should process the waveform dataRem Close the event contextstat = viClose (event)Rem Stop listening for eventsstat = viDisableEvent(sesn, VI_ALL_ENABLED_EVENTS, VI_ALL_MECH)Rem Close down the systemstat = viClose(sesn)stat = viClose(dfltRM)End Sub