嵌入式技术论坛
直播中

王超

7年用户 1229经验值
私信 关注
[问答]

为什么添加lwgps包后测试用例不能正确解析出经纬度信息呢?

在添加了lwgps后,测试用例使用不了,有同学弄过这一块吗?我的怎么就是不行呢…. 我用的是和芯星通的GPS芯片。

//*****************************************************************************
// file        : lwgps2rtt.c
// porting lwgps to rt-thread
//
// Change Logs:
// Date                 Author      Note
// 2020/09/05           Cheney      First draft version
// 2020/09/13           Cheney      1. Update the comments.
//                                  2. Support gps info query interface.
// 2020/10/10           Cheney      Support macros LWGPS_CFG_STATUS
// 2020/12/15           Cheney      Configure the uart before using it.
//
//*****************************************************************************
//*****************************************************************************
//
//! \addtogroup lwgps
//! @{
//
//*****************************************************************************
#include
#include
#include
#include "lwgps.h"
#ifndef GPS_MODULE_BAUD_RATE
#define GPS_MODULE_BAUD_RATE        BAUD_RATE_9600
#endif
static lwgps_t h_lwgps;
static rt_device_t serial = RT_NULL;
static struct rt_semaphore rx_sem;
static rt_thread_t lwgps_tid = RT_NULL;
static rt_mutex_t lwgps_mutex = RT_NULL;
/**
* \brief gps uart date received callback
*
* \param dev: uart device
* \param size: data size
* \return rt_err_t: operation result.
*/
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    rt_sem_release(&rx_sem);
    return RT_EOK;
}
#if LWGPS_CFG_STATUS == 1
void lwgps_process_cbk(lwgps_statement_t res)
{
    // TODO: give the interface to user.
}
#endif
/**
* \brief lwgps process thread
*
* \param parameter: input parameters.
*/
static void lwgps_thread_entry(void *parameter)
{
    rt_uint8_t ch;
    while (RT_TRUE)
    {
        rt_sem_take(&rx_sem, RT_TICK_PER_SECOND);
        while (rt_device_read(serial, -1, &ch, 1) == 1)
        {
            rt_mutex_take(lwgps_mutex, RT_WAITING_FOREVER);
            rt_kprintf("%c",ch);
#if LWGPS_CFG_STATUS == 1
            lwgps_process(&h_lwgps, &ch, 1, lwgps_process_cbk);
#else
            lwgps_process(&h_lwgps, &ch, 1);
//            rt_kprintf("Valid status: %d\r\n", h_lwgps.is_valid);
//            rt_kprintf("Latitude: %f degrees\r\n", h_lwgps.latitude);
//            rt_kprintf("Longitude: %f degrees\r\n", h_lwgps.longitude);
//            rt_kprintf("Altitude: %f meters\r\n", h_lwgps.altitude);
#endif
            rt_mutex_release(lwgps_mutex);
        }
    }
}
/**
* \brief get gps information
*
* \param gps_info: gps information.
*/
void lwgps2rtt_get_gps_info(lwgps_t *gps_info)
{
    rt_mutex_take(lwgps_mutex, RT_WAITING_FOREVER);
    rt_memcpy(gps_info, &h_lwgps, (sizeof(h_lwgps) - sizeof(h_lwgps.p)));
    //rt_kprintf("latitude is %f\n",gps_info)
    rt_mutex_release(lwgps_mutex);
}
/**
* \brief module init
*
* \param uart_dev_name: uart device name
*/
void lwgps2rtt_init(void)
{
    serial = rt_device_find("uart3");
    if (!serial)
    {
        rt_kprintf("uart find %s failed!\n", "uart3");
        return;
    }
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    config.baud_rate = GPS_MODULE_BAUD_RATE;
    config.data_bits = DATA_BITS_8;
    config.stop_bits = STOP_BITS_1;
    config.parity    = PARITY_NONE;
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    lwgps_mutex = rt_mutex_create("lwgps_mutex", RT_IPC_FLAG_FIFO);
    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);
    lwgps_init(&h_lwgps);
    lwgps_tid = rt_thread_create("lwgps", lwgps_thread_entry, RT_NULL, 2048, 20, 4);
    if (lwgps_tid != RT_NULL)
        rt_thread_startup(lwgps_tid);
}
/**
* \brief module deinit
*
*/
void lwgps2rtt_deinit(void)
{
    if (serial != RT_NULL)
    {
        rt_device_close(serial);
        rt_device_set_rx_indicate(serial, RT_NULL);
    }
    if (lwgps_tid != RT_NULL)
    {
        rt_thread_delete(lwgps_tid);
    }
    if (lwgps_mutex != RT_NULL)
    {
        rt_mutex_delete(lwgps_mutex);
    }
    serial = RT_NULL;
    lwgps_tid = RT_NULL;
    lwgps_mutex = RT_NULL;
}
INIT_APP_EXPORT(lwgps2rtt_init);
#include
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include
#include "lwgps/lwgps.h"
lwgps_t gps_info;
int main(void)
{
    int count = 1;
    while (1)
    {
        //LOG_D("Hello RT-Thread!");
        //rt_thread_mdelay(1000);
        lwgps2rtt_get_gps_info(&gps_info);
        rt_kprintf("hour: %d\r\n", gps_info.hours);
        rt_kprintf("minute: %d\r\n", gps_info.minutes);
        rt_kprintf("seconds: %d\r\n", gps_info.seconds);
        rt_kprintf("Latitude: %f degrees\r\n", gps_info.latitude);
        rt_kprintf("Longitude: %f degrees\r\n", gps_info.longitude);
        rt_kprintf("Altitude: %f meters\r\n", gps_info.altitude);
    }
    return RT_EOK;
}
测试打印如图所示
2.jpg

