OpenEdv-开源电子网
标题:
利用rt-thread已经串口通讯的问题
[打印本页]
作者:
也至
时间:
2016-4-26 17:16
标题:
利用rt-thread已经串口通讯的问题
这个程序为什么每次都读写一个字符,但最后能够完成多字符传输
#include <rtthread.h>
struct rx_msg
{
rt_device_t dev;
rt_size_t size;
};
/* 用于接收消息的消息队列*/
static rt_mq_t rx_mq;
/* 接收线程的接收缓冲区*/
static char uart_rx_buffer[512];
static char uart_sx_buffer[512];
/* 数据到达回调函数*/
rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
struct rx_msg msg;
msg.dev = dev;
msg.size = size;
/* 发送消息到消息队列中*/
rt_mq_send(rx_mq, &msg, sizeof(struct rx_msg));
return RT_EOK;
}
void device_thread_entry(void* parameter)
{
struct rx_msg msg;
int count = 0;
rt_device_t device, write_device;
rt_err_t result = RT_EOK;
/* 查找系统中的串口1设备 */
device = rt_device_find("uart1");
if (device!= RT_NULL)
{
/* 设置回调函数及打开设备*/
rt_device_set_rx_indicate(device, uart_input);
rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
}
/* 设置写设备为uart1设备 */
write_device = device;
/* 查找系统中的串口2设备 */
device= rt_device_find("uart2");
if (device != RT_NULL)
{
/* 设置回调函数及打开设备*/
rt_device_set_rx_indicate(device, uart_input);
rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
}
while (1)
{
/* 从消息队列中读取消息*/
result = rt_mq_recv(rx_mq, &msg,
sizeof(struct rx_msg), 50);
// if (result == -RT_ETIMEOUT)
// {
// /* 接收超时*/
// rt_kprintf("timeout count:%d\n", ++count);
// }
//
/* 成功收到消息*/
if (result == RT_EOK)
{
rt_uint32_t rx_length;
rx_length = (sizeof(uart_rx_buffer) - 1) >
msg.size ? msg.size : sizeof(uart_rx_buffer) - 1;
/* 读取消息*/
rx_length = rt_device_read(msg.dev, 0,
&uart_rx_buffer[0], rx_length);
uart_rx_buffer[rx_length] = '\0';
/* 写到写设备中*/
if (write_device != RT_NULL)
rt_device_write(write_device, 0,
&uart_rx_buffer[0], rx_length);
}
}
}
static struct rt_messagequeue s_your_mq;//消息队列控制块对象
static char s_msg_pool[1024];//消息池
int rt_application_init()
{
/* 创建devt线程*/
rt_thread_t thread = rt_thread_create("devt",
device_thread_entry, RT_NULL,
1024, 25, 7);
rt_mq_init(&s_your_mq,"test",&s_msg_pool[0],sizeof(struct rx_msg),sizeof(s_msg_pool),RT_IPC_FLAG_FIFO);//初始化消息队列控制块
rx_mq =&s_your_mq;//将rx_mq再指向s_your_mq
/* 创建成功则启动线程*/
if (thread != RT_NULL)
rt_thread_startup(thread);
}
作者:
正点原子
时间:
2016-5-1 12:18
帮顶
作者:
rhgao
时间:
2016-11-13 22:12
rx_length = rt_device_read(msg.dev, 0,
&uart_rx_buffer[0], rx_length); 读取的长度是rx_length,并且是以块为单位的
作者:
douzhipeng
时间:
2017-5-5 16:08
您好,同时使用两个串口设备的话必须要再建立一个线程吗?
作者:
douzhipeng
时间:
2017-5-5 16:10
我用一个串口通信的话可以,但是两个设备一起用的话就有一个不好用了
作者:
liaifeilove
时间:
2017-10-27 16:55
学习,帮顶
作者:
Alien2017
时间:
2017-12-29 16:49
我现在一个产品上用6个串口,STM32F407,裸奔功能已经实现了,现在在学RT-THREAD,头大啊!
欢迎光临 OpenEdv-开源电子网 (https://www.openedv.com/)
Powered by Discuz! X3.4