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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Macros | Functions
ao.c File Reference

AO Sample More...

`#include <libmoxa_rtu.h>`

## Macros
#define | AO_CHANNEL_ENABLE 1

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

## Detailed Description

AO Sample

Copyright
  Moxa Inc.

Date
  08-07-2015

Author
  TJ Tai

Version
  V1.0
  AO Sample

    Introduction:
    This is AO sample code. The controller will use AO module output staic source
    and check the AI values is correct or not.
    Example:
    1. Using default: ./ao
    2. Setting AO slot and channel only: ./ao -o5 -s3
    Default:
    Slot of AI module = 1
    Channel on AI module = 0
    Slot of AO module = 2
    Channel on AO module = 0
    Static AO output = 6.0v
    AO/AI Range = 0~10v
    Help:

    root@Moxa:/tmp#./ao -h
    AO sample program.

    Usage: ./ao [OPTIONS]

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

Library:
AO APIs

## Macro Definition Documentation

#define AO_CHANNEL_ENABLE 1

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* AO Sample Application
*
* Date Author Comment
* 08-07-2015 TJ Tai Created.
******************************************************************************/
#include <libmoxa_rtu.h>
#define AO_CHANNEL_ENABLE 1
int main(int argc, char **const argv)
{
int rc, i;
UINT32 aiSlot = 1, aoSlot = 2, slotMin = 0, slotMax = 9;
UINT32 aoChannel = 0;
int aoChannelAmount = 6;
UINT32 aiChannel = 0;
int aiChannelAmount = 8;
UINT8 aiRange = 0;
UINT8 arrMode[aoChannelAmount];
float burnout = 2.0, burnoutMin = 0.0, burnoutMax = 4.0;
UINT32 u32DOVal = 0;
UINT8 type = AO_RANGE_0_10V;
MX_RTU_Total_Slots_Get(&slotMax);
while(-1 != (rc = getopt(argc, argv, "hi:c:o: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)
{
printf("Error parameter: ai channel: %d\n", aiChannel);
return -1;
}
break;
case 'o':
aoSlot = atoi(optarg);
if(aoSlot < slotMin || aoSlot > slotMax)
{
printf("Error parameter: slot: %d\n", aoSlot);
return -1;
}
break;
case 's':
aoChannel = atoi(optarg);
if(aoChannel < 0 || aoChannel >= aoChannelAmount)
{
printf("Error parameter: ao channel: %d\n", aoChannel);
return -1;
}
break;
case '?':
case 'h':
default:
printf("AO sample program.\n\n");
printf("Usage: ./ao [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 AO module [%d-%d]. Default slot = %d\n",
"-o", slotMin, slotMax, aoSlot);
printf("\t%-8s Channel on AO module [%d-%d]. Default channel = %d\n",
"-s", 0, aoChannelAmount - 1, aoChannel);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "AI slot", aiSlot);
printf("%-10s: %d\n", "AI channel", aiChannel);
printf("%-10s: %d\n", "AO slot", aoSlot);
printf("%-10s: %d\n", "AO channel", aoChannel);
UINT8 u8TypeBuf[aoChannelAmount];
UINT8 u8EnableBuf[aoChannelAmount];
float u32EngBuf[aoChannelAmount];
UINT8 u8RangBuf[aiChannelAmount];
// Config AO
for(i = 0; i < aoChannelAmount; i++)
{
u8TypeBuf[i] = type;
u8EnableBuf[i] = AO_CHANNEL_ENABLE;
u32EngBuf[i] = 6.0;
}
rc = MX_RTU_Module_AO_Enable_Set(aoSlot, aoChannel, 1, u8EnableBuf);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_AO_Enable_Set() = %d\r\n", rc);
return 0;
}
rc = MX_RTU_Module_AO_Range_Set(aoSlot, aoChannel, 1, u8TypeBuf);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_AO_Range_Set() = %d\r\n", rc);
return 0;
}
rc = MX_RTU_Module_AO_Eng_Value_Set(aoSlot, aoChannel, 1, u32EngBuf);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_AO_Eng_Value_Set() = %d\r\n", rc);
return 0;
}
// Config AI
for (i = 0; i < aiChannelAmount; i++) {
u8RangBuf[i] = AI_RANGE_0_10V;
}
rc = MX_RTU_Module_Fast_AI_Range_Get(aiSlot, aiChannel, aiChannelAmount, u8RangBuf); //Set
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_Fast_AI_Range_Get() = %d\r\n", rc);
return 0;
}
while(1)
{
float fEngVal = 0;
// Polling AI Value
rc = MX_RTU_Module_Fast_AI_Eng_Value_Get(aiSlot, aiChannel, 1, &fEngVal, NULL);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_Fast_AI_Eng_Value_Get(%d, %d, %d), return code = %d.\n",
aiSlot, aiChannel, 1, rc);
break;
}
printf("\rAO[%d] Engineering Value = %10f, AI[%d] Engineering Value = %10f", aoChannel, u32EngBuf[aoChannel], aiChannel, fEngVal);
fflush(0);
}
return 0;
}