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

    Middleware The middleware gives a single shot to the views associated into Controllers, before execu ...

  2. Making Use of Forms and Fieldsets

    Making Use of Forms and Fieldsets So far all we have done is read data from the database. In a real- ...

  3. javah 生成header file 报错 问题解决

    环境:Android Studio, Mac OS 目标: 用javah 为MainActivity.class 生成 jni header 文件 正确的命令是 cd <class文件的路径&g ...

  4. ruby sass Encoding::CompatibilityError for changes

    在'compass create projectName','cd projectName'之后,show "Encoding::CompatibilityError on line [&q ...

  5. 浅谈android应用性能之内存(转)

    如何测试一个APP的内存占用情况?一个APP占用的内存分哪些部分?如何检查一个APP是否存在内存泄漏? 一.Android内存介绍: 在java开发过程中,是通过new来为对象分配内存的,而内存的释放 ...

  6. (原创)fedora 17安装KVM虚拟机

    1.安装KVM yum groupinstall Virtualization 'Virtualization Client' 2.安装api支持 yum install libvirt servic ...

  7. JAXB - Annotations, Type Mapping: XmlSchemaType

    The annotation XmlSchemaType defines a mapping between an arbitrary Java type and a simple schema bu ...

  8. asp.net中常用的几种身份验证方式

    转载:http://www.cnblogs.com/dinglang/archive/2012/06/03/2532664.html   前言 在B/S系统开发中,经常需要使用"身份验证&q ...

  9. -exec和|xargs

    注意xargs会被空格割裂,所以遇到带有空格的文件名就不好办了,解决方法是使用-print0 例如:删除.目录下30天之前的.png文件 -type f -name rm 或者使用-exec:删除.目 ...

  10. HTML——表格table标签,tr或者td

    表格定义和用法 <tr> 标签定义 HTML 表格中的行. tr 元素包含一个或多个 th 或 td 元素. HTML 与 XHTML 之间的差异 在 HTML 4.01 中,tr 元素的 ...