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

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Macros | Functions
tag_utility.c File Reference

TAG Utility More...

`#include <libmoxa_rtu.h>`
`#include <limits.h>`

## Macros
#define | MODE_LIST 0
#define | MODE_READ 1
#define | MODE_WRITE 2

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

## Detailed Description

TAG Utility

Copyright
  Moxa Inc.

Date
  11-14-2013

Author
  Wanhan Hsieh

Version
  V1.0

    Introduction:
    To list all of the tags' infomation and support read/write tag.
    Example:
    1. To list all of the tags' infomation: ./tag_utility -l
    2. Read tag "S0_SR_StationName": ./tag_utility -r -n S0_SR_StationName
    3. Write tag "S4_DO0_DOValue" with "1": ./tag_utility -w -n S4_DO0_DOValue -v 1
    Help:

    root@Moxa:/tmp#./tag_utility -h
    TAG utility.

    Usage: ./tag_utility [OPTIONS]

    Options:
        -l      List mode. Listing all of the tags. (default mode = list mode)
                Ex. ./tag_utility -l
        -r      Read mode. Read a tag. Using with -n.
                Ex. ./tag_utility -r -n TagName
        -w      Write mode. Write a tag. Using with -n, -v.
                Ex. ./tag_utility -w -n TagName -v TagValue
        -n NAME Tag name.
        -v VALUE Tag value.
        -t Print timestamp.

Library:
TAG APIs

## Macro Definition Documentation

#define MODE_LIST 0

#define MODE_READ 1

#define MODE_WRITE 2

## Function Documentation

void usage | ( | void |  | ) |

