Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread Studio
串口
为什么我的串口读写不了数据,卡死在读数据那里了
发布于 2024-10-04 10:57:40 浏览:296
订阅该版
```c #define SAMPLE_UART_NAME "uart3" static struct rt_semaphore rx_sem; static rt_device_t serial; rt_err_t ret = RT_EOK; static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { rt_kprintf("Semaphore released.\n"); rt_sem_release(&rx_sem); return RT_EOK; } ```  ```c static void serial_thread_entry(void *parameter) { char ch; rt_kprintf("11111\n"); while (1) { /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */ while (rt_device_read(serial, 0, &ch, 1) != 1) { rt_kprintf("2222\n"); /* 阻塞等待接收信号量,等到信号量后再次读取数据 */ rt_sem_take(&rx_sem, RT_WAITING_FOREVER); } /* 读取到的数据通过串口错位输出 */ rt_kprintf("33333\n"); ch = ch + 1; rt_device_write(serial, 0, &ch, 1); rt_kprintf("Read: %c, Write: %c\n", ch - 1, ch); } } static int uart_sample() { char str[] = "sss!\n"; serial = rt_device_find(SAMPLE_UART_NAME); if (!serial) { rt_kprintf("find failed!\n"); return RT_ERROR; } /* 初始化信号量 */ rt_sem_init(&rx_sem, "rx_sem", 1, RT_IPC_FLAG_PRIO); rt_device_open(serial, RT_DEVICE_FLAG_INT_RX); if (rt_device_open(serial, RT_DEVICE_FLAG_INT_RX)!=RT_EOK) { rt_kprintf("find failed!11111\n"); return RT_ERROR; } rt_device_set_rx_indicate(serial, uart_input); rt_device_write(serial, 0, str, (sizeof(str) - 1)); rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10); if (thread != RT_NULL) { rt_thread_startup(thread); } else { ret = RT_ERROR; } } int main() { /* user app entry */ uart_sample(); //MSH_CMD_EXPORT(uart_sample, uart device sample); while(1) { rt_thread_mdelay(100); } return 0; } ```
查看更多
1
个回答
默认排序
按发布时间排序
YZRD
2024-10-06
这家伙很懒,什么也没写!
``` /* * 程序清单:这是一个 串口 设备使用例程 * 例程导出了 uart_sample 命令到控制终端 * 命令调用格式:uart_sample uart2 * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备 * 程序功能:通过串口输出字符串"hello RT-Thread!",然后错位输出输入的字符 */ #include
#define SAMPLE_UART_NAME "uart2" /* 用于接收消息的信号量 */ static struct rt_semaphore rx_sem; static rt_device_t serial; /* 接收数据回调函数 */ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */ rt_sem_release(&rx_sem); return RT_EOK; } static void serial_thread_entry(void *parameter) { char ch; while (1) { /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */ while (rt_device_read(serial, -1, &ch, 1) != 1) { /* 阻塞等待接收信号量,等到信号量后再次读取数据 */ rt_sem_take(&rx_sem, RT_WAITING_FOREVER); } /* 读取到的数据通过串口错位输出 */ ch = ch + 1; rt_device_write(serial, 0, &ch, 1); } } static int uart_sample(int argc, char *argv[]) { rt_err_t ret = RT_EOK; char uart_name[RT_NAME_MAX]; char str[] = "hello RT-Thread!\r\n"; if (argc == 2) { rt_strncpy(uart_name, argv[1], RT_NAME_MAX); } else { rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX); } /* 查找系统中的串口设备 */ serial = rt_device_find(uart_name); if (!serial) { rt_kprintf("find %s failed!\n", uart_name); return RT_ERROR; } /* 初始化信号量 */ rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO); /* 以中断接收及轮询发送模式打开串口设备 */ rt_device_open(serial, RT_DEVICE_FLAG_INT_RX); /* 设置接收回调函数 */ rt_device_set_rx_indicate(serial, uart_input); /* 发送字符串 */ rt_device_write(serial, 0, str, (sizeof(str) - 1)); /* 创建 serial 线程 */ rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { rt_thread_startup(thread); } else { ret = RT_ERROR; } return ret; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(uart_sample, uart device sample); ``` 官方例程: 
撰写答案
登录
注册新账号
关注者
0
被浏览
296
关于作者
zyy_
这家伙很懒,什么也没写!
提问
9
回答
2
被采纳
0
关注TA
发私信
相关问题
1
rt_thread studio 啥时候能用呢
2
RT_Thread使用反馈帖子
3
RTT studio 下的 AT指令问题。
4
什么时候RTT Sdudio支持Ubuntu,Deepin和UOS操作系统
5
rt thread Studio 关于J-LINK下载问题
6
RT-Thread studio 调试设置问题
7
RTT-Studio 如何设置调试配置参数?
8
rt_thread studio 软件包配置
9
RT-Studio目前只能开发STM32的项目吗?
10
rtt studio 生成hex名字修改
推荐文章
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
ART-Pi2移植CMSIS-DAP(基于CherryUSB协议栈)
2
RT-thred的stm32h723对应bsp包CubeMX添加其他外设报错
3
RT-Thread中的time溢出问题,时间戳溢出,解决方法
4
ART-PI使用env驱动ETH网卡,pc和板子可以ping通
5
SystemView线程名字不显示
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
9
个答案
2
次被采纳
xiaorui
3
个答案
2
次被采纳
winfeng
2
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
KunYi
8
个答案
1
次被采纳
本月文章贡献
catcatbing
2
篇文章
5
次点赞
swet123
1
篇文章
4
次点赞
lizimu
1
篇文章
4
次点赞
Days
1
篇文章
4
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部