验证中...
私信发送成功
语言: C/C++
分类: 常用工具包
最后更新于 2017-12-07 19:52
keithley visa demo
原始数据 复制代码
//#include "stdafx.h"
#include <visa.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
//#include <conio.h>
#include <stdlib.h>
ViSession defaultRM; // Resource manager ID
ViSession KI200; // Identifies the power supply
ViStatus status;
long ErrorStatus;
char commandString[256];
char ReadBuffer[256];
void OpenPort();
void SendSCPI(char* pString);
void CheckError(char* pMessage);
void delay(clock_t wait);
void ClosePort();
int main(int argc, char* argv[])
{
char Buffer[256];
float setting[3][2]={
{11.9, 0.55},
{15.15, 0.25},
{2.5, 0.15}
} ; // Voltage, current for three channels
float query[3][2];
ViChar bufz[16];
ViUInt32 retCnt;
unsigned int i;
OpenPort();
// Query the power supply ID, read the response and print it
sprintf(Buffer, "*IDN?");
SendSCPI(Buffer);
printf("Instrument identification string:%s \n", Buffer);
SendSCPI("*RST"); // Reset the power supply
SendSCPI("OUTPut 1"); // Turn the output on
for (i=0; i<3; i++)
{
printf("setting Channel: %d, voltage(V):%f, current(A):%f \n", i+1,
setting[i][0], setting[i][1]);
ErrorStatus = viPrintf(KI200, "INSTrument:NSELect %d\n", i+1); // Select the channel
CheckError("Unable to select the channel");
ErrorStatus = viPrintf(KI200,"VOLTage %f\n",setting[i][0]); // Set the output voltage
CheckError("Unable to set voltage");
ErrorStatus = viPrintf(KI200, "CURRent %f\n",setting[i][1]); // Set the output current
CheckError("Unable to set current");
}
SendSCPI("*SAV 4");
delay (10);
for (i=0; i<3; i++)
{
ErrorStatus = viPrintf(KI200, "INSTrument:NSELect %d\n", i+1); //Select the channel
CheckError("Unable to select the channel");
ErrorStatus = viPrintf(KI200,"Measure:voltage?\n"); // Measure the output voltage
CheckError("Unable to write the device");
//ErrorStatus = viScanf(KI200,"%f",&query[i][0]); // Retrieve the reading
ErrorStatus = viRead(KI200,(ViBuf)&query[i][0], sizeof(query[i][0]), &retCnt); // Retrieve the reading
CheckError("Unable to read voltage");
ErrorStatus = viPrintf(KI200,"Measure:current?\n"); // Measure the output current
CheckError("Unable to write the device");
//ErrorStatus = viScanf(KI200,"%f",&query[i][1]); // Retrieve the reading
ErrorStatus = viRead(KI200,(ViBuf)&query[i][1], sizeof(query[i][0]), &retCnt); // Retrieve the reading
CheckError("Unable to read current");
printf("Channel: %d, measured voltage(V):%f, current(A):%f \n", i+1,
query[i][0], query[i][1]);
}
return 0;
}
void OpenPort()
{
ViFindList fList;
ViUInt32 numInstrs = -110;
ViChar desc[100];
//Open communication session with the power supply
status = viOpenDefaultRM(&defaultRM);
if(status < VI_SUCCESS)
printf("viopendefaultrm error!\n");
else
printf("viopendefaultrm success!\n");
ErrorStatus = viFindRsrc(defaultRM, "?*", &fList, &numInstrs, desc);
if(numInstrs == 0)
printf("num is 0 \n");
else if(numInstrs == 1)
printf("num is 1\n");
else if(numInstrs == 2)
printf("num is 2\n");
else if(numInstrs >= 3)
printf("num is >= 3\n");
//printf("numInstrs:%d\n", numInstrs);
ErrorStatus =viOpen(defaultRM,
//"USB0::0X0699::0X0397::083001106673201002::INSTR",0,0,&KI200);
"USB0::0X05e6::0X2230::083001106673201002::INSTR",0,0,&KI200);
/*When using the GPIB interface, replace the above command line with
"GPIB0::21::INSTR". Note the argument "21" is an example and refers to the GPIB address.
Substitute the appropriate GPIB address in the command line.*/
CheckError("Unable to open the port");
SendSCPI("SYSTem:REMote");
}
void SendSCPI(char* pString)
{
char* pdest;
strcpy(commandString,pString);
strcat(commandString, "\n");
ErrorStatus = viPrintf(KI200, commandString);
CheckError("Can't Write to Power Supply");
pdest = strchr(commandString, '?'); // Search for the query command
if (pdest != NULL)
{
//ErrorStatus = viBufRead(KI200, (ViBuf)ReadBuffer,
ErrorStatus = viRead(KI200, (ViBuf)ReadBuffer,
sizeof(ReadBuffer), VI_NULL);
CheckError("Can't read from driver");
strcpy(pString, ReadBuffer);
}
}
void ClosePort()
{
viClose(KI200);
viClose(defaultRM);
}
void CheckError(char* pMessage)
{
if(ErrorStatus != VI_SUCCESS)
{
printf("\n %s",pMessage);
ClosePort();
exit(0);
}
}
void delay(clock_t wait)
{
clock_t goal;
goal = wait + clock();
while(goal > clock());
}

评论列表( 0 )

你可以在登录后,对此项目发表评论

4_float_left_people 4_float_left_close