ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
Macros | Functions | Variables
ao_cgi.c File Reference

AO CGI Sample More...

#include <libmoxa_rtu.h>

Macros

#define CMD_SET_RANGE   0
 
#define CMD_SET_VALUE   1
 

Functions

int getAuth (void)
 
void startHtml (char *title)
 
void endHtml (void)
 
void startTable (int border, int width)
 
void endTable (void)
 
void startOneRow (void)
 
void endOneRow (void)
 
void insertData (int width, char *align, char *cssClass, char *data)
 
int execCmd (UINT8 slot, UINT8 chStart, UINT8 chCount, char *cmd)
 
void getCgiArg (UINT8 *pSlot, UINT8 *pChStart, UINT8 *pChCount, char *command)
 
int main (int argc, char *argv[], char *envp[])
 

Variables

char * aoRangeList []
 

Detailed Description

AO CGI Sample

Date
11-11-2014
Author
Wanhan Hsieh
Version
V1.0
ao_cgi.jpg
AO CGI Sample
Introduction:
This is AO CGI sample code. You can monitor and control the AO status and type.
Example:
1. Using default: http://192.168.127.254/cgi-bin/ao.cgi
2. Setting AO slot and channel: http://192.168.127.254/cgi-bin/ao.cgi?slot=2&chStart=0&chCount=4
Default:
Slot of AO module = 1

Library:
AO APIs

Macro Definition Documentation

#define CMD_SET_RANGE   0
#define CMD_SET_VALUE   1

Function Documentation

