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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Macros | Functions
dio_event.c File Reference

DIO Event More...

`#include <libmoxa_rtu.h>`

## Macros
#define | DEFAULT_OUT_NAME "dio_event.log"

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

## Detailed Description

DIO Event

Copyright
  Moxa Inc.

Date
  06-07-2013

Author
  Changfu Hsieh

Version
  V1.0

    Introduction:
    This sample shows how to register a DIO event for a user defined slot and channel, once the DI event occured, the event will be write into a file.
    Example:
    1. Using default: ./dio_event
    2. Setting DI filter and trigger: ./dio_event -f500 -t1
    Default:
    DIO Slot = 1
    DI Channel = 0
    DI Filter = 1000 (One unit = 100us)
    DI Trigger = 2 (L2H: 0, H2L: 1, BOTH: 2)
    Logging file name = dio_event.log
    Help:

    root@Moxa:/tmp#./dio_event -h
    DIO DI event sample program.

    Usage: ./dio_event [OPTIONS]

    Options:
      -s   Slot of DIO module [0-9]. Default slot = 0
      -c   DIO channel[0-8]. Default channel = 0
      -f   Filter of DI module [1-2147483647]. Default filter = 1000
           (One unit = 100us)
      -t   Trigger mode of DI counter [0,1,2]. Default mode = 2
           (L2H: 0, H2L: 1, BOTH: 2)
      -o   Output file name. Default name = dio_event.log

Library:
DIOEvent APIs

## Macro Definition Documentation

