ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
Macros | Functions
ioPAC8500_aopc.c File Reference

ioPAC8500 Active OPC More...

#include "libmoxa_rtu.h"

Macros

#define AOPC_SERVICE_PROFILES   1
 
#define BUFFER_SIZE   128
 
#define SERVICE_AOPC_STATUS_TAG   "Service_AOPC_Status"
 
#define SERVICE_AOPC_TRIGGER_TAG   "Service_AOPC_Trigger"
 
#define SERVICE_AOPC_CONNCONTROL_PROFILE_TAG   "Service_AOPC_ConnControl_%d"
 
#define SERVICE_AOPC_STATUS_PROFILE_TAG   "Service_AOPC_Status_%d"
 

Functions

int main (int argc, char *argv[])
 

Detailed Description

ioPAC8500 Active OPC

Date
03-04-2014
Author
Eddy Kao
Version
V1.0
ioPAC8500_aopc.jpg
ioPAC8500 Active OPC
Introduction:
Update DI and DO to Moxa AOPC Server by Interval, OnChange, or TriggerTag.
Example:
1. Update by Interval and OnChange: ./ioPAC8500_aopc
2. Update by Interval, OnChange, and TriggerTag: ./ioPAC8500_aopc -t
Help:
root@Moxa:/tmp#./ioPAC8500_aopc -h
ioPAC8500 Active OPC program.

Usage: ./ioPAC8500_aopc [OPTIONS]

Options:
    -t        Enable updating by TriggerTag according to user demand.

Library:
TAG APIs
RTUxpress Project file:
ioPAC8500_aopc.rtu
(Please right click on the link and ‘Save Target As…’ to save RTUxpress project file and open it with RTUxpress utility)

Macro Definition Documentation

#define AOPC_SERVICE_PROFILES   1
#define BUFFER_SIZE   128
#define SERVICE_AOPC_STATUS_TAG   "Service_AOPC_Status"
#define SERVICE_AOPC_TRIGGER_TAG   "Service_AOPC_Trigger"
#define SERVICE_AOPC_CONNCONTROL_PROFILE_TAG   "Service_AOPC_ConnControl_%d"
#define SERVICE_AOPC_STATUS_PROFILE_TAG   "Service_AOPC_Status_%d"

Function Documentation

