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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
tc.c File Reference

TC Sample More...

`#include <libmoxa_rtu.h>`

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

## Detailed Description

TC Sample

Copyright
  Moxa Inc.

Date
  06-11-2014

Author
  Wanhan Hsieh

Version
  V1.0
  TC Sample

    Introduction:
    This is an TC sample code. The controller will poll for TC values, and set DO values according to the TC level.
    Example:
    1. Using default: ./tc
    2. Setting TC slot and channel only: ./tc -i5 -c3
    Default:
    Slot of TC module = 1
    Channel on TC module = 0
    Slot of DO module = 2
    Help:

    root@Moxa:/tmp#./tc -h
    TC sample program.

    Usage: ./tc [OPTIONS]

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

Library:
TC APIs

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* TC Sample Application
*
* Date Author Comment
* 06-11-2014 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char **const argv)
{
int rc, i;
UINT32 tcSlot = 1;
UINT32 doSlot = 2, slotMin = 0, slotMax = 0;
UINT32 tcChannel = 0;
int tcChannelAmount = 8;
int doChannelAmount = 16;
UINT8 tcType = TC_TYPE_J;
int tcTypeAmount = 11;
UINT8 arrMode[doChannelAmount];
UINT32 u32DOVal = 0;
float fThreshold = 100.0f;
MX_RTU_Total_Slots_Get(&slotMax);
while(-1 != (rc = getopt(argc, argv, "c:hi:r:s:t:")))
{
switch(rc)
{
case 'c':
tcChannel = atoi(optarg);
if(tcChannel < 0 || tcChannel >= tcChannelAmount)
{
printf("Error parameter: channel: %d\n", tcChannel);
return -1;
}
break;
case 'i':
tcSlot = atoi(optarg);
if(tcSlot < slotMin || tcSlot > slotMax)
{
printf("Error parameter: slot: %d\n", tcSlot);
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 't':
tcType = atoi(optarg);
if(tcType < 0 || tcType >= tcTypeAmount)
{
printf("Error parameter: type: %d\n", tcType);
return -1;
}
break;
case '?':
case 'h':
default:
printf("TC sample program.\n\n");
printf("Usage: ./tc [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Channel on TC module [%d-%d]. Default channel = %d\n",
"-c", 0, tcChannelAmount - 1, tcChannel);
printf("\t%-8s Slot of TC module [%d-%d]. Default slot = %d\n",
"-i", slotMin, slotMax, tcSlot);
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("\t%-8s TC type [%d-%d]. Default type = %d\n",
"-t", 0, tcTypeAmount - 1, tcType);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "TC slot", tcSlot);
printf("%-10s: %d\n", "TC channel", tcChannel);
printf("%-10s: %d\n", "TC type", tcType);
printf("%-10s: %d\n", "DO slot", doSlot);
printf("%-10s: %.3f\n", "threshold", fThreshold);
// Config TC module
rc = MX_RTU_Module_TC_Type_Set(tcSlot, tcChannel, 1, &tcType);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_TC_Type_Set(%d, %d, %d), return code = %d.\n",
tcSlot, tcChannel, 1, 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 polling TC
printf("Start!\n");
while(1)
{
UINT8 status = 0;
float fEngVal = 0;
rc = MX_RTU_Module_DO_Value_Get(doSlot, &u32DOVal);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Get(%d), return code = %d.\n", doSlot, rc);
break;
}
// Get burnout status
rc = MX_RTU_Module_TC_Burnout_Status_Get(tcSlot, tcChannel, 1, &status);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_TC_Burnout_Status_Get(%d, %d, %d), return code = %d.\n",
tcSlot, tcChannel, 1, rc);
break;
}
// get TC value
rc = MX_RTU_Module_TC_Eng_Value_Get(tcSlot, tcChannel, 1, &fEngVal, NULL);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_TC_Eng_Value_Get(%d, %d, %d), return code = %d.\n",
tcSlot, tcChannel, 1, rc);
break;
}
// if burnout --> turn off DO0, turn on DO1
if(status == BURNOUT_STATUS_BURNOUT)
{
u32DOVal &= ~0x1;
u32DOVal |= 0x2;
}
else
{
u32DOVal &= ~0x2;
// compare TC value with threshold then set DO0
if(fEngVal > fThreshold)
u32DOVal |= 0x1;
else
u32DOVal &= ~0x1;
}
rc = MX_RTU_Module_DO_Value_Set(doSlot, u32DOVal);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Set(%d, 0x%04X), return code = %d.\n",
doSlot, u32DOVal, rc);
break;
}
}
return 0;
}