打印

stm32 lwip 做客户端程序,可是怎么都建立不了连接

[复制链接]
7640|13
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
irishehe|  楼主 | 2013-6-22 20:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
stm32 lwip 做客户端程序,可是怎么都建立不了连接,都不会进TcpCli_Connected这个回调函数 本人菜鸟级别的,麻烦大虾指点,硬件应该没有问题,pc的IP:192.168.1.16,端口:23   是我哪里写掉了什么啊?

main()中的:函数
沙发
irishehe|  楼主 | 2013-6-22 20:55 | 只看该作者

        /*初始化 以太网SPI接口*/
        ENC_SPI_Init();                

        /*初始化systick,用于定时轮询输入或给LWIP提供定时*/
        SysTick_Init();               
          /* 初始化LWIP协议栈*/
        LwIP_Init();

    while ( 1 )
        {       
            TCP_Client_Init();

            Delay1(1000000UL);
            Delay1(1000000UL);
            Delay1(1000000UL);
            Delay1(1000000UL);
    }

具体的子函数:
/******* 这是一个回调函数,当TCP客户端请求的连接建立时被调用********/
err_t TcpCli_Connected(void *arg,struct tcp_pcb *pcb,err_t err)
{
   tcp_write(pcb,TCP_TestData,sizeof(TCP_TestData),0);      //发送数据
   
   tcp_close(pcb);
   
   return ERR_OK;
}

void TCP_Client_Init()
{
  struct tcp_pcb *Clipcb;
  struct ip_addr ipaddr;
  
  IP4_ADDR(&ipaddr,192,168,1,16);
  
  Clipcb = tcp_new();                       // 建立通信的TCP控制块(Clipcb)
  
  tcp_bind(Clipcb,IP_ADDR_ANY,23);       // 绑定本地IP地址和端口号
  
  iris_pcb=Clipcb;  //the mathod is wrong //by iris 2013.6.3
  tcp_connect(Clipcb,&ipaddr,23,TcpCli_Connected);
}


unsigned long iris_IP[5]={192, 168, 1, 18};

void LwIP_Init( void )
{
//  struct ip_addr ipaddr;
  struct ip_addr netmask;
  struct ip_addr gw;

   /*调用LWIP初始化函数,
   初始化网络接口结构体链表、内存池、pbuf结构体*/
   lwip_init();       
          
#if LWIP_DHCP                                                                   //若使用DHCP协议
  ipaddr.addr = 0;
  netmask.addr = 0;
  gw.addr = 0;
#else                                                                                //
/*                //by iris 2013.6.6                                                                        //
  IP4_ADDR(&ipaddr, 192, 168, 0, 18);                  //设置网络接口的ip地址
  IP4_ADDR(&netmask, 255, 255, 255, 0);                //子网掩码
  IP4_ADDR(&gw, 192, 168, 0, 1);                        //网关
*/                //by iris 2013.6.6                                                                        //
//  IP4_ADDR(&ipaddr, 192, 168, 1, 18);                  //设置网络接口的ip地址
  IP4_ADDR(&ipaddr, iris_IP[0], iris_IP[1], iris_IP[2], iris_IP[3]);                  //设置网络接口的ip地址
  IP4_ADDR(&netmask, 255, 255, 255, 0);                //子网掩码
  IP4_ADDR(&gw, 192, 168, 1, 1);                        //网关


#endif
   
  /*初始化enc28j60与LWIP的接口,参数为网络接口结构体、ip地址、
  子网掩码、网关、网卡信息指针、初始化函数、输入函数*/
  netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);

  /*把enc28j60设置为默认网卡 .*/
  netif_set_default(&enc28j60);


#if LWIP_DHCP                                   //若使用了DHCP
  /*  Creates a new DHCP client for this interface on the first call.
  Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
  the predefined regular intervals after starting the client.
  You can peek in the netif->dhcp struct for the actual DHCP status.*/
  dhcp_start(&enc28j60);           //启动DHCP
#endif

  /*  When the netif is fully configured this function must be called.*/
  netif_set_up(&enc28j60); //使能enc28j60接口
}

#define  ENC_SPI_Init   SPI1_Init
void SPI1_Init(void)
{
   SPI_InitTypeDef SPI_InitStructure;
   GPIO_InitTypeDef GPIO_InitStructure;

   /* Enable SPI1 and GPIOA clocks */
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, F_ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, F_ENABLE);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;                //CS
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   //GPIO_Init(GPIOB, &GPIO_InitStructure);  
   GPIO_Init(GPIOC, &GPIO_InitStructure);  //BY IRIS 2013.6.3

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_Init(GPIOD, &GPIO_InitStructure);

   /* Configure SPI1 pins: NSS, SCK, MISO and MOSI */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_Init(GPIOA, &GPIO_InitStructure);

  //by iris 2013.6.3
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
   GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IPU;   //上拉输入
   GPIO_Init(GPIOA, &GPIO_InitStructure);
  //by iris 2013.6.3

   /* SPI1 configuration */
   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
   SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
   SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
   SPI_InitStructure.SPI_CRCPolynomial = 7;
   SPI_Init(SPI1, &SPI_InitStructure);

   /* Enable SPI1  */
   SPI_Cmd(SPI1, F_ENABLE);
}



使用特权

评论回复
板凳
trumpxp| | 2013-6-23 10:12 | 只看该作者
查一查硬件   看看   是不是硬件的对接问题   查一查吧  

使用特权

