#include "stdafx.h" #include "ODK_StringHelper.h" #include "ODK_Functions.h" #include #include #include #include "tchar.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #import "libid:BED7F4EA-1A96-11D2-8F08-00A0C9A6186D" // mscorlib using namespace mscorlib; #import "libid:ecf2e77d-3a90-4fb8-b0e2-f529f0cae9c9" // OpcLabs.BaseLib using namespace OpcLabs_BaseLib; #import "libid:14D1A788-0333-4D64-8547-8FB0357408EF" // OpcLabs.EasyOpcClassicInternal using namespace OpcLabs_EasyOpcClassicInternal; #import "libid:1F165598-2F77-41C8-A9F9-EAF00C943F9F" // OpcLabs.EasyOpcClassic using namespace OpcLabs_EasyOpcClassic; /* * OnLoad() is invoked after the application binary was loaded. * @return ODK_SUCCESS notify, that no error occurs * - OnRun() will be invoked automatically * any other value notify, that an error occurs (user defined) * - OnUnload() will be invoked automatically * - ODK application will be unloaded automatically */ EXPORT_API ODK_RESULT OnLoad(void) { // place your code here return ODK_SUCCESS; } /* * OnUnload() is invoked before the application binary is unloaded or when * ODK-Host terminates. * @return ODK_SUCCESS notify, that no error occurs * any other value notify, that an error occurs (user defined) * - ODK application will be unloaded anyway */ EXPORT_API ODK_RESULT OnUnload(void) { // place your code here return ODK_SUCCESS; } /* * OnRun() is invoked when the CPU transitions to the RUN state and * after OnLoad(). * @return Does not affect the load operation or state transition. */ EXPORT_API ODK_RESULT OnRun(void) { // place your code here return ODK_SUCCESS; } /* * OnStop() is invoked when the CPU transitions to the STOP/SHUTDOWN state * or before unloading the ODK application. * @return Does not affect the load operation or state transition. */ EXPORT_API ODK_RESULT OnStop(void) { // place your code here return ODK_SUCCESS; } /* * Ola Brånås, Automasjon og montajse. A.S 01.09.2016 * */ ODK_RESULT OPCResult( const ODK_S7STRING iMachineName[100], const ODK_S7STRING iServerClass[100], const ODK_S7STRING iTagName[100], const ODK_INT16& opcType, ODK_FLOAT& opcFloat, ODK_INT32& opcInt32, ODK_INT16& opcInt16, ODK_UINT32& opcDWORD, ODK_UINT16& opcWORD, ODK_BYTE& opcByte, ODK_BOOL& opcBool, ODK_INT16& opcQuality, ODK_DTL& opcTimeStamp) { char machinename[100]; char serverclass[100]; char tagname[100]; string strmachinename; string strserverclass; string strtagname; Convert_S7STRING_to_SZSTR(iMachineName, machinename, 100); Convert_S7STRING_to_SZSTR(iServerClass, serverclass, 100); Convert_S7STRING_to_SZSTR(iTagName, tagname, 100); strmachinename = machinename; strserverclass = serverclass; strtagname = tagname; _bstr_t bmachinename = machinename; _bstr_t bserverclass = serverclass; _bstr_t btagname = tagname; BSTR bstrmachinename; BSTR bstrserverclass; BSTR bstrtagname; bstrmachinename = bmachinename; bstrserverclass = bserverclass; bstrtagname = btagname; // Initialize the COM library CoInitialize(NULL); // Instatiate the EasyOPC-DA client object _EasyDAClientPtr ClientPtr(__uuidof(EasyDAClient)); _DAReadItemArgumentsPtr ReadItemArguments1Ptr(_uuidof(DAReadItemArguments)); ReadItemArguments1Ptr->ServerDescriptor->MachineName = bstrmachinename; ReadItemArguments1Ptr->ServerDescriptor->ServerClass = bstrserverclass; ReadItemArguments1Ptr->ItemDescriptor->ItemId = bstrtagname; ReadItemArguments1Ptr->ReadParameters->DataSource = DADataSource_Cache; CComSafeArray ArgumentsArray(1); ArgumentsArray.SetAt(0, _variant_t((IDispatch*)ReadItemArguments1Ptr)); LPSAFEARRAY pArgumentsArray = ArgumentsArray.Detach(); CComSafeArray ResultArray(ClientPtr->ReadMultipleItems(&pArgumentsArray)); ArgumentsArray.Attach(pArgumentsArray); _DAVtqResultPtr DAVtqResultPtr(ResultArray[0]); _DAVtqPtr DAVtqPtr(DAVtqResultPtr->Vtq); variant_t res = DAVtqPtr->Value; switch (opcType) { case 4: //Float 32 bit opcFloat = res.fltVal; opcInt32 = 0; opcInt16 = 0; opcDWORD = 0; opcWORD = 0; opcByte = 0; opcBool = false; break; case 2: //16bit signed integer opcFloat = 0.0; opcInt32 = 0; opcInt16 = res.iVal; opcDWORD = 0; opcWORD = 0; opcByte = 0; opcBool = false; break; case 3: //32bit signed integer opcFloat = 0.0; opcInt32 = res.lVal; opcInt16 = 0; opcDWORD = 0; opcWORD = 0; opcByte = 0; opcBool = false; break; case 11: //BOOL opcFloat = 0.0; opcInt32 = 0; opcInt16 = 0; opcDWORD = 0; opcWORD = 0; opcByte = 0; opcBool = res.boolVal; break; case 16: //Byte opcFloat = 0.0; opcInt32 = 0; opcInt16 = 0; opcDWORD = 0; opcWORD = 0; opcByte = res.bVal; opcBool = false; break; case 18: //16bit WORD opcFloat = 0.0; opcInt32 = 0; opcInt16 = 0; opcDWORD = 0; opcWORD = res.uiVal; opcByte = 0; opcBool = false; break; case 19: //32bit DWORD opcFloat = 0.0; opcInt32 = 0; opcInt16 = 0; opcDWORD = res.ulVal; opcWORD = 0; opcByte = 0; opcBool = false; break; default: opcFloat = 0.0; opcInt32 = 0; opcInt16 = 0; opcDWORD = 0; opcWORD = 0; opcByte = 0; opcBool = false; break; } //Quality opcQuality = (int)(DAVtqPtr->Quality->NumericalValue); //Timestamp COleDateTime timestamp(DAVtqPtr->TimestampLocal); opcTimeStamp.Year = timestamp.GetYear(); opcTimeStamp.Month = timestamp.GetMonth(); opcTimeStamp.Day = timestamp.GetDay(); opcTimeStamp.Hour = timestamp.GetHour(); opcTimeStamp.Minute = timestamp.GetMinute(); opcTimeStamp.Second = timestamp.GetSecond(); // Release all interface pointers BEFORE calling CoUninitialize() ResultArray.Destroy(); ClientPtr = NULL; CoUninitialize(); return ODK_SUCCESS; }