前言
终于来到了 serialX 的实践篇,期待很久了。
笔者曾经在 [rt-thread 使用宝典(2022-0516更新)](https://club.rt-thread.org/ask/article/2460fcd7db4821ae.html) 这篇文章的“使用篇: Q1. 串口通讯数据被分多次接收了,怎么办?”里贴了一段代码,那段代码有很强的适用性,稍作修改就能用到多种串口协议处理场合。今天我们尝试在 finsh 上应用 serialX,看看它能给我们带来什么神奇效果。
打开控制台
我们的 serialX 支持中断收发、DMA 收发。所以我们可以随意组合使用 中断收、中断发、DMA 收、DMA 发,共四种组合(前提是对应芯片底层驱动支持 DMA)。
if (rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX ) == RT_EOK) { }
或者
if (rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_DMA_RX | RT_DEVICE_FLAG_DMA_TX ) == RT_EOK) { }
因为 serialX 自带阻塞读特性,所以它不需要执行 `rt_device_set_rx_indicate(dev, finsh_rx_ind);` 这句代码,我们接收数据自有同步妙法,请往下看。
finsh 线程
对 finsh 线程入口函数稍作修改:
void finsh_thread_entry(void *parameter){ int i, cnt; char istream[32]; ... 省略部分操作 while (1) { cnt = finsh_instream(istream, 32); for (i = 0; i < cnt; i++) { finsh_handle_onebyte(istream[i]); } }}
1. finsh 线程提供一个应用层的数据缓存 `istream` ,这里只用的 32 个字节。
2. `finsh_instream` 函数代替 `finsh_getchar` ,它用来读串口终端设备数据流,函数实现见下文。它可能返回多个字节数据,返回值表示有效数据个数
3. 接下来对 `finsh_instream` 读到的每字节数据进行处理
4. `finsh_handle_onebyte` 是对原来 `finsh_thread_entry` 函数中的 `while` 循环进行的改造
读终端串口设备
如果 serialX 的阻塞模式打开的,同时串口接收缓存里是空的,执行 `rt_device_read` 会永久等待下去,当前线程进入睡眠态。
int finsh_instream(char *buf, int len){#ifdef RT_USING_DEVICE int i; RT_ASSERT(shell != RT_NULL); i = rt_device_read(shell->device, -1, buf, len); return i;#else extern char rt_hw_console_getchar(void); return rt_hw_console_getchar();#endif /* RT_USING_DEVICE */}
读串口设备的数据放到 buf 指向的内存中,最多 len 个字节,最终返回实际读到的数据量。
注:`rt_device_read` 的返回值可能是 0,也可能会是 -1。
处理命令行字符
这部分笔者把他们放到了一个单独的函数,不这么做也没影响。
笔者做了一点儿小改进。
static void finsh_handle_onebyte(int ch){ static int last_ch = 0x20; ... /* handle end of line, break */ if (last_ch == '\r' && ch == '\n') { last_ch = ch; return; } if (ch == '\r' || ch == '\n') {#ifdef FINSH_USING_HISTORY shell_push_history(shell);#endif if (shell->echo_mode) rt_kprintf("\n"); msh_exec(shell->line, shell->line_position); rt_kprintf(FINSH_PROMPT); rt_memset(shell->line, 0, sizeof(shell->line)); shell->line_curpos = shell->line_position = 0; last_ch = ch; return; } ... last_ch = ch; // ch = 0; ...}
这样一来,对以 '\r' '\n' "\r\n" 三种组合结束的命令都能识别,**更重要的是,它可以识别以 '\r' '\n' "\r\n" 分割的多条命令!!!**
如下命令列表,可以全复制,粘贴到终端,四条命令逐个被执行。
lspslist_devicelist_thread
效果图

结束语
在 rt-thread 的 finsh 终端串口设备使用 serialX 驱动。初战告捷!
这次解决两个问题:一个是, finsh 执行 `rt_device_read` 时可以一次返回多个字节。另一个是,我们可以在终端里粘贴多条命令执行啦。
审核编辑:汤梓红
-
串口
+关注
关注
14文章
1551浏览量
76463 -
RT-Thread
+关注
关注
31文章
1286浏览量
40102 -
serialX
+关注
关注
0文章
7浏览量
808
发布评论请先 登录
相关推荐
RT-Thread上CAN实践

新书发布——《RT-Thread嵌入式实时操作系统内核、驱动和应用开发威廉希尔官方网站 》

2024 RT-Thread全球巡回 线下培训火热来袭!

【好书推荐】RT-Thread设备驱动开发指南

6月6日杭州站RT-Thread线下workshop,探索RT-Thread混合部署新模式!

RT-Thread混合部署Workshop北京站来啦!

4月25日北京站RT-Thread线下workshop,探索RT-Thread混合部署新模式

4月10日深圳场RT-Thread线下workshop,探索RT-Thread混合部署新模式!

4月10日深圳场RT-Thread线下workshop,探索RT-Thread混合部署新模式!

恩智浦半导体正式加入RT-Thread全球合作伙伴计划!

RT-Thread驱动开发指南进阶篇-动手驱动先楫未适配的外设LCD

《RT-Thread设备驱动开发指南》基础篇--以先楫bsp的hwtimer设备为例

RT-Thread设备驱动开发指南基础篇—以先楫bsp的hwtimer设备为例

评论