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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
do.c File Reference

DO Sample More...

`#include <libmoxa_rtu.h>`

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

## Detailed Description

DO Sample

Copyright
  Moxa Inc.

Date
  04-16-2013

Author
  Wanhan Hsieh

Version
  V1.0
  DO Sample

    Introduction:
    This DO sample code enables the controller to poll channels 0 and 1 for DI values, and then set the DO in 4 modes.
    Example:
    1. Using default: ./do
    2. Setting channel on DO module only: ./do -c10
    Default:
    Slot of DI module = 1
    Slot of DO module = 2
    Channel on DO module = 0
    The amount of output channels = 1
    The amount of PWM output count = 0
    Help:

    root@Moxa:/tmp#./do -h
    DO sample program.

    Usage: ./do [OPTIONS]

    Options:
        -i      Slot of DI module [1-9]. Default slot = 1
        -s      Slot of DO module [1-9]. Default slot = 2
        -c      Channel on DO module [0-15]. Default channel = 0
        -n      The amount of output channels. Default count = 1
        -p      The amount of PWM output count. Default count = 0

Library:
DO APIs

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* DO Sample Application
*
* Date Author Comment
* 04-16-2013 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char **const argv)
{
int rc, i;
UINT32 diSlot = 1;
UINT32 doSlot = 2, slotMin = 0, slotMax = 0;
UINT32 doChannel = 0;
int diChannelAmount = 16;
int doChannelAmount = 16;
UINT32 pwmFlags = 0x1 << doChannel;
UINT8 arrMode[diChannelAmount];
UINT8 mode = DO_MODE_PWM;
UINT32 arru32Val[diChannelAmount];
UINT32 u32DIVal = 0;
UINT32 outMode = 0, lastOutMode = -1;
UINT32 u32Tmp = 0;
float arrfPwmFreq[doChannelAmount];
float arrfPwmDuty[doChannelAmount];
UINT32 u32PwmCnt = 0;
UINT32 arru32PwmCnt[doChannelAmount];
UINT8 numOfOutputChannel = 1;
MX_RTU_Total_Slots_Get(&slotMax);
while(-1 != (rc = getopt(argc, argv, "hi:s:c:n:p:")))
{
switch(rc)
{
case 'i':
diSlot = atoi(optarg);
if(diSlot < slotMin || diSlot > slotMax)
{
printf("Error parameter: slot: %d\n", diSlot);
return -1;
}
break;
case 's':
doSlot = atoi(optarg);
if(doSlot < slotMin || doSlot > slotMax)
{
printf("Error parameter: slot: %d\n", doSlot);
return -1;
}
break;
case 'c':
doChannel = atoi(optarg);
if(doChannel < 0 || doChannel >= doChannelAmount)
doChannel = 0;
break;
case 'n':
numOfOutputChannel = atoi(optarg);
if(numOfOutputChannel < 1 || numOfOutputChannel > doChannelAmount)
numOfOutputChannel = 1;
break;
case 'p':
u32PwmCnt = atoi(optarg);
break;
case '?':
case 'h':
default:
printf("DO sample program.\n\n");
printf("Usage: ./do [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DI module [%d-%d]. Default slot = %d\n",
"-i", slotMin, slotMax, diSlot);
printf("\t%-8s Slot of DO module [%d-%d]. Default slot = %d\n",
"-s", slotMin, slotMax, doSlot);
printf("\t%-8s Channel on DO module [%d-%d]. Default channel = %d\n",
"-c", 0, doChannelAmount - 1, doChannel);
printf("\t%-8s The amount of output channels. Default count = %d\n",
"-n", 1, doChannelAmount, numOfOutputChannel);
printf("\t%-8s The amount of PWM output count. Default count = %d\n",
"-p", 0, doChannelAmount - 1, u32PwmCnt);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "DI slot", diSlot);
printf("%-10s: %d\n", "DO slot", doSlot);
printf("%-10s: %d\n", "DO channel", doChannel);
printf("%s: %d\n", "Output channel amount", numOfOutputChannel);
printf("%s: %d\n", "PWM output count", u32PwmCnt);
for(i = 0; i < diChannelAmount; i++)
arrMode[i] = DI_MODE_DI;
for(i = 0; i < doChannelAmount; i++)
{
arru32PwmCnt[i] = u32PwmCnt;
}
pwmFlags = 0;
for(i = 0; i < numOfOutputChannel; i++)
{
pwmFlags |= (0x1 << (doChannel + i));
}
// Config DI module
rc = MX_RTU_Module_DI_Mode_Set(diSlot, 0, diChannelAmount, arrMode);
if(rc != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_DI_Mode_Set(%d, %d, %d, %d), return code = %d.\n",
diSlot, 0, diChannelAmount, mode, rc);
// Config DO module
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);
// Start to polling DI
while(1)
{
rc = MX_RTU_Module_DI_Value_Get(diSlot, &u32DIVal, NULL);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DI_Value_Get(%d), return code = %d.\n", diSlot, rc);
break;
}
outMode = u32DIVal & 0x3;
printf("\rMode: %d", outMode);
if(outMode == lastOutMode)
continue;
lastOutMode = outMode;
// Switch DO mode
switch(outMode)
{
case 0:
mode = DO_MODE_DO;
u32Tmp = 0; // OFF
for(i = 0; i < numOfOutputChannel; i++)
arrMode[i] = DO_MODE_DO;
break;
case 1:
mode = DO_MODE_PWM;
u32Tmp = 0;
for(i = 0; i < numOfOutputChannel; i++)
{
arrMode[i] = DO_MODE_PWM;
arrfPwmFreq[i] = 10; // Hz
arrfPwmDuty[i] = 50; // ON duty cycle (%)
}
break;
case 2:
mode = DO_MODE_PWM;
u32Tmp = 0;
for(i = 0; i < numOfOutputChannel; i++)
{
arrMode[i] = DO_MODE_PWM;
arrfPwmFreq[i] = 100; // Hz
arrfPwmDuty[i] = 25; // ON duty cycle (%)
}
break;
case 3:
mode = DO_MODE_DO;
u32Tmp = 1; // ON
for(i = 0; i < numOfOutputChannel; i++)
arrMode[i] = DO_MODE_DO;
break;
default:
mode = DO_MODE_DO;
u32Tmp = 0;
break;
}
if(mode != DO_MODE_PWM)
{
rc = MX_RTU_Module_DO_PWM_Stop_Set(doSlot, pwmFlags);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_PWM_Stop_Set(%d, 0x%04X), return code = %d.\n",
doSlot, pwmFlags, rc);
break;
}
}
rc = MX_RTU_Module_DO_Mode_Set(doSlot, doChannel, numOfOutputChannel, arrMode);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Mode_Set(%d, %d, %d), return code = %d.\n",
doSlot, doChannel, numOfOutputChannel, rc);
break;
}
if(u32Tmp)
{
rc = MX_RTU_Module_DO_Value_Set(doSlot, pwmFlags);
}
else
{
rc = MX_RTU_Module_DO_Value_Set(doSlot, 0);
}
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_Value_Set(%d, 0x%04X), return code = %d.\n",
doSlot, u32Tmp, rc);
break;
}
if(mode == DO_MODE_PWM)
{
rc = MX_RTU_Module_DO_PWM_Config_Set(doSlot, doChannel, numOfOutputChannel, arrfPwmFreq, arrfPwmDuty);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_PWM_Config_Set(%d, %d, %d), return code = %d.\n",
doSlot, doChannel, numOfOutputChannel, rc);
break;
}
rc = MX_RTU_Module_DO_PWM_Count_Set(doSlot, doChannel, numOfOutputChannel, arru32PwmCnt);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_PWM_Count_Set(%d, %d, %d), return code = %d.\n",
doSlot, doChannel, numOfOutputChannel, rc);
break;
}
rc = MX_RTU_Module_DO_PWM_Start_Set(doSlot, pwmFlags);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DO_PWM_Start_Set(%d, 0x%04X), return code = %d.\n",
doSlot, pwmFlags, rc);
break;
}
}
}
return 0;
}