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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
ioPAC5542_tag_dio.c File Reference

ioPAC5542 DIO TAG Sample More...

`#include "libmoxa_rtu.h"`

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

## Detailed Description

ioPAC5542 DIO TAG Sample

Copyright
  Moxa Inc.

Date
  02-21-2014

Author
  TJ Tai

Version
  V1.0
  DIO TAG 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: ./ioPAC5542_tag_dio
    Help:

    root@Moxa:/tmp#./ioPAC5542_tag_dio -h
    DIO TAG sample program.

    Usage: ./ioPAC5542_tag_dio

Library:
TAG APIs
RTUxpress Project file:
ioPAC5542_tag_dio.rtu
(Please right click on the link and ‘Save Target As…’ to save RTUxpress project file and open it with RTUxpress utility)

## Function Documentation

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

/*******************************************************************************
* Copyright Moxa Inc.
*
* DIO TAG Sample
*
* Date Author Comment
* 02-21-2014 TJ Tai Created.
******************************************************************************/
#include "libmoxa_rtu.h"
int main(int argc, char * argv[])
{
int rc, i;
TAG_ERR_CODE retval = 0;
char szFileName[80] = "/etc/RTU/config.rtu";
TAG_INFO tagInfo;
int dioChannelAmount = 8;
char dio_tagName[dioChannelAmount][TAG_MAX_NAME_SIZE];
while(-1 != (rc = getopt(argc, argv, "h")))
{
switch(rc)
{
case '?':
case 'h':
default:
printf("DO TAG sample program.\n\n");
printf("Usage: ./ioPAC5542_tag_dio \n\n");
printf("\n");
return 0;
}
}
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init(), return code = %d.\n", retval);
return 0;
}
// Config DI TAG
for(i = 0; i < dioChannelAmount / 2; i++)
{
sprintf(dio_tagName[i], "S0_DIO%d_DIValue", i);
}
// Config DO TAG
for(i = dioChannelAmount / 2; i < dioChannelAmount; i++)
{
sprintf(dio_tagName[i], "S0_DIO%d_DOValue", i);
}
UINT32 bitVal[dioChannelAmount / 2];
for(i = 0; i < dioChannelAmount / 2; i++)
{
bitVal[i] = 0;
}
while(1)
{
// GetDI
for(i = 0; i < dioChannelAmount / 2 ; i++)
{
retval = MX_RTU_Tag_Read(dio_tagName[i], &bitVal[i], sizeof(bitVal), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", dio_tagName[i], retval);
return 0;
}
}
printf("\r DI = DO ");
fflush(0);
// SetDO
for(i = dioChannelAmount / 2; i < dioChannelAmount; i++)
{
retval = MX_RTU_Tag_Get_Info(dio_tagName[i], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", dio_tagName[i], retval);
return 0;
}
retval = MX_RTU_Tag_Write(dio_tagName[i], bitVal + i - 4, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", dio_tagName[i], retval);
return 0;
}
}
sleep(1);
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}