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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Macros | Functions | Variables
fast_ai_stop.c File Reference

Fast AI Sample More...

`#include <libmoxa_rtu.h>`

## Macros
#define | DEFAULT_OUT_NAME "fast_ai_data.csv"

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

## Variables
int | forceStopFlag = 0

## Detailed Description

Fast AI Sample

Copyright
  Moxa Inc.

Date
  07-18-2016

Author
  Wanhan Hsieh

Version
  V1.0

      root@Moxa:/tmp#./fast_ai_stop -h
      Fast AI sample program.

      Usage: ./fast_ai_stop [OPTIONS]

      Options:
          -s      Slot of fast AI module [1-9]. Default slot = 2
          -c      Channel on Fast AI module [0-7]. Default channel = 0
          -r      Samplig rate [10-5000]. Default rate = 500
          -f      Fore interval [0-3]. Default interval = 1
          -b      Back interval [0-3]. Default interval = 1
          -o      Output file name. Default name = fast_ai_data.csv

  Library:
  FAST_AI APIs

## Macro Definition Documentation

#define DEFAULT_OUT_NAME "fast_ai_data.csv"

## Function Documentation

void sig_handler | ( | int | signo | ) |

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* Fast AI Sample Application
*
* Date Author Comment
* 07-18-2016 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
#define DEFAULT_OUT_NAME "fast_ai_data.csv"
int forceStopFlag = 0;
void sig_handler(int signo)
{
if (signo == SIGINT)
{
printf("STOP\n");
forceStopFlag = 1;
}
}
int main(int argc, char **const argv)
{
int rc, i;
UINT32 aiSlot = 2, slotMin = 0, slotMax = 0;
UINT32 aiChannel = 0, channelMin = 0, channelMax = 7;
UINT8 range = AI_RANGE_4_20mA;
UINT32 rate = 5000, rateMin = 10, rateMax = 5000;
UINT32 foreInterval = 1, intervalMin = 0, intervalMax = 3;
UINT32 backInterval = 1;
char szOutFlieName[64];
FILE *pOutFile;
UINT32 triggerFlags = 0x1 << aiChannel;
int isTriggered = 0;
float triggerEng = 4.0, stopEng = 4.0, triggerMin = 4.0, triggerMax = 20.0;
UINT8 *buf;
struct Timestamp triggerTime, aiTime, stopTime;
UINT32 requiredBufSize;
signal(SIGINT, sig_handler);
MX_RTU_Total_Slots_Get(&slotMax);
memset(szOutFlieName, 0, sizeof(szOutFlieName));
strncpy(szOutFlieName, DEFAULT_OUT_NAME, strlen(DEFAULT_OUT_NAME));
while(-1 != (rc = getopt(argc, argv, "hs:c:r:f:b:o:t:p:")))
{
switch(rc)
{
case 's':
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 < channelMin || aiChannel > channelMax)
{
printf("Error parameter: channel: %d\n", aiChannel);
return -1;
}
triggerFlags = 0x1 << aiChannel;
break;
case 'r':
rate = atoi(optarg);
if(rate < rateMin)
rate = rateMin;
if(rate > rateMax)
rate = rateMax;
break;
case 'f':
foreInterval = atoi(optarg);
if(foreInterval > intervalMax)
foreInterval = intervalMax;
break;
case 'b':
backInterval = atoi(optarg);
if(backInterval > intervalMax)
backInterval = intervalMax;
break;
case 'o':
memset(szOutFlieName, 0, sizeof(szOutFlieName));
strncpy(szOutFlieName, optarg, strlen(optarg));
break;
case 't':
triggerEng = atof(optarg);
if(triggerEng < triggerMin)
triggerEng = triggerMin;
if(triggerEng > triggerMax)
triggerEng = triggerMax;
break;
case 'p':
stopEng = atof(optarg);
if(stopEng < triggerMin)
stopEng = triggerMin;
if(stopEng > triggerMax)
stopEng = triggerMax;
break;
case '?':
case 'h':
default:
printf("Fast AI sample program.\n\n");
printf("Usage: ./fast_ai [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of fast AI module [%d-%d]. Default slot = %d\n",
"-s", slotMin, slotMax, aiSlot);
printf("\t%-8s Channel on Fast AI module [%d-%d]. Default channel = %d\n",
"-c", channelMin, channelMax, aiChannel);
printf("\t%-8s Samplig rate [%d-%d]. Default rate = %d\n",
"-r", rateMin, rateMax, rate);
printf("\t%-8s Fore interval [%d-%d]. Default interval = %d\n",
"-f", intervalMin, intervalMax, foreInterval);
printf("\t%-8s (One unit = 1sec)\n", "");
printf("\t%-8s Back interval [%d-%d]. Default interval = %d\n",
"-b", intervalMin, intervalMax, backInterval);
printf("\t%-8s (One unit = 1sec)\n", "");
printf("\t%-8s Output file name. Default name = %s\n",
"-o", szOutFlieName);
printf("\n");
return;
}
}
printf("%-10s: %d\n", "AI slot", aiSlot);
printf("%-10s: %d\n", "AI channel", aiChannel);
printf("%-10s: %d\n", "rate", rate);
printf("%-10s: %d\n", "fore", foreInterval);
printf("%-10s: %d\n", "back", backInterval);
printf("%-10s: %s\n", "output", szOutFlieName);
rc = MX_RTU_Module_Fast_AI_Range_Get(aiSlot, 0, 1, &range);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Range_Get(%d, %d, %d), return code = %d.\n",
aiSlot, 0, 1, rc);
printf("%-10s: %d\n", "range", range);
printf("%s: %.3f\n", "trigger value (>)", triggerEng);
printf("%s: %.3f\n", "stop value (<)", stopEng);
pOutFile = fopen(szOutFlieName, "w");
if(pOutFile == NULL)
{
fprintf(stderr, "Error open file: %s\n", szOutFlieName);
return -1;
}
// Config Fast AI module
rc = MX_RTU_Module_Fast_AI_Sampling_Rate_Set(aiSlot, rate);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Sampling_Rate_Set(%d, %d), return code = %d.\n",
aiSlot, rate, rc);
rc = MX_RTU_Module_Fast_AI_Sampling_Rate_Get(aiSlot, &rate);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Sampling_Rate_Get(%d), return code = %d.\n", aiSlot, rc);
else
printf("Sampling Rate = %d\n", rate);
rc = MX_RTU_Module_Fast_AI_Buf_Overflow_Reset(aiSlot, triggerFlags);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Buf_Overflow_Reset(%d, %d), return code = %d.\n",
aiSlot, triggerFlags, rc);
rc = MX_RTU_Module_Fast_AI_Buf_Reset(aiSlot);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Buf_Reset(%d), return code = %d.\n", aiSlot, rc);
sleep(foreInterval);
while(!forceStopFlag)
{
// Start to polling AI
printf("Start to polling AI.\n");
while(!forceStopFlag)
{
float aiEng = 0.0;
rc = MX_RTU_Module_Fast_AI_Eng_Value_Get(aiSlot, aiChannel, 1, &aiEng, &triggerTime);
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;
}
if(aiEng > triggerEng)
break;
usleep(1000);
}
// Trigger Fast AI
printf("Trigger Fast AI...\n");
do
{
if(forceStopFlag)
break;
rc = MX_RTU_Module_Fast_AI_Trigger_Set(aiSlot, triggerFlags, foreInterval, backInterval, &requiredBufSize);
if(rc != MODULE_RW_ERR_OK && rc != MODULE_RW_ERR_IO_FAST_AI_NOT_READY)
{
printf("MX_RTU_Module_Fast_AI_Trigger_Set(%d, 0x%02X, %d, %d), return code = %d.\n",
aiSlot, triggerFlags, foreInterval, backInterval, rc);
}
else
isTriggered = 1;
usleep(1000);
}
while(rc == MODULE_RW_ERR_IO_FAST_AI_NOT_READY);
printf("Done.\n");
// Get Fast AI data
if(isTriggered)
{
buf = (UINT8*) malloc(requiredBufSize);
if(buf == NULL)
{
fprintf(stderr, "Error allocate memory\n");
return -1;
}
// polling AI
printf("Polling AI.\n");
while(!forceStopFlag)
{
float aiEng = 0.0;
rc = MX_RTU_Module_Fast_AI_Eng_Value_Get(aiSlot, aiChannel, 1, &aiEng, &triggerTime);
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;
}
if(aiEng < stopEng)
{
// Stopping trigger AI
printf("Stopping trigger AI.\n");
rc = MX_RTU_Module_Fast_AI_Trigger_Stop_Set(aiSlot, triggerFlags, &stopTime);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_Fast_AI_Trigger_Stop_Set(%d, 0x%x) = %d\n", aiSlot, triggerFlags, rc);
}
break;
}
usleep(1000);
}
printf("Getting batch data...\n");
do
{
if(forceStopFlag)
break;
rc = MX_RTU_Module_Fast_AI_Batch_Data_Get(aiSlot, aiChannel,
foreInterval, backInterval, buf, &aiTime);
if(rc == MODULE_RW_ERR_OK)
{
UINT16 raw;
float eng;
fprintf(pOutFile, "AI Trigger time: %02d:%02d:%02d.%02d\n",
triggerTime.hour, triggerTime.min, triggerTime.sec, triggerTime.msec);
fprintf(pOutFile, "AI data start time: %02d:%02d:%02d.%02d\n",
aiTime.hour, aiTime.min, aiTime.sec, aiTime.msec);
fprintf(pOutFile, "AI stop time: %02d:%02d:%02d.%02d\n",
stopTime.hour, stopTime.min, stopTime.sec, stopTime.msec);
for(i = 0; i < requiredBufSize; i += 2)
{
memcpy(&raw, &buf[i], 2);
MX_RTU_AIO_Raw_to_Eng(range, 16, raw, &eng);
fprintf(pOutFile, "%f\n", eng);
}
printf("Save AI data into %s.\n", szOutFlieName);
break;
}
else if(rc != MODULE_RW_ERR_IO_FAST_AI_BUF_EMPTY)
{
printf("MX_RTU_Module_Fast_AI_Batch_Data_Get(%d, %d, %d, %d), return code = %d.\n",
aiSlot, aiChannel, foreInterval, backInterval, rc);
}
usleep(1000);
}
while(rc == MODULE_RW_ERR_IO_FAST_AI_BUF_EMPTY);
printf("Done.\n");
if(buf)
free(buf);
}
}
fclose(pOutFile);
return 0;
}

## Variable Documentation

int forceStopFlag = 0