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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
di_freq.c File Reference

DI Frequency Sample More...

`#include <libmoxa_rtu.h>`

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

## Detailed Description

DI Frequency Sample

Copyright
  Moxa Inc.

Date
  07-31-2015

Author
  TJ Tai

Version
  V1.0
  DI Frequency Sample

    Introduction:
    This is DI frequency sample code. The controller will detect DO0 channel PWM output frequency by DI0 channel
    Example:
    1. Using default: ./di_freq
    2. Setting DI slot: ./di -i5 -o6
    Default:
    Slot of DI module = 1
    DI filter = 1
    Slot of DO module = 2
    Help:

    root@Moxa:/tmp#./di_freq -h
    DI frequency sample program.

    Usage: ./di_freq [OPTIONS]

    Options:
        -i      Slot of DI module [1-9]. Default slot = 1
        -f      Filter of DI module [1-2147483647]. Default filter = 1
                (One unit = 100us)
        -o      Slot of DO module [1-9]. Default slot = 2

Library:
DI APIs

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* DI Frequency Sample Application
*
* Date Author Comment
* 07-31-2015 TJ Tai 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;
int diChannelAmount = 16;
int doChannelAmount = 16;
UINT8 start = 0; // given channel 0 only
UINT8 count = 1; // given channel 0 only
UINT8 mode = DI_MODE_FREQUENCY;
UINT32 filter = 1, filterMin = 1, filterMax = 0x7FFFFFFF;
UINT32 u32Val = 0;
UINT32 u32Buf[count];
UINT8 u8Buf[count];
float fBuf[count];
UINT32 freqStart = 0xFFFF; //0x0001;
UINT32 measTime = 1000;
float freq[count], duty[count]; // for DO PWM used
MX_RTU_Total_Slots_Get(&slotMax);
while(-1 != (rc = getopt(argc, argv, "hi:f:o:")))
{
switch(rc)
{
case 'i':
diSlot = atoi(optarg);
if(diSlot < slotMin || diSlot > slotMax)
{
printf("Error parameter: slot: %d\n", diSlot);
return -1;
}
break;
case 'f':
filter = atoi(optarg);
if(filter < filterMin)
filter = filterMin;
if(filter > filterMax)
filter = filterMin;
break;
case 'o':
doSlot = atoi(optarg);
if(doSlot < slotMin || doSlot > slotMax)
{
printf("Error parameter: slot: %d\n", doSlot);
return -1;
}
break;
case '?':
case 'h':
default:
printf("DI frequency sample program.\n\n");
printf("Usage: ./di_freq [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 Filter of DI module [%d-%d]. Default filter = %d\n",
"-f", filterMin, filterMax, filter);
printf("\t%-8s (One unit = 100us)\n", "");
printf("\t%-8s Slot of DO module [%d-%d]. Default slot = %d\n",
"-o", slotMin, slotMax, doSlot);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "DI slot", diSlot);
printf("%-10s: %d\n", "DI filter", filter);
printf("%-10s: %d\n", "DO slot", doSlot);
for(i = 0; i < count; i++)
{
u32Buf[i] = filter;
}
rc = MX_RTU_Module_DI_Filter_Set(diSlot, start, count, u32Buf);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DI_Filter_Set() = %d\r\n", rc);
}
for(i = 0; i < count; i++)
{
u8Buf[i] = mode;
}
rc = MX_RTU_Module_DI_Mode_Set(diSlot, start, count, u8Buf);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DI_Mode_Set() = %d\r\n", rc);
}
for(i = 0; i < count; i++)
{
u32Buf[i] = measTime;
}
rc = MX_RTU_Module_DI_Frequency_Measurement_Time_Set(diSlot, start, count, u32Buf);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DI_Frequency_Measurement_Time_Set() = %d\r\n", rc);
}
rc = MX_RTU_Module_DI_Frequency_Start_Set(diSlot, freqStart);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DI_Frequency_Start_Set() = %d\r\n", rc);
}
rc = MX_RTU_Module_DI_Frequency_Measurement_Time_Get(diSlot, start, count, u32Buf);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DI_Frequency_Measurement_Time_Get() = %d\r\n", rc);
}
// Config DO to PWM mode
for(i = 0; i < count; i++)
{
u8Buf[i] = DO_MODE_PWM;
}
rc = MX_RTU_Module_DO_Mode_Set(doSlot, start, count, u8Buf);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DO_Mode_Set() = %d\r\n", rc);
}
for(i = 0; i < count; i++)
{
u32Buf[i] = 1000;
}
rc = MX_RTU_Module_DO_PWM_Count_Set(doSlot, start, count, u32Buf);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DO_PWM_Count_Set() = %d\r\n", rc);
}
for(i = 0; i < count; i++)
{
freq[i] = 10.0f;
duty[i] = 50.0f;
}
rc = MX_RTU_Module_DO_PWM_Config_Set(doSlot, start, count, freq, duty);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DO_PWM_Config_Set() = %d\r\n", rc);
}
rc = MX_RTU_Module_DO_PWM_Start_Set(doSlot, freqStart);
if (rc != MODULE_RW_ERR_OK) {
printf("MX_RTU_Module_DO_PWM_Start_Set() = %d\r\n", rc);
}
// DI frequency detection
printf("DI frequency detect Start ...\n");
while(1)
{
rc = MX_RTU_Module_DI_Frequency_Get(diSlot, start, count, fBuf);
if(rc != 0)
{
printf("MX_RTU_Module_DI_Frequency_Get() = %d\r\n", rc);
break;
}
for(i = 0; i < count; i++)
{
printf("\rfreq[%2d] = %.2f Hz", start+i, fBuf[i]);
}
fflush(0);
usleep(500000);
}
return 0;
}