求在STM32上裸机移植LWIP,STM32做CLIENT,CLIENT简单地发送数据

[复制链接]
 楼主| can123dao 发表于 2013-7-18 22:00 | 显示全部楼层 |阅读模式
求在STM32上裸机移植LWIP,STM32做TCP CLIENT,CLIENT简单地发送数据
openrd 发表于 2013-7-23 21:49 | 显示全部楼层
哈哈,楼主别求了,我最近也在做这个,还是动手做吧。TCP做server刚做了一点,可以发送数据,但是不太正常有问题
客户端的还没搞定,我也是需要做一个客户端的,纠结了很久,动手做就会有发现的,多交流
haibian826 发表于 2013-7-23 23:39 | 显示全部楼层
uIP-1.0就行吧??
代码有一个,仅供参考

芯嵌STM32_网口驱动_串口_OK.rar (4.11 MB, 下载次数: 459)

 楼主| can123dao 发表于 2013-7-27 09:36 | 显示全部楼层
openrd 发表于 2013-7-23 21:49
哈哈,楼主别求了,我最近也在做这个,还是动手做吧。TCP做server刚做了一点,可以发送数据,但是不太正常 ...

这个差不多了,想加上DHCP,还有问题
openrd 发表于 2013-7-29 12:19 | 显示全部楼层
can123dao 发表于 2013-7-27 09:36
这个差不多了,想加上DHCP,还有问题

发送和接收都有吗?
 楼主| can123dao 发表于 2013-7-29 15:17 | 显示全部楼层
openrd 发表于 2013-7-29 12:19
发送和接收都有吗?

openrd 发表于 2013-7-29 21:04 | 显示全部楼层
can123dao 发表于 2013-7-29 15:17

udp_connect(uclient_pcb,IP_ADDR_BROADCAST, UDP_CLIENT_PORT);
udp_send(uclient_pcb, p);
我做了udp客户端数据发送之后,就是上面的过程,发送数据时没有问题的,紧接着
udp_recv(uclient_pcb, myudp_client_callback, NULL);接收数据,回调函数myudp_client_callback内对接收的数据进行处理,可是接收不到,请问,可是在发送之后这样接收吗?
bencn 发表于 2013-7-30 00:58 | 显示全部楼层
好高深,看不懂
 楼主| can123dao 发表于 2013-7-30 16:00 | 显示全部楼层
openrd 发表于 2013-7-29 21:04
udp_connect(uclient_pcb,IP_ADDR_BROADCAST, UDP_CLIENT_PORT);
udp_send(uclient_pcb, p);
我做了udp ...

