ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions
ai_event.c File Reference

AI Event More...

#include <libmoxa_rtu.h>

Macros

#define DEFAULT_OUT_NAME   "ai_event.log"
 

Functions

int main (int argc, char **const argv)
 

Detailed Description

AI Event

Date
03-26-2013
Author
Wanhan Hsieh
Version
V1.0
Introduction:
To register an AI Event for a specific slot and channel and log Event Data into a file if the condition and level are matched.
Example:
1. Using default: ./ai_event
2. Setting AI condition and level: ./ai_event -m1 -l8
Default:
AI Slot = 1
AI Channel = 0
AI Condition = 0 (0: '>', 1: '<', 2: '=')
AI Level = 4.000
Logging file name = ai_event.log
Help:
root@Moxa:/tmp#./ai_event -h
AI event sample program.

Usage: ./ai_event [OPTIONS]

Options:
        -s       Slot of AI module [1-9]. Default slot = 1
        -c       Channel of AI module [0-7]. Default channel = 0
        -m       Conditionl of AI event[0,1,2]. Default condition = 0
                   (0: '>', 1: '<', 2: '=')
        -l       Level of AI event. Default level = 4.000
        -o       Output file name. Default name = ai_event.log

Library:
AIEvent APIs

Macro Definition Documentation

#define DEFAULT_OUT_NAME   "ai_event.log"

Function Documentation

int main ( int  argc,
char **const  argv 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* AI Event Application
*
* Date Author Comment
* 03-26-2013 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
#define DEFAULT_OUT_NAME "ai_event.log"
/*******************************************************************************
*
* Sample code for AI event
*
******************************************************************************/
int main(int argc, char **const argv)
{
int rc, i;
UINT32 aiSlot = 1, slotMin = 0, slotMax = 0;
int aiChannelAmount = 8;
UINT8 aiChannel = 0;
char szOutFlieName[64];
FILE *pOutFile;
int handle = -1;
UINT32 aiCondition = AI_TC_RTD_EVENT_GREATER;
float aiLevel = 4.0;
UINT8 range = AI_RANGE_4_20mA;
float burnout = 0.0;
memset(szOutFlieName, 0, sizeof(szOutFlieName));
strncpy(szOutFlieName, DEFAULT_OUT_NAME, strlen(DEFAULT_OUT_NAME));
while(-1 != (rc = getopt(argc, argv, "hs:c:t:m:l:o:")))
{
switch(rc)
{
case 's':
aiSlot = atoi(optarg);
if(aiSlot < slotMin || aiSlot > slotMax)
{
printf("Error parameter: slot: %d\n", aiSlot);
return -1;
}
break;
case 'c':
aiChannel = atoi(optarg);
if(aiChannel < 0 || aiChannel >= aiChannelAmount)
{
printf("Error parameter: channel: %d\n", aiChannel);
return -1;
}
break;
case 'm':
aiCondition = atoi(optarg);
if(aiCondition != AI_TC_RTD_EVENT_GREATER
&& aiCondition != AI_TC_RTD_EVENT_SMALLER
&& aiCondition != AI_TC_RTD_EVENT_EQUAL)
{
printf("Error parameter: condition: %d\n", aiCondition);
return -1;
}
break;
case 'l':
aiLevel = atoi(optarg);
break;
case 'o':
memset(szOutFlieName, 0, sizeof(szOutFlieName));
strncpy(szOutFlieName, optarg, strlen(optarg));
break;
case '?':
case 'h':
default:
printf("AI event sample program.\n\n");
printf("Usage: ./ai_event [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of AI module [%d-%d]. Default slot = %d\n",
"-s", slotMin, slotMax, aiSlot);
printf("\t%-8s Channel of AI module [%d-%d]. Default channel = %d\n",
"-c", 0, aiChannelAmount - 1, aiChannel);
printf("\t%-8s Conditionl of AI event[%d,%d,%d]. Default condition = %d\n",
AI_TC_RTD_EVENT_EQUAL, aiCondition);
printf("\t%-8s (%d: '%c', %d: '%c', %d: '%c')\n",
printf("\t%-8s Level of AI event. Default level = %.3f\n",
"-l", aiLevel);
printf("\t%-8s Output file name. Default name = %s\n",
"-o", szOutFlieName);
printf("\n");
return;
}
}
printf("%-10s: %d\n", "AI slot", aiSlot);
printf("%-10s: %d\n", "AI channel", aiChannel);
printf("%-10s: %d\n", "AI event condition", aiCondition);
printf("%-10s: %.3f\n", "AI event level", aiLevel);
printf("%-10s: %s\n", "output", szOutFlieName);
pOutFile = fopen(szOutFlieName, "w");
if(pOutFile == NULL)
{
fprintf(stderr, "Error open file: %s\n", szOutFlieName);
return -1;
}
// Config AI module
rc = MX_RTU_Module_AI_Range_Get(aiSlot, aiChannel, 1, &range);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_AI_Range_Get(%d, %d, %d), return code = %d.\n",
aiSlot, aiChannel, 1, rc);
rc = MX_RTU_Module_AI_Burnout_Value_Set(aiSlot, aiChannel, 1, &burnout);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_AI_Burnout_Value_Set(%d, %d, %d), return code = %d.\n",
aiSlot, aiChannel, 1, rc);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_AI_Event_Reset(), return code = %d.\n", rc);
rc = MX_RTU_AI_Event_Register(aiSlot, aiChannel, aiLevel, aiCondition, &handle);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_AI_Event_Register(%d, %d, %f, %d), return code = %d.\n",
aiSlot, aiChannel, aiLevel, aiCondition, rc);
// Start to polling AI event
while(1)
{
UINT32 count = 0;
rc = MX_RTU_AI_Event_Count(handle, &count);
if(rc != IO_ERR_OK)
{
printf("MX_RTU_AI_Event_Count(%d), return code = %d.\n", handle, rc);
break;
}
while(count--)
{
float fVal = 0;
struct Timestamp time;
char szLine[64];
rc = MX_RTU_AI_Event_Get(handle, &fVal, &time);
if(rc != IO_ERR_OK)
{
printf("MX_RTU_AI_Event_Get(%d), return code = %d.\n", handle, rc);
break;
}
sprintf(szLine, "%d/%d/%d %02d:%02d:%02d.%d\t%.3f\n",
time.year, time.mon, time.day, time.hour, time.min, time.sec, time.msec, fVal);
printf(szLine);
fprintf(pOutFile, szLine);
fflush(pOutFile);
}
}
fclose(pOutFile);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_AI_Event_Unregister(%d), return code = %d.\n", handle, rc);
return 0;
}