分享 UCOS+LWIP+STM32F407+HTML网页

2020-01-11 18:15发布

这个东西原理简单,就是要懂一些HTML语言,然后LWIP网络编程模拟一个简单的网站。可用于参数配置,非常适合那种不带液晶屏和按键,但又有大量数据要配置的设备。直接用浏览器配置数据,免去了上位机配置软件的开发,方便使用。


/*web 任务*/
void task_for_web_server(void *arg)
{
    int retval;
    int web_listen_sockfd;
    int i;
    int rcvlen, sndlen;
    int curRecePos=0;
    socklen_t addrlen;
    struct sockaddr_in srv_sockaddr,cln_sockaddr;

    OSTimeDlyHMSM(0,0,1,0);//==d等系统准备好
    web_server_init();
   
    memset(&srv_sockaddr, 0, sizeof(struct sockaddr_in));
#if !(defined LWIP_BSD_SOCKER && LWIP_BSD_SOCKER > 0)
    srv_sockaddr.sin_len    = sizeof(struct sockaddr_in);
#endif
    srv_sockaddr.sin_family = AF_INET;
    srv_sockaddr.sin_port   = htons(80);
    srv_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    //=======
    printf("task_for_http_server go... ");
    for(;;)
    {
        /* 创建TCP socket. */
        web_listen_sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(web_listen_sockfd == (-1))
        {
            printf(" ERR:web_listen_sockfd ceate errlog=%d ",errno);
            close(web_listen_sockfd);
            continue;
        }
        setsockopt(web_listen_sockfd, SOL_SOCKET, SO_REUSEADDR, &attr_bind, sizeof(attr_bind));
            
        /* 绑定socket IP地址及端口号. */
        addrlen = sizeof(struct sockaddr_in);
        retval  = bind(web_listen_sockfd, (struct sockaddr *)&srv_sockaddr, addrlen);
        if(retval == (-1))
        {
            printf(" ERR:web_listen_sockfd bind errlog=%d ",errno);
            close(web_listen_sockfd);
            continue;
        }
        
        
        /* 侦听socket连接. */
        retval = listen(web_listen_sockfd, 1);
        if(retval == (-1))
        {
            printf(" ERR:web_listen_sockfd listen  errlog=%d ",errno);
            close(web_listen_sockfd);
            OSTimeDlyHMSM(0,0,3,0);
            continue;
        }

        /* 处理链接,循环接受数据. */
        while (1)
        {
            /* 阻塞等待客户端建立连接. */
            printf(" HttpServer wait accept...... ");
            socket_fd_web = accept(web_listen_sockfd, (struct sockaddr *)&cln_sockaddr, &addrlen);
            if (socket_fd_web == (-1))
            {
                printf(" ERR:socket_fd_web accept=%d errlog=%d ",socket_fd_web,errno);
                close(web_listen_sockfd);
                break;
            }
            
            //=====链接建立,循环等待接收
            curRecePos = 0;
            do
            {
                /* 阻塞接收客户端数据报文. */
                rcvlen= recv(socket_fd_web,&httpReceBuf[curRecePos],sizeof(httpReceBuf)-curRecePos,0);//MSG_DONTWAIT
                printf(" recv return len=%d ",rcvlen);
                if(rcvlen>0)
                {
                    curRecePos=curRecePos+rcvlen;                  
                    httpReceBuf[curRecePos]=0;  //增加字符串解锁标识
                    if((0==IsDataRecvFinish(httpReceBuf))||(curRecePos>=sizeof(httpReceBuf)))
                   {   
                       processhttpdata(curRecePos);
                       curRecePos=0;                       
                   }
                }
                else
                {
                    printf(" ERR:recv return len<=0 ");
                    printf(" err log =%d ",errno);
                    curRecePos = 0;
                }
            } while (rcvlen >0);
            closesocket(socket_fd_web);
        }
        close(web_listen_sockfd);
        printf(" HttpServer close listenSockfd ");
    }
}
/*打包网页HTML信息*/
******************************************************************************************************/
int web_server_packet_cfg_page_html(char *out_pack_buff)
{
    out_pack_buff[0] = 0;

    /*网页文件头部*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<HTML><head><title>网络通信参数</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><body>");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<div style="width: 400px; height: 300px; position:absolute; left: 50%;top: 50%;">");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<form name="NetcardPage" action="" method="POST">");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<table style="width: 400px; border-collapse: collapse;">");
   
    /*信息内容*/
    /*本地IP*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<tr style="border: black 1px solid; height: 3px; font-size: 14px;"><td>本  地IP:</td>");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<td><input name="IP1addr1" type="text" size="3" maxlength="3" value="%d">.", cfgdata.local_ip[0]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1addr2" type="text" size="3" maxlength="3" value="%d">.", cfgdata.local_ip[1]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1addr3" type="text" size="3" maxlength="3" value="%d">.", cfgdata.local_ip[2]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1addr4" type="text" size="3" maxlength="3" value="%d"></td></tr>",  cfgdata.local_ip[3]);

    /*子网掩码*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<tr style="border: black 1px solid; height: 30px; font-size: 14px;"><td>子网掩码:</td>");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<td><input name="IP1Mask1" type="text" size="3" maxlength="3" value="%d">.", cfgdata.sub_mask[0]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1Mask2" type="text" size="3" maxlength="3" value="%d">.", cfgdata.sub_mask[1]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1Mask3" type="text" size="3" maxlength="3" value="%d">.", cfgdata.sub_mask[2]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1Mask4" type="text" size="3" maxlength="3" value="%d"></td></tr>", cfgdata.sub_mask[3]);
   
    /*网关*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<tr style="border: black 1px solid; height: 30px; font-size: 14px;"><td>网关:</td>");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<td><input name="IP1GateWay1" type="text" size="3" maxlength="3" value="%d">.", cfgdata.gate_way[0]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1GateWay2" type="text" size="3" maxlength="3" value="%d">.", cfgdata.gate_way[1]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1GateWay3" type="text" size="3" maxlength="3" value="%d">.", cfgdata.gate_way[2]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1GateWay4" type="text" size="3" maxlength="3" value="%d"></td></tr>", cfgdata.gate_way[3]);


    /*本地端口*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<tr style="border: black 1px solid; height: 30px; font-size: 14px;"><td>本地端口:</td>");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<td><input name="LocalPort" type="text" size="5" maxlength="5" value="%d"></td></tr>", cfgdata.local_port);


    /*远方IP*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<tr style="border: black 1px solid; height: 30px; font-size: 14px;"><td>远 方IP:</td>");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<td><input name="IP1Ser1" type="text" size="3" maxlength="3" value="%d">.", cfgdata.remote_ip[0]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1Ser2" type="text" size="3" maxlength="3" value="%d">.", cfgdata.remote_ip[1]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1Ser3" type="text" size="3" maxlength="3" value="%d">.", cfgdata.remote_ip[2]);
    sprintf(out_pack_buff+strlen(out_pack_buff),"<input name="IP1Ser4" type="text" size="3" maxlength="3" value="%d"></td></tr>", cfgdata.remote_ip[3]);

    /*远方端口*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<tr style="border: black 1px solid; height: 30px; font-size: 14px;"><td>远方端口:</td>");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<td><input name="RemotePort" type="text" size="5" maxlength="5" value="5000"></td></tr>", cfgdata.remote_port);

    /*协议类型*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<tr style="border: black 1px solid; height: 30px; font-size: 14px;"><td>通信协议:</td>");
    sprintf(out_pack_buff+strlen(out_pack_buff),"<td><select name="ProcType">");
    if(cfgdata.proc_type==1)
    {
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="1" selected="selected">UDP</option>");
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="2" >TCP_SERVER</option>");
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="3">TCP_CLIENT</option>");
    }
    else if(cfgdata.proc_type==3)
    {
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="1">UDP</option>");
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="2">TCP_SERVER</option>");
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="3" selected="selected">TCP_CLIENT</option>");
    }
    else
    {
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="1">UDP</option>");
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="2" selected="selected">TCP_SERVER</option>");
        sprintf(out_pack_buff+strlen(out_pack_buff),"<option value="3">TCP_CLIENT</option>");
    }
    sprintf(out_pack_buff+strlen(out_pack_buff),"</select></td></tr>");

    /*尾部*/
    sprintf(out_pack_buff+strlen(out_pack_buff),"<tr><td style="height: 40px; text-align: center;" colspan="2"><input name="save" type="submit" value="保存">");
    sprintf(out_pack_buff+strlen(out_pack_buff),"&nbsp;&nbsp;&nbsp;<input name="reset" type="reset" value="重置"></td></tr></table></form></div></BODY></HTML>");
   
    return strlen(out_pack_buff);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
17条回答
NIC
1楼-- · 2020-01-11 19:05
楼主把整个工程分享出来呗
zxq6
2楼-- · 2020-01-11 20:05
挺好得。不过我都喜欢用udp加一个客户端,编程简单得多。
Excellence
3楼-- · 2020-01-11 22:59
好。谢谢楼主。。。。。
ljt80158015
4楼-- · 2020-01-12 01:31
lwip socket接口稳定吗?
zcllom
5楼-- · 2020-01-12 01:36
 精彩回答 2  元偷偷看……
syj0925
6楼-- · 2020-01-12 02:59
谢谢分享,后续又开发,再来参考。

一周热门 更多>