Lesen und Schreiben von DATE/TIME-Variablen
Die SPS enthält die TIME-Variable MAIN.Time1 und die DT-Variable MAIN.Date1. Dieses Beispielprojekt zeigt, wie diese Werte gelesen, angezeigt und geschrieben werden können:
Download: sample21-c-ads-dll-accessdatetimevariables.zip
#include <time.h>
// ADS headers
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsDef.h"
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsApi.h"
#define TIME_LENGHT 56
#define DATE_LENGHT 62
#define MON_START 1
#define YEAR_START 1900
int _tmain(int argc, _TCHAR* argv[])
{
long lErr, lPort;
long lTime, lMs, lSek, lMin, lHour, lDay;
AmsAddr Addr;
PAmsAddr pAddr = &Addr;
DWORD dwTime, dwDate;
ULONG lHdlTime, lHdlDate;
// Declare PLC variable
char szPlcTime []={"MAIN.Time1"};
char szPlcDate []={"MAIN.Date1"};
// Open the communication
lPort = AdsPortOpen();
lErr = AdsGetLocalAddress(pAddr);
if(lErr) printf_s((char*)"Error: Getting local adress: 0x%i \n", lErr);
// TwinCAT2 RTS1 Port = 801, TwinCAT3 RTS1 Port = 851
pAddr->port = 801;
// Get variable handle
lErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlTime), &lHdlTime, sizeof(szPlcTime), szPlcTime);
lErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlDate), &lHdlDate, sizeof(szPlcDate), szPlcDate);
// Read from MAIN.Time1
lErr = AdsSyncReadReq(pAddr,
ADSIGRP_SYM_VALBYHND, // IndexGroup
lHdlTime, // IndexOffset
0x4, // Size of DWORD
&dwTime);
if(lErr) printf_s((char*)"Error: Read time variable: 0x%i \n", lErr);
//Convert DWORD to Time
lTime = (long)dwTime;
lMs = (lTime % 1000);
lSek = (lTime / 1000) % 60;
lMin = (lTime / 60000) % 60;
lHour = (lTime / 3600000) % 24;
lDay = (lTime / 86400000) % 365;
wchar_t szTime[TIME_LENGHT];
wsprintf(szTime, L"Time from PLC: %dd %dh %dm %ds %dms \n",
lDay,
lHour,
lMin,
lSek,
lMs);
wprintf_s(szTime);
//Write to MAIN.Time1
//Manipulate DWORD for demonstration
dwTime += 3600000; //Add 3600000ms (One hour)
//AdsWrite
lErr = AdsSyncWriteReq(pAddr,
ADSIGRP_SYM_VALBYHND, //IndexGroup
lHdlTime, //IndexOffset
0x4,
&dwTime);
if(lErr) printf_s((char*)"Error: Write time variable: 0x%i \n", lErr);
//Read from MAIN.Date1
//AdsRead
lErr = AdsSyncReadReq(pAddr,
ADSIGRP_SYM_VALBYHND, //IndexGroup
lHdlDate, //IndexOffset
0x4,
&dwDate);
if(lErr) printf_s((char*)"Error: Read date variable: 0x%i \n", lErr);
//Convert long to date
time_t tDate(dwDate);
tm tmDate;
gmtime_s(&tmDate, &tDate);
wchar_t szDate[DATE_LENGHT];
wsprintf(szDate, L"Date from PLC: %d/%d/%d %d:%d:%d \n",
tmDate.tm_mday,
tmDate.tm_mon + MON_START,
tmDate.tm_year + YEAR_START,
tmDate.tm_hour,
tmDate.tm_min,
tmDate.tm_sec);
wprintf_s(szDate);
//Write to MAIN.Date1
//Manipulate DWORD for demonstration
dwDate += 3600; //Add 3600s (One hour)
//AdsWrite
lErr = AdsSyncWriteReq(pAddr,
ADSIGRP_SYM_VALBYHND, //IndexGroup
lHdlDate, //IndexOffset
0x4,
&dwDate);
if(lErr) printf_s((char*)L"Error: Write time variable: 0x%i \n", lErr);
//Releases handles of plc variable
lErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_RELEASEHND, 0, sizeof(lHdlTime), &lHdlTime);
if (lErr) printf("Error: AdsSyncWriteReq: %d \n", lErr);
lErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_RELEASEHND, 0, sizeof(lHdlDate), &lHdlDate);
if (lErr) printf("Error: AdsSyncWriteReq: %d \n", lErr);
//Close the communication
lErr = AdsPortClose();
if(lErr) printf_s((char*)L"Error: Closing connection: 0x%i \n", lErr);
printf_s("\nPress enter to exit..");
getchar();
}