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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
ioPAC8500_email_alarm_service.c File Reference

ioPAC8500 E-mail Alarm Application TAG Sample More...

`#include "libmoxa_rtu.h"`

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

## Detailed Description

ioPAC8500 E-mail Alarm Application TAG Sample

Copyright
  Moxa Inc.

Date
  01-24-2014

Author
  TJ Tai

Version
  V1.0
  Email Alarm Sample

    Introduction:
    Send E-mail alarm by user-specific trigger value and support different alarm (DI/DO) report.
    Example:
    ./ioPAC8500_email_alarm_service -m 1 -v 255
    Help:

    root@Moxa:/tmp#./ioPAC8500_email_alarm_service -h
    E-mail Alarm program.

    Usage: ./ioPAC8500_email_alarm_service [OPTIONS]

    Options:
        -s        Slot of DO module [1-9]. Using with -s. Default value: 2
                  Ex. ./ioPAC8500_email_alarm_service -s 1
        -m        E-mail report mode with DO/DI value. Using with -m.  Default value: 0 [0: DO, 1: DI]
                  Ex. ./ioPAC8500_email_alarm_service -m 1
        -v        Alarm trigger value setting. Using with -v. Default value: 255 [Range: 0 ~ 255]
                  Ex. ./ioPAC8500_email_alarm_service -v 128

Library:
TAG APIs
RTUxpress Project file:
ioPAC8500_email_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 E-mail Alarm Application
*
* Date Author Comment
* 01-24-2014 TJ Tai Created.
******************************************************************************/
#include "libmoxa_rtu.h"
/*******************************************************************************
*
* Sample code for TAG E-mail Alarm Service
*
*******************************************************************************/
void usage(void)
{
printf("%s.\n\n", "E-mail Alarm Program");
printf("Usage: ./ioPAC8500_email_alarm_service [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DO module [1-9]. Using with -s. Default value: 2 \n", "-s");
printf("\t Ex. ./ioPAC8500_email_alarm_service -s 1\n");
printf("\t%-8s E-mail report mode with DO/DI value. Using with -m. Default value: 0 [0: DO, 1: DI]\n", "-m");
printf("\t Ex. ./ioPAC8500_email_alarm_service -m 1\n");
printf("\t%-8s Alarm trigger value setting. Using with -v. Default value: 255 [Range: 0 - 255]\n", "-v");
printf("\t Ex. ./ioPAC8500_email_alarm_service -v 128\n");
printf("\n");
}
int main(int argc, char * argv[])
{
int rc;
TAG_ERR_CODE retval = 0;
TAG_INFO tagInfo;
UINT32 doSlot = 2, slotMin = 0, slotMax = 9;
int doChannelAmount = 16;
char do_tagName[doChannelAmount][TAG_MAX_NAME_SIZE];
int i, j;
int a = 0;
int mode = 0;
int *edge_trigger_init = &a;
UINT32 u32Val = 0;
INT32 bitVal = 0;
int alarm_data;
unsigned int data = 1;
const int tagAmount = 8; // only use DO0 - DO7
int triggerVal = 255; // default trigger value
char emailCtl[3][TAG_MAX_NAME_SIZE] =
{
"Service_EmailAlarm_Status_0",
"Service_EmailAlarm_Trigger_0",
"Service_EmailAlarm_Status"
};
while(-1 != (rc = getopt(argc, argv, "s:m:v:h")))
{
switch(rc)
{
case 's':
doSlot = atoi(optarg);
if(doSlot < slotMin || doSlot > slotMax)
{
printf("Error parameter: slot: %d\n", doSlot);
return -1;
}
break;
case 'm':
mode = atoi(optarg);
sprintf(emailCtl[0], "Service_EmailAlarm_Status_%d", mode);
sprintf(emailCtl[1], "Service_EmailAlarm_Trigger_%d", mode);
break;
case 'v':
triggerVal = atoi(optarg);
break;
case 'h':
case '?':
default:
usage();
return 0;
}
}
printf("%-10s: %d\n", "DO slot", doSlot);
printf("%-10s: %d\n", "Report mode", mode);
printf("%-10s: %d\n", "Trigger val", triggerVal);
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(emailCtl[2], &u32Val, sizeof(u32Val), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", emailCtl[2], retval);
}
if(u32Val == STATUS_READY)
{
printf("Email Alarm Service Init Success.\n");
break;
}
else if(u32Val == STATUS_FAIL)
{
printf("Email Alarm Service Init fail.\n");
return 0;
}
else if(u32Val == STATUS_INIT)
{
printf("Email Alarm Service Initing, please wait ...\r\n");
sleep(5);
}
}
// Config DO TAG
for(i = 0; i < doChannelAmount; i++)
{
sprintf(do_tagName[i], "S%d_DO%d_DOValue", doSlot, i);
}
// Simulate DO Input
char tagBuf[TAG_MAX_NAME_SIZE];
char triggerStat[1];
memset(tagBuf, 0, TAG_MAX_NAME_SIZE);
while(1)
{
retval = MX_RTU_Tag_Get_Info(do_tagName[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d\n", do_tagName[0], retval);
break;
}
alarm_data = 0;
i = 0;
printf("\nDO0-DO7 output value [0-255]:");
scanf("%d", &data);
if(data < 0 || data > 255)
{
printf("Input: %d : not in the range, wrong input.\n", data);
exit(1);
}
alarm_data = data;
memset(tagBuf, 0, TAG_MAX_NAME_SIZE);
while(data)
{
if(data & 1)
tagBuf[i] = 1;
else
tagBuf[i] = 0;
data >>= 1;
i++;
}
for(j = 0; j < tagAmount; j++)
{
retval = MX_RTU_Tag_Write(do_tagName[j], tagBuf + j, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", emailCtl[1], retval);
return 0;
}
}
// Trigger event handling
if(alarm_data == triggerVal && retval != 1)
{
*triggerStat = 1;
retval = MX_RTU_Tag_Write(emailCtl[1], triggerStat, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", emailCtl[1], retval);
return 0;
}
while(1)
{
retval = MX_RTU_Tag_Read(emailCtl[0], &bitVal, sizeof(bitVal), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s), return code = %d\r\n", emailCtl[0], 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(10);
}
}
// Trigger reset to default
retval = MX_RTU_Tag_Write(emailCtl[1], edge_trigger_init, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", emailCtl[1], retval);
return 0;
}
}
sleep(1);
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}