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

AI Sample More...

#include <libmoxa_rtu.h>

Functions

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

Detailed Description

AI Sample

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

Usage: ./ai [OPTIONS]

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

Library:
AI APIs

Function Documentation

int main ( int  argc,
char **const  argv 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* AI Sample Application
*
* Date Author Comment
* 04-16-2013 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char **const argv)
{
int rc, i;
UINT32 aiSlot = 1;
UINT32 doSlot = 2, slotMin = 0, slotMax = 0;
UINT32 aiChannel = 0;
int aiChannelAmount = 8;
int doChannelAmount = 16;
UINT8 aiRange = 0;
UINT8 arrMode[doChannelAmount];
float burnout = 2.0, burnoutMin = 0.0, burnoutMax = 4.0;
UINT32 u32DOVal = 0;
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 sample program.\n\n");
printf("Usage: ./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 0;
}
}
printf("%-10s: %d\n", "AI slot", aiSlot);
printf("%-10s: %d\n", "AI channel", aiChannel);
printf("%-10s: %d\n", "DO slot", doSlot);
// Config AI module
rc = MX_RTU_Module_AI_Range_Get(aiSlot, aiChannel, 1, &aiRange);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_AI_Range_Get(%d, %d, %d), return code = %d.\n",
aiSlot, aiChannel, 1, rc);
else
printf("%-10s: %s\n", "AI range", (aiRange == AI_RANGE_4_20mA) ?
"4-20mA" : (aiRange == AI_RANGE_0_10V) ? "0-10V" : "UNKNOWN");
rc = MX_RTU_Module_AI_Burnout_Value_Set(aiSlot, aiChannel, 1, &burnout);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_AI_Burnout_Value_Set(%d, %d, %d), return code = %d.\n",
aiSlot, aiChannel, 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 AI
printf("Start!\n");
while(1)
{
UINT8 status = 0;
float fEngVal = 0;
rc = MX_RTU_Module_AI_Status_Get(aiSlot, aiChannel, 1, &status);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_AI_Status_Get(%d, %d, %d), return code = %d.\n",
aiSlot, aiChannel, 1, rc);
break;
}
if(status == AI_STATUS_BURNOUT)
{
u32DOVal = ~u32DOVal;
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;
}
usleep(200000);
continue;
}
rc = MX_RTU_Module_AI_Eng_Value_Get(aiSlot, aiChannel, 1, &fEngVal, NULL);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_AI_Eng_Value_Get(%d, %d, %d), return code = %d.\n",
aiSlot, aiChannel, 1, rc);
break;
}
u32DOVal = 0;
for(i = 0; i <= 5; i++)
{
if(fEngVal >= i * 4)
u32DOVal |= (0x1 << i);
}
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;
}