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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
relay.c File Reference

Relay Sample More...

`#include <libmoxa_rtu.h>`

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

## Detailed Description

Relay Sample

Copyright
  Moxa Inc.

Date
  08-05-2015

Author
  TJ Tai

Version
  V1.0
  Relay Sample

    Introduction:
    This is Relay sample code. The controller will use Relay module to control another ioPAC power on/off.
    Example:
    1. Using default: ./relay
    2. Setting Relay slot and trigger mode only: ./relay -i5 -c1
    Default:
    Slot of Relay module = 1
    Channel of Relay module = 0
    Help:

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

    Usage: ./relay [OPTIONS]

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

Library:
Relay APIs

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* Relay Sample Application
*
* Date Author Comment
* 08-05-2015 TJ Tai Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char **const argv)
{
int rc, i;
UINT32 relaySlot = 1, slotMin = 0, slotMax = 0;
int relayChannelAmount = 6, relayChannel = 0;
UINT8 arrMode[relayChannelAmount];
UINT8 mode = RELAY_MODE_RELAY;
UINT32 arrTotal[relayChannelAmount];
UINT32 arrCurrent[relayChannelAmount];
UINT32 arru32Val[relayChannelAmount];
UINT32 u32Val = 0;
MX_RTU_Total_Slots_Get(&slotMax);
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 sample program.\n\n");
printf("Usage: ./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);
for(i = 0; i < relayChannelAmount; i++)
{
arrMode[i] = mode;
arrTotal[i] = 0;
arrCurrent[i] = 0;
arru32Val[i] = 0;
}
// Config Relay module
rc = MX_RTU_Module_Relay_Mode_Set(relaySlot, 0, relayChannelAmount, arrMode);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Mode_Set(%d, %d, %d, %d), return code = %d.\n",
relaySlot, 0, relayChannelAmount, mode, rc);
rc = MX_RTU_Module_Relay_Mode_Get(relaySlot, 0, relayChannelAmount, arrMode);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Mode_Get(%d, %d, %d, %d), return code = %d.\n",
relaySlot, 0, relayChannelAmount, mode, rc);
rc = MX_RTU_Module_Relay_Total_Count_Get(relaySlot, 0, relayChannelAmount, arrTotal);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Total_Count_Get(%d, %d, %d), return code = %d.\n",
relaySlot, 0, relayChannelAmount, rc);
rc = MX_RTU_Module_Relay_Current_Count_Get(relaySlot, 0, relayChannelAmount, arrCurrent);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Current_Count_Get(%d, %d, %d), return code = %d.\n",
relaySlot, 0, relayChannelAmount, rc);
printf("Detect Relay Channel Status:\n");
for (i = 0; i < relayChannelAmount; i++) {
printf("Mode[%d] = %d, TotalCount[%d] = %d, CurrentCount[%d] = %d\n", i, arrMode[i], i, arrTotal[i], i, arrCurrent[i]);
}
rc = MX_RTU_Module_Relay_Value_Get(relaySlot, &u32Val);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Value_Get(%d, val), return code = %d.\n",
relaySlot, rc);
if (u32Val != 1) {
printf("relay channel 0 power off\n");
u32Val = 1;
rc = MX_RTU_Module_Relay_Value_Set(relaySlot, u32Val);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Value_Set(%d, %d), return code = %d.\n",
relaySlot, u32Val, rc);
printf("---- power on ----\n");
printf("device should start booting!!!\n");
}
return 0;
}