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

Toggle Switch More...

#include <libmoxa_rtu.h>

Functions

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

Detailed Description

Toggle Switch

Date
04-10-2013
Author
Eddy Kao
Version
V1.0
Introduction:
Changing Toggle Switch to control the DO status, Mode2 off and Mode1 on.
Example:
1. Using default: ./toggle_switch
2. Setting DO: ./toggle_switch -s2 -c1
Default:
DO Slot = 1
DO Channel = 0
Help:
root@Moxa:/tmp#./toggle_switch -h
Toggle Switch program.

Usage: ./toggle_switch [OPTIONS]

Options:
        -s       DO slot [0-9]. Default DO slot = 1
                 (slot 0: Built-in IO, slot 1 ~ 9: IO Module)
        -c       DO channel [0-24]. Default DO channel = 0

Library:
ToggleSwitch APIs

Function Documentation

int main ( int  argc,
char *  argv[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* Toggle Switch
*
* Date Author Comment
* 04-10-2013 Eddy Kao Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char *argv[])
{
int retval = 0;
UINT32 rc = 0;
UINT8 toggleSwitchState = 0;
UINT8 doSlot = 1;
UINT8 doChannel = 0;
UINT32 doValue = 0;
while((retval = getopt(argc, argv, "hs:c:")) != -1)
{
switch(retval)
{
case 's':
doSlot = atoi(optarg);
if(doSlot > MAX_SLOT)
{
printf("Error DO slot = %d\r\n", doSlot);
exit(1);
}
break;
case 'c':
doChannel = atoi(optarg);
if(doChannel > MAX_CHANNEL)
{
printf("Error DO channel = %d\r\n", doChannel);
exit(1);
}
break;
case '?':
case 'h':
default:
printf("Toggle Switch program.\n\n");
printf("Usage: ./toggle_switch [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s DO slot [%d-%d]. Default DO slot = %d\n", "-s", 0, MAX_SLOT, doSlot);
printf("\t%-8s (slot 0: Built-in IO, slot 1 ~ 9: IO Module)\n", "");
printf("\t%-8s DO channel [%d-%d]. Default DO channel = %d\n", "-c", 0, MAX_CHANNEL, doChannel);
printf("\n");
return;
}
}
printf("Please change Toggle Switch to set DO of slot %d and channel %d(mode2: off, mode1: on)\r\n", doSlot, doChannel);
while(1)
{
rc = MX_RTU_Toggle_Switch_Get(&toggleSwitchState);
if(rc != MISC_ERR_OK)
{
printf("MX_RTU_Toggle_Switch_Get(&toggleSwitchState), return code = %d\r\n", rc);
exit(1);
}
if(toggleSwitchState == 0) //Mode 2 -> DO off
{
rc = MX_RTU_Module_DO_Value_Get(doSlot, &doValue);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Get(%d, &doValue), return code = %d\r\n", doSlot, rc);
exit(1);
}
doValue &= ~(1 << doChannel);
rc = MX_RTU_Module_DO_Value_Set(doSlot, doValue);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Set(%d, %x), return code = %d\r\n", doSlot, doValue, rc);
exit(1);
}
}
else if(toggleSwitchState == 1) //Mode 1 -> DO on
{
rc = MX_RTU_Module_DO_Value_Get(doSlot, &doValue);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Get(%d, &doValue), return code = %d\r\n", doSlot, rc);
exit(1);
}
doValue |= (1 << doChannel);
rc = MX_RTU_Module_DO_Value_Set(doSlot, doValue);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Set(%d, %x), return code = %d\r\n", doSlot, doValue, rc);
exit(1);
}
}
sleep(1);
}
return 0;
}