bool VISAInstrument::Initialize() { char desc[256]; char error_desc[512]; //check visa system status if(!VISASystemManager::Status()){ fErrorCode="Can't Initialize VISA system"; fStatus=VI_STATE_UNKNOWN; return false; } //find exactly the unique specified instr,no findlist int length=fRsrcName.size()+1; char *rsrcname=new char[length]; fRsrcName.copy(rsrcname,length-1); rsrcname[length-1]='\0'; fStatus = viFindRsrc(fDefaultRM,rsrcname,VI_NULL,VI_NULL,desc); delete[] rsrcname; if(fStatus < VI_SUCCESS){ viStatusDesc(fViSession,fStatus,error_desc); fErrorCode=error_desc; return false; } //viopen,default timeout 2s,default access mode fStatus = viOpen(fDefaultRM,desc,fAccessMode,fOpenTimeout,&fViSession); if(fStatus < VI_SUCCESS){ viStatusDesc(fViSession,fStatus,error_desc); fErrorCode=error_desc; return false; } //register this instrument if(!VISASystemManager::GetInstance()->Register(this)){ fStatus=VI_STATE_UNKNOWN; fErrorCode="Error! DeviceName Duplication.Please Choose Another DeviceName"; Close(); return false; } fRsrcDesc=desc; return true; }
int FindRSrc(char *instrDescriptor) { ViUInt32 numInstrs; ViFindList findList; ViStatus status; ViSession defaultRM, instr; status = viOpenDefaultRM(&defaultRM); if ( status < VI_SUCCESS ) { printf("Could not open a session to the VISA Resource Manager!"); return status; } status = viFindRsrc( defaultRM, "GPIB?*INSTR", &findList, &numInstrs, instrDescriptor ); if ( status < VI_SUCCESS ) { printf("An error occurred while finding resources."); return status; } if ( numInstrs > 0 ) { printf("%s found!\n", instrDescriptor);//Agilent->ComboBox_GPIB->Items->Add(instrDescriptor); } viClose (defaultRM); return 0; //status = viOpen( defaultRM, instrDescriptor, VI_NULL, VI_NULL, &instr ); while ( --numInstrs ) { status = viFindNext( findList, instrDescriptor ); if ( status < VI_SUCCESS ) { printf("An error occurred finding the next resource."); return status; } //这个时候的instrDescriptor就是你电脑上找到的资源,并且可用,如果为空就说明你的连线有问题 } // viClose(instr); viClose(findList); return status; }