micro_climate/App/Src/com.c

335 lines
8.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#if 0
#include <stdio.h>
#include <string.h>
#include "uart_dev.h"
#include "com.h"
#include "driver.h"
//static int dataCount;
//static unsigned char TestDataBuf[1000];
extern G_USART usart1;
extern volatile DT_BOOLEAN g_UpSenDataIP;
tagComDataRecv g_ComDataRecv; //命令缓冲区
static unsigned char gs_SendBuffer[SENDBUFFER_LEN]; //串口发送缓冲区
/******************************************************************************
* Function: Com_SendData
* Description:发送数据
* Calls: none
* Called By:
* Input: pDataBuffer-数据缓冲区 dataLen- 数据长度addr - 地址cmdtype -命令类型
* Ouput:
* Return:
* others:
* History:
* 1.luoyang created 2018-02-04
*
*******************************************************************************/
void Com_SendData(unsigned char *pDataBuffer,unsigned int dataLen,unsigned char addr,unsigned char cmdtype)
{
unsigned short FrameLen = 0;
unsigned char checkSum;
int i;
//无数据或错误操作
if(NULL == pDataBuffer )
{
dataLen = 0;
}
//数据超过缓冲区长度
if(dataLen > SENDDATA_LEN_MAX)
{
dataLen = SENDDATA_LEN_MAX;
}
FrameLen = FRAME_HEAD_LEN + dataLen;
//组帧
memset(gs_SendBuffer, 0, sizeof(gs_SendBuffer));
gs_SendBuffer[FRAME_HEAD_L] = 0x4A; //帧头
gs_SendBuffer[FRAME_HEAD_H] = 0x59;
gs_SendBuffer[FRAME_LENGTH_L] = FrameLen & 0xFF; //帧长
gs_SendBuffer[FRAME_LENGTH_H] = (FrameLen >> 8) & 0xFF;
gs_SendBuffer[FRAME_DEVICE_TYPE] = 0x02; //装置类型
gs_SendBuffer[FRAME_DEVICE_ADDR] = addr; //装置地址
gs_SendBuffer[FRAME_TYPE] = 0x02; //帧类型,传感器到设备
gs_SendBuffer[FRAME_CMD_TYPE] = cmdtype; //命令类型
//拷贝数据
if(dataLen > 0)
{
memcpy(&gs_SendBuffer[FRAME_DATE_START],pDataBuffer,dataLen);
}
//计算校验码
for(i = 0,checkSum = 0; i < FrameLen-1; i++)
{
checkSum += gs_SendBuffer[i];
}
gs_SendBuffer[FrameLen - 1] = checkSum; //校验码
//发送
drv_Send(gs_SendBuffer, FrameLen);
/*if ((dataCount-10)>=999)
dataCount=0;
memcpy(TestDataBuf+dataCount, gs_SendBuffer, FrameLen);
dataCount = dataCount+FrameLen;*/
}
/******************************************************************************
* Function: Handle_upSend_Data
* Description:解析协议,上送数据
* Calls: none
* Called By:
* Input: pDataBuffer-数据缓冲区 dataLen- 数据长度addr - 地址cmdtype -命令类型
* Ouput:
* Return:
* others:
* History:
* 1.luoyang created 2018-02-04
*
*******************************************************************************/
void Handle_upSend_Data(unsigned char *pFrame)
{
unsigned char addr,temp,cmdType;
unsigned char Data[30], workMode=0;
//float xValue=0,yValue=0,zValue=0;
unsigned short int upFreq=0;
//命令类型
cmdType = pFrame [FRAME_CMD_TYPE];
switch (cmdType)
{
//设定地址
case COMMAND_TYPE_SET_ADDR:
addr = pFrame[FRAME_DATE_START];
SetAddress(addr);
Data[0] = 0xFF; //成功
Com_SendData(Data,1,addr,COMMAND_TYPE_SET_ADDR);
break;
//查询地址
case COMMAND_TYPE_SCAN_ADDR:
addr = ReadAddress();
Data[0] = addr; //实际地址
Com_SendData(Data,1,0xFF,COMMAND_TYPE_SCAN_ADDR);
break;
//查询数据传输方式
case COMMAND_TYPE_GET_WORKMODE:
addr = ReadAddress();
ReadWorkMode(&workMode, &upFreq);
Data[0] = workMode;
memcpy(Data + 1,(void*)&upFreq,2);
Com_SendData(Data,3,addr,COMMAND_TYPE_GET_WORKMODE);
break;
//设定数据传输方式
case COMMAND_TYPE_SET_WORKMODE:
addr = ReadAddress();
workMode = pFrame[FRAME_DATE_START];
upFreq = 256*(pFrame[(FRAME_DATE_START+2)])+pFrame[(FRAME_DATE_START+1)];
SetWorkMode(workMode, upFreq);
Data[0] = 0xFF; //成功
Com_SendData(Data,1,addr,COMMAND_TYPE_SET_WORKMODE);
break;
//读取数据
/*case COMMAND_TYPE_GETDATA: //数据
addr = ReadAddress();
//获取测量值,归一化处理
if(SD_SUCCESS != GetAngleMeasureValue(&xValue,&yValue,&zValue))
{
return;
}
//X值
memcpy(Data,(void*)&xValue,4);
//Y值
memcpy(Data + 4,(void*)&yValue,4);
//Z值
memcpy(Data + 8,(void*)&zValue,4);
//发送
Com_SendData(Data,12,addr,COMMAND_TYPE_GETDATA);
break;*/
//角度校正
case COMMAND_TYPE_SCALE: //矫正
addr = ReadAddress();
AngleScale(); //校正
Data[0] = 0xFF; //成功
Com_SendData(Data,1,addr,COMMAND_TYPE_SCALE);
break;
//清除角度校正
case COMMAND_TYPE_SCALE_RESET:
addr = ReadAddress();
AngleScale_Reset(); //校正复位
Data[0] = 0xFF; //成功
Com_SendData(Data,1,addr,COMMAND_TYPE_SCALE_RESET);
break;
//召原始数据
case COMMAND_TYPE_CALL_ORIGINALVALUE:
temp = pFrame[FRAME_DATE_START];; //1x,2:y
StartWave(temp);
break;
//读取装置信息
case COMMAND_TYPE_CALLINFO:
addr = ReadAddress();
temp = GetSensorInfo(Data);
Com_SendData(Data,temp,addr,COMMAND_TYPE_CALLINFO);
break;
default:
break;
}
}
/******************************************************************************
* Function: UpSendDataActive
* Description:主动上送数据
* Calls: none
* Called By:
* Input:
* Ouput:
* Return:
* others:
* History:
* 1.luoyang created 2018-02-04
*
*******************************************************************************/
void UpSendDataActive(void)
{
unsigned char addr;
unsigned char Data[30] ;
float xValue,yValue,zValue;
if(SD_TRUE != g_UpSenDataIP)
return;
addr = ReadAddress();
//获取测量值,归一化处理
GetAngleMeasureValue(&xValue,&yValue,&zValue);
//X值
memcpy(Data,(void*)&xValue,4);
//Y值
memcpy(Data + 4,(void*)&yValue,4);
//Z值
memcpy(Data + 8,(void*)&zValue,4);
//发送
Com_SendData(Data,12,addr,COMMAND_TYPE_GETDATA);
g_UpSenDataIP = SD_FALSE;
}
/******************************************************************************
* Function: UpSendDataCall
* Description:应答上送数据
* Calls: none
* Called By:
* Input:
* Ouput:
* Return:
* others:
* History:
* 1.luoyang created 2018-02-04
*
*******************************************************************************/
void UpSendDataCall(void)
{
unsigned char addr;
unsigned char Data[30] ;
float xValue=0,yValue=0,zValue=0;
addr = ReadAddress();
//获取测量值,归一化处理
GetAngleMeasureValue(&xValue,&yValue,&zValue);
//X值
memcpy(Data,(void*)&xValue,4);
//Y值
memcpy(Data + 4,(void*)&yValue,4);
//Z值
memcpy(Data + 8,(void*)&zValue,4);
//发送
Com_SendData(Data,12,addr,COMMAND_TYPE_GETDATA);
}
/******************************************************************************
* Function: CheckComData
* Description:从接受缓冲区中解析命令
* Calls: none
* Called By:
* Input:
* Ouput:
* Return:
* others:
* History:
* 1.L.Y. created 20180210
*
*******************************************************************************/
void CheckComData(void)
{
unsigned char counter = 0;
unsigned char *pBuffer;
unsigned char readIP;
while(g_ComDataRecv.ReadIP != g_ComDataRecv.WriteIP)
{
//置锁标识
g_ComDataRecv.Lock = SD_TRUE;
readIP = g_ComDataRecv.ReadIP;
g_ComDataRecv.ReadIP = (g_ComDataRecv.ReadIP + 1) % ASDU_MUN;
//置位锁标识
g_ComDataRecv.Lock = SD_FALSE;
pBuffer = (DT_UINT8 *)g_ComDataRecv.AsduFrame[readIP];
//处理数据帧
Handle_upSend_Data(pBuffer);
counter ++;
//最多处理三帧
if(counter >= 3)
{
break;
}
}
}
/******************************************************************************
* Function: Com_RecvFram_Handle
* Description:串口收满一帧数据回调函数
* Calls: none
* Called By:
* Input:
* Ouput:
* Return:
* others:
* History:
* 1.L.Y. created 20180210
*
*******************************************************************************/
DT_VOID Com_RecvFram_Handle()
{
DT_UINT8 WriteIP;
WriteIP = g_ComDataRecv.WriteIP;
//长度超范围
if(usart1.FrameLen > ASDU_LEN_MAX)
{
return;
}
if (usart1.RecvBuf[FRAME_CMD_TYPE]==COMMAND_TYPE_GETDATA)
{
myDelay_ms(2);
UpSendDataCall();
return;
}
//满
if((WriteIP + 1) % ASDU_MUN == g_ComDataRecv.ReadIP)
{
if(SD_TRUE == g_ComDataRecv.Lock)
{
return;
}
//修改写指针,扔掉旧数据
g_ComDataRecv.ReadIP = (g_ComDataRecv.ReadIP + (ASDU_MUN / 4)) % ASDU_MUN;
}
memcpy((DT_UINT8 *)g_ComDataRecv.AsduFrame[WriteIP],usart1.RecvBuf,usart1.FrameLen);
//改变写指针
g_ComDataRecv.WriteIP = (WriteIP + 1) % ASDU_MUN;
}
#endif