/*******************************************************************************
* Copyright Moxa Inc.
*
* TAG Read/write Application
*
* Date Author Comment
* 11-14-2013 Wanhan Hsieh Created.
* 08-14-2014 TJ Tai Updated.
******************************************************************************/
#include <libmoxa_rtu.h>
#include <limits.h>
#define MODE_LIST 0
#define MODE_READ 1
#define MODE_WRITE 2
/*******************************************************************************
*
* Sample code for TAG read/write
*
******************************************************************************/
void usage(void)
{
printf("%s.\n\n", "TAG utility");
printf("Usage: ./tag_utility [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s List mode. Listing all of the tags. (default mode = list mode)\n", "-l");
printf("\t Ex. ./tag_utility -l\n");
printf("\t%-8s Read mode. Read a tag. Using with -n.\n", "-r");
printf("\t Ex. ./tag_utility -r -n TagName\n");
printf("\t%-8s Write mode. Write a tag. Using with -n, -v.\n", "-w");
printf("\t Ex. ./tag_utility -w -n TagName -v TagValue\n");
printf("\t%-8s Tag name.\n", "-n NAME");
printf("\t%-8s Tag value.\n", "-v VALUE");
printf("\t%-8s Print timestamp.\n", "-t");
printf("\n");
}
int main(int argc, char **const argv)
{
int rc;
TAG_ERR_CODE retval = 0;
int mode = MODE_LIST;
char szFileName[80] = "/etc/RTU/config.rtu";
int isNameSet = 0;
char szTagName[TAG_MAX_NAME_SIZE];
int isValueSet = 0;
char szTagValue[TAG_MAX_NAME_SIZE];
TAG_INFO tagInfo;
int i;
char ch;
char printTimestamp = 0;
struct Timestamp timestamp;
while(-1 != (rc = getopt(argc, argv, "lwrn:v:th")))
{
switch(rc)
{
case 'l':
mode = MODE_LIST;
break;
case 'r':
mode = MODE_READ;
break;
case 'w':
mode = MODE_WRITE;
break;
case 'n':
strcpy(szTagName, optarg);
isNameSet = 1;
break;
case 'v':
if (strlen(optarg) > 63) {
printf("Wrong input string length: %d\n", strlen(optarg));
return 0;
}
strcpy(szTagValue, optarg);
isValueSet = 1;
break;
case 't':
printTimestamp = 1;
break;
case 'h':
case '?':
default:
usage();
return 0;
}
}
retval = MX_RTU_Tag_Init();
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Init(), return code = %d.\n", retval);
return 0;
}
switch(mode)
{
case MODE_LIST:
MX_RTU_Tag_List_All();
break;
case MODE_READ:
{
char szVal[TAG_MAX_NAME_SIZE];
if(!isNameSet)
{
printf("no tag name, please use \"-n\".\n");
break;
}
retval = MX_RTU_Tag_Get_Info(szTagName, &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", szTagName, retval);
break;
}
memset(&timestamp, 0, sizeof(timestamp));
switch(tagInfo.dataType)
{
case TAG_DATA_TYPE_COIL:
{
UINT8 val = 0;
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
sprintf(szVal, "%u", val);
break;
}
case TAG_DATA_TYPE_INT8:
{
INT8 val = 0;
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
int val2 = val;
if (val2 > SCHAR_MAX) { // convert signed char to int
val2 -= 256;
}
sprintf(szVal, "%d", val2);
break;
}
case TAG_DATA_TYPE_UINT8:
{
UINT8 val = 0;
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
sprintf(szVal, "%u", val);
break;
}
case TAG_DATA_TYPE_INT16:
{
INT16 val = 0;
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
sprintf(szVal, "%d", val);
break;
}
case TAG_DATA_TYPE_UINT16:
{
UINT16 val = 0;
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
sprintf(szVal, "%u", val);
break;
}
case TAG_DATA_TYPE_INT32:
{
INT32 val = 0;
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
sprintf(szVal, "%ld", val);
break;
}
case TAG_DATA_TYPE_UINT32:
{
UINT32 val = 0;
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
sprintf(szVal, "%lu", val);
break;
}
case TAG_DATA_TYPE_FLOAT:
{
float val = 0;
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
sprintf(szVal, "%.3f", val);
break;
}
case TAG_DATA_TYPE_STR:
{
char val[64];
memset(val, 0 , 64);
retval = MX_RTU_Tag_Read(tagInfo.tagName, &val, tagInfo.tagSize, NULL, &timestamp);
sprintf(szVal, "%s", val);
break;
}
}
if(retval == TAG_ERR_OK)
{
if(printTimestamp)
printf("%s = %s, timestamp = %d.%d.%d-%d:%d:%d.%d\n", tagInfo.tagName, szVal, timestamp.year, timestamp.mon, timestamp.day, timestamp.hour, timestamp.min, timestamp.sec, timestamp.msec);
else
printf("%s = %s\n", tagInfo.tagName, szVal);
}
else
printf("MX_RTU_Tag_Read(%s), return code = %d\r\n", tagInfo.tagName, retval);
break;
}
case MODE_WRITE:
{
char tagBuf[TAG_MAX_NAME_SIZE];
char *endptr;
int valChk = 0;
errno = 0;
if(!isNameSet)
{
printf("no tag name, please use \"-n\".\n");
break;
}
if(!isValueSet)
{
printf("no tag value, please use \"-v\".\n");
break;
}
retval = MX_RTU_Tag_Get_Info(szTagName, &tagInfo);
if(retval != TAG_ERR_OK)
{
printf("MX_RTU_Tag_Get_Info(%s), return code = %d.\n", szTagName, retval);
break;
}
memset(tagBuf, 0, TAG_MAX_NAME_SIZE);
switch(tagInfo.dataType)
{
case TAG_DATA_TYPE_COIL:
{
unsigned int data = strtoul(szTagValue, &endptr, 10);
if (errno != 0) valChk = -1;
else if (*szTagValue == '-') valChk = -1;
else if (*endptr !='\0') valChk = -1;
else if (data < 0 || data > 1) valChk = -1;
if (valChk == -1) {
printf("Wrong input value\n");
return 0;
}
else {
memcpy(tagBuf, &data, tagInfo.tagSize);
}
break;
}
case TAG_DATA_TYPE_UINT8:
{
unsigned int data = strtoul(szTagValue, &endptr, 10);
if (errno != 0) valChk = -1;
else if (*szTagValue == '-') valChk = -1;
else if (*endptr !='\0') valChk = -1;
else if (data < 0 || data > UCHAR_MAX) valChk = -1;
if (valChk == -1) {
printf("Wrong input value\n");
return 0;
}
else {
memcpy(tagBuf, &data, tagInfo.tagSize);
}
break;
}
case TAG_DATA_TYPE_UINT16:
{
unsigned int data = strtoul(szTagValue, &endptr, 10);
if (errno != 0) valChk = -1;
else if (*szTagValue == '-') valChk = -1;
else if (*endptr !='\0') valChk = -1;
else if (data < 0 || data > USHRT_MAX) valChk = -1;
if (valChk == -1) {
printf("Wrong input value\n");
return 0;
}
else {
memcpy(tagBuf, &data, tagInfo.tagSize);
}
break;
}
case TAG_DATA_TYPE_UINT32:
{
unsigned long data = strtoul(szTagValue, &endptr, 10);
if (errno != 0) valChk = -1;
else if (*szTagValue == '-') valChk = -1;
else if (*endptr !='\0') valChk = -1;
else if (data < 0 || data > ULONG_MAX) valChk = -1;
if (valChk == -1) {
printf("Wrong input value\n");
return 0;
}
else {
memcpy(tagBuf, &data, tagInfo.tagSize);
}
break;
}
case TAG_DATA_TYPE_INT8:
{
int data = strtol(szTagValue, &endptr, 10);
if (errno != 0) valChk = -1;
else if (*endptr !='\0') valChk = -1;
else if (data < SCHAR_MIN || data > SCHAR_MAX) valChk = -1;
if (valChk == -1) {
printf("Wrong input value\n");
return 0;
}
else {
memcpy(tagBuf, &data, tagInfo.tagSize);
}
break;
}
case TAG_DATA_TYPE_INT16:
{
int data = strtol(szTagValue, &endptr, 10);
if (errno != 0) valChk = -1;
else if (*endptr !='\0') valChk = -1;
else if (data < SHRT_MIN || data > SHRT_MAX) valChk = -1;
if (valChk == -1) {
printf("Wrong input value\n");
return 0;
}
else {
memcpy(tagBuf, &data, tagInfo.tagSize);
}
break;
}
case TAG_DATA_TYPE_INT32:
{
long data = strtol(szTagValue, &endptr, 10);
if (errno != 0) valChk = -1;
else if (*endptr !='\0') valChk = -1;
else if (data < LONG_MIN || data > LONG_MAX) valChk = -1;
if (valChk == -1) {
printf("Wrong input value\n");
return 0;
}
else {
memcpy(tagBuf, &data, tagInfo.tagSize);
}
break;
}
case TAG_DATA_TYPE_FLOAT:
{
float fData = strtof(szTagValue, &endptr);
if (errno != 0) valChk = -1;
else if (*endptr !='\0') valChk = -1;
if (valChk == -1) {
printf("Wrong input value\n");
return 0;
}
else {
memcpy(tagBuf, &fData, tagInfo.tagSize);
}
break;
}
case TAG_DATA_TYPE_STR:
{
memcpy(tagBuf, szTagValue, strlen(szTagValue));
break;
}
}
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);
break;
}
default:
printf("unknown command.\n");
break;
}
retval = MX_RTU_Tag_Uninit();
if(retval != TAG_ERR_OK)
printf("MX_RTU_Tag_Uninit() = %d\n", retval);
return 0;
}

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