#define DEFAULT_OUT_NAME "dio_event.log"

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* DIO Event Application
*
* Date Author Comment
* 03-26-2013 Wanhan Hsieh Created.
* 06-07-2013 Changfu Hsieh Modified for DIO.
******************************************************************************/
#include <libmoxa_rtu.h>
#define DEFAULT_OUT_NAME "dio_event.log"
/*******************************************************************************
*
* Sample code for DIO event
*
******************************************************************************/
int main(int argc, char **const argv)
{
int rc, i;
UINT8 diSlot = 0, slotMin = 0;
UINT32 slotMax = 0;
int diChannelAmount = 16;
UINT32 filter = 1000, filterMin = 1, filterMax = 0x7FFFFFFF;
UINT8 eventTrigger = DI_EVENT_TOGGLE_BOTH;
UINT8 diChannel = 0;
char szOutFlieName[64];
FILE *pOutFile;
int handle = -1;
UINT8 chMode[16];
UINT32 dioMap;
MX_RTU_Total_Slots_Get(&slotMax);
memset(szOutFlieName, 0, sizeof(szOutFlieName));
strncpy(szOutFlieName, DEFAULT_OUT_NAME, strlen(DEFAULT_OUT_NAME));
while(-1 != (rc = getopt(argc, argv, "hs:c:f:t:o:")))
{
switch(rc)
{
case 's':
diSlot = atoi(optarg);
if(diSlot < slotMin || diSlot > slotMax)
{
printf("Error parameter: slot: %d\n", diSlot);
return -1;
}
break;
case 'c':
diChannel = atoi(optarg);
if(diChannel < 0 || diChannel >= diChannelAmount)
{
printf("Error parameter: channel: %d\n", diChannel);
return -1;
}
break;
case 'f':
filter = atoi(optarg);
if(filter < filterMin)
filter = filterMin;
if(filter > filterMax)
filter = filterMin;
break;
case 't':
eventTrigger = atoi(optarg);
if(eventTrigger != DI_EVENT_TOGGLE_L2H && eventTrigger != DI_EVENT_TOGGLE_H2L
&& eventTrigger != DI_EVENT_TOGGLE_BOTH)
eventTrigger = DI_EVENT_TOGGLE_BOTH;
break;
case 'o':
memset(szOutFlieName, 0, sizeof(szOutFlieName));
strncpy(szOutFlieName, optarg, strlen(optarg));
break;
case '?':
case 'h':
default:
printf("DIO DI event sample program.\n\n");
printf("Usage: ./dio_event [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DIO module [%d-%d]. Default slot = %d\n",
"-s", slotMin, slotMax, diSlot);
printf("\t%-8s Channel of DI module [%d-%d]. Default channel = %d\n",
"-c", 0, diChannelAmount - 1, diChannel);
printf("\t%-8s Filter of DI module [%d-%d]. Default filter = %d\n",
"-f", filterMin, filterMax, filter);
printf("\t%-8s (One unit = 100us)\n", "");
printf("\t%-8s Trigger mode of DI counter [%d,%d,%d]. Default mode = %d\n",
"-t", DI_EVENT_TOGGLE_L2H, DI_EVENT_TOGGLE_H2L,
DI_EVENT_TOGGLE_BOTH, eventTrigger);
printf("\t%-8s (L2H: %d, H2L: %d, BOTH: %d)\n",
"", DI_EVENT_TOGGLE_L2H, DI_EVENT_TOGGLE_H2L, DI_EVENT_TOGGLE_BOTH);
printf("\t%-8s Output file name. Default name = %s\n",
"-o", szOutFlieName);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "DIO slot", diSlot);
printf("%-10s: %d\n", "DI channel", diChannel);
printf("%-10s: %d\n", "DI filter", filter);
printf("%-10s: %d\n", "DI trigger", eventTrigger);
printf("%-10s: %s\n", "output", szOutFlieName);
pOutFile = fopen(szOutFlieName, "w");
if(pOutFile == NULL)
{
fprintf(stderr, "Error open file: %s\n", szOutFlieName);
return -1;
}
//Set DIO direction
dioMap = 0x00;
rc = MX_RTU_Module_DIO_Map_Set(diSlot, dioMap);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DIO_Map_Set err:%d\n", rc);
return -1;
}
//Set all DI channel mode to DI
for(i = 0; i < 8; i++)
{
chMode[i] = DI_MODE_DI;
}
rc = MX_RTU_Module_DIO_DI_Mode_Set(diSlot, 0, 8, chMode);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DIO_DI_Mode_Set err:%d\n", rc);
return -1;
}
//Set DI filter
rc = MX_RTU_Module_DIO_DI_Filter_Set(diSlot, diChannel, 1, &filter);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DIO_DI_Filter_Set(%d, %d, %d), return code = %d.\n",
diSlot, diChannel, 1, rc);
rc = MX_RTU_DIO_DI_Event_Reset();
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_DIO_DI_Event_Reset(), return code = %d.\n", rc);
rc = MX_RTU_DIO_DI_Event_Register(diSlot, diChannel, eventTrigger, &handle);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_DIO_DI_Event_Register(%d, %d, %d), return code = %d.\n",
diSlot, diChannel, eventTrigger, rc);
// Start to polling DI event
while(1)
{
UINT32 count = 0;
rc = MX_RTU_DIO_DI_Event_Count(handle, &count);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_DIO_DI_Event_Count(%d), return code = %d.\n", handle, rc);
break;
}
while(count--)
{
UINT32 status = 0;
struct Timestamp time;
char szLine[64];
rc = MX_RTU_DIO_DI_Event_Get(handle, &status, &time);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_DIO_DI_Event_Get(%d), return code = %d.\n", handle, rc);
MX_RTU_DIO_DI_Event_Clear(handle);
break;
}
sprintf(szLine, "%d/%d/%d %02d:%02d:%02d.%d\t%d\n",
time.year, time.mon, time.day, time.hour, time.min, time.sec, time.msec, status);
printf(szLine);
fprintf(pOutFile, szLine);
fflush(pOutFile);
}
}
fclose(pOutFile);
rc = MX_RTU_DIO_DI_Event_Unregister(handle);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_DIO_DI_Event_Unregister(%d), return code = %d.\n", handle, rc);
return 0;
}