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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
ioPAC8500_data_logging_service_dio_on_change.c File Reference

ioPAC8500 Data Logging DIO on Change Condition TAG Sample More...

`#include "libmoxa_rtu.h"`

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

## Detailed Description

ioPAC8500 Data Logging DIO on Change Condition TAG Sample

Copyright
  Moxa Inc.

Date
  02-26-2014

Author
  TJ Tai

Version
  V1.0
  Data Logging DIO On Change Sample

    Introduction:
    DI channel data logging by DIO on change condition and FTP upload file to user-specific FTP server.
    Logging tags are chose by the user. Data Logging files store on SD card.
    Example:
    1. Using default: ./ioPAC8500_data_logging_service_dio_on_change
    2. Setting profile number and enabled FTP upload mode: ./ioPAC8500_data_logging_service_dio_on_change -p5 -u1
    Default:
    Profile of Data Logging = 0
    Slot of DI module = 1 [Setting in RTUxpress only]
    Logging Channel = DI0 and DI1 [Setting in RTUxpress only]
    File Split record per file: 500 [Setting in RTUxpress only]
    Enable FTP upload mode = 1 [FTP Server connection setting in RTUxpress only]
    Data Logging for 5 mins
    Help:

    root@Moxa:/tmp#./ioPAC8500_data_logging_service_dio_on_change -h
    Data Logging TAG program.

    Usage: ./ioPAC8500_data_logging_service_dio_on_change [OPTIONS]

    Options:
        -p        Profile of Data Logging. Default Profile = 0
        -u        Enable FTP upload mode. Default Enable = 1 [Setting Disable: 0]
        -t        Data Logging Time in min(s). Default Time = 5

Library:
TAG APIs
RTUxpress Project file:
ioPAC8500_data_logging_service_dio_on_change.rtu
(Please right click on the link and ‘Save Target As…’ to save RTUxpress project file and open it with RTUxpress utility)

## Function Documentation

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

/*******************************************************************************
* Copyright Moxa Inc.
*
* TAG Service for Data Logging Application
*
* Date Author Comment
* 02-26-2014 TJ Tai Created.
******************************************************************************/
#include "libmoxa_rtu.h"
/*******************************************************************************
*
* Sample code for TAG Data Logging Service
*
*******************************************************************************/
int main(int argc, char * argv[])
{
int rc, i;
TAG_ERR_CODE retval = 0;
char szFileName[80] = "/etc/RTU/config.rtu";
TAG_INFO tagInfo;
UINT32 u32Val = 0;
int profile = 0; // default profile = 1
int ftpEnable = 1; // default enabled ftp upload
double minTime = 5; // default mins time for data logging
double timer = 0;
char dataLog[6][TAG_MAX_NAME_SIZE];
UINT32 bitVal[2] = { 0, 1};
time_t time1, time2;
double diffSec;
while(-1 != (rc = getopt(argc, argv, "hp:u:t:")))
{
switch(rc)
{
case 'p':
profile = atoi(optarg);
if(profile < 0)
{
printf("Error parameter: profile: %d\n", profile);
return -1;
}
break;
case 'u':
ftpEnable = atoi(optarg);
if(ftpEnable < 0 || ftpEnable > 1)
{
printf("Error parameter: FTP Enable: %d\n", ftpEnable);
return -1;
}
break;
case 't':
minTime = atof(optarg);
if(minTime <= 0)
{
printf("Error parameter: Time: %d\n", minTime);
return -1;
}
break;
case '?':
case 'h':
default:
printf("%s.\n\n", "Data Logging TAG program");
printf("Usage: ./ioPAC8500_data_logging_service_dio_on_change [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Profile of Data Logging. Default Profile = %d\n",
"-p", 0);
printf("\t%-8s Enable FTP upload mode. Default Enable = %d [Setting Disable: %d]\n",
"-u", 1 , 0);
printf("\t%-8s Data Logging Time in min(s). Default Time = %d\n",
"-t", 5);
printf("\n");
return 0;
}
}
printf("%-25s: %d\n", "Profile", profile);
printf("%-25s: %d\n", "FTP Enable", ftpEnable);
printf("%-25s: %lf\n", "Logging Time (mins)", minTime);
sprintf(dataLog[0], "Service_Datalog_Status");
sprintf(dataLog[1], "Service_Datalog_StartStop_%d", profile);
sprintf(dataLog[2], "Service_Datalog_Status_%d", profile);
sprintf(dataLog[3], "Service_Datalog_Trigger_%d", profile);
sprintf(dataLog[4], "Datalog_FTPUpload_Trigger_%d", profile);
sprintf(dataLog[5], "Datalog_Storage_Usage_%d", profile);
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init(), return code = %d.\n", retval);
return 0;
}
while(1)
{
// Check Service Init
retval = MX_RTU_Tag_Read(dataLog[0], &u32Val, sizeof(u32Val), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", dataLog[0], retval);
}
if(u32Val == STATUS_READY)
{
printf("Data Logging Service Init Success.\n");
break;
}
else if(u32Val == STATUS_FAIL)
{
printf("Data Logging Service Init fail.\n");
return 0;
}
else if(u32Val == STATUS_INIT)
{
printf("Email Alarm Service Initing, please wait ...\r\n");
sleep(5);
}
}
// Data Logging Start
time(&time1);
retval = MX_RTU_Tag_Get_Info(dataLog[1], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", dataLog[1], retval);
return 0;
}
retval = MX_RTU_Tag_Write(dataLog[1], bitVal + 1, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", dataLog[1], retval);
return 0;
}
// Config FTP Upload Start
retval = MX_RTU_Tag_Get_Info(dataLog[4], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", dataLog[4], retval);
return 0;
}
if(ftpEnable == 1)
{
retval = MX_RTU_Tag_Write(dataLog[4], bitVal + 1, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", dataLog[4], retval);
return 0;
}
}
else
{
retval = MX_RTU_Tag_Write(dataLog[4], bitVal + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", dataLog[4], retval);
return 0;
}
}
while(1)
{
time(&time2);
diffSec = difftime(time2, time1);
if(diffSec == minTime * 60)
{
break;
}
retval = MX_RTU_Tag_Read(dataLog[2], &u32Val, sizeof(u32Val), NULL, NULL);
fflush(0);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s), return code = %d\r\n", dataLog[2], retval);
return 0;
}
if(u32Val == STATUS_SUCCESS)
{
printf("\rService success");
}
else if(u32Val == STATUS_FAIL)
{
printf("Service fail\n");
return 0;
}
else if(u32Val == STATUS_RUNNING)
{
printf("\rService running");
sleep(1);
}
else if(u32Val == STATUS_READY)
{
printf("\rService waiting");
sleep(1);
}
}
// Storage Checking
retval = MX_RTU_Tag_Read(dataLog[5], &u32Val, sizeof(u32Val), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s), return code = %d\r\n", dataLog[5], retval);
return 0;
}
printf("\rDatalog Storage Usage: %d%\n", u32Val);
// Data Logging Stop
// FTP Upload Stop
retval = MX_RTU_Tag_Get_Info(dataLog[4], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", dataLog[4], retval);
return 0;
}
retval = MX_RTU_Tag_Write(dataLog[4], bitVal + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", dataLog[4], retval);
return 0;
}
// Data Logging Service Stop
retval = MX_RTU_Tag_Get_Info(dataLog[1], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", dataLog[1], retval);
return 0;
}
retval = MX_RTU_Tag_Write(dataLog[1], bitVal + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", dataLog[1], retval);
return 0;
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}