|

楼主 |
发表于 2009-3-14 12:41:23
|
显示全部楼层
#include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */
#include <userint.h>
#include <ansi_c.h>
#include <cviauto.h>
#include "dataskt.h"
#include <formatio.h>
#include "OpcClient.h"
static int panelHandle;
static int connectedFlag = 0;
static DSHandle dsHandle = 0;
static int OPCToURL(char** url);
static void URLToOPC(void);
static void ShowDataSocketError(HRESULT errorCode);
static double sMax = 1.0, sMin = 0.0;
/* This is the callback function for the DataSocket */
void CVICALLBACK DSCallback (DSHandle dsHandle, int event, void *callbackData);
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0) /* Needed if linking in external compiler; harmless otherwise */
return -1; /* out of memory */
/* Making this thread Apartment-threaded so that the panel gets repainted
when the DS_SelectURL dialog box is active */
CA_InitActiveXThreadStyleForCurrentThread (0,
COINIT_APARTMENTTHREADED);
if ((panelHandle = LoadPanel (0, "OpcClient.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
return 0;
}
int CVICALLBACK StartCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT hr = S_OK;
char* url;
short selected;
switch (event) {
case EVENT_COMMIT:
if (dsHandle == 0) {
OPCToURL(&url);
hr = DS_Open (url, DSConst_ReadAutoUpdate, DSCallback,
NULL, &dsHandle);
SetCtrlVal(panelHandle, PANEL_URL, url);
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, TRUE);
free(url);
}
break;
}
return 0;
}
int CVICALLBACK SelectCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT hr = S_OK;
char* startURL;
char* url;
short selected;
switch (event) {
case EVENT_COMMIT:
OPCToURL(&startURL);
SetCtrlVal(panelHandle, PANEL_URL, startURL);
hr = DS_SelectURL ("Select URL", startURL,
0, "", &selected, &url);
if (selected) {
SetCtrlVal(panelHandle, PANEL_URL, url);
DS_FreeMemory(url);
URLToOPC();
}
free(startURL);
break;
}
return 0;
}
int CVICALLBACK StopCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT hr = S_OK;
switch (event) {
case EVENT_COMMIT:
if ((dsHandle != 0) && connectedFlag) {
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, FALSE);
hr = DS_DiscardObjHandle (dsHandle);
dsHandle = 0;
SetCtrlVal (panelHandle, PANEL_STATUS, "Disconnected");
connectedFlag = 0;
}
break;
}
return 0;
}
int CVICALLBACK QuitCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT hr = S_OK;
switch (event) {
case EVENT_COMMIT:
if ((dsHandle != 0) && connectedFlag) {
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, FALSE);
hr = DS_DiscardObjHandle (dsHandle);
dsHandle = 0;
connectedFlag = 0;
}
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK TimerCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT hr = S_OK;
double theValue;
unsigned type;
switch (event) {
case EVENT_TIMER_TICK:
if (dsHandle != 0) {
hr = DS_GetDataType (dsHandle, &type, NULL, NULL);
if (!SUCCEEDED(hr)) {
ShowDataSocketError(hr);
goto Error;
}
hr = DS_GetDataValue (dsHandle, CAVT_DOUBLE, &theValue, 1, NULL, NULL);
if (!SUCCEEDED(hr)) {
ShowDataSocketError(hr);
goto Error;
}
if (theValue <= sMin) {
sMin = theValue - sMax * 0.5;
SetAxisScalingMode (panelHandle, PANEL_STRIPCHART,
VAL_LEFT_YAXIS, VAL_MANUAL,
sMin, sMax);
}
if (theValue >= sMax) {
sMax = theValue + sMax * 0.5;
SetAxisScalingMode (panelHandle, PANEL_STRIPCHART,
VAL_LEFT_YAXIS, VAL_MANUAL,
sMin, sMax);
}
PlotStripChartPoint (panelHandle, PANEL_STRIPCHART, theValue);
}
break;
}
Error:
return 0;
}
void DSCallback (DSHandle localDSHandle, int event, void *pUserData)
{
HRESULT hr = S_OK;
char message[1000];
switch (event) {
case DS_EVENT_DATAUPDATED:
connectedFlag = 1;
break;
case DS_EVENT_STATUSUPDATED:
if (dsHandle != 0) {
hr = DS_GetLastMessage (localDSHandle, message, 1000);
if (SUCCEEDED(hr))
SetCtrlVal (panelHandle, PANEL_STATUS, message);
}
break;
}
Error:
return;
}
static int OPCToURL(char** url)
{
char hostName[500];
char server[500];
char itemName[100];
GetCtrlVal(panelHandle, PANEL_HOSTNAME, hostName);
GetCtrlVal(panelHandle, PANEL_SERVER, server);
GetCtrlVal(panelHandle, PANEL_ITEM, itemName);
*url = malloc(strlen(hostName) + strlen(server) + strlen(itemName) + 50);
if ((*url) == NULL)
return -1;
(*url)[0] = '\0';
if (strlen(hostName) == 0 || (strcmp(hostName, "localhost") == 0)) {
strcat(*url, "opc:");
}
else {
strcat(*url, "opc://");
strcat(*url, hostName);
}
strcat(*url, "/");
strcat(*url, server);
strcat(*url, "/");
strcat(*url, itemName);
return 0;
}
static void URLToOPC()
{
char* hostName;
char* server;
char* itemName;
int indexMachineName;
char url[500];
GetCtrlVal(panelHandle, PANEL_URL, url);
indexMachineName = FindPattern (url, 0, -1, "//", 0, 0);
if (indexMachineName >= 0){
hostName = strtok(&url[indexMachineName+2], "/");
if (strcmp(hostName, "localHost") == 0)
SetCtrlVal(panelHandle, PANEL_HOSTNAME, "");
else
SetCtrlVal(panelHandle, PANEL_HOSTNAME, hostName);
}
else{
strtok(url, "/");
SetCtrlVal(panelHandle, PANEL_HOSTNAME, "");
}
if ((server = strtok(NULL, "/")) != NULL)
SetCtrlVal(panelHandle, PANEL_SERVER, server);
if ((itemName = strtok(NULL, "\0"))!= NULL)
SetCtrlVal(panelHandle, PANEL_ITEM, itemName);
}
static void ShowDataSocketError(HRESULT errorCode)
{
CA_DisplayErrorInfo(dsHandle, "DataSocket Error", errorCode, NULL);
return;
} |
|