ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
Functions
ioPAC8500_tag_tc.c File Reference

ioPAC8500 TC TAG Sample More...

#include <libmoxa_rtu.h>

Functions

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

Detailed Description

ioPAC8500 TC TAG Sample

Date
06-18-2014
Author
TJ Tai
Version
V1.0
8500_tag_tc.jpg
TC TAG Sample
Introduction:
This is TC TAG sample code. The controller will poll for TC values, and set DO values according to the TC level.
Example:
1. Using default: ./ioPAC8500_tag_tc
2. Setting TC slot and channel only: ./ioPAC8500_tag_tc -i5 -c3
Default:
Slot of TC module = 1
Channel on TC module = 0
Slot of DO module = 2
Help:
root@Moxa:/tmp#./ioPAC8500_tag_tc -h
TC TAG sample program.

Usage: ./ioPAC8500_tag_tc [OPTIONS]

Options:
    -i       Slot of TC module [0-9]. Default slot = 1
    -c       Channel on TC module [0-7]. Default channel = 0
    -r       Test threshold. Default threshold = 100.000
    -s       Slot of DO module [0-9]. Default slot = 2

Library:
TC APIs
RTUxpress Project file:
ioPAC8500_tag_tc.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 **const  argv 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* TC TAG Sample Application
*
* Date Author Comment
* 06-18-2014 TJ Tai Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char **const argv)
{
int rc, i;
TAG_ERR_CODE retval = 0;
TAG_INFO tagInfo;
UINT32 tcSlot = 1;
UINT32 doSlot = 2, slotMin = 0, slotMax = 9;
UINT32 tcChannel = 0;
unsigned short burnStat;
int tcChannelAmount = 8;
int doChannelAmount = 2; // Only use DO0 and DO1
float u32Val = 0;
char tc[tcChannelAmount][TAG_MAX_NAME_SIZE];
char do_tagName[doChannelAmount][TAG_MAX_NAME_SIZE];
UINT32 bitVal[2] = { 0, 1};
float fThreshold = 100.0f;
float fVal = 0;
while(-1 != (rc = getopt(argc, argv, "hi:c:r:s:")))
{
switch(rc)
{
case 'i':
tcSlot = atoi(optarg);
if(tcSlot < slotMin || tcSlot > slotMax)
{
printf("Error parameter: slot: %d\n", tcSlot);
return -1;
}
break;
case 'c':
tcChannel = atoi(optarg);
if(tcChannel < 0 || tcChannel >= tcChannelAmount)
{
printf("Error parameter: channel: %d\n", tcChannel);
return -1;
}
break;
case 'r':
fThreshold = atof(optarg);
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("TC TAG sample program.\n\n");
printf("Usage: ./ioPAC8500_tag_tc [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of TC module [%d-%d]. Default slot = %d\n",
"-i", slotMin, slotMax, tcSlot);
printf("\t%-8s Channel on TC module [%d-%d]. Default channel = %d\n",
"-c", 0, tcChannelAmount - 1, tcChannel);
printf("\t%-8s Test threshold. Default threshold = %.3f\n",
"-r", fThreshold);
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", "TC slot", tcSlot);
printf("%-10s: %d\n", "TC channel", tcChannel);
printf("%-10s: %.3f\n", "Threshold", fThreshold);
printf("%-10s: %d\n", "DO slot", doSlot);
sprintf(tc[0], "S%d_TC%d_TCValue", tcSlot, tcChannel);
sprintf(tc[1], "S%d_TC%d_TCMaxValue", tcSlot, tcChannel);
sprintf(tc[2], "S%d_TC%d_TCMinValue", tcSlot, tcChannel);
sprintf(tc[3], "S%d_TC%d_ResetMaxMin", tcSlot, tcChannel);
sprintf(tc[4], "S%d_TC%d_BurnOutState",tcSlot, tcChannel);
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init(), return code = %d.\n", retval);
return 0;
}
// Config DO TAG
for(i = 0; i < doChannelAmount; i++)
{
sprintf(do_tagName[i], "S%d_DO%d_DOValue", doSlot, i);
}
// Reset DO to default
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 + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[i], retval);
break;
}
}
while(1)
{
// GetTC
retval = MX_RTU_Tag_Read(tc[0], &fVal, sizeof(fVal), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", tc[0], retval);
break;
}
// Burnout State Checking
retval = MX_RTU_Tag_Read(tc[4], &burnStat, sizeof(burnStat), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", tc[4], retval);
break;
}
printf("\rTC Value = %20f, TC Burnout Status = %d", fVal, burnStat);
fflush(0);
// SetDO
// if burnout --> turn off DO0, turn on DO1
if(burnStat == 1)
{
retval = MX_RTU_Tag_Get_Info(do_tagName[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_tagName[0], retval);
break;
}
retval = MX_RTU_Tag_Write(do_tagName[0], bitVal + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[0], retval);
break;
}
retval = MX_RTU_Tag_Get_Info(do_tagName[1], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_tagName[1], retval);
break;
}
retval = MX_RTU_Tag_Write(do_tagName[1], bitVal + 1, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[1], retval);
break;
}
}
else {
// turn off DO1
retval = MX_RTU_Tag_Get_Info(do_tagName[1], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_tagName[1], retval);
break;
}
retval = MX_RTU_Tag_Write(do_tagName[1], bitVal + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[1], retval);
break;
}
// compare TC value with threshold then set DO0 on
if(fVal > fThreshold)
{
retval = MX_RTU_Tag_Get_Info(do_tagName[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_tagName[0], retval);
break;
}
retval = MX_RTU_Tag_Write(do_tagName[0], bitVal + 1, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[0], retval);
break;
}
}
else {
retval = MX_RTU_Tag_Get_Info(do_tagName[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_tagName[0], retval);
break;
}
retval = MX_RTU_Tag_Write(do_tagName[0], bitVal + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[0], retval);
break;
}
}
}
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}