LWIP版本号
0.0 LWIP使用问题
0.1 TCP
0.2 Modbus-TCP
/ * @brief modbus-tcp 初始化 * @note * @param * @retval None */ void MODBUS_TCP_init( void ) {
eMBDisable(); eMBTCPInit( lan_para.modbus_tcp_port ); /启动FreeModbus*/ eMBEnable(); if ( g_modbus_tcp_task_handle ) {
osThreadTerminate( g_modbus_tcp_task_handle ); } /* Create the Modbus Process handler thread */ osThreadDef( ModbusTcpServerConn, MODBUS_TCP_server_conn, osPriorityNormal, 0, 128 ); g_modbus_tcp_task_handle = osThreadCreate( osThread( ModbusTcpServerConn ), NULL ); } / * @brief MODBUS_TCP_server_conn * MODBUS_TCP 服务器监听任务 * @param arg 任务参数 * @return void */ static void MODBUS_TCP_server_conn( void const *arg ) {
struct netconn *conn, *newconn; err_t err; /* 创建一个TCP连接 */ conn = netconn_new( NETCONN_TCP ); if ( conn != NULL ) {
/* 绑定modbus端口号 */ err = netconn_bind( conn, IP_ADDR_ANY, lan_para.modbus_tcp_port ); if ( err == ERR_OK ) {
/* 进入监听模式 */ netconn_listen_with_backlog(conn,0); // netconn_listen( conn ); while ( 1 ) {
/* 阻塞,直到有modbus tcp 连接请求 */ err = netconn_accept( conn, &newconn ); if ( err == ERR_OK ) {
if(MODBUS_TCP_conn_count<2) {
/* 连接成功,创建新的线程处理modbus tcp连接请求 */ osThreadDef( ModbusProcessTask, MODBUS_process_task, osPriorityHigh, 0, 500 ); osThreadCreate( osThread( ModbusProcessTask ), newconn ); } else {
netconn_close( newconn ); netconn_delete( newconn ); } } } } } } / * @brief MODBUS TCP处理程序。 * @note MODBUS TCP处理程序 * @param None * @retval None */ void MODBUS_process_task( void const *arg ) {
struct netconn *conn; struct netbuf *buf; err_t accept_err; err_t err; void *data; u16_t len; LWIP_UNUSED_ARG( arg ); MODBUS_TCP_conn_count++; /* 当前控制端口连接 */ conn = ( struct netconn * )arg; /* 开启连接保活检测 */ ip_set_option( conn->pcb.tcp, SOF_KEEPALIVE ); /* 阻塞,直到接收数据 */ while ( netconn_recv( conn, &buf ) == ERR_OK ) {
do {
/* 提取数据指针 */ netbuf_data( buf, ( void ** )&data, &len ); if ( lan_para.modbus_tcp_enable ) {
if ( len > MB_TCP_BUF_SIZE ) {
ucTCPRequestLen = MB_TCP_BUF_SIZE; memcpy( ucTCPRequestFrame, data, ucTCPRequestLen ); } else {
ucTCPRequestLen = len; memcpy( ucTCPRequestFrame, data, ucTCPRequestLen ); } // MY_DEBUG_LOG("MODBUS_process_task free stack size : %u\n",(int32_t)uxTaskGetStackHighWaterMark(NULL)); // osDelay(10); /* 向 modbus poll发送消息 */ xMBPortEventPost( EV_FRAME_RECEIVED ); eMBPoll(); err = netconn_write( conn, pucTCPResponseFrame, ucTCPResponseLen, NETCONN_COPY); if( ERR_OK != err ) {
MY_DEBUG_LOG( "netconn_write err : %u\n", err ); } } } while( netbuf_next( buf ) >= 0 ); netbuf_delete( buf ); } MODBUS_TCP_conn_count--; netbuf_delete( buf ); /* Close connection and discard connection identifier. */ netconn_close( conn ); netconn_delete( conn ); vTaskDelete( NULL ); /* 断开连接时,删除自己 */ }
? 1 设置主机名称
https://blog.csdn.net/weixin_/article/details/
? 2 编程API
https://blog.csdn.net/_33559992/article/details/112616178?l
STM32每个系列都会有唯一的一个芯片序列号(96位bit):
STM32F10X 的地址是 0x1FFFF7E8 STM32F20X 的地址是 0x1FFF7A10 STM32F30X 的地址是 0x1FFFF7AC STM32F40X 的地址是 0x1FFF7A10 STM32L1XX 的地址是 0x1FF80050
芯片STM32F207
#define STM32_SERIAL0 (*(__IO uint32_t ) 0x1fff7a10)
#define STM32_SERIAL1 (
(__IO uint32_t ) 0x1fff7a14)
#define STM32_SERIAL2 (
(__IO uint32_t *) 0x1fff7a18)
读取96位的芯片ID,
MAC地址的第1字节的第8Bit(00-50-BA-…对应的00000000-0–…,加粗字体的Bit)标识这个地址是组播地址还是单播地址,0单播,1组播
uint32 McuSerNo[2],tmp[2];
uint8 MacAdress[6];
McuSerNo[0] = STM32_SERIAL0;
McuSerNo[1] = STM32_SERIAL1;
tmp[0] = McuSerNo[0] << 2 ;
tmp[1] = ((McuSerNo[0] >> 30) & 0x03 ) + (McuSerNo[1] << 2) & 0xfffffffc;
memcpy(MacAdress,tmp,6);
?3 MAC地址格式规定第一个字节为偶数(MAC地址16进制中的第一个字节第二个数一定是偶数)
0-0-0-00-00011110-00 单播:第八位为0
如果你将MAC改为:61-62-6D-26-1E-29,这样的地址是组播地址,修改不会成功。
注:我们在写程序随机生成MAC地址时,切记MAC地址16进制中的第一个字节第二个数一定是偶数。
4 TFTP
TFTP服务器的文件上传和下载速度有1MB/S左右,比FTP服务器要慢一半
理论上来说UDP传输速率会大于TCP,主要是因为TCP传输存在慢启动和用塞避免。当网络中存在冲突的时候,会大幅的降低传输速度,这个时候UDP就是抢占上来,而TCP会越来越慢。
虽然tftp是基于UDP的,ftp是基于tcp的,但是tftp的传输速度远不及tfp。tftp采用的是简单的停止等待协议,发出去的UDP包必须要等待对方的回答或者超时才能开始下一个UDP发送或者重传。而FTP只要对方有ACK表示有win空间就可以持续的发,所以FTP要比TFTP快很多。

