| ioPAC RTU Controllers C/C++ Sample Code Programming Guide

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
ioPAC8600_trap_alarm_service.c File Reference

ioPAC8600 Trap Alarm Application TAG Sample More...

`#include "libmoxa_rtu.h"`

## Functions
void | usage (void)
int | main (int argc, char *argv[])

## Detailed Description

ioPAC8600 Trap Alarm Application TAG Sample

Copyright
  Moxa Inc.

Date
  07-29-2015

Author
  TJ Tai

Version
  V1.0
  Trap Alarm Sample

    Introduction:
    Send Trap alarm by the following 2 scenario:
    l) If Detect DI0 on and DI15 off then send trap with 8 tags information
    2) If Detect DI0 and DI15 both ON, then send trap with message
    Example:
    ./ioPAC8600_trap_alarm_service
    Help:

    root@Moxa:/tmp#./ioPAC8600_trap_alarm_service -h
    Trap Alarm program.

    Usage: ./ioPAC8600_trap_alarm_service [OPTIONS]

    Options:
        -i        Slot of DI module [1-9]. Using with -i. Default value: 1
                  Ex. ./ioPAC8600_trap_alarm_service -i 1
        -o        Slot of DO module [1-9]. Using with -o. Default value: 2
                  Ex. ./ioPAC8600_trap_alarm_service -o 2

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

## Function Documentation

void usage | ( | void |  | ) |

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* TAG Service for Trap Alarm Application
*
* Date Author Comment
* 07-29-2015 TJ Tai Created.
******************************************************************************/
#include "libmoxa_rtu.h"
/*******************************************************************************
*
* Sample code for TAG Trap Alarm Service
*
*******************************************************************************/
void usage(void)
{
printf("%s.\n\n", "Trap Alarm Program");
printf("Usage: ./ioPAC8600_trap_alarm_service [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DI module [1-9]. Using with -i. Default value: 1 \n", "-i");
printf("\t Ex. ./ioPAC8600_trap_alarm_service -i 1\n");
printf("\t%-8s Slot of DO module [1-9]. Using with -o. Default value: 2 \n", "-o");
printf("\t Ex. ./ioPAC8600_trap_alarm_service -o 2\n");
printf("\n");
}
int main(int argc, char * argv[])
{
int rc;
TAG_ERR_CODE retval = 0;
TAG_INFO tagInfo;
UINT32 diSlot = 1, doSlot = 2, slotMin = 0, slotMax = 9;
int diChannelAmount = 16;
int doChannelAmount = 16;
char di_tagName[diChannelAmount][TAG_MAX_NAME_SIZE];
char do_tagName[doChannelAmount][TAG_MAX_NAME_SIZE];
int i, j;
UINT32 u32Val = 0;
INT32 bitVal = 0;
INT32 ch00Val = 0;
INT32 ch15Val = 0;
char trapServiceStatus[1][TAG_MAX_NAME_SIZE];
int profileIdx = 2; // two scenario
int keep_running;
char triggerStat[1];
while(-1 != (rc = getopt(argc, argv, "s:v:h")))
{
switch(rc)
{
case 'i':
diSlot = atoi(optarg);
if(diSlot < slotMin || diSlot > slotMax)
{
printf("Error parameter: slot: %d\n", diSlot);
return -1;
}
break;
case 'o':
doSlot = atoi(optarg);
if(doSlot < slotMin || doSlot > slotMax)
{
printf("Error parameter: slot: %d\n", doSlot);
return -1;
}
break;
case 'h':
case '?':
default:
usage();
return 0;
}
}
char trapCtl[profileIdx][TAG_MAX_NAME_SIZE];
char trapSt[profileIdx][TAG_MAX_NAME_SIZE];
sprintf(trapServiceStatus[0], "Service_SNMPAlarm_Status");
for (i = 0; i < profileIdx; i++) {
sprintf(trapCtl[i], "Service_SNMPAlarm_Trigger_%d", i);
sprintf(trapSt[i], "Service_SNMPAlarm_Status_%d", i);
}
printf("%-10s: %d\n", "DI slot", diSlot);
printf("%-10s: %d\n", "DO slot", doSlot);
printf("%-10s: %d\n", "Profile Scenario Count", profileIdx);
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init(), return code = %d.\n", retval);
return 0;
}
while(1)
{
// Check Service Init
retval = MX_RTU_Tag_Read(trapServiceStatus[0], &u32Val, sizeof(u32Val), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", trapServiceStatus[0], retval);
}
if(u32Val == STATUS_RUNNING)
{
printf("Trap Alarm Service Init Success.\n");
break;
}
else if(u32Val == STATUS_FAIL)
{
printf("Trap Alarm Service Init fail.\n");
return 0;
}
else if(u32Val == STATUS_INIT)
{
printf("Trap Alarm Service Initing, please wait ...\r\n");
sleep(5);
}
}
// Config DI TAG
for(i = 0; i < diChannelAmount; i++)
{
sprintf(di_tagName[i], "S%d_DI%d_DIValue", diSlot, i);
}
// Config DO TAG
for(i = 0; i < doChannelAmount; i++)
{
sprintf(do_tagName[i], "S%d_DO%d_DOValue", doSlot, i);
}
while(1)
{
retval = MX_RTU_Tag_Get_Info(di_tagName[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d\n", di_tagName[0], retval);
break;
}
retval = MX_RTU_Tag_Read(di_tagName[0], &ch00Val, sizeof(ch00Val), NULL, NULL);
retval = MX_RTU_Tag_Read(di_tagName[15], &ch15Val, sizeof(ch15Val), NULL, NULL);
if (ch00Val == 1 && ch15Val != 1) {
printf("Scenario 1 proceeding...\n");
keep_running = 1;
profileIdx = 0;
}
else if (ch00Val == 1 && ch15Val == 1) {
printf("Scenario 2 proceeding...\n");
keep_running = 1;
profileIdx = 1;
}
else {
keep_running = 0;
}
// Trigger event handling
if(keep_running == 1)
{
i = profileIdx;
*triggerStat = 1;
// turn on trap trigger
retval = MX_RTU_Tag_Get_Info(trapCtl[i], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d\n", trapCtl[i], retval);
break;
}
retval = MX_RTU_Tag_Write(trapCtl[i], triggerStat, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", trapCtl[i], retval);
return 0;
}
while(1)
{
retval = MX_RTU_Tag_Read(trapSt[i], &bitVal, sizeof(bitVal), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s), return code = %d\r\n", trapSt[i], retval);
return 0;
}
if(bitVal == STATUS_SUCCESS)
{
printf("Service success\n");
break;
}
else if(bitVal == STATUS_FAIL)
{
printf("Service fail\n");
break;
}
else
{
printf("Service running\n");
sleep(3);
}
}
}
sleep(1);
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}