Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
shell 在tcp上的应用
发布于 2014-10-21 20:29:05 浏览:3978
订阅该版
大家都知道RTT的 shell很好用,但是只能通过串口,无疑在系统使用网络,而串口又不足的情况增加了不少限制, 本帖提供一种将 shell 设备设置为tcp的方案 设计为创建一个tcp服务器,和一个serial 设备,tcp服务器收到数据则等同于串口ISR, 操作serial 缓存, 初始化之后 使用超级终端 telnet 目标板即可启动 tcp上的 shell 下面为代码 ```#include "serial.h" #include
#include
#include
#include
#include
#include
int connected; /* RT-Thread Device Interface */ static rt_err_t rt_tcp_serial_init (rt_device_t dev) { struct stm32_serial_device* uart = (struct stm32_serial_device*) dev->user_data; if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) { if (dev->flag & RT_DEVICE_FLAG_INT_RX) { rt_memset(uart->int_rx->rx_buffer, 0, sizeof(uart->int_rx->rx_buffer)); uart->int_rx->read_index = 0; uart->int_rx->save_index = 0; } dev->flag |= RT_DEVICE_FLAG_ACTIVATED; } return RT_EOK; } static rt_err_t rt_tcp_serial_open(rt_device_t dev, rt_uint16_t oflag) { return RT_EOK; } static rt_err_t rt_tcp_serial_close(rt_device_t dev) { return RT_EOK; } static rt_size_t rt_tcp_serial_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) { rt_uint8_t* ptr; rt_err_t err_code; struct stm32_serial_device* uart; ptr = buffer; err_code = RT_EOK; uart = (struct stm32_serial_device*)dev->user_data; if (dev->flag & RT_DEVICE_FLAG_INT_RX) { /* interrupt mode Rx */ while (size) { rt_base_t level; /* disable interrupt */ level = rt_hw_interrupt_disable(); if (uart->int_rx->read_index != uart->int_rx->save_index) { /* read a character */ *ptr++ = uart->int_rx->rx_buffer[uart->int_rx->read_index]; size--; /* move to next position */ uart->int_rx->read_index ++; if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE) uart->int_rx->read_index = 0; } else { /* set error code */ err_code = -RT_EEMPTY; /* enable interrupt */ rt_hw_interrupt_enable(level); break; } /* enable interrupt */ rt_hw_interrupt_enable(level); } } else { /* polling mode */ while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size) { while (uart->uart_device->SR & USART_FLAG_RXNE) { *ptr = uart->uart_device->DR & 0xff; ptr ++; } } } /* set error code */ rt_set_errno(err_code); return (rt_uint32_t)ptr - (rt_uint32_t)buffer; } static rt_size_t rt_tcp_serial_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) { rt_uint8_t* ptr; rt_err_t err_code; struct stm32_serial_device* uart; err_code = RT_EOK; ptr = (rt_uint8_t*)buffer; uart = (struct stm32_serial_device*)dev->user_data; if (dev->flag & RT_DEVICE_FLAG_INT_TX) { /* interrupt mode Tx, does not support */ RT_ASSERT(0); } { /* polling mode */ if (dev->flag & RT_DEVICE_FLAG_STREAM) { if(*(ptr+size-1)==' ') { char n[3]={' ',' ',0}; lwip_write(connected,buffer,size-1); lwip_write(connected,n,2); size++; } else lwip_write(connected,buffer,size); } else lwip_write(connected,buffer,size); } /* set error code */ rt_set_errno(err_code); return size; } static rt_err_t rt_tcp_serial_control (rt_device_t dev, rt_uint8_t cmd, void *args) { struct stm32_serial_device* uart; RT_ASSERT(dev != RT_NULL); uart = (struct stm32_serial_device*)dev->user_data; switch (cmd) { case RT_DEVICE_CTRL_SUSPEND: /* suspend device */ dev->flag |= RT_DEVICE_FLAG_SUSPENDED; break; case RT_DEVICE_CTRL_RESUME: /* resume device */ dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED; break; } return RT_EOK; } /* * serial register for STM32 * support STM32F103VB and STM32F103ZE */ rt_err_t rt_hw_tcp_serial_register(rt_device_t device, const char* name, rt_uint32_t flag, struct stm32_serial_device *serial) { RT_ASSERT(device != RT_NULL); if ((flag & RT_DEVICE_FLAG_DMA_RX) || (flag & RT_DEVICE_FLAG_INT_TX)) { RT_ASSERT(0); } device->type = RT_Device_Class_Char; device->rx_indicate = RT_NULL; device->tx_complete = RT_NULL; device->init = rt_tcp_serial_init; device->open = rt_tcp_serial_open; device->close = rt_tcp_serial_close; device->read = rt_tcp_serial_read; device->write = rt_tcp_serial_write; device->control = rt_tcp_serial_control; device->user_data = serial; /* register a character device */ return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag); } struct stm32_serial_int_rx rx_buff; struct stm32_serial_device tcp_serial={0,&rx_buff,0}; struct rt_device tcp_serial_device; char* old_shell; void tcp_serial_rev(void*arg) { char buff[UART_RX_BUFFER_SIZE]; int recv_len; while(1) { recv_len=lwip_recv(*(int*)arg,buff,UART_RX_BUFFER_SIZE-1,0); if(recv_len<=0) { int err,errsize=4; lwip_getsockopt(*(int*)arg,SOL_SOCKET,SO_ERROR,&err,&errsize); if(err== ENOTCONN) { lwip_close(*(int*)arg); if(old_shell) { rt_console_set_device(old_shell); finsh_set_device(old_shell);//?è?????????? shell finsh_set_echo(1); } return ; } } {int i; if(buff[recv_len-2]==' ') { buff[recv_len-2]=' '; recv_len--; } for(i=0;i
rx_buffer[tcp_serial.int_rx->save_index++]=buff*; if(tcp_serial.int_rx->save_index>=UART_RX_BUFFER_SIZE) tcp_serial.int_rx->save_index=0; } } if (tcp_serial.int_rx->save_index == tcp_serial.int_rx->read_index) { tcp_serial.int_rx->read_index ++; if (tcp_serial.int_rx->read_index >= UART_RX_BUFFER_SIZE) tcp_serial.int_rx->read_index = 0; } /* invoke callback */ if (tcp_serial_device.rx_indicate != RT_NULL) { rt_size_t rx_length; /* get rx length */ rx_length = tcp_serial.int_rx->read_index > tcp_serial.int_rx->save_index ? UART_RX_BUFFER_SIZE - tcp_serial.int_rx->read_index + tcp_serial.int_rx->save_index : tcp_serial.int_rx->save_index - tcp_serial.int_rx->read_index; tcp_serial_device.rx_indicate(&tcp_serial_device, rx_length); } } } void serial_init(void*arg) { rt_thread_t serial_thread; struct sockaddr_in addr; err_t err; int sock=-1; rt_uint32_t sin_size; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { rt_kprintf("create socket error "); //goto _exit; } /* initialize server address */ addr.sin_family = AF_INET; addr.sin_port = htons(23); addr.sin_addr.s_addr = INADDR_ANY; rt_memset(&(addr.sin_zero),8, sizeof(addr.sin_zero)); /* bind to server address */ if (lwip_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) { rt_kprintf("bind address failed "); //goto _exit; } /* listen */ if (listen(sock, 1) == -1) { rt_kprintf("listen error "); //goto _exit; } sin_size = sizeof(struct sockaddr_in); for(;;) { connected = lwip_accept(sock, (struct sockaddr *)&addr, &sin_size); { old_shell=(char*)finsh_get_device(); rt_console_set_device("tcp_serial"); finsh_set_device("tcp_serial"); finsh_set_echo(0); serial_thread=rt_thread_create("tcp_serial_rev",tcp_serial_rev,&connected,1024,10,5); if(serial_thread!=RT_NULL) rt_thread_startup(serial_thread); } } } void rt_hw_tcp_serial_init(void) { rt_thread_t serial_init_thread; serial_init_thread=rt_thread_create("serial_init",serial_init,RT_NULL,512,15,5); if(serial_init_thread!=RT_NULL) rt_thread_startup(serial_init_thread); rt_hw_tcp_serial_register(&tcp_serial_device,"tcp_serial",RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, &tcp_serial); } /*@}*/ ```
查看更多
6
个回答
默认排序
按发布时间排序
pangwei
2014-10-21
这家伙很懒,什么也没写!
跟telnet的例子有啥区别?
枫魔龙少
2014-10-21
这家伙很懒,什么也没写!
>跟telnet的例子有啥区别? --- telnet 例程,或者,telnet 服务器需要自己实现。 而我这种方式,是直接shell运行在 tcp上了,也就是运行机制已经由shell实现
pangwei
2014-10-22
这家伙很懒,什么也没写!
早就有了。
viewtopic.php?t=1958
枫魔龙少
2014-10-22
这家伙很懒,什么也没写!
>早就有了。
viewtopic.php?t=1958
--- 这个倒没注意看 刚浏览了一下帖子,发现他实现的稍复杂 呵呵过程不必在意实现功能就行了,
小ARM菜菜
2015-04-16
这家伙很懒,什么也没写!
好
撰写答案
登录
注册新账号
关注者
0
被浏览
4k
关于作者
枫魔龙少
这家伙很懒,什么也没写!
提问
4
回答
10
被采纳
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
【NXP-MCXA153】 定时器驱动移植
2
GD32F450 看门狗驱动适配
3
【NXP-MCXA153】看门狗驱动移植
4
RT-Thread Studio V2.2.9 Release Note
5
CherryUSB的bootuf2配置
热门标签
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
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
PWM
freemodbus
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
编译报错
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
踩姑娘的小蘑菇
7
个答案
2
次被采纳
a1012112796
16
个答案
1
次被采纳
Ryan_CW
5
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
本月文章贡献
YZRD
3
篇文章
6
次点赞
catcatbing
3
篇文章
6
次点赞
lizimu
2
篇文章
9
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部