头文件定义结构体
#ifndef __APP_UART_H
#define __APP_UART_H
#include"stm32f10x.h"
#include "app_fifo.h"
#define UART_MAX_NUM 5
#define UART_RCV_BUF_LEN 256
#define UART_SND_BUF_LEN 1024
#define UART_RCV_TIMEOUT 2 // 20ms
#define SendHostCycleBufLen (4096)
#define ReceiveHostCycleBufLen (4096)
typedef struct _BufInfo_t_
{
uint8_t uchBuf[200];
uint32_t unLen;
}BufInfo_t;
typedef struct UART_SndInfo_t_
{
uint8_t SndBuf[UART_SND_BUF_LEN];
uint32_t SndLen;
}UART_SndInfo_t;
typedef struct _UART_RcvInfo_t_
{
uint8_t RcvBuf[UART_RCV_BUF_LEN];
uint32_t RcvLen;
uint32_t RcvTimer;
}UART_RcvInfo_t;
/* 串口接收和发送的buf */
typedef struct UART_Info_t_
{
UART_SndInfo_t stSndInfo; // 临时存储往外发送的数据buf
CycleBufInfo_t stSendToHostCycleInfo; // 按16字节发送时记录发给主机数据循环buf 状态的结构
uint8_t uchSendToHostBuf[SendHostCycleBufLen]; // 按16字节发送时存储发给主机数据的循环buf
BufInfo_t stHostFrameDataInfo; //临时存放完整的一帧数据
CycleBufInfo_t stRcvHostCycleInfo; //接收主机的数据使用的循环结构
uint8_t uchRcvHostCycleBuf[(ReceiveHostCycleBufLen+256)];//主机数据存放的Buf
BufInfo_t stObdFrameDataInfo; //临时存放完整的一帧数据
CycleBufInfo_t stRcvObdCycleInfo; //接收OBD 的数据使用的循环结构
uint8_t uchRcvObdCycleBuf[ReceiveHostCycleBufLen];//OBD 数据存放的Buf
uint32_t unRcvPcTimer; //PC 透传模式下超时计数值
}UART_Info_t;
typedef struct systemParam_t_
{
/*串口*/
UART_Info_t stUartInfo;
}systemParam_t;
void USART1_Configuration(void);
void USART1_SendData(uint8_t SendData);
void UART1_SendByte(uint16_t Data);
int32_t DrvUART_Write(uint8_t uartId, uint8_t *pData, uint32_t dataLen);
#endif
轮询函数里调用
static void Polling_100ms(void)
{
DrvWatchDog_Feed(); //喂狗
Obd_TransStart(); //下达OBD开启数据流传输指令给卡车模块
}
/*
功 能: 开始OBD 数据流传输;
参数1 : 无;
返回值: 无;
*/
void Obd_TransStart(void)
{
UART_SndInfo_t *pSndInfo = pObdUartSndInfo;
pSndInfo->SndLen = OBD_IfStartTsCmdPack(1, pSndInfo->SndBuf);
DrvUART_Write(3, pSndInfo->SndBuf, pSndInfo->SndLen);
}
int OBD_IfStartTsCmdPack(int ReSendCnt, unsigned char *oCmdBuff)
{
int cmdBuffLen = 0;
OBD_PkgInData_t pkgdata;
unsigned char *pstr = NULL;
unsigned char *pCmdBuff = NULL;
unsigned char data[OBD_PKGBUF_SIZE] = {0};
if (oCmdBuff == NULL)
{
return OBD_FAILED;
}
pCmdBuff = oCmdBuff;
memset(data, 0x00, OBD_PKGBUF_SIZE);
memset(&pkgdata, 0x00, sizeof(OBD_PkgInData_t));
pkgdata.data = data;
pstr = pkgdata.data;
*pstr++ = 0xff;
*pstr++ = 0x00;
*pstr++ = 0x00;
*pstr++ = 0x00;
*pstr++ = 0x00;
pkgdata.datasize += 5;
cmdBuffLen = OBD_GenerateObdPackage(CMD_TS_START, &pkgdata, pCmdBuff, ReSendCnt);
return cmdBuffLen;
}
/*将OBD数据按照协议打包上传
函数返回值:数据包的长度
*/
static int OBD_GenerateObdPackage(obdcmd_t cmdId, OBD_PkgInData_t *pPkgData, unsigned char *pBuf, int isReSend)
{
unsigned char *pstr = NULL;
unsigned char *pstrtmp = NULL;
unsigned short pkglen = 0;
unsigned short datalen = 0;
int pkgsize = 0;
unsigned int cmd = ObdCmdArray[cmdId];
if (pBuf == NULL)
{
return OBD_FAILED;
}
if (pPkgData == NULL)
datalen = 0;
else
datalen = pPkgData->datasize;
if (isReSend == 0)
m_nPkgCounter++;
pkglen = 1+2+datalen; /* 加上一个字节的包长度和两个字节的命令字*/
pstr = pBuf;
pstrtmp = pBuf;
//fill header
*pstr++ = 0x55;
pkgsize++;
*pstr++ = 0xaa;
pkgsize++;
//fill dst addr
*pstr++ = 0xf0;
pkgsize++;
//fill src addr
*pstr++ = 0xf8;
pkgsize++;
//fill len
*pstr++ = (unsigned char)((pkglen>>8)&0xff);
pkgsize++;
*pstr++ = (unsigned char)(pkglen&0xff);
pkgsize++;
//fill counter
*pstr++ = m_nPkgCounter;
pkgsize++;
//fill cmd
*pstr++ = (unsigned char)((cmd>>8)&0xff);
pkgsize++;
*pstr++ = (unsigned char)(cmd&0xff);
pkgsize++;
//fill data
if (pPkgData != NULL)
{
memcpy(pstr, pPkgData->data, datalen);
pstr += datalen;
pkgsize += datalen;
}
//fill crc
pstrtmp += 2;
*pstr = CalXOR(pstrtmp, pkgsize-2);
pkgsize++;
return pkgsize;
} |