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

Relay CGI Sample More...

#include <libmoxa_rtu.h>

Macros

#define CMD_SET_RELAY_VAULE   0
 

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

Relay CGI Sample

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

Library:
Relay APIs

Macro Definition Documentation

#define CMD_SET_RELAY_VAULE   0

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.
*
* Relay CGI Sample
*
* Date Author Comment
* 11-11-2014 Wanhan Hsieh Created.
******************************************************************************/
#include <libmoxa_rtu.h>
#define CMD_SET_RELAY_VAULE 0
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)
{
{
UINT32 relayValues = 0;
ret = MX_RTU_Module_Relay_Value_Get(slot, &relayValues);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Value_Get = %d<br>\n", ret);
relayValues &= ~(0x1 << channel);
relayValues |= (setValue << channel);
ret = MX_RTU_Module_Relay_Value_Set(slot, relayValues);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_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;
int ret = 0;
int chAmount = 6;
UINT8 relaySlot = 1, chStart = 0, chCount = 6;
char command[100] = {0};
UINT8 relayMode[chAmount];
UINT32 relayValue = 0;
char *cgiName;
cgiName = getenv("SCRIPT_NAME");
startHtml("Relay CGI Sample");
getCgiArg(&relaySlot, &chStart, &chCount, command);
if(0 == execCmd(relaySlot, chStart, chCount, command)) // execute successfully, then reload page
{
printf("<script Language=\"JavaScript\">");
printf("window.location.assign(\"%s?slot=%d&chStart=%d&chCount=%d\");",
cgiName, relaySlot, chStart, chCount);
printf("</script>");
}
else
{
startTable(0, 100);
insertData(15, "left", "block_title", "Relay Channel");
insertData(15, "center", "block_title", "Mode");
insertData(15, "center", "block_title", "Status");
ret = MX_RTU_Module_Relay_Mode_Get(relaySlot, chStart, chCount, relayMode);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Mode_Get = %d<br>\n", ret);
ret = MX_RTU_Module_Relay_Value_Get(relaySlot, &relayValue);
if(ret != MODULE_RW_ERR_OK)
printf("MX_RTU_Module_Relay_Value_Get = %d<br>\n", ret);
for(i = 0; i < chCount; i++)
{
int chValue = ((relayValue >> (chStart+i)) & 0x1) ? 1 : 0;
char chMsg[20], modeMsg[100], statusMsg[100];
char cssClass[20];
if(i % 2)
sprintf(cssClass, "column_title_bg");
else
sprintf(cssClass, "column_title");
sprintf(chMsg, "Relay-%02d", chStart+i);
if(relayMode[i] == RELAY_MODE_RELAY)
sprintf(modeMsg, "Relay");
else
sprintf(modeMsg, "PWM");
sprintf(statusMsg, "<a href=\"%s?slot=%d&chStart=%d&chCount=%d&cmd=%d&ch=%d&set=%d\">%s</a>",
cgiName, relaySlot, chStart, chCount, CMD_SET_RELAY_VAULE, chStart+i, (chValue) ? 0 : 1, (chValue) ? "ON" : "OFF");
insertData(15, "left", cssClass, chMsg); // channel
insertData(15, "center", cssClass, modeMsg); // mode
insertData(15, "center", cssClass, statusMsg); // status
}
}
return ret;
}