How to retrieve all local PXIe Instruments information in PXIe chassis via ASCII C in Visual Studio

Updated Sep 18, 2024

Environment

Hardware

  • PXI Chassis
  • PXI Controller
  • PXIe-4135

Software

  • Microsoft Visual Studio
  • LabWindows/CVI

In this article, I will introduce how to detect all local PXIe instruments via ASCII C inVisual Studio. It uses the System Configuration API in Visual Studio.
 

0. Open the Visual Studio.
1. Create a new console App project.
image.png
2. Click Next, and rename the project name as you want. For example, nisyscfg_FindAll.
image.png
3. Add below path in the Include Directories
C:\Program Files (x86)\National Instruments\Shared\ExternalCompilerSupport\C\include
4. Add below path in Library Directories. The picture of Step3 and Step4 are as below:
C:\Program Files (x86)\National Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc
image.png
5. Add below path to Linker -> Inpu -> Additional Dependencies(Please note that you must specific the detailed lib file as below).
C:\Program Files (x86)\National Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc\nisyscfg.lib
image.png
6. Include necessary head file.
#include <stdio.h>
#include "nisyscfg.h"
#include <string.h>
7. Copy attached source code in main().
int main()
{
	NISysCfgStatus status = NISysCfg_OK;
	NISysCfgSessionHandle session = NULL;
	NISysCfgEnumResourceHandle resourcesHandle = NULL;
	NISysCfgResourceHandle resource = NULL;
	NISysCfgFilterHandle filter = NULL;
	char expertName[NISYSCFG_SIMPLE_STRING_LENGTH] = "";
	char resourceName[NISYSCFG_SIMPLE_STRING_LENGTH] = "";
	char alias[NISYSCFG_SIMPLE_STRING_LENGTH] = "";
	char name[NISYSCFG_SIMPLE_STRING_LENGTH] = "";
	char productName[NISYSCFG_SIMPLE_STRING_LENGTH] = "";
	char serialNumber[NISYSCFG_SIMPLE_STRING_LENGTH] = "";
	char target[NISYSCFG_SIMPLE_STRING_LENGTH] = "localhost";
	double temp = -1;
	char* detailedDescription = NULL;
	char networkShow = 'y';
	NISysCfgIsPresentType isPresent = NISysCfgIsPresentTypePresent;
	//	Initialize System Configuration session and find all hardware on the target
	printf("\nInitializing session and finding hardware... \n");
	if (NISysCfg_Succeeded(status = NISysCfgInitializeSession(target, NULL, NULL, NISysCfgLocaleDefault, NISysCfgBoolTrue, 10000, NULL, &session)))
		if (NISysCfg_Succeeded(status = NISysCfgCreateFilter(session, &filter)))
		{
			NISysCfgSetFilterProperty(filter, NISysCfgFilterPropertyIsDevice, NISysCfgBoolTrue);
			//NISysCfgSetFilterProperty(filter, NISysCfgFilterPropertyIsSimulated, NISysCfgBoolFalse);

			if (NISysCfg_Succeeded(status = NISysCfgFindHardware(session, NISysCfgFilterModeAll, filter, NULL, &resourcesHandle)))
			{
				//	Iterate through all found resources and obtain properties
				//	Disable run-time checking for properties that may not be valid

				while (NISysCfg_Succeeded(status) && (status = NISysCfgNextResource(session, resourcesHandle, &resource)) == NISysCfg_OK)
				{
					NISysCfgGetResourceIndexedProperty(resource, NISysCfgIndexedPropertyExpertName, 0, expertName);
					NISysCfgGetResourceProperty(resource, NISysCfgResourcePropertyProductName, productName);
					NISysCfgGetResourceProperty(resource, NISysCfgResourcePropertyIsPresent, &isPresent);

					if ((strcmp(expertName, "network") != 0) && (strstr(productName, "PXIe") != NULL) && (isPresent == NISysCfgIsPresentTypePresent))
						//Filter all PXIe instrument which is present in ni Max and not "network" device.

					{
						NISysCfgGetResourceIndexedProperty(resource, NISysCfgIndexedPropertyExpertResourceName, 0, resourceName);
						NISysCfgGetResourceIndexedProperty(resource, NISysCfgIndexedPropertyExpertUserAlias, 0, alias);
						NISysCfgGetResourceProperty(resource, NISysCfgResourcePropertyProductName, name);

						//	Print the Device Alias if it exists (e.g. Dev1, PXI1Slot2, etc), otherwise print the Resource Name
						if (strlen(alias))
							printf("Device Alias  : %s\n", alias);
						else
							printf("Resource Name  : %s\n", resourceName);
						printf("the product name is : %s\n", name);

						NISysCfgGetResourceProperty(resource, NISysCfgResourcePropertyProductName, productName);
						printf("Product Name  : %s\n", productName);

						NISysCfgGetResourceProperty(resource, NISysCfgResourcePropertySerialNumber, serialNumber);
						printf("Serial Number : %s\n\n", serialNumber);
						status = NISysCfgCloseHandle(resource);
					}
				}


			}
		

		if (NISysCfg_Failed(status))
		{
			NISysCfgGetStatusDescription(session, status, &detailedDescription);
			printf("Error: %s\n", detailedDescription);
			NISysCfgFreeDetailedString(detailedDescription);
		}
		printf("Press Enter to close session and exit\n");
		status = NISysCfgCloseHandle(filter);
		status = NISysCfgCloseHandle(resourcesHandle);
		status = NISysCfgCloseHandle(session);
	}	
	return 0;
}
8. Build and run the program, then you can retrive information of all instruments as below picture.
image.png
 

This is the link of example code.