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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
ioPAC8600_tag_relay.c File Reference

ioPAC8600 Relay TAG Sample More...

`#include "libmoxa_rtu.h"`

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

## Detailed Description

ioPAC8600 Relay TAG Sample

Copyright
  Moxa Inc.

Date
  08-04-2015

Author
  TJ Tai

Version
  V1.0
  relay TAG Sample

    Introduction:
    This is Relay TAG sample code. The controller will use relay module channel 0
    to controller another ioPAC power on/off.
    Example:
    1. Using default: ./ioPAC8600_tag_relay
    2. Setting DI slot and counter mode channel: ./ioPAC8600_tag_relay -i5 -c3
    Default:
    Slot of Relay module = 1
    Channel of Relay module = 0
    Help:

    root@Moxa:/tmp#./ioPAC8600_tag_relay -h
    Relay TAG sample program.

    Usage: ./ioPAC8600_tag_relay [OPTIONS]

    Options:
        -i      Slot of Relay module [1-9]. Default slot = 1
        -c      Channel of Relay counter module [0-15]. Default Channel = 0

Library:
TAG APIs
RTUxpress Project file:
ioPAC8600_tag_relay.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.
*
* Relay TAG Sample code
*
* Date Author Comment
* 08-04-2015 TJ Tai Created.
******************************************************************************/
#include "libmoxa_rtu.h"
int main(int argc, char * argv[])
{
int rc, i;
TAG_ERR_CODE retval = 0;
TAG_INFO tagInfo;
UINT32 relaySlot = 1, slotMin = 0, slotMax = 9;
UINT32 u32Val = 0;
int relayChannelAmount = 6, relayChannel= 0, relayChannelTagAmount = 3;
char relay_tag[relayChannelTagAmount][TAG_MAX_NAME_SIZE];
UINT32 arrReset[relayChannelAmount];
UINT32 arrTrigger[relayChannelAmount];
while(-1 != (rc = getopt(argc, argv, "hi:c:")))
{
switch(rc)
{
case 'i':
relaySlot = atoi(optarg);
if(relaySlot < slotMin || relaySlot > slotMax)
{
printf("Error parameter: slot: %d\n", relaySlot);
return -1;
}
break;
case 'c':
relayChannel = atoi(optarg);
if(relayChannel < 0 || relayChannel > relayChannelAmount-1)
{
printf("Error parameter: Channel: %d\n", relayChannel);
return -1;
}
break;
case '?':
case 'h':
default:
printf("Relay TAG sample program.\n\n");
printf("Usage: ./ioPAC8600_tag_relay [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of Relay module [%d-%d]. Default slot = %d\n",
"-i", slotMin, slotMax, relaySlot);
printf("\t%-8s Channel of Relay module [%d-%d]. Default Channel = %d\n",
"-c", 0, relayChannelAmount - 1, relayChannel);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "Relay slot", relaySlot);
printf("%-10s: %d\n", "Relay channel", relayChannel);
sprintf(relay_tag[0], "S%d_Relay%d_RelayCurrentCount", relaySlot, relayChannel);
sprintf(relay_tag[1], "S%d_Relay%d_RelayTotalCount", relaySlot, relayChannel);
sprintf(relay_tag[2], "S%d_Relay%d_RelayValue", relaySlot, relayChannel);
for (i = 0; i < relayChannelAmount; i++) {
arrReset[i] = 0;
arrTrigger[i] = 1;
}
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init(), return code = %d.\n", retval);
return 0;
}
// Check relay life time
retval = MX_RTU_Tag_Get_Info(relay_tag[1], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", relay_tag[1], retval);
return 0;
}
retval = MX_RTU_Tag_Read(relay_tag[1], &u32Val, sizeof(u32Val), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", relay_tag[1], retval);
return 0;
}
printf("%-10s: %d\n", "Relay channel life time count", u32Val);
// Reset current count to default
retval = MX_RTU_Tag_Get_Info(relay_tag[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", relay_tag[0], retval);
return 0;
}
retval = MX_RTU_Tag_Write(relay_tag[0], arrReset, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", relay_tag[0], retval);
return 0;
}
// Relay On
retval = MX_RTU_Tag_Get_Info(relay_tag[2], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", relay_tag[2], retval);
return 0;
}
retval = MX_RTU_Tag_Write(relay_tag[2], arrTrigger, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", relay_tag[2], retval);
return 0;
}
printf("---- power on ----\n");
printf("device should start booting!!!\n");
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}