int getAuth ( void  )
void startHtml ( char *  title)
void endHtml ( void  )
void startTable ( int  border,
int  width 
)
void endTable ( void  )
void startOneRow ( void  )
void endOneRow ( void  )
void insertData ( int  width,
char *  align,
char *  cssClass,
char *  data 
)
int execCmd ( UINT8  slot,
UINT8  chStart,
UINT8  chCount,
char *  cmd 
)
void getCgiArg ( UINT8 pSlot,
UINT8 pChStart,
UINT8 pChCount,
char *  command 
)
int main ( int  argc,
char *  argv[],
char *  envp[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* AO CGI Sample
*
* Date Author Comment
* 11-11-2014 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
#define CMD_SET_RANGE 0
#define CMD_SET_VALUE 1
char *aoRangeList[] =
{
"+/-5V",
"+/-10V",
"0-10V",
"0-20mA",
"4-20mA"
};
int getAuth(void)
{
return setuid(0);
}
void startHtml(char *title)
{
printf("Content-type: text/html; charset=utf-8\n\n");
printf("<html>");
printf("<head>");
printf("<link href=\"../iopac.css\" rel=stylesheet type=text/css>");
printf("<title>%s</title>", title);
printf("<script Language=\"JavaScript\">");
printf("function refreshPage(){window.location.reload();}");
//printf("setTimeout('refreshPage()',3000);"); // refresh page periodically
printf("</script>");
printf("</head>");
printf("<body>\n");
printf("<input type=\"button\" value=\"Refresh page\" onClick=\"refreshPage()\">");
}
void endHtml(void)
{
printf("</body>");
printf("</html>\n");
}
void startTable(int border, int width)
{
printf("<table border=\"%d\" width=\"%d%%\">", border, width);
}
void endTable(void)
{
printf("</table>");
}
void startOneRow(void)
{
printf("<tr>");
}
void endOneRow(void)
{
printf("</tr>");
}
void insertData(int width, char *align, char *cssClass, char *data)
{
printf("<td width=\"%d%%\" align=\"%s\" class=\"%s\">%s</td>", width, align, cssClass, data);
}
int execCmd(UINT8 slot, UINT8 chStart, UINT8 chCount, char *cmd)
{
int cmdIdx = -1;
int channel = 0;
char szSetValue[80] = {0};
int ret;
if(cmd == NULL || strcmp(cmd, "") == 0)
{
//printf("Null command.\n");
return -1;
}
sscanf(cmd, "cmd=%d&ch=%d&set=%s", &cmdIdx, &channel, szSetValue);
//printf("cmd=%d, ch=%d, set=%s<br>\n", cmdIdx, channel, szSetValue);
switch(cmdIdx)
{
{
int i = 0;
UINT8 range = (UINT8) atoi(szSetValue);
ret = MX_RTU_Module_AO_Range_Set(slot, channel, 1, &range);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_AO_Range_Set = %d<br>\n", ret);
break;
}
{
int i = 0;
float value = (float) atof(szSetValue);
ret = MX_RTU_Module_AO_Eng_Value_Set(slot, channel, 1, &value);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_AO_Eng_Value_Set = %d<br>\n", ret);
break;
}
default:
printf("Error command.\n");
break;
}
return 0;
}
void getCgiArg(UINT8 *pSlot, UINT8 *pChStart, UINT8 *pChCount, char *command)
{
char *p;
int slot, chStart, chCount;
char buf[100] = {0};
if((p = getenv("QUERY_STRING")))
{
if(0 == strcmp(p, ""))
{
//printf("Empty QUERY_STRING.\n");
return;
}
}
else
{
//printf("Null QUERY_STRING.\n");
return;
}
sscanf(p, "slot=%d&chStart=%d&chCount=%d&%s", &slot, &chStart, &chCount, buf);
if(buf)
memcpy(command, buf, strlen(buf));
*pSlot = slot;
*pChStart = chStart;
*pChCount = chCount;
}
int main(int argc, char* argv[], char *envp[])
{
int i, j;
int ret = 0;
int chAmount = 4;
UINT8 aoSlot = 1, chStart = 0, chCount = 4;
char command[100] = {0};
UINT8 aoRange[chAmount];
float aoValue[chAmount];
char *cgiName;
cgiName = getenv("SCRIPT_NAME");
startHtml("AO CGI Sample");
getCgiArg(&aoSlot, &chStart, &chCount, command);
if(0 == execCmd(aoSlot, chStart, chCount, command)) // execute successfully, then reload page
{
printf("<script Language=\"JavaScript\">");
printf("window.location.assign(\"%s?slot=%d&chStart=%d&chCount=%d\");",
cgiName, aoSlot, chStart, chCount);
printf("</script>");
}
else
{
startTable(0, 100);
insertData(30, "left", "block_title", "AO Channel");
insertData(35, "center", "block_title", "Sensor Type");
insertData(35, "center", "block_title", "Value");
ret = MX_RTU_Module_AO_Range_Get(aoSlot, chStart, chCount, aoRange);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_AO_Range_Get = %d<br>\n", ret);
ret = MX_RTU_Module_AO_Eng_Value_Get(aoSlot, chStart, chCount, aoValue);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_AO_Eng_Value_Get = %d<br>\n", ret);
for(i = 0; i < chCount; i++)
{
char chMsg[20], valueMsg[20];
char cssClass[20];
float engMin = 0, engMax = 0;
float fTmp;
if(i % 2)
sprintf(cssClass, "column_title_bg");
else
sprintf(cssClass, "column_title");
sprintf(chMsg, "AO-%02d", chStart+i);
sprintf(valueMsg, "%.3f", aoValue[i]);
insertData(30, "left", cssClass, chMsg); // channel
// Range
printf("<td width=\"%d%%\" align=\"%s\" class=\"%s\">", 35, "center", cssClass);
printf("<select onChange=\"location = this.options[this.selectedIndex].value;\">");
printf("<option value=\"#\">%s</option>", aoRangeList[aoRange[i]]);
for(j = 0; j < sizeof(aoRangeList)/sizeof(aoRangeList[0]); j++)
{
if(j == 0) // AO_RANGE_5V: not support
printf("<option value=\"%s?slot=%d&chStart=%d&chCount=%d&cmd=%d&ch=%d&set=%d\" disabled>%s</option>",
cgiName, aoSlot, chStart, chCount, CMD_SET_RANGE, chStart+i, j, aoRangeList[j]);
else
printf("<option value=\"%s?slot=%d&chStart=%d&chCount=%d&cmd=%d&ch=%d&set=%d\">%s</option>",
cgiName, aoSlot, chStart, chCount, CMD_SET_RANGE, chStart+i, j, aoRangeList[j]);
}
printf("</select></td>");
// value
printf("<td width=\"%d%%\" align=\"%s\" class=\"%s\">", 35, "center", cssClass);
printf("<input type=text id=\"%s%d\" size=\"%d\" value=\"%s\" style=\"text-align: right\">", "value", i, 8, valueMsg);
printf("<button onclick=\"javascript:location.href='%s?slot=%d&chStart=%d&chCount=%d&cmd=%d&ch=%d&set='+document.getElementById('%s%d').value\">submit</button>",
cgiName, aoSlot, chStart, chCount, CMD_SET_VALUE, chStart+i, "value", i);
printf("</td>");
}
}
return ret;
}

Variable Documentation

char* aoRangeList[]
Initial value:
=
{
"+/-5V",
"+/-10V",
"0-10V",
"0-20mA",
"4-20mA"
}