打印

关于rtthread 串口通信问题!原来手册例程缺了一个函数初始化

[复制链接]
424|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
王将|  楼主 | 2018-7-17 14:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f4xx.h"
#include <rtthread.h>
#include <rthw.h>
#include "stm32f4xx_conf.h"
static char msg_pool[2048];
struct rx_msg
{
rt_device_t dev;  
rt_size_t size;
};
//static rt_mq_t rx_mq;
static struct rt_messagequeue rx_mq;
static char uart_rx_buffer[64];
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;

   rt_mq_init(&rx_mq, "mqt",&msg_pool[0],128 - sizeof(void*),sizeof(msg_pool),RT_IPC_FLAG_FIFO);   //重点就在这里
        
   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|RT_DEVICE_FLAG_INT_RX);
   }
   write_device = device;
   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);
       }
//      rt_kprintf("AA");
     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);
      
     }
   }
}
在这个例程中,将启动一个devt线程,然后打开串口1和2
* 当串口1和2有输入时,将读取其中的输入数据然后写入到
* 串口1设备中

例程稍作修改已经通了

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

419

主题

419

帖子

0

粉丝