#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;
int tagValue = 0;
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", "ioPAC5542 Active OPC program");
printf("Usage: ./ioPAC5542_aopc [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Enable updating by TriggerTag according to user demand.\n", "-t");
printf("\n");
return;
}
}
printf("\r\nTag Init\r\n");
{
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));
{
goto exit;
}
if(triggerTagFlag)
{
memset(&aopcTriggerTagInfo, 0, sizeof(aopcTriggerTagInfo));
{
goto exit;
}
}
{
memset(tagName, 0, sizeof(tagName));
memset(&aopcConnControlProfileTagInfo[profileIndex], 0,
sizeof(
TAG_INFO));
{
printf("tagName = %s, MX_RTU_Tag_Get_Info error = %d\r\n", tagName, rc);
goto exit;
}
memset(tagName, 0, sizeof(tagName));
memset(&aopcStatusProfileTagInfo[profileIndex], 0,
sizeof(
TAG_INFO));
{
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));
{
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("Active OPC Service initing, please wait...\r\n");
}
{
printf("Active OPC Service is ready!\r\n");
break;
}
{
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");
{
printf("%s\r\n", cmdBuf[i]);
}
printf("Enter:");
cmd = getchar();
cmd -= 0x30;
printf("\r\ncommand = %d\r\n", cmd);
getchar();
switch(cmd)
{
case 0:
memset(tagBuf, 0, sizeof(tagBuf));
{
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);
}
break;
case 1:
{
memset(tagName, 0, sizeof(tagName));
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:
{
memset(tagName, 0, sizeof(tagName));
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:
{
memset(tagName, 0, sizeof(tagName));
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;
tagValue = 0;
}
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");
{
printf("MX_RTU_Tag_Uninit error = %d\r\n", retval);
}
else printf("Tag Uninit ok\r\n");
return 0;
}