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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Macros | Functions
ioPAC8600_tag_ao.c File Reference

ioPAC8600 AO TAG Sample More...

`#include <libmoxa_rtu.h>`

## Macros
#define | AO_STATIC_MODE 1
#define | AO_WAVEFORM_MODE 2

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

## Detailed Description

ioPAC8600 AO TAG Sample

Copyright
  Moxa Inc.

Date
  08-11-2015

Author
  TJ Tai

Version
  V1.0
  AO TAG Sample

    Introduction:
    This is AO TAG sample code. The controller will set AO output throught RTUxpress
    and then check for AI values is matching or not.
    Example:
    1. Using default: ./ioPAC8600_tag_ao
    2. Setting AO slot and channel only: ./ioPAC8600_tag_ao -o5 -s3
    3. Setting AI channel 2 connect to AO channel 1: ./ioPAC8600_tag_ao -c 2 -s 1
    Default:
    Slot of AI module = 1
    Slot of AO module = 2
    Channel on AO module = 0
    Help:

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

    Usage: ./ioPAC8600_tag_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:
TAG APIs
RTUxpress Project file:
ioPAC8600_tag_ao.rtu
(Please right click on the link and ‘Save Target As…’ to save RTUxpress project file and open it with RTUxpress utility)

## Macro Definition Documentation

#define AO_STATIC_MODE 1

#define AO_WAVEFORM_MODE 2

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* AO TAG Sample Application
*
* Date Author Comment
* 08-11-2015 TJ Tai Created.
******************************************************************************/
#include <libmoxa_rtu.h>
#define AO_STATIC_MODE 1
#define AO_WAVEFORM_MODE 2
int main(int argc, char **const argv)
{
int rc, i;
TAG_ERR_CODE retval = 0;
TAG_INFO tagInfo;
UINT32 aiSlot = 1, aoSlot = 2, slotMin = 0, slotMax = 9;
UINT32 aoChannel = 0;
int aoChannelAmount = 6;
UINT32 aiChannel = 0;
int aiChannelAmount = 8;
char aiEngTag[aiChannelAmount][TAG_MAX_NAME_SIZE]; // ai engineering value
char aoEngTag[aoChannelAmount][TAG_MAX_NAME_SIZE]; // ao engineering value
char aoWaveTag[aoChannelAmount][TAG_MAX_NAME_SIZE];// ao waveform start stop
UINT32 aoMode[aoChannelAmount]; // ao channel mode
UINT32 bitVal[1] = {1};
float u32AIVal = 0;
float u32AOVal = 0;
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;
}
}
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);
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init(), return code = %d.\n", retval);
return 0;
}
// Config AI/AO Engineering value TAG
for(i = 0; i < aiChannelAmount; i++)
{
sprintf(aiEngTag[i], "S%d_AI%d_EngineeringValue", aiSlot, i);
}
for(i = 0; i < aoChannelAmount; i++)
{
sprintf(aoEngTag[i], "S%d_AO%d_EngineeringValue", aoSlot, i);
}
// Config AO Waveform start stop TAG
for(i = 0; i < aoChannelAmount; i++)
{
sprintf(aoWaveTag[i], "S%d_AO%d_WaveformStartStop", aoSlot, i);
}
// Detect AO channel mode
retval = MX_RTU_Tag_Read(aoEngTag[aoChannel], &u32AOVal, sizeof(u32AOVal), NULL, NULL);
if(retval == TAG_ERR_OK) {
// This Channel in static mode
aoMode[aoChannel] = AO_STATIC_MODE;
}
else if (retval == TAG_ERR_NO_TAG) {
retval = MX_RTU_Tag_Read(aoWaveTag[aoChannel], &u32AOVal, sizeof(u32AOVal), NULL, NULL);
if(retval == TAG_ERR_OK) {
// This Channel in Waveform mode
aoMode[aoChannel] = AO_WAVEFORM_MODE;
}
else if(retval != TAG_ERR_OK)
{
printf("AO mode setting error, please config correct through RTUxpress.\n");
return 0;
}
}
else if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", aoEngTag[aoChannel], retval);
return 0;
}
// If AO channel mode is in Waveform, start to output waveform before poll AI value
if (aoMode[aoChannel] == AO_WAVEFORM_MODE) {
retval = MX_RTU_Tag_Get_Info(aoWaveTag[aoChannel], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", aoWaveTag[aoChannel], retval);
return 0;
}
retval = MX_RTU_Tag_Write(aoWaveTag[aoChannel], bitVal, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", aoWaveTag[aoChannel], retval);
return 0;
}
}
while(1)
{
// Get AI
retval = MX_RTU_Tag_Read(aiEngTag[aiChannel], &u32AIVal, sizeof(u32AIVal), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", aiEngTag[aiChannel], retval);
break;
}
if (aoMode[aoChannel] == AO_STATIC_MODE) {
retval = MX_RTU_Tag_Read(aoEngTag[aoChannel], &u32AOVal, sizeof(u32AOVal), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", aoEngTag[aoChannel], retval);
break;
}
printf("\rAO[%d] Engineering Value = %10f, AI[%d] Engineering Value = %10f", aoChannel, u32AOVal, aiChannel, u32AIVal);
fflush(0);
}
else if (aoMode[aoChannel] == AO_WAVEFORM_MODE) {
printf("\rAI[%d] Engineering Value = %10f", aiChannel, u32AIVal);
fflush(0);
}
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}