int main ( int  argc,
char *  argv[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* TAG Service for Active OPC
*
* Date Author Comment
* 03-04-2014 Eddy Kao Created.
******************************************************************************/
/*******************************************************************************
*
* Sample code for Active OPC Service
*
*******************************************************************************/
#include "libmoxa_rtu.h"
#define AOPC_SERVICE_PROFILES 1
#define BUFFER_SIZE 128
#define SERVICE_AOPC_STATUS_TAG "Service_AOPC_Status"
#define SERVICE_AOPC_TRIGGER_TAG "Service_AOPC_Trigger"
#define SERVICE_AOPC_CONNCONTROL_PROFILE_TAG "Service_AOPC_ConnControl_%d"
#define SERVICE_AOPC_STATUS_PROFILE_TAG "Service_AOPC_Status_%d"
int main(int argc, char * argv[])
{
int i = 0;
int retval = 0;
char triggerTagFlag = 0;
UINT8 profileIndex = 0;
UINT8 timeOut = 60; //60s
UINT8 cmd = 0;
TAG_INFO aopcStatusTagInfo;
TAG_INFO aopcTriggerTagInfo;
TAG_INFO aopcConnControlProfileTagInfo[AOPC_SERVICE_PROFILES];
TAG_INFO aopcStatusProfileTagInfo[AOPC_SERVICE_PROFILES];
UINT32 readBytes = 0;
int tagValue = 0;
UINT8 tagBuf[BUFFER_SIZE];
char cmdBuf[][BUFFER_SIZE] = {{"0: read Active OPC Service status"},
{"1: read Active OPC Profile statuses"},
{"2: connect to AOPC Server"},
{"3: disconnect from AOPC Server"},
{"4: update by TriggerTag"},
{"5: exit program"},
};
while((retval = getopt(argc, argv, "ht")) != -1)
{
switch(retval)
{
case 't':
triggerTagFlag = 1;
break;
case '?':
case 'h':
default:
printf("%s.\n\n", "ioPAC8500 Active OPC program");
printf("Usage: ./ioPAC8500_aopc [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Enable updating by TriggerTag according to user demand.\n", "-t");
printf("\n");
return 0;
}
}
printf("\r\nTag Init\r\n");
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init error = %d\r\n", retval);
return -1;
}
else printf("Tag Init ok!\r\n\r\n");
printf("Get Tag Info\r\n");
memset(&aopcStatusTagInfo, 0, sizeof(aopcStatusTagInfo));
rc = MX_RTU_Tag_Get_Info(SERVICE_AOPC_STATUS_TAG, &aopcStatusTagInfo);
if(rc != TAG_ERR_OK)
{
printf("tagName = %s, MX_RTU_Tag_Get_Info error = %d\r\n", SERVICE_AOPC_STATUS_TAG, rc);
goto exit;
}
if(triggerTagFlag)
{
memset(&aopcTriggerTagInfo, 0, sizeof(aopcTriggerTagInfo));
rc = MX_RTU_Tag_Get_Info(SERVICE_AOPC_TRIGGER_TAG, &aopcTriggerTagInfo);
if(rc != TAG_ERR_OK)
{
printf("tagName = %s, MX_RTU_Tag_Get_Info error = %d\r\n", SERVICE_AOPC_TRIGGER_TAG, rc);
goto exit;
}
}
for(profileIndex = 0; profileIndex < AOPC_SERVICE_PROFILES; profileIndex++)
{
memset(tagName, 0, sizeof(tagName));
sprintf(tagName, SERVICE_AOPC_CONNCONTROL_PROFILE_TAG, profileIndex);
memset(&aopcConnControlProfileTagInfo[profileIndex], 0, sizeof(TAG_INFO));
rc = MX_RTU_Tag_Get_Info(tagName, &aopcConnControlProfileTagInfo[profileIndex]);
if(rc != TAG_ERR_OK)
{
printf("tagName = %s, MX_RTU_Tag_Get_Info error = %d\r\n", tagName, rc);
goto exit;
}
memset(tagName, 0, sizeof(tagName));
sprintf(tagName, SERVICE_AOPC_STATUS_PROFILE_TAG, profileIndex);
memset(&aopcStatusProfileTagInfo[profileIndex], 0, sizeof(TAG_INFO));
rc = MX_RTU_Tag_Get_Info(tagName, &aopcStatusProfileTagInfo[profileIndex]);
if(rc != TAG_ERR_OK)
{
printf("tagName = %s, MX_RTU_Tag_Get_Info error = %d\r\n", tagName, rc);
goto exit;
}
}
printf("Get Tag Info ok\r\n\r\n");
printf("Get Active OPC Service status\r\n\r\n");
while(timeOut)
{
memset(tagBuf, 0, sizeof(tagBuf));
rc = MX_RTU_Tag_Read(SERVICE_AOPC_STATUS_TAG, (void *)tagBuf, sizeof(tagBuf), &readBytes, NULL);
if(rc != TAG_ERR_OK || readBytes != aopcStatusTagInfo.tagSize)
{
printf("tagName = %s, MX_RTU_Tag_Read error = %d, readBytes = %d, tagSize = %d\r\n", SERVICE_AOPC_STATUS_TAG, rc, readBytes, aopcStatusTagInfo.tagSize);
goto exit;
}
else
{
tagValue = 0;
memcpy(&tagValue, tagBuf, aopcStatusTagInfo.tagSize);
printf("tagName = %s, value = %d\r\n", SERVICE_AOPC_STATUS_TAG, tagValue);
}
if(tagValue == STATUS_INIT)
{
printf("Active OPC Service initing, please wait...\r\n");
}
else if(tagValue == STATUS_READY || tagValue == STATUS_RUNNING)
{
printf("Active OPC Service is ready!\r\n");
break;
}
else if(tagValue == STATUS_FAIL)
{
printf("Active OPC Service init fail...exit program!\r\n");
goto exit;
}
else
{
printf("Invalid status...exit program!\r\n");
goto exit;
}
timeOut--;
sleep(1);
}
if(timeOut == 0)
{
printf("Timeout!!! Active OPC Service is not ready...exit program!\r\n");
goto exit;
}
while(1)
{
printf("\r\n");
for(i = 0; i < (sizeof(cmdBuf) / BUFFER_SIZE); i++)
{
printf("%s\r\n", cmdBuf[i]);
}
printf("Enter:");
cmd = getchar();
cmd -= 0x30; //ASCII 0 = 0x30
printf("\r\ncommand = %d\r\n", cmd);
getchar(); //filter Enter
switch(cmd)
{
case 0:
memset(tagBuf, 0, sizeof(tagBuf));
rc = MX_RTU_Tag_Read(SERVICE_AOPC_STATUS_TAG, (void *)tagBuf, sizeof(tagBuf), &readBytes, NULL);
if(rc != TAG_ERR_OK || readBytes != aopcStatusTagInfo.tagSize)
{
printf("tagName = %s, MX_RTU_Tag_Read error = %d, readBytes = %d, tagSize = %d\r\n", SERVICE_AOPC_STATUS_TAG, rc, readBytes, aopcStatusTagInfo.tagSize);
}
else
{
tagValue = 0;
memcpy(&tagValue, tagBuf, aopcStatusTagInfo.tagSize);
printf("tagName = %s, value = %d\r\n", SERVICE_AOPC_STATUS_TAG, tagValue);
}
break;
case 1:
for(profileIndex = 0; profileIndex < AOPC_SERVICE_PROFILES; profileIndex++)
{
memset(tagName, 0, sizeof(tagName));
sprintf(tagName, SERVICE_AOPC_STATUS_PROFILE_TAG, profileIndex);
memset(tagBuf, 0, sizeof(tagBuf));
rc = MX_RTU_Tag_Read(tagName, (void *)tagBuf, sizeof(tagBuf), &readBytes, NULL);
if(rc != TAG_ERR_OK || readBytes != aopcStatusProfileTagInfo[profileIndex].tagSize)
{
printf("tagName = %s, MX_RTU_Tag_Read error = %d, readBytes = %d, tagSize = %d\r\n", tagName, rc, readBytes, aopcStatusProfileTagInfo[profileIndex].tagSize);
continue;
}
else
{
tagValue = 0;
memcpy(&tagValue, tagBuf, aopcStatusProfileTagInfo[profileIndex].tagSize);
printf("tagName = %s, value = %d\r\n", tagName, tagValue);
}
}
break;
case 2:
for(profileIndex = 0; profileIndex < AOPC_SERVICE_PROFILES; profileIndex++)
{
memset(tagName, 0, sizeof(tagName));
sprintf(tagName, SERVICE_AOPC_CONNCONTROL_PROFILE_TAG, profileIndex);
tagValue = 1;
rc = MX_RTU_Tag_Write(tagName, (void *)&tagValue, aopcConnControlProfileTagInfo[profileIndex].tagSize);
printf("tagName = %s, MX_RTU_Tag_Write = %d\r\n", tagName, rc);
}
break;
case 3:
for(profileIndex = 0; profileIndex < AOPC_SERVICE_PROFILES; profileIndex++)
{
memset(tagName, 0, sizeof(tagName));
sprintf(tagName, SERVICE_AOPC_CONNCONTROL_PROFILE_TAG, profileIndex);
tagValue = 0;
rc = MX_RTU_Tag_Write(tagName, (void *)&tagValue, aopcConnControlProfileTagInfo[profileIndex].tagSize);
printf("tagName = %s, MX_RTU_Tag_Write = %d\r\n", tagName, rc);
}
break;
case 4:
if(triggerTagFlag)
{
printf("TriggerTag is raising trigger...\r\n");
tagValue = 1;
rc = MX_RTU_Tag_Write(SERVICE_AOPC_TRIGGER_TAG, (void *)&tagValue, aopcTriggerTagInfo.tagSize);
printf("tagName = %s, tagValue = %d, MX_RTU_Tag_Write = %d\r\n", SERVICE_AOPC_TRIGGER_TAG, tagValue, rc);
tagValue = 0;
rc = MX_RTU_Tag_Write(SERVICE_AOPC_TRIGGER_TAG, (void *)&tagValue, aopcTriggerTagInfo.tagSize);
printf("tagName = %s, tagValue = %d, MX_RTU_Tag_Write = %d\r\n", SERVICE_AOPC_TRIGGER_TAG, tagValue, rc);
}
else
{
printf("Update by TriggerTag is not enabled!\r\n");
}
break;
case 5:
goto exit;
default:
printf("Invalid command...exit program!\r\n");
goto exit;
}
}
exit:
printf("\r\nTag Uninit\r\n");
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit error = %d\r\n", retval);
}
else printf("Tag Uninit ok\r\n");
return 0;
}