最近在使用linux板子跑lvgl,想先实现简单的串口通信功能,即让串口接收到的数据实时显示到lvgl的文本框里,但是发现在多次显示是屏幕无法清除上一次显示的内容,而且文本框显示串口数据的速度不理想,请问在这种lvgl多线程工作中的文本更新是否有更好的建议?
以下是·我的代码段:
static void *uart_read(void *arg)
{
char receive_buf[255] = {0};
int recv_len = 0;
int fd = *(int *)arg;
while (1)
{
recv_len = read(fd, receive_buf, sizeof(receive_buf) - 1);
if (recv_len > 0)
{
printf("Receive_len=%d\n", recv_len);
printf("Received: %.*s\n", recv_len, receive_buf);
pthread_mutex_lock(&mutex);
lv_textarea_set_text(label_1, ""); //在这里并不能及时清除文本框的内容
lv_textarea_set_text(label_1, receive_buf);//label_1为接收文本框
pthread_mutex_unlock(&mutex);
}
usleep(5 * 1000);
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
const char *fb_path = "/dev/fb0";
const char *tp_path = "/dev/input/event0";
char buf[255];
int n;
// 打开串口设备
fd = open_port("/dev/ttyS3");
if (fd == -1)
{
printf("open err\n");
exit(1);
}
else
{
printf("open uart\n");
}
lv_init();
int ret;
ret = lvgl_init_fb_display(fb_path);
assert(!ret);
ret = lvgl_init_tp_input(tp_path);
// assert(!ret);
lv_demo(); //lvgl工程
pthread_t tid1;
ret = pthread_create(&tid1, NULL, uart_read, &fd);
while (1)
{
pthread_mutex_lock(&mutex);
lvgl_usleep_once(5 * 1000); //封装的lv_timer_handler 函数
pthread_mutex_unlock(&mutex);
}
return 0;
}
当前效果:
第一次串口数据:12345678
第二次串口数据:mjkl