评论回复
地板
irishehe|  楼主 | 2013-6-23 15:45 | 只看该作者
trumpxp 发表于 2013-6-23 10:12
查一查硬件   看看   是不是硬件的对接问题   查一查吧

可是我如果把主函数的程序变成服务器的程序:
        /*初始化 以太网SPI接口*/
        ENC_SPI_Init();                 

        /*初始化systick,用于定时轮询输入或给LWIP提供定时*/
        SysTick_Init();               
          /* 初始化LWIP协议栈*/
        LwIP_Init();
          /* 初始化telnet   远程控制 程序 */   
        CMD_init();        //by iris 2013.6.22                              

    while ( 1 )
        {        
            LwIP_Periodic_Handle(LocalTime);       
         }
是可以正常做服务器的列。
其中子函数代码:

void CMD_init(void)
{
  struct tcp_pcb *pcb;                //定义一个tcp控制块                           

  /* Create a new TCP control block  */
  pcb = tcp_new();                           //给tcp控制块分配内存空间                                     

  /* Assign to the new pcb a local IP address and a port number */
  /* Using IP_ADDR_ANY allow the pcb to be used by any local interface */
  tcp_bind(pcb, IP_ADDR_ANY, 23);     //把PCB控制块绑定到本机的所有IP地址,端口为23  


  /* Set the connection to the LISTEN state */
  pcb = tcp_listen(pcb);                          //监听该端口       

  /* Specify the function to be called when a connection is established */       
  tcp_accept(pcb, CMD_accept);            //监听的端口接通后调用的函数HelloWorld_accept

  iris_pcb=pcb;  //the mathod is wrong //by iris 2013.6.3
}
//////////////////by iris 2013.6.3


/*
* 函数名:LwIP_Periodic_Handle
* 描述  :lwip协议栈要求周期调用一些函数
                        tcp_tmr  etharp_tmr   dhcp_fine_tmr dhcp_coarse_tmr                        
* 输入  :无
* 输出  : 无
* 调用  :外部调用
*/
void LwIP_Periodic_Handle(__IO uint32_t localtime)
{
        //err_t  err;
       
       
                    /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
                        ethernetif_input(&enc28j60);                         //轮询是否接收到数据
                        //        err = ethernetif_input(&enc28j60); //轮询是否接收到数据

                        //                if (err !=ERR_OK  )
                        //                {
                        //                       
                        //
                        //                        }
       

          /* TCP periodic process every 250 ms */
          if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
          {
            TCPTimer =  localtime;
            tcp_tmr();                 //每250ms调用一次
          }
          /* ARP periodic process every 5s */
          if (localtime - ARPTimer >= ARP_TMR_INTERVAL)
          {
            ARPTimer =  localtime;
            etharp_tmr();          //每5s调用一次
          }

#if LWIP_DHCP
          /* Fine DHCP periodic process every 500ms */
          if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
          {
            DHCPfineTimer =  localtime;
            dhcp_fine_tmr();
          }
          /* DHCP Coarse periodic process every 60s */
          if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS)
          {
            DHCPcoarseTimer =  localtime;
            dhcp_coarse_tmr();
          }
#endif

}

使用特权

评论回复
5
irishehe|  楼主 | 2013-6-23 15:47 | 只看该作者
trumpxp 发表于 2013-6-23 10:12
查一查硬件   看看   是不是硬件的对接问题   查一查吧

这种情况还会跟硬件有关系么?为什么啊 呵呵 不太懂诶

使用特权

评论回复
6
irishehe|  楼主 | 2013-6-23 16:20 | 只看该作者
感谢五木大师的指点,终于好了,应该把初始化放在循环外面,循环里面加上轮询。通过这个问题,我深刻的体会到写程序不能生搬硬套,以后一定深刻反省 嘿嘿

使用特权

评论回复
7
wg150986| | 2013-7-5 13:39 | 只看该作者
楼主你好,轮询的是什么状态啊

使用特权

评论回复
8
湖北泽翔| | 2014-4-11 20:56 | 只看该作者
谢谢分享

使用特权

评论回复
9
梦里看客| | 2016-5-28 17:36 | 只看该作者
你好啊,我最近也在调stm32作为客户端的程序,但是一直连接不上PC端的网络调试助手,能分享一下你的经验吗?真心十分感谢你哈。
我qq1198484350

使用特权

评论回复
10
赛程中的乌龟| | 2016-10-26 14:30 | 只看该作者
梦里看客 发表于 2016-5-28 17:36
你好啊,我最近也在调stm32作为客户端的程序,但是一直连接不上PC端的网络调试助手,能分享一下你的经验吗 ...

你现在调通了吗?能否给我指点指点

使用特权

评论回复
11
lumilu| | 2017-9-11 22:16 | 只看该作者
梦里看客 发表于 2016-5-28 17:36
你好啊,我最近也在调stm32作为客户端的程序,但是一直连接不上PC端的网络调试助手,能分享一下你的经验吗 ...

哥们,你现在调通了没有?

使用特权

评论回复
12
futureiam123| | 2019-7-12 10:54 | 只看该作者
你现在调通了没有?

使用特权

评论回复
13
hao2014| | 2020-7-17 11:49 | 只看该作者
赛程中的乌龟 发表于 2016-10-26 14:30
你现在调通了吗?能否给我指点指点

调通了,需要的话私聊

使用特权

评论回复
评论
章鱼zhangyu 2021-5-8 22:12 回复TA
大哥,我刚注册无法给你发消息,求stm32作为客户端怎么调通呀?qq:2013748103 
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

5

帖子

0

粉丝