不用labview,我是用VS08,现在VI写函数不知道哪里错了,编译又没错,但是写不成功,以下是代码:
#define NIVISA_USB
#include <afx.h>
#include <stdio.h>
#include<stdlib.h>
#include <visa.h>
#include<string.h>
ViSession defaultRM,vi;//定义保存会话句柄的变量
static ViUInt32 writeCount;
static ViStatus status;
unsigned char* lpoutbuffer;
char DataChange(CString str)//十六进制转换函数
{
int k=0,j=0,Revalue;
for (j=0;j<2;j++)
{
if(j==0)
Revalue=16;
else
Revalue=1;
switch (str[j])
{
case '0':k+=0;
break;
case '1':k+=Revalue;
break;
case '2':k+=Revalue*2;
break;
case '3':k+=Revalue*3;
break;
case '4':k+=Revalue*4;
break;
case '5':k+=Revalue*5;
break;
case '6':k+=Revalue*6;
break;
case '7':k+=Revalue*7;
break;
case '8':k+=Revalue*8;
break;
case '9':k+=Revalue*9;
break;
case 'A':k+=Revalue*10;
break;
case 'B':k+=Revalue*11;
break;
case 'C':k+=Revalue*12;
break;
case 'D':k+=Revalue*13;
break;
case 'E':k+=Revalue*14;
break;
case 'F':k+=Revalue*15;
break;
}
}
k=(char)k;
return k;
}
int main()
{
CString datastr,data;
int length,m,i;
datastr.Empty();
data.Empty();
datastr="0A0170000000010000020200038F0004C0700500000600000700000804000900000C0F000D00000A01600B0B300A8050170202960400060C0F000D00000A01600A805004C07096";
length=datastr.GetLength();
m=length/2;
lpoutbuffer=new unsigned char[m+1];
for (i=0;i<m;i++)
{
data=datastr.Mid(i*2,2);
lpoutbuffer=DataChange(data);
data.Empty();
}
lpoutbuffer='\0';
status=viOpenDefaultRM(&defaultRM);//打开缺省资源管理器会话
if (status < VI_SUCCESS)
{
printf ("Could not open a session to the VISA Resource Manager!\n");
exit (EXIT_FAILURE);
}
status=viOpen(defaultRM,"USB0::0x0547::0x1002::NI-VISA-0::RAW",VI_NULL,VI_NULL,&vi);//打开仪器会话
if (status < VI_SUCCESS)
{
printf ("Cannot open a session to the device.\n");
exit (EXIT_FAILURE);
}
else
printf ("open a session to the device successs.\n");
status =viSetAttribute(vi,VI_ATTR_USB_BULK_OUT_PIPE,2);
if(status<VI_SUCCESS)
printf ("Error setting a response from the device\n");
else
printf (" setting a response from the device\n");
status = viWrite(vi,(ViBuf)lpoutbuffer, (ViUInt32)(m+1),&writeCount);
// status = viWrite(vi,(ViBuf)datach, (ViUInt32)strlen(datach),&writeCount);
if(status<VI_SUCCESS)
printf ("Error writting a response from the device\n");
else
printf (" writing a response from the device\n");
// delete[] lpoutbuffer;
viClose (vi);
viClose(defaultRM);
return 0;
}
麻烦看一下,我实在找不到原因了 |