Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
TCP/IP网络组件Lwip实现win超级终端远程命令行交互
发布于 2014-02-06 13:03:39 浏览:3534
订阅该版
参考“TCP/IP网络组件Lwip之telnet远程命令行交互”帖子,实现win超级终端下的远程命令行交互 ```#include
#include "rtdevice.h" #include
#include
#include
#define TCPHYP_PORT 23 #define TCPHYP_RX_BUFFER 4096 #define TCPHYP_TX_BUFFER 4096 #define NW_RX 0x01 #define NW_TX 0x02 #define NW_CLOSED 0x04 #define NW_MASK (NW_RX | NW_TX | NW_CLOSED) struct hyptcp_session { struct rt_ringbuffer rx_ringbuffer; struct rt_ringbuffer tx_ringbuffer; rt_sem_t rx_ringbuffer_lock; rt_sem_t tx_ringbuffer_lock; struct rt_device device; rt_event_t nw_event; struct netconn* conn; }; struct hyptcp_session* hyptcp; /* RT-Thread Device Driver Interface */ static rt_err_t hyptcp_init(rt_device_t dev) { return RT_EOK; } static rt_err_t hyptcp_open(rt_device_t dev, rt_uint16_t oflag) { return RT_EOK; } static rt_err_t hyptcp_close(rt_device_t dev) { return RT_EOK; } static rt_size_t hyptcp_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) { rt_size_t result; /* read from rx ring buffer */ rt_sem_take(hyptcp->rx_ringbuffer_lock, RT_WAITING_FOREVER); result = rt_ringbuffer_get(&(hyptcp->rx_ringbuffer), buffer, size); rt_sem_release(hyptcp->rx_ringbuffer_lock); return result; } static rt_size_t hyptcp_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) { const rt_uint8_t *ptr; ptr = (rt_uint8_t*)buffer; rt_sem_take(hyptcp->tx_ringbuffer_lock, RT_WAITING_FOREVER); while (size) { if (*ptr == ' ') rt_ringbuffer_putchar(&hyptcp->tx_ringbuffer, ' '); if (rt_ringbuffer_putchar(&hyptcp->tx_ringbuffer, *ptr) == 0) /* overflow */ break; ptr ++; size --; } rt_sem_release(hyptcp->tx_ringbuffer_lock); /* send to network side */ rt_event_send(hyptcp->nw_event, NW_TX); return (rt_uint32_t)ptr - (rt_uint32_t)buffer; } static rt_err_t hyptcp_control(rt_device_t dev, rt_uint8_t cmd, void *args) { return RT_EOK; } /* netconn callback function */ void rx_callback(struct netconn *conn, enum netconn_evt evt, rt_uint16_t len) { if (evt == NETCONN_EVT_RCVPLUS) { rt_event_send(hyptcp->nw_event, NW_RX); } if (conn->state == NETCONN_CLOSE) { rt_event_send(hyptcp->nw_event, NW_CLOSED); } } /* process tx data */ void hyptcp_process_tx(struct hyptcp_session* hyptcp, struct netconn *conn) { rt_size_t length; rt_uint8_t tx_buffer[512]; while (1) { rt_memset(tx_buffer, 0, sizeof(tx_buffer)); rt_sem_take(hyptcp->tx_ringbuffer_lock, RT_WAITING_FOREVER); /* get buffer from ringbuffer */ length = rt_ringbuffer_get(&(hyptcp->tx_ringbuffer), tx_buffer, sizeof(tx_buffer)); rt_sem_release(hyptcp->tx_ringbuffer_lock); /* do a tx procedure */ if (length > 0) { netconn_write(conn, tx_buffer, length, NETCONN_COPY); } else break; } } /* process rx data */ void hyptcp_process_rx(struct hyptcp_session* hyptcp, rt_uint8_t *data, rt_size_t length) { rt_size_t rx_length, index; for (index = 0; index < length; index ++) { /* take semaphore */ rt_sem_take(hyptcp->rx_ringbuffer_lock, RT_WAITING_FOREVER); /* put buffer to ringbuffer */ rt_ringbuffer_putchar(&(hyptcp->rx_ringbuffer), *data); /* release semaphore */ rt_sem_release(hyptcp->rx_ringbuffer_lock); data ++; } rt_sem_take(hyptcp->rx_ringbuffer_lock, RT_WAITING_FOREVER); /* get total size */ rx_length = RT_RINGBUFFER_SIZE(&hyptcp->rx_ringbuffer); rt_sem_release(hyptcp->rx_ringbuffer_lock); /* indicate there are reception data */ if ((rx_length > 0) && (hyptcp->device.rx_indicate != RT_NULL)) hyptcp->device.rx_indicate(&hyptcp->device, rx_length); return; } /* process netconn close */ void hyptcp_process_close(struct hyptcp_session* hyptcp, struct netconn *conn) { /* set console */ rt_console_set_device("Uart0"); /* set finsh device */ finsh_set_device("Uart0"); /* close connection */ netconn_close(conn); /* restore shell option */ // finsh_set_echo(hyptcp->echo_mode); rt_kprintf("Resume console to Uart0 "); } /* hyptcp server thread entry */ void hyptcp_thread(void* parameter) { rt_err_t result = 0;//, err = 0; rt_uint32_t event; struct netbuf *buf; struct netconn *conn, *newconn; /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number 7. */ netconn_bind(conn, NULL, TCPHYP_PORT); /* Tell connection to go into listening mode. */ netconn_listen(conn); /* register hyptcp device */ hyptcp->device.type = RT_Device_Class_Char; hyptcp->device.init = hyptcp_init; hyptcp->device.open = hyptcp_open; hyptcp->device.close = hyptcp_close; hyptcp->device.read = hyptcp_read; hyptcp->device.write = hyptcp_write; hyptcp->device.control = hyptcp_control; /* no private */ hyptcp->device.user_data = RT_NULL; /* register hyptcp device */ rt_device_register(&hyptcp->device, "HypTcp", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM); while (1) { // rt_kprintf("hyptcp server waiting for connection "); /* Grab new connection. */ // err = netconn_accept(conn, &newconn); netconn_accept(conn, &newconn); //if (err == RT_EOK) continue; /* set network rx call back */ newconn->callback = rx_callback; rt_kprintf("New hyptcp connection, switch console to hyptcp... "); /* Process the new connection. */ /* set console */ rt_console_set_device("HypTcp"); /* set finsh device */ finsh_set_device("HypTcp"); // hyptcp->echo_mode = finsh_get_echo(); // /* disable echo mode */ // finsh_set_echo(0); hyptcp_process_tx(hyptcp, newconn); while (1) { /* try to send all data in tx ringbuffer */ hyptcp_process_tx(hyptcp, newconn); /* receive network event */ result = rt_event_recv(hyptcp->nw_event, NW_MASK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_TICK_PER_SECOND, &event); if (result == RT_EOK) { /* get event successfully */ if (event & NW_RX) { /* do a rx procedure */ // err= netconn_recv(newconn, &buf); netconn_recv(newconn, &buf); if (buf != RT_NULL) { rt_uint8_t *data; rt_uint16_t length; /* get data */ netbuf_data(buf, (void**)&data, &length); hyptcp_process_rx(hyptcp, data, length); /* release buffer */ netbuf_delete(buf); } else { if (newconn->last_err == ERR_CLSD) { /* close connection */ hyptcp_process_close(hyptcp, newconn); break; } } } if (event & NW_TX) hyptcp_process_tx(hyptcp, newconn); if (event & NW_CLOSED) { /* process close */ hyptcp_process_close(hyptcp, newconn); break; } } else if (result == -RT_ETIMEOUT) { if (newconn->state == NETCONN_CLOSE) { hyptcp_process_close(hyptcp, newconn); break; } } } } } /* hyptcp server */ void hyptcp_srv(void) { rt_thread_t tid; if (hyptcp == RT_NULL) { rt_uint8_t *ptr; hyptcp = rt_malloc (sizeof(struct hyptcp_session)); if (hyptcp == RT_NULL) { rt_kprintf("hyptcp: no memory "); return; } /* init ringbuffer */ ptr = rt_malloc (TCPHYP_RX_BUFFER); rt_ringbuffer_init(&hyptcp->rx_ringbuffer, ptr, TCPHYP_RX_BUFFER); /* create rx ringbuffer lock */ hyptcp->rx_ringbuffer_lock = rt_sem_create("rxrb", 1, RT_IPC_FLAG_FIFO); ptr = rt_malloc (TCPHYP_TX_BUFFER); rt_ringbuffer_init(&hyptcp->tx_ringbuffer, ptr, TCPHYP_TX_BUFFER); /* create tx ringbuffer lock */ hyptcp->tx_ringbuffer_lock = rt_sem_create("txrb", 1, RT_IPC_FLAG_FIFO); /* create network event */ hyptcp->nw_event = rt_event_create("hyptcp", RT_IPC_FLAG_FIFO); } tid = rt_thread_create("HypTcp", hyptcp_thread, RT_NULL, 4096, 13, 5); if (tid != RT_NULL) rt_thread_startup(tid); } // #ifdef RT_USING_FINSH // #include
// FINSH_FUNCTION_EXPORT(hyptcp_srv, startup hyptcp server); // #endif``` ![QQ截图20140206130438.png](https://oss-club.rt-thread.org/uploads/4169_3d12cd930dfda98d9344e2aa7874429d.png) ![QQ截图20140206130854.png](https://oss-club.rt-thread.org/uploads/4169_dd54a906df6b758b522839650d61e868.png) ![QQ截图20140206131404.png](https://oss-club.rt-thread.org/uploads/4169_0f2537e75d5d65607925b7f3a0c0e5de.png)
查看更多
5
个回答
默认排序
按发布时间排序
bernard
2014-02-06
这家伙很懒,什么也没写!
超级终端的tcp/ip连接,好像和telnet是非常类似的
Q007
2015-11-02
这家伙很懒,什么也没写!
第一次从串口切换到telnet是没有问题,但telnet断开连接后,控制器不能再回到串口,再次连接telnet也连不上了。
pangwei
2015-11-03
这家伙很懒,什么也没写!
已经存在的bug,只是没人去修复
bernard
2015-11-03
这家伙很懒,什么也没写!
毕竟示例级的和产品级的差别很大,我记得这个是一份示例代码,离demo级都还差些啊
撰写答案
登录
注册新账号
关注者
0
被浏览
3.5k
关于作者
sea_start
这家伙很懒,什么也没写!
提问
3
回答
0
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
ulog 日志 LOG_HEX 输出时间改为本地日期时间
2
在RT-Thread Studio中构建前执行python命令
3
研究一了一段时间RTT,直接标准版上手太难,想用nano,但又舍不得组件
4
CherryUSB开发笔记(一):FSDEV USB IP核的 HID Remote WakeUp (USB HID 远程唤醒) 2025-01-18 V1.1
5
RT-thread 缩写字典
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
PWM
cubemx
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
7
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
xusiwei1236
4
个答案
1
次被采纳
张世争
1
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
2
次点赞
Ghost_Girls
1
篇文章
6
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部