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

ioPAC5542-HSPA DI TAG Sample More...

#include "libmoxa_rtu.h"

Functions

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

Detailed Description

ioPAC5542-HSPA DI TAG Sample

Date
02-17-2014
Author
TJ Tai
Version
V1.0
5542-HSPA_tag_di.jpg
DI TAG Sample
Introduction:
This is DI TAG sample code. The controller will poll the DI value and set DI channel 7 in
DI counter mode, and set the DIO channels to DO mode, then set the DO value same as DI value.
Example:
1. Using default: ./ioPAC5542-HSPA_tag_di
2. Setting counter mode channel: ./ioPAC5542-HSPA_tag_di -c1
Default:
DI filter = 1   [Setting in RTUxpress only]
Trigger mode of DI counter = Rising Edge [Setting in RTUxpress only]
DI Counter mode Channel = 7  [Setting in RTUxpress only]
Help:
root@Moxa:/tmp#./ioPAC5542-HSPA_tag_di -h
DI TAG sample program.

Usage: ./ioPAC5542-HSPA_tag_di [OPTIONS]

Options:
    -c      Channel of DI counter module [0-7]. Default Channel = 7

Library:
TAG APIs

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 u32Val = 0;
int cnt = 0, lastCnt = 0, stopCnt = 10;
int diChannelAmount = 8;
int doChannelAmount = 8;
int diCounterChannel = 7;
char di_tagName[diChannelAmount][TAG_MAX_NAME_SIZE];
char do_tagName[doChannelAmount][TAG_MAX_NAME_SIZE];
char di_counter[4][TAG_MAX_NAME_SIZE];
int reset[] = {1}; // reset counter value
while(-1 != (rc = getopt(argc, argv, "hc:")))
{
switch(rc)
{
case 'c':
diCounterChannel = atoi(optarg);
if(diCounterChannel < 0 || diCounterChannel > diChannelAmount - 1)
{
printf("Error parameter: Channel: %d\n", diCounterChannel);
return -1;
}
break;
case '?':
case 'h':
default:
printf("DI TAG sample program.\n\n");
printf("Usage: ./ioPAC5542-HSPA_tag_di [OPTIONS]\n\n");
printf("Options:\n");
// Config DI Counter Mode TAG
printf("\t%-8s Channel of DI counter module [%d-%d]. Default Channel = %d\n",
"-c", 0, diChannelAmount - 1, 7);
printf("\n");
return;
}
}
printf("%-10s: %d\n", "DI counter channel", diCounterChannel);
sprintf(di_counter[0], "S0_DI%d_CounterOverflowClear", diCounterChannel);
sprintf(di_counter[1], "S0_DI%d_CounterOverflowStatus", diCounterChannel);
sprintf(di_counter[2], "S0_DI%d_CounterReset", diCounterChannel);
sprintf(di_counter[3], "S0_DI%d_CounterValue", 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(di_tagName[i], "S0_DI%d_DIValue", i);
}
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(do_tagName[i], "S0_DIO%d_DOValue", i);
}
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;
}