找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5635|回复: 2
打印 上一主题 下一主题
收起左侧

stm32通过w5500使用dns解读IP地址 源程序

[复制链接]
跳转到指定楼层
楼主
stm32通过w5500使用dns解读IP地址的源程序:

完整源码下载:
stm32_w5500_dns.zip (779.32 KB, 下载次数: 85)





源程序预览(主程序):
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * $Author: 飞鸿踏雪 $
  5.   * $Revision: 17 $
  6.   * $Date:: 2014-10-25 11:16:48 +0800 #$
  7.   * @brief   主函数.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   *
  12.   ******************************************************************************
  13.   */
  14. /* Includes ------------------------------------------------------------------*/
  15. #include "main.h"
  16. #include "usart.h"
  17. #include "delay.h"
  18. #include "spi.h"
  19. #include "socket.h"        // Just include one header for WIZCHIP
  20. #include "Internet/DNS/dns.h"
  21. /* Private typedef -----------------------------------------------------------*/
  22. /* Private define ------------------------------------------------------------*/
  23. #define _MAIN_DEBUG_
  24. #define SOCK_DNS                        0
  25. #define DATA_BUF_SIZE   2048
  26. /* Private macro -------------------------------------------------------------*/
  27. uint8_t gDATABUF[DATA_BUF_SIZE];
  28. // Default Network Configuration
  29. wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
  30.                             .ip = {192, 168, 1, 123},
  31.                             .sn = {255,255,255,0},
  32.                             .gw = {192, 168, 1, 1},
  33.                             .dns = {8,8,8,8},
  34.                             .dhcp = NETINFO_STATIC };
  35. uint8_t DNS_2nd[4]    = {192, 168, 1, 1};                // Secondary DNS server IP
  36. uint8_t Domain_name[] = "www点embed-net点com";                // for Example domain name
  37. uint8_t Domain_IP[4]  = {0};                                        // Translated IP address by DNS
  38. volatile uint32_t msTicks; /* counts 100ms timeTicks */
  39. /* Private variables ---------------------------------------------------------*/
  40. /* Private function prototypes -----------------------------------------------*/
  41. /* Private functions ---------------------------------------------------------*/
  42. void platform_init(void);                                                                // initialize the dependent host peripheral
  43. void network_init(void);

  44. /*****************************************************************************
  45. * @brief SysTickIntHandler
  46. * Interrupt Service Routine for system tick counter
  47. *****************************************************************************/
  48. void SysTick_Handler(void)
  49. {
  50.         msTicks++; /* increment counter necessary in Delay()*/
  51.         // SHOULD BE Added DNS Timer Handler your 1s tick timer
  52.         if((msTicks % 1000) == 0){
  53.                 DNS_time_handler();
  54.         }
  55. }

  56. /**
  57.   * @brief  串口打印输出
  58.   * @param  None
  59.   * @retval None
  60.   */
  61. int main(void)
  62. {
  63.    int8_t  ret;
  64.    uint8_t tmp;
  65.    uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
  66.         //Host dependent peripheral initialized
  67.         platform_init();
  68.         // First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP
  69.         /* Critical section callback */
  70.         reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit);        //注册临界区函数
  71.         /* Chip selection call back */
  72. #if   _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
  73.         reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注册SPI片选信号函数
  74. #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
  75.         reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);  // CS must be tried with LOW.
  76. #else
  77.    #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
  78.       #error "Unknown _WIZCHIP_IO_MODE_"
  79.    #else
  80.       reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
  81.    #endif
  82. #endif
  83.         /* SPI Read & Write callback function */
  84.         reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte);        //注册读写函数

  85.         /* WIZCHIP SOCKET Buffer initialize */
  86.         if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
  87.                  printf("WIZCHIP Initialized fail.\r\n");
  88.                  while(1);
  89.         }

  90.         /* PHY link status check */
  91.         do{
  92.                  if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
  93.                                 printf("Unknown PHY Link stauts.\r\n");
  94.                  }
  95.         }while(tmp == PHY_LINK_OFF);

  96.     /* Network initialization */
  97.     network_init(); // Static netinfo setting

  98.     /************************************************/
  99.     /* WIZnet W5500 Code Examples :                                 */
  100.     /* Implemented using ioLibrary_BSD Socket APIs        */
  101.     /************************************************/
  102.     /* >> DNS Client                                                                 */
  103.     /************************************************/

  104. #ifdef _MAIN_DEBUG_
  105.    printf("\r\n=== DNS Client Example ===============\r\n");
  106.    printf("> DNS 1st : %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0], gWIZNETINFO.dns[1], gWIZNETINFO.dns[2], gWIZNETINFO.dns[3]);
  107.    printf("> DNS 2nd : %d.%d.%d.%d\r\n", DNS_2nd[0], DNS_2nd[1], DNS_2nd[2], DNS_2nd[3]);
  108.    printf("======================================\r\n");
  109.    printf("> Example Domain Name : %s\r\n", Domain_name);
  110. #endif
  111.    /* DNS client initialization */
  112.    DNS_init(SOCK_DNS, gDATABUF);

  113.    /* DNS procssing */
  114.    if ((ret = DNS_run(gWIZNETINFO.dns, Domain_name, Domain_IP)) > 0) // try to 1st DNS
  115.    {
  116. #ifdef _MAIN_DEBUG_
  117.       printf("> 1st DNS Reponsed\r\n");
  118. #endif
  119.    }
  120.    else if ((ret != -1) && ((ret = DNS_run(DNS_2nd, Domain_name, Domain_IP))>0))     // retry to 2nd DNS
  121.    {
  122. #ifdef _MAIN_DEBUG_
  123.       printf("> 2nd DNS Reponsed\r\n");
  124. #endif
  125.    }
  126.    else if(ret == -1)
  127.    {
  128. #ifdef _MAIN_DEBUG_
  129.       printf("> MAX_DOMAIN_NAME is too small. Should be redefined it.\r\n");
  130. #endif
  131.    }
  132.    else
  133.    {
  134. #ifdef _MAIN_DEBUG_
  135.       printf("> DNS Failed\r\n");
  136. #endif
  137.    }

  138.    if(ret > 0)
  139.    {
  140. #ifdef _MAIN_DEBUG_
  141.       printf("> Translated %s to %d.%d.%d.%d\r\n",Domain_name,Domain_IP[0],Domain_IP[1],Domain_IP[2],Domain_IP[3]);
  142. #endif
  143.       //
  144.       // TO DO
  145.       //
  146.    }
  147.         /* Main Loop */
  148.         while(1)
  149.         {

  150.         }
  151. } // end of main()




  152. /**
  153.   * @brief  Loopback Test Example Code using ioLibrary_BSD        
  154.   * @retval None
  155.   */
  156. void platform_init(void)
  157. {
  158.         SystemInit();//系统时钟初始化
  159.         USART_Configuration();//串口1初始化
  160.         printf("\x0c");printf("\x0c");//超级终端清屏
  161.         printf("\033[1;40;32m");//设置超级终端背景为黑色,字符为绿色
  162.         printf("\r\n*******************************************************************************");
  163.         printf("\r\n************************ Copyright 2009-2014, EmbedNet ************************");
  164.         printf("\r\n*************************** http://www点embed-net点com **************************");
  165.         printf("\r\n***************************** All Rights Reserved *****************************");
  166.         printf("\r\n*******************************************************************************");
  167.         printf("\r\n");
  168.         //Config SPI
  169.         SPI_Configuration();
  170.         //滴答定时器初始化
  171.         SysTick_Config(72000000 / 1000);//1ms
  172. }

  173. /******************************************************************************
  174. * @brief  Network Init
  175. * Intialize the network information to be used in WIZCHIP
  176. *****************************************************************************/
  177. void network_init(void)
  178. {
  179. #ifdef _MAIN_DEBUG_
  180.         uint8_t tmpstr[6] = {0,};
  181.         wiz_NetInfo netinfo;
  182. #endif

  183.         // Set Network information from netinfo structure
  184.         ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);

  185. #ifdef _MAIN_DEBUG_
  186.         // Get Network information
  187.         ctlnetwork(CN_GET_NETINFO, (void*)&netinfo);

  188.         // Display Network Information
  189.         ctlwizchip(CW_GET_ID,(void*)tmpstr);

  190.         if(netinfo.dhcp == NETINFO_DHCP) printf("\r\n=== %s NET CONF : DHCP ===\r\n",(char*)tmpstr);
  191.         else printf("\r\n=== %s NET CONF : Static ===\r\n",(char*)tmpstr);

  192.         printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",netinfo.mac[0],netinfo.mac[1],netinfo.mac[2],
  193.                         netinfo.mac[3],netinfo.mac[4],netinfo.mac[5]);
  194.         printf("SIP: %d.%d.%d.%d\r\n", netinfo.ip[0],netinfo.ip[1],netinfo.ip[2],netinfo.ip[3]);
  195.         printf("GAR: %d.%d.%d.%d\r\n", netinfo.gw[0],netinfo.gw[1],netinfo.gw[2],netinfo.gw[3]);
  196.         printf("SUB: %d.%d.%d.%d\r\n", netinfo.sn[0],netinfo.sn[1],netinfo.sn[2],netinfo.sn[3]);
  197.         printf("DNS: %d.%d.%d.%d\r\n", netinfo.dns[0],netinfo.dns[1],netinfo.dns[2],netinfo.dns[3]);
  198.         printf("===========================\r\n");
  199. #endif

  200. }

  201. /*********************************END OF FILE**********************************/
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:137543 发表于 2018-1-27 14:29 | 只看该作者
感谢,看了10分钟就调通了,楼主威武
回复

使用道具 举报

板凳
ID:35312 发表于 2018-3-2 20:11 | 只看该作者
非常需要.谢谢楼主
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表