大家好!想请教一下:我写了一个传感器采集数据,并打印和通过串口发送一串报文驱动数码管显示内容。采集的数据放在一个数组中。在测试中发现串口发送一段时间后就不再发送数据。想请教一下这是什么环节出现了问题。
1、传感器采集数据的程序
#include <SHT85.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "sht3x.h"
#include <Usart.h>
#include <stdio.h>
#define SHT85_I2C_BUS_NAME "i2c1"
#define SHT85_ADDR 0x44
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 1024
#define THREAD_TIMESLICE 5
static rt_thread_t Sht85_Thread = RT_NULL;
static rt_thread_t Sht85_Print_Thread = RT_NULL;
static rt_sem_t Sht85_sem = RT_NULL;
float humidity = 0;
void SHT85_Init(void)
{
Sht85_sem = rt_sem_create("SHT85_sem", 0, RT_IPC_FLAG_FIFO);
//i2c1采集线程初始化 //
Sht85_Thread = rt_thread_create("SHT85_Get", SHT85_Thread_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if(Sht85_Thread!=RT_NULL)
{
rt_thread_startup(Sht85_Thread);
}
else {
rt_kprintf("sht85 thread create failed\n");
}
Sht85_Print_Thread = rt_thread_create("SHT85_Print",SHT85_Print_Thread_entry,RT_NULL,THREAD_STACK_SIZE,THREAD_PRIORITY-1,THREAD_TIMESLICE);
if(Sht85_Print_Thread!=RT_NULL)
{
rt_thread_startup(Sht85_Print_Thread);
}
}
static void SHT85_Thread_entry(void* parameter)
{
sht3x_device_t sht85_device;
sht85_device = sht3x_init(SHT85_I2C_BUS_NAME, SHT85_ADDR);
int i = 0;
while(1)
{
if(sht3x_read_singleshot(sht85_device)==RT_EOK)
{
humidity = sht85_device->humidity;
I2c1_Buffer[i++] = humidity;
if(i==10)
{
rt_sem_release(Sht85_sem);
i = 0;
}
}
else {
rt_kprintf("read sht85 fail.\r\n");
break;
}
rt_thread_mdelay(200);
}
}
static void SHT85_Print_Thread_entry(void* parameter)
{
static rt_err_t sem_result;
while(1)
{
sem_result = rt_sem_take(Sht85_sem, RT_WAITING_FOREVER);
if(sem_result==RT_EOK)
{
printf("i2c1 value: %.2f\n",I2c1_Buffer[0]);
uart6_transmit(I2c1_Buffer[0],0x02);
}
else {
rt_sem_delete(Sht85_sem);
return;
}
rt_thread_mdelay(2000);
}
}
2、串口发送的程序
#include <rtthread.h>
#include <Usart.h>
#include <rtdevice.h>
#include <math.h>
#define UART_NAME "uart6" /* 需要操作的设备 */
static rt_device_t serial; /* 设备句柄 */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
void uart6_transmit(float x,uint8_t func) /*x为需要显示的变量(湿度、风速等),function为显示功能参数 */ //
{ //0x01--温度、0x02--湿度、0x03--风速、0x04--等效温度
rt_err_t ret = RT_EOK;
rt_size_t send_len = 0;
uint8_t PN; //正负位//
uint8_t digits; //小数点位//
Data_TypeDef num;
num.Frame_Head = 0xAA;
num.Length = 5;
num.Function = func;
if(x<0)
{
PN = 0;
}
else {
PN = 1;
}
if(x>=10 || (x<=-1&&x>-10)) //显示一位小数//
{
digits = 1;
if(x>0)
{
x = x*10;
}
else {
x = -x*10;
}
}
else if(x>-1 && x<10) //显示两位小数//
{
digits = 2;
if(x>0)
{
x = x*100;
}
else {
x = -x*100;
}
}
else if(x<=-10) //显示零位小数//
{
digits = 0;
x = -x;
}
num.Data_Nature = digits + PN*16;
num.Data_High = (int)x>>8; //数据化为十六进制高八位//
num.Data_Low = (int)x & (0xFF); //数据化为十六进制低八位//
num.BBC = num.Function^num.Data_High^num.Data_Low^num.Data_Nature;
uint8_t str[7] = {num.Frame_Head,num.Length,num.Function,num.Data_High,num.Data_Low,num.Data_Nature,num.BBC};
/* 查找系统中的串口设备 */
serial = rt_device_find(UART_NAME);
if (!serial)
{
rt_kprintf("find %s failed!\n",UART_NAME);
}
/*配置参数*/
config.baud_rate = BAUD_RATE_9600; //修改波特率为 9600
config.data_bits = DATA_BITS_8; //数据位 8
config.stop_bits = STOP_BITS_1; //停止位 1
config.bufsz = 256; //修改缓冲区 buff size
config.parity = PARITY_NONE; //无奇偶校验位
rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
/* 以中断接收及轮询发送模式打开串口设备 */
ret = rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
if (ret != RT_EOK)
{
rt_kprintf("open device failed\r\n");
}
/* 发送字符串 */
send_len = rt_device_write(serial, 0, str, sizeof(str));
if (send_len != sizeof(str))
{
rt_kprintf("send data failed\r\n");
}
///* 关闭设备 */
// ret = rt_device_close(serial);
// if (ret != RT_EOK)
// {
// rt_kprintf("close device failed\r\n");
// }
//
//// rt_kprintf("serial device test successful\r\n");
}
在串口调试助手捕获到这样的消息,是因为我的内存设置的原因吗?
如上几张图,猜测大概率是串口的fifo buffer满了,排查下吧。
(components/drivers/serial/serial.c:358)
把串口线程的优先级提高一下试试,有可能串口发送或者接收被打断。
好的谢谢!不好意思请问一下这个buff排查要怎么做呢😥,是不是进行单步调试来看?