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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
di.c File Reference

DI Sample More...

`#include <libmoxa_rtu.h>`

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

## Detailed Description

DI Sample

Copyright
  Moxa Inc.

Date
  04-16-2013

Author
  Wanhan Hsieh

Version
  V1.0
  DI Sample

    Introduction:
    This is DI sample code. The controller will poll the DI value and DI counter, and then set the DO value.
    Example:
    1. Using default: ./di
    2. Setting DI slot and trigger mode only: ./di -i5 -t1
    Default:
    Slot of DI module = 1
    DI filter = 1000
    Trigger mode of DI counter = 0
    Slot of DO module = 2
    Help:

    root@Moxa:/tmp#./di -h
    DI sample program.

    Usage: ./di [OPTIONS]

    Options:
        -i      Slot of DI module [1-9]. Default slot = 1
        -f      Filter of DI module [1-2147483647]. Default filter = 1000
                (One unit = 100us)
        -t      Trigger mode of DI counter [0,1,2]. Default mode = 0
                (L2H: 0, H2L: 1, BOTH: 2)
        -s      Slot of DO module [1-9]. Default slot = 2

Library:
DI APIs

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* DI Sample Application
*
* Date Author Comment
* 04-16-2013 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char **const argv)
{
int rc, i;
UINT32 diSlot = 1;
UINT32 doSlot = 2, slotMin = 0, slotMax = 0;
int diChannelAmount = 16;
int doChannelAmount = 16;
UINT8 arrMode[diChannelAmount];
UINT8 mode = DI_MODE_COUNTER;
UINT8 arrTrigger[diChannelAmount];
UINT8 trigger = DI_EVENT_TOGGLE_L2H;
UINT32 arrFilter[diChannelAmount];
UINT32 filter = 1000, filterMin = 1, filterMax = 0x7FFFFFFF;
UINT32 arru32Val[diChannelAmount];
UINT32 u32Val = 0;
int cnt = 0, lastCnt = 0, stopCnt = 10;
struct Timestamp diTriggerTime;
MX_RTU_Total_Slots_Get(&slotMax);
while(-1 != (rc = getopt(argc, argv, "hi:f:t:s:")))
{
switch(rc)
{
case 'i':
diSlot = atoi(optarg);
if(diSlot < slotMin || diSlot > slotMax)
{
printf("Error parameter: slot: %d\n", diSlot);
return -1;
}
break;
case 'f':
filter = atoi(optarg);
if(filter < filterMin)
filter = filterMin;
if(filter > filterMax)
filter = filterMin;
break;
case 't':
trigger = atoi(optarg);
if(trigger != DI_EVENT_TOGGLE_L2H && trigger != DI_EVENT_TOGGLE_H2L
&& trigger != DI_EVENT_TOGGLE_BOTH)
trigger = DI_EVENT_TOGGLE_BOTH;
break;
case 's':
doSlot = atoi(optarg);
if(doSlot < slotMin || doSlot > slotMax)
{
printf("Error parameter: slot: %d\n", doSlot);
return -1;
}
break;
case '?':
case 'h':
default:
printf("DI sample program.\n\n");
printf("Usage: ./di [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DI module [%d-%d]. Default slot = %d\n",
"-i", slotMin, slotMax, diSlot);
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, trigger);
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 Slot of DO module [%d-%d]. Default slot = %d\n",
"-s", slotMin, slotMax, doSlot);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "DI slot", diSlot);
printf("%-10s: %d\n", "DI filter", filter);
printf("%-10s: %d\n", "DI trigger", trigger);
printf("%-10s: %d\n", "DO slot", doSlot);
diChannelAmount = doChannelAmount = 16;
for(i = 0; i < diChannelAmount; i++)
{
arrMode[i] = mode;
arrFilter[i] = filter;
arrTrigger[i] = trigger;
arru32Val[i] = 0;
}
// Config DI module
rc = MX_RTU_Module_DI_Mode_Set(diSlot, 0, diChannelAmount, arrMode);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DI_Mode_Set(%d, %d, %d, %d), return code = %d.\n",
diSlot, 0, diChannelAmount, mode, rc);
rc = MX_RTU_Module_DI_Filter_Set(diSlot, 0, diChannelAmount, arrFilter);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DI_Filter_Set(%d, %d, %d, %d), return code = %d.\n",
diSlot, 0, diChannelAmount, filter, rc);
rc = MX_RTU_Module_DI_Counter_Trigger_Set(diSlot, 0, diChannelAmount, arrTrigger);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DI_Counter_Trigger_Set(%d, %d, %d, %d), return code = %d.\n",
diSlot, 0, diChannelAmount, trigger, rc);
rc = MX_RTU_Module_DI_Counter_Value_Set(diSlot, 0, diChannelAmount, arru32Val);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DI_Counter_Value_Set(%d, %d, %d), return code = %d.\n",
diSlot, 0, diChannelAmount, rc);
rc = MX_RTU_Module_DI_Counter_Start_Set(diSlot, 0xFFFF);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DI_Counter_Start_Set(%d, 0x%04X), return code = %d.\n",
diSlot, 0xFFFF, rc);
// Config DO module
for(i = 0; i < doChannelAmount; i++)
arrMode[i] = DO_MODE_DO;
rc = MX_RTU_Module_DO_Mode_Set(doSlot, 0, doChannelAmount, arrMode);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DO_Mode_Set(%d, %d, %d, %d), return code = %d.\n",
doSlot, 0, doChannelAmount, DO_MODE_DO, rc);
// Start to poll DI
while(1)
{
rc = MX_RTU_Module_DI_Value_Get(diSlot, &u32Val, &diTriggerTime);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DI_Value_Get(%d), return code = %d.\n", diSlot, rc);
break;
}
// Turn on DO
rc = MX_RTU_Module_DO_Value_Set(doSlot, u32Val);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Set(%d, 0x%04X), return code = %d.\n",
doSlot, u32Val, rc);
break;
}
// Counting
rc = MX_RTU_Module_DI_Counter_Value_Get(diSlot, 0, diChannelAmount,
arru32Val, &diTriggerTime);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DI_Counter_Value_Get(%d, %d, %d), return code = %d.\n",
diSlot, 0, diChannelAmount, rc);
break;
}
else
{
for(i = 0, cnt = 0; i < diChannelAmount; i++)
cnt += arru32Val[i];
if(cnt > stopCnt)
{
printf("\nCount > %d --> Stop!\n", stopCnt);
break;
}
lastCnt = cnt;
}
printf("\rDI = 0x%04X, Counter = %d", u32Val, cnt);
}
printf("\n");
// Turn on DO
u32Val = 0xFFFF;
rc = MX_RTU_Module_DO_Value_Set(doSlot, u32Val);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Set(%d, 0x%04X), return code = %d.\n",
doSlot, u32Val, rc);
}
return 0;
}