5 超时设置
#define LWIP_TCP_KEEPALIVE 1 #define TCP_KEEPIDLE_DEFAULT 5000UL//UL /* Default KEEPALIVE timer in milliseconds */ #define TCP_KEEPINTVL_DEFAULT 1000UL//75000UL /* Default Time between KEEPALIVE probes in milliseconds */ #define TCP_KEEPCNT_DEFAULT 5U//9U /* Default Counter for KEEPALIVE probes */ /* 创建一个TCP连接 */ conn = netconn_new( NETCONN_TCP ); if ( conn != NULL ) {
/* 开启连接保活检测 */ ip_set_option( conn->pcb.tcp, SOF_KEEPALIVE ); /* 绑定modbus端口号 */ err = netconn_bind( conn, IP_ADDR_ANY, lan_para.modbus_tcp_port ); if ( err == ERR_OK ) {
/* 进入监听模式 */ // netconn_listen_with_backlog(conn,0); netconn_listen( conn ); while ( 1 ) {
/* 阻塞,直到有modbus tcp 连接请求 */ err = netconn_accept( conn, &newconn ); if ( accept_err == ERR_OK ) {
while ( netconn_recv( newconn, &buf ) == ERR_OK ) {
} netbuf_delete( buf ); /* Close connection and discard connection identifier. */ netconn_close( newconn ); netconn_delete( newconn ); } } } }
/* 开启接收超时功能 */ #define LWIP_SO_RCVTIMEO 1 /* 设置接收超时 */ netconn_set_recvtimeout(conn,60000);


6 内存管理
opt.h / * MEM_SIZE: the size of the heap memory. If the application will send * a lot of data that needs to be copied, this should be set high. */ #if !defined MEM_SIZE || defined __DOXYGEN__ #define MEM_SIZE (20*1024) #endif mem.c #define MEM_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SIZE) /*----- Default Value for H7 devices: 0x -----*/ #define LWIP_RAM_HEAP_POINTER 0x / * Zero the heap and initialize start, end and lowest-free */ void mem_init(void) {
struct mem *mem; LWIP_ASSERT("Sanity check alignment", (SIZEOF_STRUCT_MEM & (MEM_ALIGNMENT - 1)) == 0); /* align the heap */ ram = (u8_t *)LWIP_MEM_ALIGN(LWIP_RAM_HEAP_POINTER); /* initialize the start of the heap */ mem = (struct mem *)(void *)ram; mem->next = MEM_SIZE_ALIGNED; mem->prev = 0; mem->used = 0; /* initialize the end of the heap */ ram_end = ptr_to_mem(MEM_SIZE_ALIGNED); ram_end->used = 1; ram_end->next = MEM_SIZE_ALIGNED; ram_end->prev = MEM_SIZE_ALIGNED; MEM_SANITY(); /* initialize the lowest-free pointer to the start of the heap */ lfree = (struct mem *)(void *)ram; MEM_STATS_AVAIL(avail, MEM_SIZE_ALIGNED); if (sys_mutex_new(&mem_mutex) != ERR_OK) {
LWIP_ASSERT("failed to create mem_mutex", 0); } }
7 netconn
lwIP TCP/IP 协议栈笔记之十六: NETCONN 接口编程
netconn_listen_with_backlog(conn,1);
listen函数中backlog的含义
8 FTP



9 socket
《嵌入操作系统 – 玩转ART-Pi开发板》第9章 基于Select/Poll实现并发服务器(二)
dhcp
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218048.html原文链接:https://javaforall.net
