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

2019-07-20 13:26发布

#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 ", ++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] = '';
   
       if (write_device != RT_NULL)
       rt_device_write(write_device, 0, &uart_rx_buffer[0],
       rx_length);
      
     }
   }
}
在这个例程中,将启动一个devt线程,然后打开串口1和2
* 当串口1和2有输入时,将读取其中的输入数据然后写入到
* 串口1设备中


例程稍作修改已经通了     
0条回答

一周热门 更多>