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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
rotary_switch.c File Reference

Rotary Switch More...

`#include <libmoxa_rtu.h>`

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

## Detailed Description

Rotary Switch

Copyright
  Moxa Inc.

Date
  07-29-2015

Author
  TJ Tai

Version
  V1.0
  rotary switch

    Introduction:
    Changing Rotary Switch to control the DO status, Mode5 off and Mode6 on.
    Example:
    1. Using default: ./rotary_switch
    2. Setting DO slot and channel: ./rotary_switch -s2 -c1
    Default:
    DO Slot = 1
    DO Channel = 0
    Help:

    root@Moxa:/tmp#./rotary_switch -h
    Rotary Switch program.

    Usage: ./rotary_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-15]. Default DO channel = 0

Library:
RotarySwitch APIs

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* Rotary Switch
*
* Date Author Comment
* 07-29-2015 TJ Tai Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char *argv[])
{
int retval = 0;
int i;
UINT32 rc = 0;
UINT8 doSlot = 1;
UINT8 doChannel = 0;
UINT8 doChannelAmount = 16;
UINT8 arrMode[doChannelAmount];
UINT8 numOfOutputChannel = 1;
//UINT32 mode[1];
UINT32 u32Tmp = 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_SLOT)
{
printf("Error DO channel = %d\r\n", doChannel);
exit(1);
}
break;
case '?':
case 'h':
default:
printf("Rotary Switch program.\n\n");
printf("Usage: ./rotary_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 0;
}
}
// config DO
for(i = 0; i < doChannelAmount; i++)
arrMode[i] = DO_MODE_DO;
rc = MX_RTU_Module_DO_Mode_Set(doSlot, 0, doChannelAmount, arrMode);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DO_Mode_Set(%d, %d, %d, %d), return code = %d.\n", doSlot, 0, doChannelAmount, DO_MODE_DO, rc);
while (1) {
UINT32 mode;
// check rotary_switch mode
rc = MX_RTU_Rotary_Switch_Mode_Get(&mode);
if (rc != MISC_ERR_OK ) {
printf("MX_RTU_Rotary_Switch_Mode_Get(&mode), return code = %d\r\n", rc);
}
if (mode == 5) {
// DO0 on
u32Tmp = 0x0001;
rc = MX_RTU_Module_DO_Value_Set(doSlot, u32Tmp);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Set(%d, 0x%04X), return code = %d.\n",
doSlot, u32Tmp, rc);
break;
}
}
else if (mode == 6) {
// DO0 off
u32Tmp = 0x0;
rc = MX_RTU_Module_DO_Value_Set(doSlot, u32Tmp);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Set(%d, 0x%04X), return code = %d.\n",
doSlot, u32Tmp, rc);
break;
}
}
else {
;// do nothing
}
}
return 0;
}