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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Macros | Functions
ioPAC8500_di_tag_event.c File Reference

ioPAC8500 DI Tag Event More...

`#include "libmoxa_rtu.h"`

## Macros
#define | DI_CHANNELS 16
#define | DI_TAG "S1_DI%d_DIValue"

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

## Detailed Description

ioPAC8500 DI Tag Event

Copyright
  Moxa Inc.

Date
  03-04-2014

Author
  Eddy Kao

Version
  V1.0
  ioPAC8500 DI Tag Event

    Introduction:
    Monitor DIs by different event conditions.
    Example:
     1. Using default: ./ioPAC8500_di_tag_event
     2. Setting the event confition: ./ioPAC8500_di_tag_event -e1
    Help:

    root@Moxa:/tmp#./ioPAC8500_di_tag_event -h
    ioPAC8500 DI Tag Event program.

    Usage: ./ioPAC8500_di_tag_event [OPTIONS]

    Options:
        -e      Event Condition [0-2]. Default event condition = 0
                (0: EDGE, 1: RAISING EDGE, 2: FALLING EDGE)

Library:
TAG APIs
RTUxpress Project file:
ioPAC8500_di_tag_event.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 DI_CHANNELS 16

#define DI_TAG "S1_DI%d_DIValue"

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* DI Tag Event Sample
*
* Date Author Comment
* 03-04-2014 Eddy Kao Created.
******************************************************************************/
/*******************************************************************************
*
* Sample code for DI Tag Event
*
*******************************************************************************/
#include "libmoxa_rtu.h"
#define DI_CHANNELS 16
#define DI_TAG "S1_DI%d_DIValue"
int main(int argc, char * argv[])
{
int retval = 0;
unsigned char channel = 0;
unsigned char parameter = 0;
unsigned char eventCondition = TAG_EVENT_CONDITION_EDGE;
TAG_EVENT_CONDITION eventConditionStruct;
TAG_ERR_CODE rc = TAG_ERR_OK;
UINT8 tagName[TAG_MAX_NAME_SIZE];
TAG_INFO diTagInfo[DI_CHANNELS];
unsigned char tagValue = 0;
int handle[DI_CHANNELS];
struct Timestamp timestamp;
while((retval = getopt(argc, argv, "he:")) != -1)
{
switch(retval)
{
case 'e':
parameter = atoi(optarg);
if(parameter > 2)
{
printf("Error event condition = %d\r\n", parameter);
return -1;
}
if(parameter == 0) eventCondition = TAG_EVENT_CONDITION_EDGE;
else if(parameter == 1) eventCondition = TAG_EVENT_CONDITION_RAISING_EDGE;
else if(parameter == 2) eventCondition = TAG_EVENT_CONDITION_FALLING_EDGE;
break;
case '?':
case 'h':
default:
printf("%s.\n\n", "ioPAC8500 DI Tag Event program");
printf("Usage: ./ioPAC8500_di_tag_event [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Event Condition [%d-%d]. Default event condition = %d\n", "-e", 0, 2, parameter);
printf("\t%-8s (0: EDGE, 1: RAISING EDGE, 2: FALLING EDGE)\n", "");
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(diTagInfo, 0, sizeof(diTagInfo));
for(channel = 0; channel < DI_CHANNELS; channel++)
{
memset(tagName, 0, sizeof(tagName));
sprintf(tagName, DI_TAG, channel);
rc = MX_RTU_Tag_Get_Info(tagName, &diTagInfo[channel]);
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("DIs Tag Event register\r\n");
memset(handle, 0, sizeof(handle));
memset(&eventConditionStruct, 0, sizeof(eventConditionStruct));
eventConditionStruct.condition = eventCondition;
for(channel = 0; channel < DI_CHANNELS; channel++)
{
memset(tagName, 0, sizeof(tagName));
sprintf(tagName, DI_TAG, channel);
if(diTagInfo[channel].dataType == TAG_DATA_TYPE_COIL)
{
rc = MX_RTU_Tag_Event_Register(tagName, &eventConditionStruct, &handle[channel]);
if(rc != TAG_ERR_OK)
{
printf("tagName = %s, MX_RTU_Tag_Event_Register error = %d\r\n", tagName, rc);
goto unregister;
}
}
else goto unregister;
}
printf("DIs Tag Event register ok\r\n\r\n");
printf("Wait for DI Events...\r\n\r\n");
while(1)
{
for(channel = 0; channel < DI_CHANNELS; channel++)
{
memset(tagName, 0, sizeof(tagName));
sprintf(tagName, DI_TAG, channel);
tagValue = 0;
memset(&timestamp, 0, sizeof(timestamp));
rc = MX_RTU_Tag_Event_Get(handle[channel], (void *)&tagValue, &timestamp);
if(rc == TAG_ERR_EVENT_EMPTY)
{
continue;
}
else if(rc != TAG_ERR_OK)
{
printf("tagName = %s, handle = %d, MX_RTU_Tag_Event_Get error = %d\r\n", tagName, handle[channel], rc);
break;
}
else
{
printf("tagName = %s, value = %d, timestamp = %d:%d:%d.%d(hour:min:sec.msec)\r\n", tagName, tagValue, timestamp.hour, timestamp.min, timestamp.sec, timestamp.msec);
}
}
}
unregister:
printf("\r\nDIs Tag Event unregister\r\n");
for(channel = 0; channel < DI_CHANNELS; channel++)
{
MX_RTU_Tag_Event_Clear(handle[channel]);
MX_RTU_Tag_Event_Unregister(handle[channel]);
}
printf("DIs Tag Event unregister finish\r\n");
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;
}