回帖(3)

李鑫

2023-4-17 17:51:03
lwgps_process(&h_lwgps, &ch, 1); 一个字节字符就想解析出来信息?
举报

王超

2023-4-17 17:51:25
我也看到了,他是一个字节,具体的程序解析源码我没有去看,我现在修改好了。
举报

王超

2023-4-17 17:51:43
对lwgps2rtt.c文件做了如下修改,大家可以对照,可以正确的解析GPS数据了。

//*****************************************************************************
// file        : lwgps2rtt.c
// porting lwgps to rt-thread
//
// Change Logs:
// Date                 Author      Note
// 2020/09/05           Cheney      First draft version
// 2020/09/13           Cheney      1. Update the comments.
//                                  2. Support gps info query interface.
// 2020/10/10           Cheney      Support macros LWGPS_CFG_STATUS
// 2020/12/15           Cheney      Configure the uart before using it.
//
//*****************************************************************************
//*****************************************************************************
//
//! \addtogroup lwgps
//! @{
//
//*****************************************************************************
#include
#include
#include
#include "lwgps.h"
#ifndef GPS_MODULE_BAUD_RATE
#define GPS_MODULE_BAUD_RATE        BAUD_RATE_9600
#endif
static lwgps_t h_lwgps;
struct rt_ringbuffer  rb ;
static rt_uint8_t gpspool[200];
static rt_device_t serial = RT_NULL;
static struct rt_semaphore rx_sem;
static rt_thread_t lwgps_tid = RT_NULL;
static rt_mutex_t lwgps_mutex = RT_NULL;
static rt_mutex_t uart_mutex = RT_NULL;
/**
* \brief gps uart date received callback
*
* \param dev: uart device
* \param size: data size
* \return rt_err_t: operation result.
*/
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    rt_sem_release(&rx_sem);
    return RT_EOK;
}
#if LWGPS_CFG_STATUS == 1
void lwgps_process_cbk(lwgps_statement_t res)
{
    // TODO: give the interface to user.
}
#endif
/**
* \brief lwgps process thread
*
* \param parameter: input parameters.
*/
static void lwgps_thread_entry(void *parameter)
{
    rt_uint8_t ch[100];
    rt_uint8_t arr[150];
    rt_uint8_t count = 0,result;
    while (RT_TRUE)
    {
        rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
        /*add by xinggaoyong */
        result = rt_device_read(serial, -1, ch, 100);
        rt_ringbuffer_put(&rb, ch, result);
        count = count + result;
        if (count >= 100)
        {
            rt_mutex_take(lwgps_mutex, RT_WAITING_FOREVER);
            rt_ringbuffer_get(&rb, arr, count);
#if LWGPS_CFG_STATUS == 1
            lwgps_process(&h_lwgps, arr, count, lwgps_process_cbk);
#else
            lwgps_process(&h_lwgps, arr, count);
            /*处理结束后,清零count*/
            count = 0;
#endif
            rt_mutex_release(lwgps_mutex);
        }
    }
}
/**
* \brief get gps information
*
* \param gps_info: gps information.
*/
void lwgps2rtt_get_gps_info(lwgps_t *gps_info)
{
    rt_mutex_take(lwgps_mutex, RT_WAITING_FOREVER);
    rt_memcpy(gps_info, &h_lwgps, (sizeof(h_lwgps) - sizeof(h_lwgps.p)));
    //rt_kprintf("latitude is %f\n",gps_info)
    rt_mutex_release(lwgps_mutex);
}
/**
* \brief module init
*
* \param uart_dev_name: uart device name
*/
void lwgps2rtt_init(void)
{
    rt_ringbuffer_init(&rb,gpspool,150);
    serial = rt_device_find("uart3");
    if (!serial)
    {
        rt_kprintf("uart find %s failed!\n", "uart3");
        return;
    }
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    config.baud_rate = GPS_MODULE_BAUD_RATE;
    config.data_bits = DATA_BITS_8;
    config.stop_bits = STOP_BITS_1;
    config.parity    = PARITY_NONE;
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    lwgps_mutex = rt_mutex_create("lwgps_mutex", RT_IPC_FLAG_FIFO);
    uart_mutex = rt_mutex_create("uart_mutex", RT_IPC_FLAG_FIFO);
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    //rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
    rt_device_set_rx_indicate(serial, uart_input);
    lwgps_init(&h_lwgps);
    lwgps_tid = rt_thread_create("lwgps", lwgps_thread_entry, RT_NULL, 2048, 20, 4);
    if (lwgps_tid != RT_NULL)
        rt_thread_startup(lwgps_tid);
}
/**
* \brief module deinit
*
*/
void lwgps2rtt_deinit(void)
{
    if (serial != RT_NULL)
    {
        rt_device_close(serial);
        rt_device_set_rx_indicate(serial, RT_NULL);
    }
    if (lwgps_tid != RT_NULL)
    {
        rt_thread_delete(lwgps_tid);
    }
    if (lwgps_mutex != RT_NULL)
    {
        rt_mutex_delete(lwgps_mutex);
    }
    serial = RT_NULL;
    lwgps_tid = RT_NULL;
    lwgps_mutex = RT_NULL;
}
/*add by xinggaoyong*/
INIT_APP_EXPORT(lwgps2rtt_init);
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
举报

更多回帖

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