这几天我搜索写的初始化串口是这样的:
#include “serial_main.h”
serial_handler_t *serial_handler;
void serial_init()
{
#if 0
if (serial_hander_init(ydt_handler, 0, 9600, 8, 0, 1) != SERIAL_SUCCESS) //使用通道一485口
{
printf(“serial_init error!\r\n”);
}
#else
if (serial_hander_init(&serial_handler, 1, 9600, 8, 0, 1) != SERIAL_SUCCESS) //使用通道二485口
{
printf(“serial_init error!\r\n”);
}
#endif
}
void serial_uinit(serial_handler_t *serial_handler)
{
serial_hander_uninit(serial_handler);
}
void *serial_send(char *text)
{
serial_status_t status = SERIAL_SUCCESS;
sprintf((char *)serial_handler->serial_frame_buff, text);
serial_handler->serial_frame_length = strlen((char *)serial_handler->serial_frame_buff);
serial_handler->frame_send(serial_handler); //发送数据
}
void *serial_recv(void)
{
serial_status_t status = SERIAL_SUCCESS;
status = serial_handler->frame_recv(serial_handler);
if (status == SERIAL_SUCCESS)
{
printf("recv len = %d,recv=", serial_handler->serial_frame_length);
for (int i = 0; i < serial_handler->serial_frame_length; i++)
{
printf("%c", serial_handler->serial_frame_buff[i]); //打印接收到的数据
}
}
}
int serial_main(int argc, char *argv[])
{
serial_init(&serial_handler);
#if 0
while (1)
{
serial_send(“123456789”);
serial_recv();
sleep(1);
}
#else
while (1)
{
sprintf((char *)serial_handler->serial_frame_buff, “123456789”);
serial_handler->serial_frame_length = strlen((char *)serial_handler->serial_frame_buff);
serial_handler->frame_send(serial_handler); //发送数据
serial_handler->frame_recv(serial_handler); //接收数据
sleep(1);
}
#endif
serial_uinit(serial_handler);
return 0;
}
这是写的按钮:
#include “app.h”
/事件函数/
void serial_btn_event_cb(lv_event_t * e){
/获取事件类型/
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED){
serial_send(“123456”);
}
}
lv_obj_t * create_serial_btn(lv_obj_t * parent){
lv_obj_t * btn = lv_btn_create(parent);
lv_obj_center(btn);
lv_obj_add_event_cb(btn, serial_btn_event_cb, LV_EVENT_CLICKED, NULL);
}
串口功能头文件:
#ifndef _SERIAL_MAIN_H
#define _SERIAL_MAIN_H
#include “serial_mutex.h”
#include “serial_api.h”
#include “serial_define.h”
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
/**
- Serial port initialization
- @param handler This is the struct pointer
- @return no return value
*/
void serial_init();
/**
- Serial port uninitialization
- @param serial port pointer
- @return no return value
*/
void serial_uinit(serial_handler_t *handler);
/**
- Serial send data
- @param what to send
- @return no return value
*/
void *serial_send(char *text);
/**
*print received content
- @param NULL
- @return no return value
*/
void *serial_recv(void);
int serial_main(int argc, char *argv[]);
#endif