ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
Functions
ioPAC8600_snmp_agent_service.c File Reference

ioPAC8600 SNMP Agent Application TAG Sample More...

#include "libmoxa_rtu.h"

Functions

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

Detailed Description

ioPAC8600 SNMP Agent Application TAG Sample

Date
07-29-2015
Author
TJ Tai
Version
V1.0
8600_snmp_agent.jpg
SNMP Agent Service Sample
Introduction:
Check DI0 and DI15 status and update to IR tag which is monitor by snmp tagtable
Please use mib browser to monitor snmp tagtable
Example:
./ioPAC8600_snmp_agent_service 
Help:
root@Moxa:/tmp#./ioPAC8600_snmp_agent_service -h
SNMP agent service program.

Usage: ./ioPAC8600_snmp_agent_service [OPTIONS]

Options:
    -i        Slot of DI module [1-9]. Using with -i. Default value: 1
              Ex. ./ioPAC8600_snmp_agent_service -i 1

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

Function Documentation

void usage ( void  )
int main ( int  argc,
char *  argv[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* TAG Service for SNMP Agent Service Application
*
* Date Author Comment
* 07-29-2015 TJ Tai Created.
******************************************************************************/
#include "libmoxa_rtu.h"
/*******************************************************************************
*
* Sample code for TAG SNMP Agent Service
*
*******************************************************************************/
void usage(void)
{
printf("%s.\n\n", "SNMP agent service Program");
printf("Usage: ./ioPAC8600_snmp_agent_service [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DI module [1-9]. Using with -i. Default value: 1 \n", "-i");
printf("\t Ex. ./ioPAC8600_snmp_agent_service -i 1\n");
printf("\n");
}
int main(int argc, char * argv[])
{
int rc;
TAG_ERR_CODE retval = 0;
TAG_INFO tagInfo;
UINT32 diSlot = 1, slotMin = 0, slotMax = 9;
int diChannelAmount = 16;
char di_tagName[diChannelAmount][TAG_MAX_NAME_SIZE];
int i, j;
UINT32 u32Val = 0;
INT32 bitVal = 0;
INT32 ch00Val = 0;
INT32 ch15Val = 0;
int chStatus[1];
char agentServiceStatus[1][TAG_MAX_NAME_SIZE];
char IRCtl[1][TAG_MAX_NAME_SIZE];
while(-1 != (rc = getopt(argc, argv, "i:h")))
{
switch(rc)
{
case 'i':
diSlot = atoi(optarg);
if(diSlot < slotMin || diSlot > slotMax)
{
printf("Error parameter: slot: %d\n", diSlot);
return -1;
}
break;
case 'h':
case '?':
default:
usage();
return 0;
}
}
sprintf(agentServiceStatus[0], "Service_SNMPAgent_Status");
sprintf(IRCtl[0], "S0_IR_BOOL_RW0Example");
printf("%-10s: %d\n", "DI slot", diSlot);
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(agentServiceStatus[0], &u32Val, sizeof(u32Val), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", agentServiceStatus[0], retval);
}
if(u32Val == STATUS_RUNNING)
{
printf("SNMP Agent Service Init Success.\n");
break;
}
else if(u32Val == STATUS_FAIL)
{
printf("SNMP Agent Service Init fail.\n");
return 0;
}
else if(u32Val == STATUS_INIT)
{
printf("SNMP Agent Service Initing, please wait ...\r\n");
sleep(5);
}
}
// Config DI TAG
for(i = 0; i < diChannelAmount; i++)
{
sprintf(di_tagName[i], "S%d_DI%d_DIValue", diSlot, i);
}
while(1)
{
retval = MX_RTU_Tag_Get_Info(di_tagName[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d\n", di_tagName[0], retval);
break;
}
retval = MX_RTU_Tag_Read(di_tagName[0], &ch00Val, sizeof(ch00Val), NULL, NULL);
retval = MX_RTU_Tag_Read(di_tagName[15], &ch15Val, sizeof(ch15Val), NULL, NULL);
// represent DI0 and DI15 status
*chStatus = ch00Val*ch15Val;
// update IR tag which is monitor by snmp tagtable
retval = MX_RTU_Tag_Get_Info(IRCtl[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d\n", IRCtl[0], retval);
break;
}
retval = MX_RTU_Tag_Write(IRCtl[0], chStatus, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", IRCtl[0], retval);
return 0;
}
sleep(1);
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}