HC32F4A0-LwIP应用之TCP串口服务器
[复制链接]
HC32F4A0_FreeRTOS_LwIP移植 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)
上篇,已经移植了LwIP,并且成功Ping通。本篇,我们开始运用LwIP的Netconn API接口,最终实现TCP串口服务器。
首先,我们先实现TCP回显服务器。功能如下,在HC32F4A0所在的板子上创建TCP服务器,PC机使用网络调试助手,发送任意数据,服务器回送该数据。由于,前两篇已经实现FreeRTOS和LwIP的移植,现在直接创建TCP服务器任务和编写回显服务器。
先使用struct netconn结构体,创建全局指针变量,*tcpconn和*newconn,tcpconn是用来创建新的连接,连接类型可以为TCP或UDP,这里我们选TCP。newconn用来存放侦听到的新的连接。总体步骤为 创建连接-->绑定端口-->进入侦听状态-->等待新的连接。新的TCP客户端连接上后,进入一个循环,在这个循环中,我们以阻塞的方式等待数据,当有数据时,调用netbuf_data函数将数据取出来,进行相应的操作。以下是TCP回显服务器的代码:
/**************************************************************
* [url=home.php?mod=space&uid=32621]@name[/url] xTaskTcpServer
* [url=home.php?mod=space&uid=159083]@brief[/url] * @param pvParameters: [输入/出]
* @retval
* [url=home.php?mod=space&uid=1315547]@author[/url] Zachary
* [url=home.php?mod=space&uid=34591]@data[/url] 2023-03-27
**************************************************************/
uint8_t vlist = 0x0;
static void xTaskTcpServer( void *pvParameters )
{
err_t err = ERR_OK;
struct netbuf *recvbuf;
void *data = NULL;
uint16_t len = 0;
tcpconn = netconn_new( NETCONN_TCP );
netconn_bind( tcpconn, IPADDR_ANY, 5000 );
netconn_listen( tcpconn );
for( ;; )
{
err = netconn_accept( tcpconn, &newconn );
char *DispChar = NULL;
LCD_ShowString( 10, 370, lcddev.width, lcddev.height, 24, ( uint8_t * )"Tcp Client is Connected...", WHITE, BLACK );
LCD_ShowString( 10, 400, lcddev.width, lcddev.height, 24, ( uint8_t * )"Receive:", WHITE, BLACK );
if( err == ERR_OK )
{
for( ;; )
{
if( ( err = netconn_recv( newconn, &recvbuf ) ) == ERR_OK )
{
do
{
netbuf_data( recvbuf, &data, &len );
netconn_write( newconn, data, len, NETCONN_COPY );
DispChar = ( char * )malloc( len );
memcpy( DispChar, data, len );
if( vlist >= 5 )
{
vlist = 0;
LCD_ShowString( 106, 400 + 0 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 1 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 2 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 3 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 4 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 5 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400, lcddev.width, lcddev.height, 24, ( uint8_t * )DispChar, WHITE, BLACK );
}
else
{
LCD_ShowString( 106, 400 + vlist * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )DispChar, WHITE, BLACK );
}
vlist++;
} while( netbuf_next( recvbuf ) >= 0 );
netbuf_delete( recvbuf );
free( DispChar );
}
else
{
//__RS232_SVR_DESTROY:
netconn_close( newconn );
while( ERR_OK != netconn_delete( newconn ) )
{
vTaskDelay( 1 );
}
vlist = 0;
LCD_ShowString( 10, 370, lcddev.width, lcddev.height, 24, ( uint8_t * )"Tcp Client is Closed... ", WHITE, BLACK );
LCD_ShowString( 10, 400, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 0 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 1 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 2 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 3 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 4 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
LCD_ShowString( 106, 400 + 5 * 24, lcddev.width, lcddev.height, 24, ( uint8_t * )" ", WHITE, BLACK );
break;
}
}
}
vTaskDelay( 10 );
}
}
实验现象:
TcpServer
工程源码:
|