给你我的做CLIENT的代码吧,是TCP的,
  1. #include "lwip/tcp.h"
  2. #include "lwip/err.h"
  3. #include "stm32f10x.h"
  4. #include <string.h>
  5. #include <stdio.h>
  6. extern struct netif *enc28j60;
  7. enum tcp_client_state{
  8.         CLIENT_CONNECTED = 0x00U,
  9.         CLIENT_WAITING_FOR_CMD,
  10.         CLIENT_BUSY,
  11.         CLIENT_SENT,
  12.         CLIENT_ERROR,
  13.         CLIENT_CLOSE,
  14.         CLIENT_WAITING_FOR_CONNECTION
  15. };

  16. struct tcp_client_app_arg{
  17.         uint8_t app_state;
  18.         uint8_t textlen;
  19.         uint8_t* dataptr;
  20. };

  21. static struct ip_addr destip;

  22. void tcp_client_errf(void *arg, err_t err)
  23. {
  24.         struct tcp_client_app_arg* pro_arg = (struct tcp_client_app_arg*)arg;

  25.         printf("tcp client err\n");
  26.        
  27.         pro_arg->app_state = CLIENT_ERROR;

  28.         switch(err){
  29.                 case ERR_MEM: printf("Out of memory error\n"); break;
  30.                 case ERR_BUF: printf("Buffer error\n"); break;
  31.                 case ERR_TIMEOUT: printf("Timeout\n"); break;
  32.                 case ERR_RTE: printf("Routing problem\n"); break;
  33.                 case ERR_ABRT: printf("Connection aborted\n"); break;
  34.                 case ERR_RST: printf("Connection reset\n"); break;
  35.                 case ERR_CLSD: printf("Connection closed\n"); break;
  36.                 case ERR_CONN: printf("Not connected\n"); break;
  37.                 case ERR_VAL: printf("Illegal value\n"); break;
  38.                 case ERR_ARG: printf("Illegal argument\n"); break;
  39.                 case ERR_USE: printf("Address in use\n"); break;
  40.                 case ERR_IF: printf("Low-level netif error\n"); break;
  41.                 case ERR_ISCONN: printf("Already connected\n"); break;
  42.                 case ERR_INPROGRESS: printf("Operation in progress\n"); break;
  43.                 default: printf("Unknown error\n");
  44.         }
  45. }
  46. err_t tcp_client_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  47. {
  48.         struct pbuf *q;
  49.         struct tcp_client_app_arg *appcmd = (struct tcp_client_app_arg *)arg;
  50.         uint16_t i = 0;
  51.         uint8_t ip[4];
  52.         uint8_t* str;

  53.         const char* cmd1 = "hello";
  54.         const char* cmd2 = "LEDON";
  55.         const char* cmd3 = "LEDOFF";
  56.         const char* cmd4 = "echo IP";
  57.         const char* cmd5 = "bye";

  58.         printf("tcp client recieve\n");
  59.                
  60.         /* We perform here any necessary processing on the pbuf */
  61.         if (p != NULL)
  62.         {        
  63.                 /* We call this function to tell the LwIp that we have processed the data */
  64.                 /* This lets the stack advertise a larger window, so more data can be received*/
  65.                 tcp_recved(pcb, p->tot_len);
  66.                
  67.                 /* Check the name if NULL, no data passed, return withh illegal argument error */
  68.                 if(appcmd == NULL)
  69.                 {
  70.                   pbuf_free(p);
  71.                   return ERR_ARG;
  72.                 }
  73.                 appcmd->dataptr = (uint8_t*)mem_calloc(sizeof(uint8_t), p->tot_len);
  74.                 if(appcmd->dataptr == NULL){
  75.                         printf("Memory error\n");
  76.                         return ERR_MEM;
  77.                 }
  78.                 appcmd->textlen = p->tot_len;
  79.                 for(q=p; q!=NULL; q=q->next){
  80.                         memcpy((uint8_t*)&appcmd->dataptr[i], q->payload, q->len);  //同一个数据包会存在一个pbuf链表中
  81.                         i = i + q->len;
  82.                 }
  83.                 //应用层代码
  84.                 switch(appcmd->app_state){
  85.                         case CLIENT_CONNECTED:
  86.                         case CLIENT_WAITING_FOR_CMD: {
  87.                                 if(memcmp(appcmd->dataptr, cmd1, strlen(cmd1)) == 0)
  88.                                 {
  89.                                         str = "hello\n";
  90. //                                        tcp_write(pcb, (uint8_t*)str, strlen(str), 1);
  91.                                 }
  92.                                 else if(memcmp(appcmd->dataptr, cmd2, strlen(cmd2)) == 0)
  93.                                 {
  94.                                         GPIO_ResetBits(GPIOF,GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
  95.                                         str="LEDON\n";
  96.                                         //还可以加上这样
  97.                                         /*if(GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_6) == RESET)str="LEDON\n";*/
  98.                                 }
  99.                                 else if(memcmp(appcmd->dataptr, cmd3, strlen(cmd3)) == 0)
  100.                                 {
  101.                                         GPIO_SetBits(GPIOF,GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
  102.                                         str="LEDOFF\n";
  103.                                 }
  104.                                 else if(memcmp(appcmd->dataptr, cmd4, strlen(cmd4)) == 0)
  105.                                 {
  106.                                         ip[0] = 192;  
  107.                                         ip[1] = 168;  
  108.                                         ip[2] = 0;   
  109.                                         ip[3] = 16;
  110. //                                        ip[0] = enc28j60->ip_addr.addr>>24;         //打出来有点不对         
  111. //                                        ip[1] = enc28j60->ip_addr.addr>>16;
  112. //                                        ip[2] = enc28j60->ip_addr.addr>>8;   
  113. //                                        ip[3] = enc28j60->ip_addr.addr;
  114. //                                        ip[0] = pcb->remote_ip.addr>>24;                 
  115. //                                        ip[1] = pcb->remote_ip.addr>>16;
  116. //                                        ip[2] = pcb->remote_ip.addr>>8;   
  117. //                                        ip[3] = pcb->remote_ip.addr;
  118.                                         str = mem_calloc(sizeof(uint8_t), 30);
  119.                                         sprintf((char*)str, "ipaddr:%d,%d,%d,%d\n", ip[3], ip[2], ip[1], ip[0]);
  120.                                         tcp_write(pcb, (const char *)str, strlen(str), 1);
  121.                                         mem_free(str);
  122.                                         break;
  123.                                 }
  124.                                 else if(memcmp(appcmd->dataptr, cmd5, strlen(cmd5)) == 0)
  125.                                 {
  126.                                         appcmd->app_state = CLIENT_CLOSE;
  127.                                        
  128.                                         break;
  129.                                 }
  130.                                 else
  131.                                 {
  132.                                         str = "unknown cmd\n";
  133. //                                        tcp_write(pcb, (uint8_t*)str, strlen(str), 1);
  134.                                 }
  135.                                 tcp_write(pcb, (const char *)str, strlen(str), 1);
  136.                                 break;
  137.                         }
  138.                         default:
  139.                         {
  140.                                 str = "lwip down\n";
  141.                                 tcp_write(pcb, (const char *)str, strlen(str), 1);
  142.                                 break;
  143.                         }
  144.                 }
  145.                 mem_free(appcmd->dataptr);
  146.                 //取得应用层数据后,先释放pbuf队列相应数据链,再对数据进行解析与执行操作
  147.                 /* End of processing, we free the pbuf */
  148.                 pbuf_free(p);
  149.         }  
  150.         else if (err == ERR_OK)
  151.         {
  152.                 /* When the pbuf is NULL and the err is ERR_OK, the remote end
  153.                                                     is closing the connection. */
  154.                 /* We free the allocated memory and we close the connection */
  155.                 mem_free(appcmd);
  156.                 return tcp_close(pcb);
  157.         }
  158.         return ERR_OK;
  159. }
  160. err_t tcp_client_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
  161. {
  162.         struct tcp_client_app_arg* pro_arg = (struct tcp_client_app_arg*)arg;

  163.         printf("tcp client sent\n");

  164.         switch(pro_arg->app_state){
  165.                 case CLIENT_WAITING_FOR_CMD:{
  166.                         printf("acked!\n");
  167.                         break;
  168.                 }
  169.                 case CLIENT_CLOSE:{
  170.                         mem_free(pro_arg);
  171.                         return tcp_close(pcb);
  172.                 }
  173.                 default:{}
  174.         }

  175.         return ERR_OK;
  176. }

  177. err_t tcp_client_poll(void *arg, struct tcp_pcb *pcb)
  178. {
  179.         const char* str = "The data come from the stm32 client\n";

  180.         printf("tcp client poll\n");

  181.         tcp_write(pcb, (const char*)str, strlen(str), 1);

  182.         return ERR_OK;
  183. }
  184. err_t tcp_client_connected(void *arg, struct tcp_pcb *pcb, err_t err)
  185. {
  186.         struct tcp_client_app_arg* app_arg = (struct tcp_client_app_arg*)arg;
  187.         uint8_t* str = "Welcome to the client\n";
  188.        
  189.         printf("tcp client connected\n");

  190.         tcp_err(pcb, tcp_client_errf);                 //指定出错时的回调函数
  191.           tcp_recv(pcb, tcp_client_recv);                 //指定接收到新数据时的回调函数
  192.         tcp_sent(pcb, tcp_client_sent);                 //指定远程主机成功接收到数据的回调函数
  193.         tcp_poll(pcb, tcp_client_poll, 4);         //指定轮询的时间间隔和回调函数(*250ms)

  194.         tcp_write(pcb, (const char *)str, strlen(str), 1);
  195.         app_arg->app_state = CLIENT_WAITING_FOR_CMD;

  196.         return ERR_OK;
  197. }

  198. void tcp_client_init(void)
  199. {
  200.         struct tcp_pcb* client_pcb;
  201.         struct tcp_client_app_arg* app_arg;

  202.         printf("tcp client inti\n");

  203.         destip.addr = (uint32_t)192+(168<<8)+(0<<16)+(105<<24);

  204.         client_pcb = tcp_new();
  205.         if(client_pcb != NULL)
  206.         {
  207. //                tcp_bind(client_pcb,IP_ADDR_ANY,2200);
  208.                 tcp_arg(client_pcb, mem_calloc(sizeof(struct tcp_client_app_arg), 1));   
  209.                 app_arg = client_pcb->callback_arg;
  210.                 app_arg->app_state = CLIENT_WAITING_FOR_CONNECTION;          
  211.                 tcp_connect(client_pcb, &destip, 2200, tcp_client_connected);         //按需求连接,加上条件
  212.         }
  213.         else
  214.         {

  215.                 printf("tcp alloc failed\n");
  216.         }
  217. }


outstanding 发表于 2013-7-30 16:12 | 显示全部楼层
openrd 发表于 2013-7-30 16:37 | 显示全部楼层
can123dao 发表于 2013-7-30 16:00
给你我的做CLIENT的代码吧,是TCP的,

TCP面向连接,过程虽然复杂些,但是感觉靠谱点。请问你有做个UDP接收吗?我觉得我的是回调函数部分有点问题。不过很感谢
 楼主| can123dao 发表于 2013-7-30 16:50 | 显示全部楼层
openrd 发表于 2013-7-30 16:37
TCP面向连接,过程虽然复杂些,但是感觉靠谱点。请问你有做个UDP接收吗?我觉得我的是回调函数部分有点问 ...

我也是这段时间才在弄LWIP以前没学过TCP/IP,UDP还没做过呢,这在弄DHCP,这里面有UDP的,现在有问题,源码里面有行代码
udp_recv(dhcp->pcb, dhcp_recv, netif);
执行时发现并没有调用dhcp_recv。好多东西不懂。
openrd 发表于 2013-7-30 17:32 | 显示全部楼层
can123dao 发表于 2013-7-30 16:50
我也是这段时间才在弄LWIP以前没学过TCP/IP,UDP还没做过呢,这在弄DHCP,这里面有UDP的,现在有问题,源 ...

哦,这样,共同学习,我只是做了简单的发送信息和接收信息,现在分开是可以的。但是还是很多东西不懂,下一步是结合起来,发送特定的数据,然后接收应答。
之前是因为工具问题,换了个工具可以接收了。
hkcj 发表于 2013-7-30 18:56 | 显示全部楼层
问题解决了就好  帮你顶一个   呵呵  很好
 楼主| can123dao 发表于 2013-7-31 18:12 | 显示全部楼层
加上DHCP功能,分配IP成功后,SERVER功能能实现,但作为CLIENT连不上PC,是怎么回事呢
openrd 发表于 2013-8-1 11:15 | 显示全部楼层
UDP收到数据后使用udp_sendto回显,使用UDP测试工具可以看到发送的数据,问题是怎么把数据提取出来?memcpy(Recvdata,p->payload,p->len);貌似不太对啊,Recvdata数组内的数据并不是发送的数据,请教高人解答
likfire 发表于 2013-8-17 21:10 | 显示全部楼层
can123dao 发表于 2013-7-30 16:00
给你我的做CLIENT的代码吧,是TCP的,

您好,我是一名stm32的初学者,现在在做stm32 client上遇到了困难,您可以提供这个工程的源码吗?我的邮箱likfire@qq.com,如果不方便的话也没关系,谢谢!
tao122188 发表于 2014-5-30 01:25 | 显示全部楼层
正在做LWIP 求交流!!!
xinyunliushui 发表于 2014-7-9 16:04 | 显示全部楼层
tao122188 发表于 2014-5-30 01:25
正在做LWIP 求交流!!!

我也是啊,求交流,我的qq412492751,求加上能交流下
gjianw217 发表于 2015-7-13 15:54 | 显示全部楼层
去正点原子论坛看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则

36

主题

114

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部

36

主题

114

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部