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

AI TAG Sample More...

#include <libmoxa_rtu.h>

Functions

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

Detailed Description

AI TAG Sample

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

Usage: ./ioPAC5542_tag_ai [OPTIONS]

Options:
    -i      Slot of AI module [1-9]. Default slot = 3
    -c      Channel on AI module [0-7]. Default channel = 0
    -s      Slot of DO module [1-9]. Default slot = 2

Library:
TAG APIs

Function Documentation

int main ( int  argc,
char **const  argv 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* AI TAG Sample Application
*
* Date Author Comment
* 02-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 aiSlot = 3;
UINT32 doSlot = 2, slotMin = 0, slotMax = 9;
UINT32 aiChannel = 0;
unsigned short burnStat;
int aiChannelAmount = 8;
int doChannelAmount = 2; // Only use DO0 and DO1
float u32Val = 0;
char ai[8][TAG_MAX_NAME_SIZE];
char do_tagName[doChannelAmount][TAG_MAX_NAME_SIZE];
UINT32 bitVal[2] = { 0, 1};
while(-1 != (rc = getopt(argc, argv, "hi:c:s:")))
{
switch(rc)
{
case 'i':
aiSlot = atoi(optarg);
if(aiSlot < slotMin || aiSlot > slotMax)
{
printf("Error parameter: slot: %d\n", aiSlot);
return -1;
}
break;
case 'c':
aiChannel = atoi(optarg);
if(aiChannel < 0 || aiChannel >= aiChannelAmount)
aiChannel = 0;
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("AI TAG sample program.\n\n");
printf("Usage: ./ioPAC5542_tag_ai [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of AI module [%d-%d]. Default slot = %d\n",
"-i", slotMin, slotMax, aiSlot);
printf("\t%-8s Channel on AI module [%d-%d]. Default channel = %d\n",
"-c", 0, aiChannelAmount - 1, aiChannel);
printf("\t%-8s Slot of DO module [%d-%d]. Default slot = %d\n",
"-s", slotMin, slotMax, doSlot);
printf("\n");
return;
}
}
printf("%-10s: %d\n", "AI slot", aiSlot);
printf("%-10s: %d\n", "AI channel", aiChannel);
printf("%-10s: %d\n", "DO slot", doSlot);
sprintf(ai[0], "S%d_AI%d_AIMaxValue", aiSlot, aiChannel);
sprintf(ai[1], "S%d_AI%d_AIMinValue", aiSlot, aiChannel);
sprintf(ai[2], "S%d_AI%d_BurnOutState", aiSlot, aiChannel);
sprintf(ai[3], "S%d_AI%d_EngineeringValue", aiSlot, aiChannel);
sprintf(ai[4], "S%d_AI%d_RawMaxValue", aiSlot, aiChannel);
sprintf(ai[5], "S%d_AI%d_RawMinValue", aiSlot, aiChannel);
sprintf(ai[6], "S%d_AI%d_RawValue", aiSlot, aiChannel);
sprintf(ai[7], "S%d_AI%d_ResetMaxMin", aiSlot, aiChannel);
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) {
// GetAI
retval = MX_RTU_Tag_Read(ai[3], &u32Val, sizeof(u32Val), NULL, NULL);
if (retval != TAG_ERR_OK ) {
printf("MX_RTU_Tag_Read(%s) = %d\n", ai[3], retval);
break;
}
// Burnout State Checking
retval = MX_RTU_Tag_Read(ai[2], &burnStat, sizeof(burnStat), NULL, NULL);
if (retval != TAG_ERR_OK ) {
printf("MX_RTU_Tag_Read(%s) = %d\n", ai[2], retval);
break;
}
printf("\rAI ENG Value = %10f, AI Status = %d", u32Val, burnStat);
fflush(0);
// SetDO
if (u32Val >= 8.0) {
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+1, tagInfo.tagSize);
if (retval != TAG_ERR_OK) {
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[i], retval);
break;
}
}
}
else if ( u32Val < 8.0 && u32Val >= 4.0) {
for (i = 0; i < doChannelAmount-1; 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+1, tagInfo.tagSize);
if (retval != TAG_ERR_OK) {
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[i], retval);
break;
}
}
}
}
retval = MX_RTU_Tag_Uninit();
if (retval != TAG_ERR_OK) {
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}