Visual C++ 6.0 Programming Example

¡¡

Enter the Visual C++6.0 programming environment and follow the steps below:

¡¡

1. Build a MFC project based on dialog box.

2. Open the Link tab in Project¡ú Settings and add visa32.lib to the Object/library modules manually.

¡¡

3. Open the Directories tab in Tools¡ú Options.

Select Include files in Show directories for and double-click the blank in Directories to add the path of Include:

C:\Program Files\IVI Foundation\VISA\WinNT\include.

¡¡

Select Library files in Show directories for and double-click the blank in Directories to add the path of Lib:

C:\Program Files\IVI Foundation\VISA\WinNT\lib\msc.

Note: at present, VISA library has been added.

¡¡

4. Add the Text, Edit and Button controls as shown in the figure below.

5. Add the control variables.

Open the Member Variables tab in View¡ú ClassWizard and add the following three variables:

Instrument address: CString m_strInstrAddr

Command: CString m_strCommand

Return value: CString m_strResult

6. Encapsulate the read and write operations of VISA.

1) Encapsulate the write operation of VISA to facilitate the operation.

bool CDemoForDSADlg::InstrWrite(CString strAddr, CString strContent) //Write //operation

{

ViSession defaultRM,instr;

ViStatus status;

ViUInt32 retCount;

char * SendBuf = NULL;

char * SendAddr = NULL;

bool bWriteOK = false;

CString str;

// Change the address's data style from CString to char*

SendAddr = strAddr.GetBuffer(strAddr.GetLength());

strcpy(SendAddr,strAddr);

strAddr.ReleaseBuffer();

// Change the command's data style from CString to char*

SendBuf = strContent.GetBuffer(strContent.GetLength());

strcpy(SendBuf,strContent);

strContent.ReleaseBuffer();

//open a VISA resource

status = viOpenDefaultRM(&defaultRM);

if (status < VI_SUCCESS)

{

AfxMessageBox("No VISA resource was opened!");

return false;

}

status = viOpen(defaultRM, SendAddr, VI_NULL, VI_NULL, &instr);

//Write command to the instrument

status = viWrite(instr, (unsigned char *)SendBuf, strlen(SendBuf), &retCount);

//Close the system

status = viClose(instr);

status = viClose(defaultRM);

return bWriteOK;

}

¡¡

2) Encapsulate the read operation of VISA to facilitate the operation.

bool CDemoForDSADlg::InstrRead(CString strAddr, CString *pstrResult) //Read //operation

{

ViSession defaultRM,instr;

ViStatus status;

ViUInt32 retCount;

char * SendAddr = NULL;

unsigned char RecBuf[MAX_REC_SIZE];

bool bReadOK = false;

CString str;

// Change the address's data style from CString to char*

SendAddr = strAddr.GetBuffer(strAddr.GetLength());

strcpy(SendAddr,strAddr);

strAddr.ReleaseBuffer();

memset(RecBuf,0,MAX_REC_SIZE);

//Open a VISA resource

status = viOpenDefaultRM(&defaultRM);

if (status < VI_SUCCESS)

{

// Error Initializing VISA...exiting

AfxMessageBox("No VISA resource was opened!");

return false;

}

//Open the instrument

status = viOpen(defaultRM, SendAddr, VI_NULL, VI_NULL, &instr);

//Read from the instrument

status = viRead(instr, RecBuf, MAX_REC_SIZE, &retCount);

//close the system

status = viClose(instr);

status = viClose(defaultRM);

(*pstrResult).Format("%s",RecBuf);

return bReadOK;

}

¡¡

7. Add the control message response codes.

1) Connect the instrument

void CDemoForDSADlg::OnBtConnectInstr() // Connect to the instrument

{

// TODO: Add your control notification handler code here

ViStatus status;

ViSession defaultRM;

ViString expr = "?*";

ViPFindList findList = new unsigned long;

ViPUInt32 retcnt = new unsigned long;

ViChar instrDesc[1000];

CString strSrc = "";

CString strInstr = "";

unsigned long i = 0;

bool bFindDSA = false;

status = viOpenDefaultRM(&defaultRM);

if (status < VI_SUCCESS)

{

// Error Initializing VISA...exiting

MessageBox("No VISA instrument was opened ! ");

return ;

}

memset(instrDesc,0,1000);

// Find resource

status = viFindRsrc(defaultRM,expr,findList, retcnt, instrDesc);

for (i = 0;i < (*retcnt);i++)

{

// Get instrument name

strSrc.Format("%s",instrDesc);

InstrWrite(strSrc,"*IDN?");

::Sleep(200);

InstrRead(strSrc,&strInstr);

// If the instrument(resource) belongs to the DSA series then jump out //from the loop

strInstr.MakeUpper();

if (strInstr.Find("DG") >= 0)

{

bFindDSA = true;

m_strInstrAddr = strSrc;

break;

}

//Find next instrument

status = viFindNext(*findList,instrDesc);

}

if (bFindDSA == false)

{

MessageBox("Didn¡¯t find any DG!");

}

UpdateData(false);

}

¡¡

2) Write operation

void CDemoForDSADlg::OnBtWrite() //Write operation

{

// TODO: Add your control notification handler code here

UpdateData(true);

if (m_strInstrAddr.IsEmpty())

{

MessageBox("Please connect to the instrument first!");

}

InstrWrite(m_strInstrAddr,m_strCommand);

m_strResult.Empty();

UpdateData(false);

}

3) Read operation

void CDemoForDSADlg::OnBtRead() //Read operation

{

// TODO: Add your control notification handler code here

UpdateData(true);

InstrRead(m_strInstrAddr,&m_strResult);

UpdateData(false);

}

¡¡

8. Execution result

1) Click "Connect" to search the signal generator;

2) Input "*IDN?" in the "Command" edit box;

3) Click "Write" to write the command into the signal generator;

4) Click "Read" to read the return value.

¡¡

The execution result is as shown in the figure below.

¡¡