ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Functions
data_logging_service_interval.c File Reference

Data Logging Interval Condition TAG Sample More...

#include "libmoxa_rtu.h"

Functions

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

Detailed Description

Data Logging Interval Condition TAG Sample

Date
02-25-2014
Author
TJ Tai
Version
V1.0
Introduction:
DI channel data logging by interval condition and FTP upload file to user-specific FTP server.
Example:
1. Using default: ./data_logging_service_interval 
2. Setting profile number and enabled FTP upload mode: ./data_logging_service_interval -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] 
Log Interval = 5 sec [Setting in RTUxpress only] 
File Split Separated by Interval = 5 mins [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#./data_logging_service_interval -h
Data Logging TAG program.

Usage: ./data_logging_service_interval [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

Function Documentation

int main ( int  argc,
char *  argv[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* TAG Service for Data Logging Application
*
* Date Author Comment
* 02-25-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: ./data_logging_service_interval [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;
}
}
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;
}
// 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 == 1) {
printf("Data Logging Service Init Success.\n");
}
else {
printf("Data Logging Service Init fail.\n");
return 0;
}
// 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;
}
while (1) {
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 == 3) {
printf("\rService success");
break;
}
else if (u32Val == 4) {
printf("Service fail\n");
break;
}
else if (u32Val == 2) {
printf("\rService running");
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;
}