RT-Thread论坛
直播中

李晓鹏

7年用户 1260经验值
私信 关注

在main里面调用uart_sample(),可以发送出去,但是接收不到发进来的数据,为什么?

在main里面调用这个uart_sample();
可以发送出去,但是接收不到发进来的数据


///*
// * Copyright (c) 2006-2018, RT-Thread Development Team
// *
// * SPDX-License-Identifier: Apache-2.0
// *
// * Change Logs:
// * Date           Author       Notes
// * 2018-08-15     misonyo      first implementation.
// */
///*
// * 程序清单:这是一个 串口 设备使用例程
// * 例程导出了 uart_sample 命令到控制终端
// * 命令调用格式:uart_sample uart2
// * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
// * 程序功能:通过串口输出字符串"hello RT-Thread!",然后错位输出输入的字符
//*/
//#include
//#include
//#include
//#include
//#define SAMPLE_UART_NAME       "uart3"      /* 串口设备名称 */
//struct serial_configure config ;  // 初始化配置参数
///* 用于接收消息的信号量 */
//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[200];
//    while (1)
//    {
//        /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
//        //ch =
//        //rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
//        
//        rt_device_read(serial, 0, &ch, sizeof( ch )) ;
//        //rt_device_read(serial, 0, &ch, 1 ) ;
////        while (rt_device_read(serial, 0, &ch, sizeof(ch) ) != 1)
////        {
////            /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
////            rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
////        }
//        /* 读取到的数据通过串口错位输出 */
//        //ch = ch + 1;
//        //rt_device_write(serial, 0, &ch, sizeof(ch));
//        rt_thread_mdelay(1000);
//        rt_device_write(serial , 0 , ch , ( sizeof( ch )));
//        //rt_kprintf("%s", ch);
//        rt_thread_mdelay(500);
//    }
//}
//int uart_sample(void)
//{
//    rt_err_t ret = RT_EOK;
//    //char uart_name[RT_NAME_MAX];
//    char str[] = "hello RT-Thread123!\r\n";
//   
////    char *argc = "uart3";
////   
////    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(SAMPLE_UART_NAME);
//   
//    if (!serial)
//    {
//        rt_kprintf("find %s failed!\n", SAMPLE_UART_NAME);
//        return RT_ERROR;
//    }
//   
//     /* 以读写及中断接收方式打开串口设备 */
//    rt_device_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
//   
//    /* 修改串口配置参数 */
//   
//    config.baud_rate = BAUD_RATE_115200;        //修改波特率为 115200
//    config.data_bits = DATA_BITS_8;           //数据位 8
//    config.stop_bits = STOP_BITS_1;           //停止位 1
//    config.bufsz     = 128;                   //修改缓冲区 buff size 为 128
//    config.parity    = PARITY_NONE;           //无奇偶校验位
//    /* 控制串口设备。通过控制接口传入命令控制字,与控制参数 */
//    /* 初始化信号量 */
//    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
//   
//   
//   
//    /* 设置接收回调函数 */
//    rt_device_set_rx_indicate(serial, uart_input);
//   
//    /* 发送字符串 */
//    rt_device_write(serial, 0, str, (sizeof( str )));
//   
//    /* 创建 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;
//        rt_kprintf("thread err\n");
//    }
//    //return ret;
//}
/* 导出到 msh 命令列表中 */
//MSH_CMD_EXPORT(uart_sample, uart device sample);
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date           Author       Notes
* 2024-02-03     RT-Thread    first version
*/
#include
#include
#include
#define SAMPLE_UART_NAME       "uart3"      /* 串口设备名称 */
/* 串口设备句柄 */
static rt_device_t serial;
/* 消息队列控制块 */
static struct rt_messagequeue rx_mq;
/* 串口接收消息结构*/
struct rx_msg
{
    rt_device_t dev;
    rt_size_t size;
};
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    struct rx_msg msg;
    rt_err_t result;
    msg.dev = dev;
    msg.size = size;
    result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
    if ( result == -RT_EFULL)
    {
        /* 消息队列满 */
        rt_kprintf("message queue full!\n");
    }
    return result;
}
static void serial_thread_entry(void *parameter)
{
    struct rx_msg msg;
    rt_err_t result;
    rt_uint32_t rx_length;
    static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
    while (1)
    {
        rt_memset(&msg, 0, sizeof(msg));
        /* 从消息队列中读取消息*/
        result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);   //接收不到消息一直等
        if (result == RT_EOK)
        {
//            rt_kprintf("100000000\n");
            /* 从串口读取数据*/
            rx_length = rt_device_read( serial , 0 , rx_buffer , msg.size );
            rx_buffer[rx_length] = '\0';
            /* 通过串口设备 serial 输出读取到的消息 */
            rt_device_write(serial, 0, rx_buffer, rx_length );
            rt_kprintf("%s\n",rx_buffer);
        }
    }
}
int uart_sample(void)
{
    rt_err_t ret = RT_EOK;
    char str[] = "hello RT-Thread!\r\n";
    static char msg_pool[256];
    /* 查找串口设备 */
      serial = rt_device_find(SAMPLE_UART_NAME);
      if (!serial)
      {
          rt_kprintf("find %s failed!\n", SAMPLE_UART_NAME);
          return RT_ERROR;
      }
      /* 初始化消息队列 */
       rt_mq_init(&rx_mq, "rx_mq", msg_pool, /* 存放消息的缓冲区 */
                                   sizeof(struct rx_msg), /* 一条消息的最大长度 */
                                   sizeof(msg_pool), /* 存放消息的缓冲区大小 */
                                   RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
       /* 以 DMA 接收及轮询发送方式打开串口设备 */
        rt_device_open(serial, RT_DEVICE_FLAG_DMA_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;
        rt_kprintf("thread err\n");
    }
//    return ret;
}

回帖(1)

闷~~

2024-7-25 17:15:20
根据您提供的信息,您在main函数中调用了uart_sample()函数,可以成功发送数据,但是无法接收到发进来的数据。为了解决这个问题,我们需要从以下几个方面进行分析:

1. **硬件连接**:首先确保您的硬件连接是正确的。检查串口线是否连接正确,以及是否使用了正确的串口号。

2. **串口配置**:检查您的串口配置是否正确。这包括波特率、数据位、停止位和奇偶校验位。如果配置不正确,可能会导致接收数据失败。

3. **中断配置**:确保您的中断配置是正确的。如果中断没有正确配置,可能会导致接收到的数据无法被处理。

4. **接收缓冲区**:检查您的接收缓冲区是否足够大,以容纳接收到的数据。如果缓冲区太小,可能会导致数据丢失。

5. **代码逻辑**:检查您的uart_sample()函数中的代码逻辑,确保在发送数据后,有适当的逻辑来处理接收到的数据。

6. **调试和日志**:在uart_sample()函数中添加调试和日志输出,以帮助您了解程序在运行过程中的状态。这可以帮助您确定问题所在。

7. **串口驱动**:确保您的串口驱动是最新的,并且与您的硬件和操作系统兼容。

8. **其他干扰**:检查是否有其他设备或软件干扰您的串口通信。例如,某些软件可能会占用串口资源,导致您的程序无法正常接收数据。

9. **电源问题**:检查您的设备电源是否稳定,不稳定的电源可能会影响串口通信。

10. **固件/软件版本**:确保您的固件或软件版本是最新的,以修复可能存在的bug。

通过以上步骤,您应该能够找到问题所在,并采取相应的措施来解决接收不到数据的问题。如果问题仍然存在,您可能需要进一步检查硬件或寻求专业帮助。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分