LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

call dll with 1D array parameter

Hi, I am trying to interface a Keyence laser displacement sensor controller with labview. Keyence has a dll API and I wrote a wrapper in C with DEV-C++. The wrapper is compiled into another DLL so I can call in my application. (Keyence DLL cannot be called directly for some reason)

 

These are tested in Matlab and worked well. The problem is when I try to use the "Call Library Function Node " in Labview, it always report me errors. The C code is pasted as below. The arrays are 1-D and not been resized in the function call.

 

Any input is highly appreciated.

 

 

Bo 

 

----------------------------------------------------------------------------------------------------------------

 

 DLLIMPORT int AcqData(float *DataA, float *DataB)
{
  typedef int (_stdcall *DataStorageStartPtr) (void);
  typedef int (_stdcall *DataStorageStopPtr) (void);
  typedef int (_stdcall *DataStorageInitPtr) (void);
  typedef int (_stdcall *DataStorageGetDataPtr) (int OutNo,int NumOutBuffer, LKIF_FLOATVALUE *OutBuffer, int *NumReceived);
  typedef int (_stdcall *DataStorageGetStatusPtr) (int OutNo, int *IsStorage, int *NumStorageData);

  DataStorageStartPtr DataStorageStart;
  DataStorageStopPtr DataStorageStop;
  DataStorageInitPtr DataStorageInit;
  DataStorageGetDataPtr DataStorageGetData;
  DataStorageGetStatusPtr DataStorageGetStatus;
 
  HINSTANCE hLib;

  int feedback;
  int OutNo;
  int NumOutBuffer = MAX_NUM_DATA;
  int IsStorageA, IsStorageB;
  int NumStorageData;
  int NumReceivedA, NumReceivedB;
  LKIF_FLOATVALUE OutBufferA[NumOutBuffer];
  LKIF_FLOATVALUE OutBufferB[NumOutBuffer];
 
  //---------------- Import the DLL --------------------//
  hLib = LoadLibrary("LkIF.dll");
  if(!hLib)
  {
   return -1;
  }
  //---------------- Get the addresses of functions ---------------//
  DataStorageStart = (DataStorageStartPtr)GetProcAddress(hLib, "LKIF_DataStorageStart");
  if(DataStorageStart == NULL)
  {
   return -2;
  }
  DataStorageStop = (DataStorageStopPtr)GetProcAddress(hLib, "LKIF_DataStorageStop");
  if(DataStorageStop == NULL)
  {
   return -3;
  }
  DataStorageInit = (DataStorageInitPtr)GetProcAddress(hLib, "LKIF_DataStorageInit");
  if(DataStorageInit == NULL)
  {
   return -4;
  }
  DataStorageGetData = (DataStorageGetDataPtr)GetProcAddress(hLib, "LKIF_DataStorageGetData");
  if(DataStorageGetData == NULL)
  {
   return -5;
  }
  DataStorageGetStatus = (DataStorageGetStatusPtr)GetProcAddress(hLib, "LKIF_DataStorageGetStatus");
  if(DataStorageGetStatus == NULL)
  {
   return -6;
  }
 
  //---------------------------- Data Acqusition --------------------------//
  feedback = (DataStorageInit)();
  if(!feedback)
  {
    return -7;
  }
 
  feedback = (DataStorageStart)();
  if(!feedback)
  {
    return -8;
  }
 
  sleep(10000);
 
 
  sleep(500);
  OutNo = 0;
  feedback = (DataStorageGetStatus)(OutNo, &IsStorageA, &NumStorageData);
  if(!feedback)
  {
   return -9;
  }
  OutNo = 1;
  feedback = (DataStorageGetStatus)(OutNo, &IsStorageB, &NumStorageData);
  if(!feedback)
  {
   return -10;
  }
 
  while(IsStorageA || IsStorageB)
  {
   sleep(500);
   OutNo = 0;
   feedback = (DataStorageGetStatus)(OutNo, &IsStorageA, &NumStorageData);
   if(!feedback)
   {
    return -9;
   }
   OutNo = 1;
   feedback = (DataStorageGetStatus)(OutNo, &IsStorageB, &NumStorageData);
   if(!feedback)
   {
    return -10;
   }
  }
 
 
  feedback = (DataStorageStop)();
  if(!feedback)
  {
   return -11;
  }
 
  OutNo = 0;
  feedback = (DataStorageGetData)(OutNo,NumOutBuffer,&OutBufferA[0],&NumReceivedA);
  if(!feedback)
  {
   return -12;
  }
 
  OutNo = 1;
  feedback = (DataStorageGetData)(OutNo,NumOutBuffer,&OutBufferB[0],&NumReceivedB);
  if(!feedback)
  {
   return -13;
  }
 
  int i;
 
  for(i = 0; i < NumReceivedA; i++)
  {
    DataA[i] = OutBufferA[i].Value;
  }
  for(i = 0; i < NumReceivedB; i++)
  {
    DataB[i] = OutBufferB[i].Value;
  }

  return 0;
}

 

Download All
0 Kudos
Message 1 of 12
(5,505 Views)
Try changing the calling conventions to C instead of WinAPI.
0 Kudos
Message 2 of 12
(5,499 Views)
Thanks. But still the same error message.
0 Kudos
Message 3 of 12
(5,495 Views)
You've also got the return value is an int in your export, but a long in LabVIEW.  Are your ints 32 bits?
0 Kudos
Message 4 of 12
(5,486 Views)
I notice you have corrections dot on the input side. Check the type used then building the array at the input side. Then you build a array on the input side, you do this to reserve memory for the dll. If you have not reserved the correct amount of memory before you call the function an erreor or a crash might happend. A float in C is 4 byte wide, equal to SGL in labview. A double is 8 byte wide, equal to DBL


Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
0 Kudos
Message 5 of 12
(5,481 Views)
Yeah, I think so. Actually, I tried signed 8-bit integer, signed 16-bit integer and signed 32-bit integer. All of them have the same error. Thanks.
0 Kudos
Message 6 of 12
(5,479 Views)
Post your vi. That will help in easy debugging. Post dll also. Put everything in a zip file and attach
0 Kudos
Message 7 of 12
(5,453 Views)

Hi All,

 I agree with mathan. I think seeing the VI may help us help you.

Adam H
National Instruments
Applications Engineer
0 Kudos
Message 8 of 12
(5,422 Views)

I am sorry for the delay. Here is the vi and dlls and the C source code. Thanks.

0 Kudos
Message 9 of 12
(5,371 Views)

Bo2008:

 

I have used lkIF.dll in several applications, using a call library function node.  I have never had to write any C or other "extra" code (in LV 8.0 and 8.5). 

 

Check the version number on your LK-Navigator disk.  The USB driver from disks at version 1.40 will work with lkIF.dll, but the driver from a version 1.51 disk will not.  I cannot tell you where the good-to-bad transition point occurs.

 

My VI is attached.

 

Cheers,

-GN

0 Kudos
Message 10 of 12
(4,708 Views)