Thông báo

Collapse
No announcement yet.

Thay đổi port trên giao diện web điều khiển

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Thay đổi port trên giao diện web điều khiển

    Hiện tại mình đang làm đồ án " Điều khiển thiết bị qua TCP/IP". Mọi thứ thì đã hoàn tất hết, nhưng có một yêu cầu mà mình không hiểu và cũng không biết cách làm mong nhận được sự giúp đỡ!
    Yêu cầu là trên giao diện web điều khiển, hãy tạo nút thay đổi port trực tiếp trên đó.(thường thì vào sửa code trước khi nạp vào IC là được, nhưng yêu cầu là sau khi đã chạy chương trình và đang chạy chương trình thì đổi port trực tiếp trên giao diện web).
    Đây là code:
    Code:
    //quick auto configure for different tcp/ip demo boards
    #define STACK_USE_CCS_PICNET        0  //PICNET rev 0-2 board sold by CCS.  Has a 56K modem and a 10BaseT Network IC and 18F6620
    #define STACK_USE_CCS_PICNET3       0  //PICNET rv 3 and up board sold by CCS.  Has a 56K modem and a 10BaseT Network IC and 18F8622
    #define STACK_USE_CCS_PICENS        1  //CCS PICENS (Embedded Ethernet) development kit.  18F4620 + ENC28J60
    #define STACK_USE_CCS_PICEEC        0  //CCS PICEEC (Embedded Ethernet) development kit.  18F67J60
    #define STACK_USE_CCS_EWL5V         0  //CCS 5V EZ Web Lynx Device
    #define STACK_USE_CCS_EWL3V         0  //CCS 3.3V EZ Web Lynx Device
    
    //You must set one (and only) one of these to TRUE to tell the stack what hardware layer you are using
    #define STACK_USE_MAC   1      //use the nic card
    #define STACK_USE_PPP   0      //use PPP on modem
    #define STACK_USE_SLIP  0      //use slip (NOT TESTED)
    
    /// *** END USER CONFIGURABLE DEFINITIONS
    
    #if STACK_USE_CCS_EWL5V || STACK_USE_CCS_EWL3V
       #define STACK_USE_CCS_EWL  1
    #else
       #define STACK_USE_CCS_EWL  0
    #endif
    
    #if (STACK_USE_CCS_PICENS+STACK_USE_CCS_PICNET3+STACK_USE_CCS_PICNET+STACK_USE_CCS_PICEEC+STACK_USE_CCS_EWL5V+STACK_USE_CCS_EWL3V >1)
     #error You can only use one hardware definition
    #endif
    
    #if (STACK_USE_CCS_PICENS+STACK_USE_CCS_PICNET3+STACK_USE_CCS_PICNET+STACK_USE_CCS_PICEEC+STACK_USE_CCS_EWL5V+STACK_USE_CCS_EWL3V==0)
     #error You must define one hardware definition
    #endif
    
    #if STACK_USE_CCS_PICNET3
     #undef STACK_USE_CCS_PICNET
     #define STACK_USE_CCS_PICNET 1
    #endif
    
    #if STACK_USE_CCS_PICNET3
     #include <18f6722.h>
     #use delay(clock=40000000)
     #fuses H4
     #fuses CCP2C1
     #fuses MCLR
     #fuses NOPROTECT
    #elif STACK_USE_CCS_PICNET
     #include <18f6720.h>
     #use delay(clock=20000000) //CCS picnet is 20Mhz
     #fuses HS
     #fuses CCP2C1
     #fuses NOPROTECT
    #elif STACK_USE_CCS_PICENS
     #include <18f4620.h>
     #use delay(clock=20000000)
     #fuses HS
     //#include<1wire.c>
     //#include<ds1820.c>
     #fuses NOFCMEN
     #fuses NOXINST
     #fuses NOIESO
     #fuses NOPBADEN
     #fuses MCLR
     #fuses NOPROTECT
    #elif STACK_USE_CCS_PICEEC||STACK_USE_CCS_EWL3V
     #include <18F67J60.H>
     #use delay(clock=25M)
     #fuses HS
     #fuses NOIESO
     #fuses NOFCMEN
     #fuses PRIMARY
     #fuses ETHLED
     #fuses NOPROTECT
    #elif STACK_USE_CCS_EWL5V
     #include <18F2685.H>
     #use delay(clock=25M)
     #fuses HS
     #fuses NOPBADEN
     #fuses MCLR
    #endif
    
    #fuses NOWDT
    #fuses NODEBUG
    #fuses NOSTVREN
    
    #if !(STACK_USE_CCS_PICEEC||STACK_USE_CCS_EWL3V)
     #fuses PUT
     #fuses NOLVP
     #fuses NOBROWNOUT
     #fuses NOCPD
     #fuses WRT
     #fuses NOWRTD
     #fuses NOEBTR
     #fuses NOCPB
     #fuses NOEBTRB
     #fuses WRTB
    #endif
    
    #define STACK_USE_SERIAL         TRUE
    
    //set to true to put out information on the LCD screen
    #if STACK_USE_CCS_EWL
     //due to limited IO space on the ezweblynx, we will not use LCD
     #define STACK_USE_PICDEM_LCD     FALSE
    #else
     #define STACK_USE_PICDEM_LCD     TRUE
    #endif
    
    
    #if STACK_USE_MAC
     #define STACK_USE_DHCP       TRUE  //for auto resolution of IP address
     #define STACK_USE_ARP        TRUE  //needed to resolve MAC addresses of IP addresses
     #define STACK_USE_UDP        TRUE  //needed for dhcp and announce
     #define STACK_USE_ANNOUNCE   TRUE  //announce our IP periodically to the network
    #endif
    
    #if (STACK_USE_CCS_PICENS||STACK_USE_CCS_EWL5V) && STACK_USE_MAC
     #define STACK_USE_MCPENC  TRUE     //tell the stack to use the ENC28J60 driver
    #else
     #define STACK_USE_MCPENC  FALSE    //tell the stack to use the Realtek driver
    #endif
    
    #if (STACK_USE_CCS_PICEEC||STACK_USE_CCS_EWL3V) && STACK_USE_MAC
     #define STACK_USE_MCPINC  TRUE
    #else
     #define STACK_USE_MCPINC  FALSE
    #endif
    
    //use a ds1631 temperature sensor.  Note that on the EZ Web Lynx,
    //the temperature sensor's i2c lines are on B6/B7.  Remove the ICD cable
    //or the sensor won't function.
    #if STACK_USE_CCS_EWL
     #define STACK_USE_TEMP    TRUE
    #endif
    
    
    #define STACK_USE_ICMP        TRUE   //Enabled for ping support
    #define STACK_USE_TCP         TRUE   //To send TCP packets (talk to HTTP clients)
    #define STACK_USE_HTTP        TRUE   //Enable the CCS webserver
    #define STACK_USE_IP_GLEANING FALSE  //Disable Microchip's propietary auto-IP configuration
    
    #include "tcpip/stacktsk.c"    //include Microchip's TCP/IP stack
    
    #include "ccshelper.c"   //include CCS's PICDEM.net/PICNET helper functions
    #include "ccshelperui.c"
    
    /////***** WEB PAGES STORED INTO PROGRAM MEMORY *****/////
    
    //The following escape characters are used:
    //  %0 - AN0
    //  %1 - AN1
    //  %Q - AN2
    //  %W - AN3
    //  %E - AN4
    //  %2 - BUTTON0
    //  %3 - BUTTON1
    //  %4 - Current LCD Message
    //  %5 - CHECKED if LED1 is ON
    //  %6 - CHECKED if LED1 is OFF
    //  %7 - CHECKED if LED2 is ON
    //  %8 - CHECKED if LED2 is OFF
    //  %9 - Temperature (EWL ONLY)
    //  %F - Message stack
    
    /////***** WEB PAGES STORED INTO PROGRAM MEMORY *****/////
    
    const char  HTML_INDEX_PAGE[]=
    "<html><title>DIEU KHIEN QUA MANG</title>
    <head>
    <script type=\"text/javascript\">
    function varify(){
    var youracct = \"admin\"; //define your username here
    var yourpw = \"123\"; //define your password here
    
    if(document.getElementById(\"acct\").value != youracct || document.getElementById(\"pw\").value != yourpw){
    alert(\"User or Pass incorrect\");
    return false;
    }
    window.location=\"/main\";
    return true;
    }
    </script>
    </head>
    <center><body bgcolor=#FFFFF5 text=#000000>
    <img src= \"file:///c:/home.jpg\">
    <br/>
    <h1 style=\"color:#FF0000\">ĐỒ ÁN 2</h1>
    <h2 style=\"color:#00FF00\">ĐIỀU KHIỂN THIẾT BỊ QUA  INTERNET <h2>
    <br/>
    <img src= \"file:///c:/login.gif\">
    <br/>
    User name  <input id=\"acct\" name=\"acct\" type=textarea value=\"\" /><br />
    Password   <input id=\"pw\" name=\"pw\" type=password value=\"\" /><br /><br />
    <input type=button value=\"BẮT ĐẦU\" onclick=\"varify()\"/><br />
    <span id=\"mesg\"></span>
    </body></center>
    </html>";
    
    const char  HTML_MAIN_PAGE[]=
    "<html><title>DIEU KHIEN QUA MANG</title>
    <meta http-equiv=\"refresh\" content=\"20\">
    <body  bgcolor=\"#000000\" text=#000000>
    <script language=\"JavaScript\">
    function showtime ()
    {
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    //var seconds = now.getSeconds();
    var timeValue = hours;
    timeValue += ((minutes < 10) ? \":0\" : \":\") + minutes
    return timeValue ;
    }
    </script>
    <table align=\"center\" bgcolor=\"#ffcc99\">
    <tr><td align=\"center\">
    <table width=\"600px\">
    <tr>
    <td align=\"center\">
      <p align=\"left\"><img src= \"file:///c:/home.jpg\">
          <font color=\"#33FFFF\"><b>
          <marquee behavior=\"alternate\" scrollamount=\"20\" bgcolor=\"#800000\" direction=\"right\">
          ĐỒ ÁN 2
          </marquee>
          </b></font><font color=\"#FFFFFF\"><b><img src=\"file:///c:/main.gif\" alt=\"x\" width=\"762\" height=\"94\"></b></font></p>
      <h3>
    
    </h3>
    <form method=post>
    <table border=1>
    
    <tr><th align=left><font color=\"#009900\">Thiết bị 1</font></th>
    <td><input type=\"radio\" name=\"led1\" value=1 %0></td><td>BẬT</td>
    <td><input type=\"radio\" name=\"led1\" value=0 %1></td><td>TẮT</td></tr>
    
    <tr><th align=left><font color=\"#0000FF\">Trạng thái Role 2</font></th>
    <td><input type=\"radio\" name=\"led2\" value=1 %2></td><td>BẬT</td>
    <td><input type=\"radio\" name=\"led2\" value=0 %3></td><td>TẮT</td></tr>
    
    <tr><th align=left><font color=\"#FF0000\">Trạng thái Role 3</font></th>
    <td><input type=\"radio\" name=\"led3\" value=1 %5></td><td>BẬT</td>
    <td><input type=\"radio\" name=\"led3\" value=0 %6></td><td>TẮT</td></tr>
    
    <tr><th align=left>Thời gian hiện tại</th>
    <td><b><script>
    var sToday = showtime ();
    document.write(sToday);
    </script></b></td></tr>
    </table>
    <input type=\"hidden\" name=\"msgStack\" value=\"LCD/LEDs changed!\" ><input type=\"submit\" value=\"Apply\"></form></td>
    <tr>
    <td align=center><b><a href=\"/\"><font color=\"#800000\">Thoát</font><font color=\"#800000\"> chương trình</font></a></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <b> <a href=\"/about2\"><font color=\"#800000\">Thông</font><font color=\"#800000\"> tin đồ án</font></a></b></td>
    </tr>
    </table>
    </td></tr>
    </table>
    </body></html>";
    
    const char  HTML_ABOUT_PAGE[]=
    "<html><title>DIEU KHIEN QUA MANG</title>
    <center><body bgcolor=#FFFFF5 text=#000000>
    <img src= \"file:///c:/home.jpg\">
    <br/>
    
    <img src= \"file:///c:/about.gif\">
    </br>
    <p><a href=\"/\">Đăng nhập</a></p>
    <p><a href=\"/main\">Chương trình điều khiển</a></p>
    <p><a href=\"/about2\">Thông tin đồ án</a></p>
    </body></center></html>";
    
    /////***** end code web *****/////
    
    char g_HTTPMessageStack[20]="";
    char g_HTTPLCDMessage[20]="Nhiet do: ";
    int8 g_LEDState[8]={0,0,0,0,0,0,0,0};
    
    //this is a callback function to the HTTP stack.  see http.c
    //this demo provides to web "pages", an index (/) and an about page (/about)
    int32 http_get_page(char *file_str)
    {
       int32 file_loc=0;
       static char index[]="/";
       static char about[]="/main";
       static char about2[]="/about2";
    
       if (stricmp(file_str,index)==0)
          file_loc=label_address(HTML_INDEX_PAGE);
    
       else if (stricmp(file_str,about)==0)
          file_loc=label_address(HTML_MAIN_PAGE);
    
       else if (stricmp(file_str,about2)==0)
          file_loc=label_address(HTML_ABOUT_PAGE);
    
       return(file_loc);
    }
    
    //this isn't a required callback, this is an application function being
    //used by http_format_char.
    int http_format_char_checked(int8 flag, char *str)
    {
       int len=0;
    
       if (flag)
       {
          sprintf(str,"checked=\"checked\"");
          len = 7;
       }
    
       return(len);
    }
    
    //this is a callback function to the HTTP stack. see http.c
    //The following escape characters are used:
    //  %0 - AN0
    //  %1 - AN1
    //  %2 - BUTTON1
    //  %3 - BUTTON2
    //  %A - BUTTON3
    //  %B - BUTTON4
    //  %C - BUTTON5
    //  %4 - Current LCD Message
    //  %5 - CHECKED if LED1 is ON
    //  %6 - CHECKED if LED1 is OFF
    //  %7 - CHECKED if LED2 is ON
    //  %8 - CHECKED if LED2 is OFF
    //  %F - Message stack
    int8 http_format_char(int32 file, char id, char *str, int8 max_ret) {
       char new_str[25];
       int8 len=0;
       float i; int nd;
    
      switch(id) {
    
          case '4':
             strncpy(new_str, g_HTTPLCDMessage, sizeof(new_str));
             new_str[sizeof(new_str)-1] = 0;
             len = strlen(new_str);
             break;
    
          case '0':
             len = http_format_char_checked(g_LEDState[0], new_str);
             break;
          case '1':
             len = http_format_char_checked(!g_LEDState[0], new_str);
             break;
          case '2':
             len = http_format_char_checked(g_LEDState[1], new_str);
             break;
          case '3':
             len = http_format_char_checked(!g_LEDState[1], new_str);
             break;
          case '5':
             len = http_format_char_checked(g_LEDState[2], new_str);
             break;
    
          case '6':
             len = http_format_char_checked(!g_LEDState[2], new_str);
             break;
    
          case '7':
             len = http_format_char_checked(g_LEDState[3], new_str);
             break;
    
          case '8':
             len = http_format_char_checked(!g_LEDState[3], new_str);
             break;
    
          case '9':
             len = http_format_char_checked(g_LEDState[4], new_str);
             break;
    
          case 'x':
             len = http_format_char_checked(!g_LEDState[4], new_str);
             break;
          case 'a':
             len = http_format_char_checked(g_LEDState[5], new_str);
             break;
    
          case 'b':
             len = http_format_char_checked(!g_LEDState[5], new_str);
             break;
             
          case 'c':
             len = http_format_char_checked(g_LEDState[6], new_str);
             break;
    
          case 'd':
             len = http_format_char_checked(!g_LEDState[6], new_str);
             break;
             
          case 'g':
             len = http_format_char_checked(g_LEDState[7], new_str);
             break;
    
          case 'h':
             len = http_format_char_checked(!g_LEDState[7], new_str);
             break;
    
          case 'F':
             strncpy(new_str, g_HTTPMessageStack, sizeof(new_str));
             new_str[sizeof(new_str)-1] = 0;
             len = strlen(new_str);
             g_HTTPMessageStack[0] = 0;
             break;
       }
    
       if (len){
          strncpy(str, new_str, max_ret);
       }else
          *str=0;
    
       return(len);
    }
    
    //this is a callback function to the HTTP stack. see http.c
    //in this example it verifies that "pwd" is "master", if it is
    //then it sets led1 and led2 ("led1" and "led2") based on their value
    //and changes the lcd screen ("lcd").
    void http_exec_cgi(int32 file, char *key, char *val) {
       static char led1_key[]="led1";
       static char led2_key[]="led2";
       static char led3_key[]="led3";
       static char led4_key[]="led4";
       static char led5_key[]="led5";
       static char led6_key[]="led6";
       static char led7_key[]="led7";
       static char led8_key[]="led8";
       static char lcd_key[]="lcd";
       static char msgStack_key[]="msgStack";
       int8 v;
    
    // #if defined(USER_LED1)
       if (stricmp(key,led1_key)==0) {
          v=atoi(val);
          g_LEDState[0]=v;
       }
    // #endif
    
    // #if defined(USER_LED2)
       if (stricmp(key,led2_key)==0) {
          v=atoi(val);
          g_LEDState[1]=v;
       }
    // #endif
    
    // #if defined(USER_LED2)
       if (stricmp(key,led3_key)==0) {
          v=atoi(val);
          g_LEDState[2]=v;
       }
    // #endif
    
    // #if defined(USER_LED3)
       if (stricmp(key,led4_key)==0) {
          v=atoi(val);
          g_LEDState[3]=v;
       }
    // #endif
    
    // #if defined(USER_LED2)
       if (stricmp(key,led5_key)==0) {
          v=atoi(val);
          g_LEDState[4]=v;
       }
    // #endif
    
    // #if defined(USER_LED2)
       if (stricmp(key,led6_key)==0) {
          v=atoi(val);
          g_LEDState[5]=v;
       }
    // #endif
    
    // #if defined(USER_LED2)
       if (stricmp(key,led7_key)==0) {
          v=atoi(val);
          g_LEDState[6]=v;
       }
    // #endif
    
    // #if defined(USER_LED2)
       if (stricmp(key,led8_key)==0) {
          v=atoi(val);
          g_LEDState[7]=v;
       }
    // #endif
    
       if (stricmp(key, msgStack_key)==0)
       {
          strncpy(g_HTTPMessageStack, val, sizeof(g_HTTPMessageStack));
          g_HTTPMessageStack[sizeof(g_HTTPMessageStack)-1] = 0;
       }
    
       if (stricmp(key,lcd_key)==0) {
          strncpy(g_HTTPLCDMessage, val, sizeof(g_HTTPLCDMessage));
          g_HTTPLCDMessage[sizeof(g_HTTPLCDMessage)-1] = 0;
    #if STACK_USE_PICDEM_LCD
     //     printf(lcd_putc,"\f%s",val);
    #endif
       }
    }
    
    void splash(void) {
      #IF STACK_USE_PICDEM_LCD
       printf(lcd_putc,"\fDia chi IP");
      #ENDIF
     #if STACK_USE_ANNOUNCE
       InitAnnounce(" - EX_ST_WEBSERVER");
     #endif
     #if STACK_USE_SERIAL
       fprintf(USER,"\r\n\r\nDo an 2\r\nPIC Webserver");
       ui_settings();
       ui_menu();
     #endif
    }
    
    void main(void)
    {
       float i; int nd;
    
       init();
       splash();
    
    
       while (TRUE) {
          restart_wdt();
    
         #if STACK_USE_SERIAL
          if (kbhit(USER)) {
             ui_handle();
          }
         #endif
      
          
                if (g_LEDState[0])
                   output_high(USER_LED1);
                else
                   output_low(USER_LED1);
                if (g_LEDState[1])
                   output_high(USER_LED2);
                else
                   output_low(USER_LED2);
                if (g_LEDState[2])
                   output_high(USER_LED3);
                else
                   output_low(USER_LED3);
               
          //every second: refresh current IP, blink LED
          if (second_counter>=1) {
    
    
    
    
             display_ip_lcd();
             second_counter=0;
          }
    
          #if STACK_USE_PPP
          if (ppp_is_connected()) {
          #endif
          StackTask();            //Microchip's TCP/IP stack handler.  Must be called routinely.
          #if STACK_USE_PPP
          }
          #endif
    
       }
    }

Về tác giả

Collapse

thinhktdt Tìm hiểu thêm về thinhktdt

Bài viết mới nhất

Collapse

Đang tải...
X