#include "serial_main.h"
serial_handler_t *serial_handler;
char *received_data;
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, 115200, 8, 0, 1) != SERIAL_SUCCESS) //使用通道二uart口
{
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]); //打印接收到的数据
}
//received_data = (char *)serial_handler->serial_frame_buff;
}
}
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;
}
主函数中调用serial_init();串口初始化
serial_recv();串口接收数据
用vscode进行模拟时,程序运行到这行代码就出现异常
status = serial_handler->frame_recv(serial_handler);
#ifndef _SERIAL_DEFINE_H
#define _SERIAL_DEFINE_H
#include <stdint.h>
#include <pthread.h>
#define SERIAL_BUF_MAX_LENGTH (1024 * 3) // serial buffer max length
#define SERIAL_CONFIG_HANDLER_MAX 2 // serial config handler max number
#define SERIAL_MUTEX_T pthread_mutex_t // serial mutex type
/**
* Serial port status
* @param SERIAL_SUCCESS success
* @param SERIAL_FRAME_SEND_ERR frame send error
* @param SERIAL_SLAVE_NO_RESPOND slave no respond
* @param SERIAL_MUTEX_ERROR mutex error
* @param SERIAL_HANDER_INIT_ERROR handler init error
* @param SERIAL_INIT_ERROR serial init error
**/
typedef enum serial_status
{
SERIAL_SUCCESS = 0,
SERIAL_FRAME_SEND_ERR,
SERIAL_SLAVE_NO_RESPOND,
SERIAL_MUTEX_ERROR,
SERIAL_HANDER_INIT_ERROR,
SERIAL_INIT_ERROR
} serial_status_t;
/**
* Serial port parity
* @param SERIAL_PAR_NONE none
* @param SERIAL_PAR_ODD odd
* @param SERIAL_PAR_EVEN even
**/
typedef enum
{
SERIAL_PAR_NONE,
SERIAL_PAR_ODD,
SERIAL_PAR_EVEN
} serial_parity_t;
typedef struct serial_handler serial_handler_t;
typedef serial_status_t (*serial_send_func_t)(serial_handler_t *handler);
typedef serial_status_t (*serial_recv_func_t)(serial_handler_t *handler);
/**
* serial data structure
*/
typedef struct serial_handler
{
uint8_t used; /**< The value is 1 when used */
uint8_t serial_frame_buff[SERIAL_BUF_MAX_LENGTH]; /**< Buffer used to send and receive frame*/
uint32_t serial_offset; /**< Offset of the ydt on ydt_frame_buff[] */
uint32_t serial_frame_length; /**< The length of the whole frame */
uint32_t respond_timeout; // ms
serial_send_func_t frame_send; /**< The callback funciton to send the frame */
serial_recv_func_t frame_recv; /**< The callback funciton to receive the frame */
SERIAL_MUTEX_T serial_mutex;
void *private;
} serial_handler_t;
#endif
上面这个库文件是厂家串口函数定义结构体和枚举的头文件;
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]); //打印接收到的数据
}
//received_data = (char *)serial_handler->serial_frame_buff;
}
}
问了一下厂家说在这个recv函数中写个函数显示上去,可是这个函数都运行报错,这个代码要怎么修改呀