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

DIO Sample More...

#include <libmoxa_rtu.h>

Functions

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

Detailed Description

DIO Sample

Date
06-07-2013
Author
Changfu Hsieh
Version
V1.0
dio.jpg
DIO Sample
Introduction:
This DIO sample code sets the direction of DIO(0-3) to DI and DIO(4-7) to DO,
and it reads DIO(0-3) status and output the status to DIO(4-7).
Therefore, if you change the DIO(0) status to false, the output of DIO(4) would be false as well.
The polling delay is 1 second.
Example:
1. Using default: ./dio
Default:
Slot = 0
Help:
root@Moxa:/home#./dio -h
DIO sample program.

Usage: ./dio [OPTIONS]

Options:
 -s       Slot of DIO module [0-0]. Default slot = 0

Library:
DIO APIs

Function Documentation

int main ( int  argc,
char **const  argv 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* DIO Sample Application
*
* Date Author Comment
* 06-07-2013 Changfu Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char **const argv)
{
INT32 rc;
UINT32 idx;
UINT32 dioSlot = 0;
UINT32 slotMin = 0, slotMax = 9;
UINT32 dioMap;
UINT8 chMode[4];
UINT32 diValue;
UINT32 doValue;
struct Timestamp timeStamp;
while(-1 != (rc = getopt(argc, argv, "hs:")))
{
switch(rc)
{
case 's':
dioSlot = atoi(optarg);
if(dioSlot < slotMin || dioSlot > slotMax)
{
printf("Error parameter: slot: %d\n", dioSlot);
return -1;
}
break;
case '?':
case 'h':
default:
printf("DIO sample program.\n\n");
printf("Usage: ./dio [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DIO module [%d-%d]. Default slot = %d\n",
"-s", slotMin, slotMax, dioSlot);
printf("\n");
return;
}
}
//Set DIO direction
dioMap = 0xf0;//0-3(DI), 4-7(DO)
rc = MX_RTU_Module_DIO_Map_Set(dioSlot, dioMap);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DIO_Map_Set err:%d\n", rc);
return -1;
}
//Set DI channel to DI mode
for(idx = 0; idx < 4; idx++)
{
chMode[idx] = DI_MODE_DI;
}
rc = MX_RTU_Module_DIO_DI_Mode_Set(dioSlot, 0, 4, chMode);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DIO_DI_Mode_Set err:%d\n", rc);
return -1;
}
//Set DO channel to DO mode
for(idx = 0; idx < 4; idx++)
{
chMode[idx] = DO_MODE_DO;
}
rc = MX_RTU_Module_DIO_DO_Mode_Set(dioSlot, 4, 4, chMode);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DIO_DI_Mode_Set err:%d\n", rc);
return -1;
}
//Read DI channels and output the result to DO channels
while(1)
{
rc = MX_RTU_Module_DIO_DI_Value_Get(dioSlot, &diValue, &timeStamp);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DIO_DI_Value_Get err:%d\n", rc);
return -1;
}
doValue = (diValue & 0x0f) << 4;
rc = MX_RTU_Module_DIO_DO_Value_Set(dioSlot, doValue);
if(rc != MODULE_RW_ERR_OK)
{
printf("MX_RTU_Module_DIO_DO_Value_Set err:%d\n", rc);
return -1;
}
for(idx = 0; idx < 4; idx++)
{
printf("DIO[%d]DI:%d ", idx, (diValue >> idx) & 0x1);
}
printf("\n");
for(idx = 4; idx < 8; idx++)
{
printf("DIO[%d]DO:%d ", idx, (doValue >> idx) & 0x1);
}
printf("\n\n");
sleep(1);
}
return 0;
}