最近在做智能家居,物联网项目,用到了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

STM32F407移植contiki2.6后使用LWIP库实现tcp client的更多相关文章

  1. Android APP使用NDK编译后的ffmpeg库出现undefined reference to 'posix_memalign'错误

    在android程序中使用NDK编译后的ffmpeg库的时候出现了如下错误: jni/libs/libavutil.a(mem.o): in function av_malloc:libavutil/ ...

  2. 淘淘商城项目_同步索引库问题分析 + ActiveMQ介绍/安装/使用 + ActiveMQ整合spring + 使用ActiveMQ实现添加商品后同步索引库_匠心笔记

    文章目录 1.同步索引库问题分析 2.ActiveM的介绍 2.1.什么是ActiveMQ 2.2.ActiveMQ的消息形式 3.ActiveMQ的安装 3.1.安装环境 3.2.安装步骤 4.Ac ...

  3. (转)lwip TCP client & FreeRTOS 打开TCP 的 保活机制 LWIP_TCP_KEEPALIVE==1

    参考大神教程:http://blog.sina.com.cn/s/blog_62a85b950101aw8x.html   老衲五木 :http://blog.sina.com.cn/s/blog_6 ...

  4. lwip TCP client & FreeRTOS 打开TCP 的 保活机制 LWIP_TCP_KEEPALIVE==1

    参考大神教程:http://blog.sina.com.cn/s/blog_62a85b950101aw8x.html   老衲五木 :http://blog.sina.com.cn/s/blog_6 ...

  5. LwIP应用开发笔记之六:LwIP无操作系统TCP客户端

    上一篇我们基于LwIP协议栈的RAW API实现了一个TCP服务器的简单应用,接下来一节我们来实现一个TCP客户端的简单应用. 1.TCP简述 TCP(Transmission Control Pro ...

  6. LwIP应用开发笔记之五:LwIP无操作系统TCP服务器

    前面我们实现了UDP服务器及客户端以及基于其上的TFTP应用服务器.接下来我们将实现同样广泛应用的TCP协议各类应用. 1.TCP简述 TCP(Transmission Control Protoco ...

  7. Modbus库开发笔记之四:Modbus TCP Client开发

    这一次我们封装Modbus TCP Client应用.同样的我们也不是做具体的应用,而是实现TCP客户端的基本功能.我们将TCP客户端的功能封装为函数,以便在开发具体应用时调用. 对于TCP客户端我们 ...

  8. 升级_宽视野Oracle图形升级(升级后dbca建库)—10.2.0.1.0提拔10.2.0.5.0

    ***********************************************声明**********************************************  原创作 ...

  9. freeType移植总结①——使用keil编译freeType2库

    在各个技术博客搜索相关资料后,终于将freeType的源码用keil工程编译通过,这里记录一下步骤和遇到的问题. 因为网上的资料都是旧版本freeType的工程,这里博主使用的是freeType2.9 ...

随机推荐

  1. nginx利用image_filter动态生成缩略图

    转自:http://www.nginx.cn/2160.html "我如今是有些图片须要生成缩略图.这个如今加了image_filter这个已经实现了.但我不知道怎么样才干訪问我上传的原图& ...

  2. HT518V311

    HT518V311.rar

  3. 解决安装包在win7,win8系统下安装后运行没有管理员权限

    今天打包一个程序在客户机上安装运行:一直报没有管理员权限:客户机是win8系统:直接右键管理员身份运行则都可以:为了避免不让用户每次都这么麻烦:只有问哈群友和百度,终于找到解决方法: 第一步:项目属性 ...

  4. Helpers\Assets

    Helpers\Assets The assets helper is for loading CSS and JS files rather than writing out the full sc ...

  5. centos6.5 安装jdk7和tomcat7

    JDK安装: 安装说明 系统环境:centos-6.5安装方式:rpm安装软件:jdk-7-linux-x64.rpm下载地址:http://www.oracle.com/technetwork/ja ...

  6. 学习strcat与strcpy

    前几天刚有点认识了strlen和sizeof,今天又有点认识了strcpy和strcat. 在vc2010环境下 1 strcpy strcpy(p,q),p要么为数组,要么为空指针,要是为空指针操作 ...

  7. ajax数据保存及解析

    1.   $('#frmCam').serialize():serialize() 方法通过序列化表单值,创建 URL 编码文本字符串. 2.自动对应ID和值 window.camConfig = [ ...

  8. [置顶] c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date'

    c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date' ...

  9. /lib /usr/lib /usr/local/lib 区别

    简单说,/lib是内核级的,/usr/lib是系统级的,/usr/local/lib是用户级的. /lib/ — 包含许多被 /bin/ 和 /sbin/ 中的程序使用的库文件.目录 /usr/lib ...

  10. 求关注 wcf bindipendpointdelegate 端口限制的功能

    我最近也需要实现一个功能:1)一个客户端(192.168.0.15),10个服务端(提供A接口.B接口)如下:      192.168.0.1-5685       192.168.0.2-5685 ...