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

RTD Sample More...

#include <libmoxa_rtu.h>

Functions

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

Detailed Description

RTD Sample

Date
06-11-2014
Author
Wanhan Hsieh
Version
V1.0
rtd.png
RTD Sample
Introduction:
This is an RTD sample code. The controller will poll for RTD values, and set DO values according to the RTD level.
Example:
1. Using default: ./rtd
2. Setting RTD slot and channel only: ./rtd -i5 -c3
Default:
Slot of RTD module = 1
Channel on RTD module = 0
Slot of DO module = 2
Help:
root@Moxa:/tmp#./rtd -h
RTD sample program.

Usage: ./rtd [OPTIONS]

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

Library:
RTD APIs

Function Documentation

int main ( int  argc,
char **const  argv 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* RTD 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 rtdSlot = 1;
UINT32 doSlot = 2, slotMin = 0, slotMax = 0;
UINT32 rtdChannel = 0;
int rtdChannelAmount = 6;
int doChannelAmount = 16;
UINT8 rtdType = RTD_TYPE_PT100;
int rtdTypeAmount = 18;
UINT8 arrMode[doChannelAmount];
UINT32 u32DOVal = 0;
float fThreshold = 100.0f;
while(-1 != (rc = getopt(argc, argv, "c:hi:r:s:t:")))
{
switch(rc)
{
case 'c':
rtdChannel = atoi(optarg);
if(rtdChannel < 0 || rtdChannel >= rtdChannelAmount)
{
printf("Error parameter: channel: %d\n", rtdChannel);
return -1;
}
break;
case 'i':
rtdSlot = atoi(optarg);
if(rtdSlot < slotMin || rtdSlot > slotMax)
{
printf("Error parameter: slot: %d\n", rtdSlot);
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':
rtdType = atoi(optarg);
if(rtdType < 0 || rtdType >= rtdTypeAmount)
{
printf("Error parameter: type: %d\n", rtdType);
return -1;
}
break;
case '?':
case 'h':
default:
printf("RTD sample program.\n\n");
printf("Usage: ./rtd [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Channel on RTD module [%d-%d]. Default channel = %d\n",
"-c", 0, rtdChannelAmount - 1, rtdChannel);
printf("\t%-8s Slot of RTD module [%d-%d]. Default slot = %d\n",
"-i", slotMin, slotMax, rtdSlot);
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 RTD type [%d-%d]. Default type = %d\n",
"-t", 0, rtdTypeAmount - 1, rtdType);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "RTD slot", rtdSlot);
printf("%-10s: %d\n", "RTD channel", rtdChannel);
printf("%-10s: %d\n", "RTD type", rtdType);
printf("%-10s: %d\n", "DO slot", doSlot);
printf("%-10s: %.3f\n", "threshold", fThreshold);
// Config RTD module
rc = MX_RTU_Module_RTD_Type_Set(rtdSlot, rtdChannel, 1, &rtdType);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_RTD_Type_Set(%d, %d, %d), return code = %d.\n",
rtdSlot, rtdChannel, 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 RTD
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_RTD_Burnout_Status_Get(rtdSlot, rtdChannel, 1, &status);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_RTD_Burnout_Status_Get(%d, %d, %d), return code = %d.\n",
rtdSlot, rtdChannel, 1, rc);
break;
}
// get RTD value
rc = MX_RTU_Module_RTD_Eng_Value_Get(rtdSlot, rtdChannel, 1, &fEngVal, NULL);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_RTD_Eng_Value_Get(%d, %d, %d), return code = %d.\n",
rtdSlot, rtdChannel, 1, rc);
break;
}
// if burnout --> turn off DO0, turn on DO1
if(status == BURNOUT_STATUS_BURNOUT)
{
u32DOVal &= ~0x1;
u32DOVal |= 0x2;
}
else
{
u32DOVal &= ~0x2;
// compare RTD 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;
}