ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
Functions
fast_ai_cgi.c File Reference

Fast_AI CGI Sample More...

#include <libmoxa_rtu.h>

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[])
 

Detailed Description

Fast_AI CGI Sample

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

Library:
Fast_AI APIs

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.
*
* Fast_AI CGI Sample
*
* Date Author Comment
* 11-11-2014 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
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;
int setValue = 0;
int ret;
if(cmd == NULL || strcmp(cmd, "") == 0)
{
//printf("Null command.\n");
return -1;
}
sscanf(cmd, "cmd=%d&ch=%d&set=%d", &cmdIdx, &channel, &setValue);
//printf("cmd=%d, ch=%d, set=%d<br>\n", cmdIdx, channel, setValue);
switch(cmdIdx)
{
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;
int ret = 0;
int chAmount = 8;
UINT8 aiSlot = 1, chStart = 0, chCount = 8;
char command[100] = {0};
UINT8 aiRange[chAmount];
UINT8 aiStatus[chAmount];
float aiValue[chAmount];
float aiMin[chAmount];
float aiMax[chAmount];
char *cgiName;
cgiName = getenv("SCRIPT_NAME");
startHtml("Fast AI CGI Sample");
getCgiArg(&aiSlot, &chStart, &chCount, command);
if(0 == execCmd(aiSlot, chStart, chCount, command)) // execute successfully, then reload page
{
printf("<script Language=\"JavaScript\">");
printf("window.location.assign(\"%s?slot=%d&chStart=%d&chCount=%d\");",
cgiName, aiSlot, chStart, chCount);
printf("</script>");
}
else
{
startTable(0, 100);
insertData(15, "left", "block_title", "Fast AI Channel");
insertData(15, "center", "block_title", "Range");
insertData(20, "center", "block_title", "Value");
insertData(20, "center", "block_title", "Min");
insertData(20, "center", "block_title", "Max");
ret = MX_RTU_Module_Fast_AI_Range_Get(aiSlot, chStart, chCount, aiRange);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Range_Get = %d<br>\n", ret);
ret = MX_RTU_Module_Fast_AI_Eng_Value_Get(aiSlot, chStart, chCount, aiValue, NULL);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Eng_Value_Get = %d<br>\n", ret);
ret = MX_RTU_Module_Fast_AI_Status_Get(aiSlot, chStart, chCount, aiStatus);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Status_Get = %d<br>\n", ret);
ret = MX_RTU_Module_Fast_AI_Min_Eng_Get(aiSlot, chStart, chCount, aiMin);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Min_Eng_Get = %d<br>\n", ret);
ret = MX_RTU_Module_Fast_AI_Max_Eng_Get(aiSlot, chStart, chCount, aiMax);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Fast_AI_Max_Eng_Get = %d<br>\n", ret);
for(i = 0; i < chCount; i++)
{
char chMsg[20], rangeMsg[100], valueMsg[20], minMsg[20], maxMsg[20];
char cssClass[20];
if(i % 2)
sprintf(cssClass, "column_title_bg");
else
sprintf(cssClass, "column_title");
sprintf(chMsg, "AI-%02d", chStart+i);
sprintf(minMsg, "%.3f", aiMin[i]);
sprintf(maxMsg, "%.3f", aiMax[i]);
if(aiStatus == AI_STATUS_BURNOUT)
sprintf(valueMsg, "Burn Out");
else
sprintf(valueMsg, "%.3f", aiValue[i]);
sprintf(rangeMsg, "%s", (aiRange[i] == AI_RANGE_10V) ? "0-10V" :
(aiRange[i] == AI_RANGE_0_10V) ? "0-10V" :
(aiRange[i] == AI_RANGE_0_20mA) ? "0-20mA" :
(aiRange[i] == AI_RANGE_4_20mA) ? "4-20mA" : "Unknown");
insertData(15, "left", cssClass, chMsg); // channel
insertData(15, "center", cssClass, rangeMsg); // range
insertData(20, "center", cssClass, valueMsg); // value
insertData(20, "center", cssClass, minMsg); // min
insertData(20, "center", cssClass, maxMsg); // max
}
}
return ret;
}