STM32F407移植contiki2.6后使用LWIP库实现tcp client

时间:2022-12-16 23:48:20

最近在做智能家居,物联网项目,用到了C下的contiki移植

经过一阵调试,终于在 STM32F407移植contiki2.6后使用LWIP库实现tcp client.

一路艰辛谁人知道....唯有留下代码以资青春

#include "main.h"
#include "contiki.h"
#include "sys\autostart.h"
void led_init();
#define SYSTEMTICK_PERIOD_MS 10
#define AUTOSTART_ENABLE 1
unsigned int idle_count = ; __IO uint32_t LocalTime = ; /* this variable is used to create a time reference incremented by 10ms */
uint32_t timingdelay;
float f_adc_value[ADC_CHANNLE_NUM];
unsigned char str[]; //添加
const static uint8_t TCP_TestData[]=
{
"LwIP TCP Client on sm32f407 to server connected!" //
};
void TCP_Client_Inits();
err_t TCP_Client_Recvs(void *arg, struct tcp_pcb *pcb,struct pbuf *p,err_t err); PROCESS(blink_process, "Blink");
AUTOSTART_PROCESSES(&blink_process);
PROCESS_THREAD(blink_process, ev, data)
{ PROCESS_BEGIN();
while()
{
static struct etimer et;
etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); //打开LED
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
printf("LED ON\r\n");
etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); //关闭LED
GPIO_SetBits(GPIOA,GPIO_Pin_4);
printf("LED OFF\r\n"); }
PROCESS_END();
} int main(void)
{
struct tcp_pcb *pcb;
unsigned int i = ; ETH_BSP_Config();
LwIP_Init();
TCP_Client_Init(TCP_LOCAL_PORT,TCP_SERVER_PORT,TCP_SERVER_IP); //tcp客户端初始化
ADC_Configuration();
RS232_DMA_Init();
USART_232_Configuration(); // dbg_setup_uart();
led_init();
printf("Initialising\r\n");
clock_init();
process_init();
process_start(&etimer_process, NULL);
autostart_start(autostart_processes);
//process_start(&blink_process,NULL);
printf("Processes running\r\n"); while ()
{ TCP_Client_Inits();
//SysCtlDelay(50000);
Delay_s(0x7FFFFFF); //必要的延时 0xfffff
}
} /******* 这是一个回调函数,当TCP客户端请求的连接建立时被调用********/
err_t TcpCli_Connected(void *arg,struct tcp_pcb *pcb,err_t err)
{
tcp_write(pcb,TCP_TestData,sizeof(TCP_TestData)-,); //发送数据 tcp_close(pcb); return ERR_OK;
} /*********************************************************************************************************
** Function name: TCP_Client_Init
** Descriptions: TCP客户端的初始化,当需要建立客户端连接时调用
** input parameters: 无
** output parameters: 无
** Returned value: 0
*********************************************************************************************************/
void TCP_Client_Inits()
{
struct tcp_pcb *Clipcb;
struct ip_addr ipaddr; IP4_ADDR(&ipaddr,,,,); Clipcb = tcp_new(); // 建立通信的TCP控制块(Clipcb) tcp_bind(Clipcb,IP_ADDR_ANY,); // 绑定本地IP地址和端口号 tcp_connect(Clipcb,&ipaddr,,TcpCli_Connected);
tcp_recv(Clipcb,TCP_Client_Recvs);
} /***********************************************************************
函数名称:TCP_Client_Recvs(void *arg, struct tcp_pcb *pcb,struct pbuf *p,err_t err)
功 能:tcp客户端接收数据回调函数
输入参数:
输出参数:
编写时间:2013.4.25
编 写 人:
注 意:这是一个回调函数,当TCP服务器发来数据时调用
***********************************************************************/ err_t TCP_Client_Recvs(void *arg, struct tcp_pcb *pcb,struct pbuf *p,err_t err)
{ char *c;
struct pbuf *q;
if (p != NULL)//如果有数据来了
{
tcp_recved(pcb, p->tot_len);
for(q=p; q != NULL; q = q->next)
{
c = q->payload;
printf("received from server msg :%s\r\n",c);
tcp_write(pcb, c, p->len, );//接着就直接将收到的数据直接发送出去。
//tcp_output(pcb);
}
pbuf_free(p);
}
err = ERR_OK;
return err;
}
/**
* @brief Inserts a delay time.
* @param nCount: number of 10ms periods to wait for.
* @retval None
*/
void Delay(uint32_t nCount)
{
/* Capture the current local time */
timingdelay = LocalTime + nCount; /* wait until the desired delay finish */
while(timingdelay > LocalTime)
{
}
} void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the GPIO_LED Clock */
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
} /**
* @brief Updates the system local time
* @param None
* @retval None
*/
void Time_Update(void)
{
LocalTime += SYSTEMTICK_PERIOD_MS;
} #ifdef USE_FULL_ASSERT /**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */
while ()
{}
} #endif