📄 mexopc.cpp
字号:
// mexOPC.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "mexOPC.h"
#include "OPC ClientDoc.h"
#include "WriteItemDlg.h"
#include "OPCServerDlg.h"
#include "AddItem.h"
#include "Time.h"
#include "mex.h"
#include <afxmt.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CString allitems[100];
LPCTSTR itemID;
LPCTSTR accessPath;
extern double DoubleItemValue[1000];
extern CString CStringItemValue[1000];
mxArray *input;
const mxArray *inth;
const mxArray* sleeptime;
CArray<Item*,Item*> clientitems1;
BOOL start=1;
CTime T1,T2;
extern int itemcount;
OPCClientDoc* pDoc =NULL;
OPCItemMgt itemMgt;
TCHAR input_buf4[1024];
BOOL init=0;
int m_synctime =10;
extern CEvent* event;
CSingleLock lock(event);
const CLSID CLSID_OPCServerList = {0x13486D51,0x4821,0x11D2,{0xA4,0x94,0x3C,0xB3,0x06,0xC1,0x00,0x00}};
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CMexOPCApp
BEGIN_MESSAGE_MAP(CMexOPCApp, CWinApp)
//{{AFX_MSG_MAP(CMexOPCApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMexOPCApp construction
CMexOPCApp::CMexOPCApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
{
}
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CMexOPCApp object
CMexOPCApp theApp;
void gill(CString para,CString name,CString Data,double *z)
{
//*******************************************READ读函数**************************************
if(para.Compare("directread")==0||para.Compare("Directread")==0||para.Compare("DIRECTREAD")==0)
{
HRESULT *pErrors;
CWaitCursor wait;
Item* pCurrentItem =new Item;
for(int i=0;i<clientitems1.GetSize();i++)
if(clientitems1.GetAt(i)->name.Compare(name)==0)
pCurrentItem = clientitems1.GetAt(i);
{ // also test sync read since data is returned differently
OPCSyncIO opcSyncIO;
if( opcSyncIO.Attach( pDoc->opcGroup ) == S_OK )
{
OPCITEMSTATE* pItemState;
HRESULT hr = opcSyncIO.Read(OPC_DS_DEVICE, 1, &pCurrentItem->hServerHandle, &pItemState, &pErrors);
if( SUCCEEDED(hr) )
{
if( FAILED(pErrors[0]))
{
pDoc->ReportError( _T("Sync Read: %s"), pErrors[0] );
z[0] = 2789561;
return;
}
else
{
pCurrentItem->quality = pItemState->wQuality;
pCurrentItem->value = pItemState->vDataValue;
}
VariantClear( &pItemState->vDataValue );
CoTaskMemFree( pItemState );
CoTaskMemFree( pErrors );
}
else
{
pDoc->ReportError( _T("Sync Read: %s "), pErrors[0] );
z[0] = 2789561;
return ;
}
}
}
if( pDoc->usingCP )
{
OPCAsyncIO2 opcAsyncIO2;
if( opcAsyncIO2.Attach( pDoc->opcGroup ) == S_OK )
{
pDoc->transactionID = 3;
HRESULT hr = opcAsyncIO2.Read(1, &pCurrentItem->hServerHandle, pDoc->transactionID, &pDoc->transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
if( FAILED(pErrors[0]) )
{
pDoc->ReportError( _T("Async Read: %s"), pErrors[0] );
z[0] = 2789561;
return ;
}
CoTaskMemFree( pErrors );
}
else
{
pDoc->ReportError( _T("Async Read:%s "), pErrors[0] );
z[0] = 2789561;
return ;
}
#ifdef FULL_TEST
transactionID = 3;
hr = opcAsyncIO2.Read(1, &pCurrentItem->hServerHandle, transactionID, &transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
CoTaskMemFree( pErrors );
}
transactionID = 3;
hr = opcAsyncIO2.Read(1, &pCurrentItem->hServerHandle, transactionID, &transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
CoTaskMemFree( pErrors );
}
// ask for a few this time (it doesn't matter that its the same one)
transactionID = 3;
OPCHANDLE serverHandles[3];
for( int i=0; i<3; i++ )
serverHandles[i] = pCurrentItem->hServerHandle;
hr = opcAsyncIO2.Read(3, serverHandles, transactionID, &transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
CoTaskMemFree( pErrors );
}
#endif // FULL_TEST
}
}
else
{
OPCAsyncIO opcAsyncIO;
if( opcAsyncIO.Attach( pDoc->opcGroup ) == S_OK )
{
HRESULT hr = opcAsyncIO.Read(pDoc->dwConnection1, OPC_DS_CACHE, 1, &pCurrentItem->hServerHandle, &pDoc->transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
if( FAILED(pErrors[0]) )
{
pDoc->ReportError( _T("Async Read:%s "), pErrors[0] );
z[0] = 2789561;
return ;
}
CoTaskMemFree( pErrors );
}
else
{
pDoc->ReportError( _T("Async Read: %s"), pErrors[0] );
z[0] = 2789561;
return ;
}
#ifdef FULL_TEST
hr = opcAsyncIO.Read(dwConnection1, OPC_DS_DEVICE, 1, &pCurrentItem->hServerHandle, &transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
CoTaskMemFree( pErrors );
}
hr = opcAsyncIO.Read(dwConnection1, OPC_DS_CACHE, 1, &pCurrentItem->hServerHandle, &transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
CoTaskMemFree( pErrors );
}
// ask for a few this time (it doesn't matter that its the same one)
OPCHANDLE serverHandles[3];
for( int i=0; i<3; i++ )
serverHandles[i] = pCurrentItem->hServerHandle;
hr = opcAsyncIO.Read(dwConnection1, OPC_DS_DEVICE, 3, serverHandles, &transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
CoTaskMemFree( pErrors );
}
#endif // FULL_TEST
}
}
switch (pCurrentItem->value.vt)
{
case VT_I2: //SHORT
{
DoubleItemValue[999] = double(pCurrentItem->value.iVal);
break;
}
case VT_I4: //LONG
{
DoubleItemValue[999] = double(pCurrentItem->value.iVal);
break;
}
case VT_R4: //Float
{
DoubleItemValue[999] = double(pCurrentItem->value.fltVal);
break;
}
case VT_R8: //Double
{
DoubleItemValue[999] = double(pCurrentItem->value.dblVal);
break;
}
case VT_BOOL:
{
DoubleItemValue[999] =pCurrentItem->value.boolVal;
break;
}
case VT_UI2: //WORD
{
DoubleItemValue[999] =pCurrentItem->value.uiVal;
break;
}
case VT_UI4: //DWORD
{
DoubleItemValue[999] =pCurrentItem->value.ulVal;
break;
}
case VT_BSTR: //String
{
CStringItemValue[999]=pCurrentItem->value.bstrVal;
break;
}
}
if(pCurrentItem->value.vt==VT_BSTR)
{
lstrcpy(input_buf4, CStringItemValue[999]);
}
else
{
z[0]=DoubleItemValue[999];
}
}
if(para.Compare( "read" ) == 0 ||para.Compare( "Read" ) == 0||para.Compare( "READ" ) == 0)
{
USES_CONVERSION;
int h=0;
int l=0;
for(int i=0;i<100;i++)
{
if(clientitems1.ElementAt(i)->name.Compare(name)==0)//allitems[i]
{
h=1;
l=i;
break;
}
else
{
h =0;
AfxMessageBox("你输入的数据项名不正确");
z[0] = 2789561;
return ;
}
}
if(clientitems1[i]->value.vt==VT_BSTR)
{
lstrcpy(input_buf4, CStringItemValue[l]);
}
else
{
z[0]=DoubleItemValue[l];
}
}
if(para.Compare( "write" ) == 0||para.Compare( "Write" ) == 0||para.Compare( "WRITE" ) == 0
||para.Compare( "directwrite" ) == 0||para.Compare( "Directwrite" ) == 0||para.Compare( "DirectWrite" ) == 0||para.Compare( "DIRECTWRITE" ) == 0)
{
USES_CONVERSION;
HRESULT *pErrors;
WriteItemDlg dlg;
Item* item = new Item;
dlg.m_item = name;
double * y;
y = (double*)mxGetData(inth);
dlg.m_value =y[0];
for(int i=0;i<clientitems1.GetSize();i++)
{
if(clientitems1.ElementAt(i)->name.Compare(name)==0)
{
item->hServerHandle =clientitems1.ElementAt(i)->hServerHandle;
item->value.vt =clientitems1.ElementAt(i)->value.vt;
break;
}
}
if(para.Compare( "directwrite" ) == 0||para.Compare( "Directwrite" ) == 0||para.Compare( "DirectWrite" ) == 0||para.Compare( "DIRECTWRITE" ) == 0)
{
if( pDoc->usingCP )
{
OPCAsyncIO2 opcAsyncIO2;
if( opcAsyncIO2.Attach( pDoc->opcGroup ) == S_OK )
{
pDoc->transactionID = 2; // any number the client wants
COleVariant vt( dlg.m_value ); // initialize as a string
vt.ChangeType( item->value.vt ); // let COleVariant convert!
HRESULT hr = opcAsyncIO2.Write(1, &item->hServerHandle,
vt, pDoc->transactionID, &pDoc->transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
if( FAILED(pErrors[0]) )
{
pDoc->ReportError( _T("ASync Write: %s"), pErrors[0] );
z[0] = 2789561;
return ;
}
CoTaskMemFree( pErrors );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -