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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
ioPAC8500_tag_do.c File Reference

ioPAC8500 DO TAG Sample More...

`#include "libmoxa_rtu.h"`

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

## Detailed Description

ioPAC8500 DO TAG Sample

Copyright
  Moxa Inc.

Date
  02-21-2014

Author
  TJ Tai

Version
  V1.0
  DO TAG Sample

    Introduction:
    This DO TAG sample code enables the controller to poll channels 0 and 1 for DI values, and then set the DO in 4 modes.
    00: DO OFF
    01: DO PWM Mode start[Pulse setting in RTUxpress only]
    10: DO PWM Mode stop [Pulse setting in RTUxpress only]
    11: DO ON
    Example:
    1. Using default: ./ioPAC8500_tag_do
    2. Setting channel on DO PWM module only: ./ioPAC8500_tag_do -c10
    Help:

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

    Usage: ./ioPAC8500_tag_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 of DO PWM module [0-15]. Default channel = 10
        -p        The amount of PWM count. Default count = 0

Library:
TAG APIs
RTUxpress Project file:
ioPAC8500_tag_do.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.
*
* DO 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;
UINT32 diSlot = 1;
UINT32 doSlot = 2, slotMin = 0, slotMax = 9;
UINT32 u32Val = 0;
UINT32 outMode = 0, lastOutMode = -1;
int diChannelAmount = 2; // only need DI0 and DI1 in this sample
int doChannelAmount = 16;
int doPWMChannel = 10; // default DO PWM mode Channel = 10
int PWMcount = 0; // default DO PWM mode Count = 0;
char di_tagName[diChannelAmount][TAG_MAX_NAME_SIZE];
char do_tagName[doChannelAmount][TAG_MAX_NAME_SIZE];
char do_PWM[2][TAG_MAX_NAME_SIZE];
char buffer[TAG_MAX_NAME_SIZE];
char tagBuf[TAG_MAX_NAME_SIZE];
memset(tagBuf, 0, TAG_MAX_NAME_SIZE);
while(-1 != (rc = getopt(argc, argv, "hi:s:c: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':
doPWMChannel = atoi(optarg);
if(doPWMChannel < 0 || doPWMChannel > doChannelAmount - 1)
{
printf("Error parameter: Channel: %d\n", doPWMChannel);
return -1;
}
break;
case 'p':
PWMcount = atoi(optarg);
if(PWMcount < 0)
{
printf("Error parameter: PWMcount: %d\n", PWMcount);
return -1;
}
break;
case '?':
case 'h':
default:
printf("DO TAG sample program.\n\n");
printf("Usage: ./ioPAC8500_tag_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 of DO PWM module [%d-%d]. Default Channel = %d\n",
"-c", 0, diChannelAmount - 1, 10);
printf("\t%-8s The amount of PWM output count. Default count = %d\n",
"-p", 0);
printf("\n");
return 0;
}
}
printf("%-10s: %d\n", "DI slot", diSlot);
printf("%-10s: %d\n", "DO slot", doSlot);
printf("%-10s: %d\n", "PWMcount", PWMcount);
sprintf(do_PWM[0], "S%d_DO%d_PWMCount", doSlot, doPWMChannel);
sprintf(do_PWM[1], "S%d_DO%d_PWMStartStop", doSlot, doPWMChannel);
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 < diChannelAmount; i++)
{
sprintf(buffer, "S%d_DI%d_DIValue", diSlot, i);
strncpy(di_tagName[i], buffer, sizeof(buffer));
}
// Config DO TAG
for(i = 0; i < doChannelAmount; i++)
{
sprintf(buffer, "S%d_DO%d_DOValue", doSlot, i);
strncpy(do_tagName[i], buffer, sizeof(buffer));
}
UINT32 bitVal[diChannelAmount];
for(i = 0; i < diChannelAmount; i++)
{
bitVal[i] = 0;
}
// Config PWM count value
retval = MX_RTU_Tag_Get_Info(do_PWM[0], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_PWM[0], retval);
return 0;
}
memcpy(tagBuf, &PWMcount, tagInfo.tagSize);
retval = MX_RTU_Tag_Write(tagInfo.tagName, tagBuf, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", tagInfo.tagName, retval);
return 0;
}
while(1)
{
// GetDI
for(i = 0; i < diChannelAmount ; i++)
{
retval = MX_RTU_Tag_Read(di_tagName[i], &bitVal[i], sizeof(bitVal), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", di_tagName[i], retval);
break;
}
}
outMode = bitVal[0] + 2 * bitVal[1];
printf("\rMode: %d", outMode);
fflush(0);
if(outMode == lastOutMode)
continue;
lastOutMode = outMode;
// SetDO
if(outMode == 0)
{
// DO OFF mode
for(i = 0; i < doChannelAmount; i++)
{
retval = MX_RTU_Tag_Get_Info(do_tagName[i], &tagInfo);
if(retval != TAG_ERR_OK && i != doPWMChannel)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_tagName[i], retval);
break;
}
retval = MX_RTU_Tag_Write(do_tagName[i], bitVal, tagInfo.tagSize);
if(retval != TAG_ERR_OK && i != doPWMChannel)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[i], retval);
break;
}
}
}
else if(outMode == 1)
{
// 01: DO PWM Mode start
retval = MX_RTU_Tag_Get_Info(do_PWM[1], &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_PWM[1], retval);
break;
}
retval = MX_RTU_Tag_Write(do_PWM[1], bitVal + 0, tagInfo.tagSize); // write 1 for tag to start
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_PWM[1], retval);
break;
}
}
else if(outMode == 2)
{
// 10: DO PWM Mode stop
retval = MX_RTU_Tag_Write(do_PWM[1], bitVal + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_PWM[1], retval);
break;
}
retval = MX_RTU_Tag_Read(do_PWM[1], &u32Val, sizeof(u32Val), NULL, NULL);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Read(%s) = %d\n", do_PWM[1], retval);
break;
}
}
else if(outMode == 3)
{
// DO ON mode
for(i = 0; i < doChannelAmount; i++)
{
retval = MX_RTU_Tag_Get_Info(do_tagName[i], &tagInfo);
if(retval != TAG_ERR_OK && i != doPWMChannel)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", do_tagName[i], retval);
break;
}
retval = MX_RTU_Tag_Write(do_tagName[i], bitVal + 0, tagInfo.tagSize);
if(retval != TAG_ERR_OK && i != doPWMChannel)
{
printf("MX_RTU_Tag_Write(%s), return code = %d\r\n", do_tagName[i], retval);
break;
}
}
}
else
{
printf("Error DI Value Input.\n");
return 0;
}
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Uninit(), return code = %d\n", retval);
}
return 0;
}