Samples: NI Measurement Studio

To use the TcAdsDll.dll in Measurement Studio, the header file def.h must be included. This file contains missing definitions and __int64 structure which are not supported by Measurement Studio.  For demonstration purposes, the necessary changes are shown here in a small sample. The sample Event-driven reading was used as a template.

 

Header file def.h:

#define __inline
#define _declspec __declspec

typedef struct
{
   unsigned long LowPart;
   unsigned long HighPart;
} __int64;

 

Sample program:

#include <ansi_c.h>
#include <windows.h>
#include <winbase.h>
#include <utility.h>
#include "def.h"

#include "c:\twincat\adsapi\tcadsdll\include\tcadsdef.h"
#include "c:\twincat\adsapi\tcadsdll\include\tcadsapi.h"

static void __stdcall NotificationCallback(AmsAddr*pAddr,
AdsNotificationHeader* pNotification, unsigned long hUser)
{
   SYSTEMTIME SystemTime, LocalTime;
   FILETIME FileTime;
   TIME_ZONE_INFORMATION TimeZoneInformation;

   FileTime.dwLowDateTime = pNotification->nTimeStamp.LowPart;
   FileTime.dwHighDateTime = pNotification->nTimeStamp.HighPart;
   FileTimeToSystemTime(&FileTime, &SystemTime);

   // Zeit umrechnen in lokaler Zeiteinheit
   GetTimeZoneInformation(&TimeZoneInformation);
   SystemTimeToTzSpecificLocalTime(&TimeZoneInformation, &SystemTime, &LocalTime);

   // Zeitstempel ausgeben
   printf("%i:%i:%i.%i den: %i.%i.%i\n",LocalTime.wHour, LocalTime.wMinute, LocalTime.wSecond, LocalTime.wSecond, LocalTime.wDay, LocalTime.wMonth, LocalTime.wYear);

   printf("NotificationCallback: nValue = %i\n", *(int *)pNotification->data);
}


int main(int argc, char *argv[])
{
   AdsNotificationAttrib adsNotificationAttrib;
   int nValue = 0;
   int nErr = ADSERR_NOERR;
   int port = AdsPortOpen();
   int hNotification;
   unsigned long hUser;

   AmsNetId netId = {172,16,8,56,1,1};
   AmsAddr addr;
   addr.netId = netId;
   addr.port = 801;

   nErr = AdsSyncReadReq( &addr, 0x4020, 0x0,
sizeof(nValue), &nValue);

   if( nErr != ADSERR_NOERR )
      printf("ErrorAdsSyncRead:%i\n", nErr);
   else
      printf("nValue = %i\n",nValue);

   adsNotificationAttrib.cbLength = 4;
   adsNotificationAttrib.nTransMode = ADSTRANS_SERVERONCHA;
   adsNotificationAttrib.nMaxDelay = 20000000; // 2sec
   adsNotificationAttrib.nCycleTime = 10000000; // 1sec

   hUser = 3474573467;

   nErr = AdsSyncAddDeviceNotificationReq( &addr, 0x4020, 0x0, &adsNotificationAttrib,
   NotificationCallback, hUser, &hNotification);

   if( nErr != ADSERR_NOERR )
   {
      printf("ErrorAdsSyncAddDeviceNotificationReq:%i\n", nErr);
      return 0;
   }


   while(!KeyHit());


   nErr = AdsSyncDelDeviceNotificationReq( &addr,
hNotification);

   if( nErr != ADSERR_NOERR )

      printf("Error
AdsSyncDelDeviceNotificationReq:%i\n", nErr);


   AdsPortClose();
   return 0;
}

Unzip sample program 'Adaptations for Measurement Studio'.