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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
ioPAC8500_tag_di.c File Reference

ioPAC8500 DI TAG Sample More...

`#include "libmoxa_rtu.h"`

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

## Detailed Description

ioPAC8500 DI TAG Sample

Copyright
  Moxa Inc.

Date
  02-17-2014

Author
  TJ Tai

Version
  V1.0
  DI TAG Sample

    Introduction:
    This is DI TAG sample code. The controller will poll the DI value and set DI channel 15 in
    DI counter mode, and then set the DO value same as DI value.
    Example:
    1. Using default: ./ioPAC8500_tag_di
    2. Setting DI slot and counter mode channel: ./ioPAC8500_tag_di -i5 -c1
    Default:
    Slot of DI module = 1
    DI filter = 1000   [Setting in RTUxpress only]
    Trigger mode of DI counter = DI_EVENT_TOGGLE_L2H [Setting in RTUxpress only]
    DI Counter mode Channel = 15  [Setting in RTUxpress only]
    Slot of DO module = 2
    Help:

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

    Usage: ./ioPAC8500_tag_di [OPTIONS]

    Options:
        -i      Slot of DI module [1-9]. Default slot = 1
        -c      Channel of DI counter module [0-15]. Default Channel = 15
        -s      Slot of DO module [1-9]. Default slot = 2

Library:
TAG APIs
RTUxpress Project file:
ioPAC8500_tag_di.rtu
(Please right click on the link and ‘Save Target As…’ to save RTUxpress project file and open it with RTUxpress utility)

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* DI TAG Sample code
*
* Date Author Comment
* 02-17-2014 TJ Tai Created.
******************************************************************************/
#include "libmoxa_rtu.h"
int main(int argc, char * argv[])
{
int rc, i;
TAG_ERR_CODE retval = 0;
char szFileName[80] = "/etc/RTU/config.rtu";
TAG_INFO tagInfo;
UINT32 diSlot = 1;
UINT32 doSlot = 2, slotMin = 0, slotMax = 9;
UINT32 u32Val = 0;
int cnt = 0, lastCnt = 0, stopCnt = 10;
int diChannelAmount = 16;
int doChannelAmount = 16;
int diCounterChannel = 15; // default DI counter mode Channel = 15
char di_tagName[diChannelAmount][TAG_MAX_NAME_SIZE];
char do_tagName[doChannelAmount][TAG_MAX_NAME_SIZE];
char di_counter[4][TAG_MAX_NAME_SIZE];
char buffer[TAG_MAX_NAME_SIZE];
int reset[] = {1}; // reset counter value
while(-1 != (rc = getopt(argc, argv, "hi:c:s:")))
{
switch(rc)
{
case 'i':
diSlot = atoi(optarg);
if(diSlot < slotMin || diSlot > slotMax)
{
printf("Error parameter: slot: %d\n", diSlot);
return -1;
}
break;
case 'c':
diCounterChannel = atoi(optarg);
if(diCounterChannel < 0 || diCounterChannel > diChannelAmount - 1)
{
printf("Error parameter: Channel: %d\n", diCounterChannel);
return -1;
}
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 TAG sample program.\n\n");
printf("Usage: ./ioPAC8500_tag_di [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DI module [%d-%d]. Default slot = %d\n",
"-i", slotMin, slotMax, diSlot);
// Config DI Counter Mode TAG
printf("\t%-8s Channel of DI counter module [%d-%d]. Default Channel = %d\n",
"-c", 0, diChannelAmount - 1, 15);
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", "DO slot", doSlot);
sprintf(di_counter[0], "S%d_DI%d_CounterOverflowClear", diSlot, diCounterChannel);
sprintf(di_counter[1], "S%d_DI%d_CounterOverflowStatus", diSlot, diCounterChannel);
sprintf(di_counter[2], "S%d_DI%d_CounterReset", diSlot, diCounterChannel);
sprintf(di_counter[3], "S%d_DI%d_CounterValue", diSlot, diCounterChannel);
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init(), return code = %d.\n", retval);
return 0;
}
// Config DI TAG
for(i = 0; i < diChannelAmount; i++)
{
sprintf(buffer, "S%d_DI%d_DIValue", diSlot, i);
strncpy(di_tagName[i], buffer, sizeof(buffer));
}
retval = MX_RTU_Tag_Get_Info(di_counter[2], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", di_counter[2], retval);
return 0;
}
retval = MX_RTU_Tag_Write(di_counter[2], reset, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", di_counter[2], retval);
return 0;
}
// Config DO TAG
for(i = 0; i < doChannelAmount; i++)
{
sprintf(buffer, "S%d_DO%d_DOValue", doSlot, i);
strncpy(do_tagName[i], buffer, sizeof(buffer));
}
UINT32 bitVal[diChannelAmount];
for(i = 0; i < diChannelAmount; i++)
{
bitVal[i] = 0;
}
while(1)
{
// GetDI
for(i = 0; i < diChannelAmount ; i++)
{
retval = MX_RTU_Tag_Read(di_tagName[i], &bitVal[i], sizeof(bitVal), NULL, NULL);
if(retval != TAG_ERR_OK && i != diCounterChannel)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", di_tagName[i], retval);
break;
}
}
// SetDO
for(i = 0; i < doChannelAmount; i++)
{
retval = MX_RTU_Tag_Get_Info(do_tagName[i], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_tagName[i], retval);
break;
}
retval = MX_RTU_Tag_Write(do_tagName[i], bitVal + i, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[i], retval);
break;
}
}
// DI Counting in channel
retval = MX_RTU_Tag_Read(di_counter[3], &u32Val, sizeof(u32Val), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", di_counter[3], retval);
break;
}
else
{
cnt = u32Val;
if(cnt > stopCnt)
{
printf("\nCount > %d ---> Stop!\n", stopCnt);
break;
}
lastCnt = cnt;
}
printf("\rDI = 0x%04X, Counter = %d", u32Val, cnt);
fflush(0);
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}