micro_climate/App/Src/com.c

335 lines
8.2 KiB
C
Raw Normal View History

2024-07-05 03:52:43 +00:00
#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; //<2F><><EFBFBD><EFBFBD><EEBBBA><EFBFBD><EFBFBD>
static unsigned char gs_SendBuffer[SENDBUFFER_LEN]; //<2F><><EFBFBD>ڷ<EFBFBD><DAB7>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>
/******************************************************************************
* Function: Com_SendData
* Description:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Calls: none
* Called By:
* Input: pDataBuffer-<EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dataLen- <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>addr - <EFBFBD><EFBFBD>ַcmdtype -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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;
//<2F><><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(NULL == pDataBuffer )
{
dataLen = 0;
}
//<2F><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(dataLen > SENDDATA_LEN_MAX)
{
dataLen = SENDDATA_LEN_MAX;
}
FrameLen = FRAME_HEAD_LEN + dataLen;
//<2F><>֡
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; //֡<><D6A1>
gs_SendBuffer[FRAME_LENGTH_H] = (FrameLen >> 8) & 0xFF;
gs_SendBuffer[FRAME_DEVICE_TYPE] = 0x02; //װ<><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
gs_SendBuffer[FRAME_DEVICE_ADDR] = addr; //װ<>õ<EFBFBD>ַ
gs_SendBuffer[FRAME_TYPE] = 0x02; //֡<><D6A1><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
gs_SendBuffer[FRAME_CMD_TYPE] = cmdtype; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(dataLen > 0)
{
memcpy(&gs_SendBuffer[FRAME_DATE_START],pDataBuffer,dataLen);
}
//<2F><><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
for(i = 0,checkSum = 0; i < FrameLen-1; i++)
{
checkSum += gs_SendBuffer[i];
}
gs_SendBuffer[FrameLen - 1] = checkSum; //У<><D0A3><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>
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:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Calls: none
* Called By:
* Input: pDataBuffer-<EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dataLen- <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>addr - <EFBFBD><EFBFBD>ַcmdtype -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cmdType = pFrame [FRAME_CMD_TYPE];
switch (cmdType)
{
//<2F><EFBFBD><E8B6A8>ַ
case COMMAND_TYPE_SET_ADDR:
addr = pFrame[FRAME_DATE_START];
SetAddress(addr);
Data[0] = 0xFF; //<2F>ɹ<EFBFBD>
Com_SendData(Data,1,addr,COMMAND_TYPE_SET_ADDR);
break;
//<2F><>ѯ<EFBFBD><D1AF>ַ
case COMMAND_TYPE_SCAN_ADDR:
addr = ReadAddress();
Data[0] = addr; //ʵ<>ʵ<EFBFBD>ַ
Com_SendData(Data,1,0xFF,COMMAND_TYPE_SCAN_ADDR);
break;
//<2F><>ѯ<EFBFBD><D1AF><EFBFBD>ݴ<EFBFBD><DDB4>ʽ
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;
//<2F><EFBFBD><E8B6A8><EFBFBD>ݴ<EFBFBD><DDB4>ʽ
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; //<2F>ɹ<EFBFBD>
Com_SendData(Data,1,addr,COMMAND_TYPE_SET_WORKMODE);
break;
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
/*case COMMAND_TYPE_GETDATA: //<2F><><EFBFBD><EFBFBD>
addr = ReadAddress();
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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);
//<2F><><EFBFBD><EFBFBD>
Com_SendData(Data,12,addr,COMMAND_TYPE_GETDATA);
break;*/
//<2F>Ƕ<EFBFBD>У<EFBFBD><D0A3>
case COMMAND_TYPE_SCALE: //<2F><><EFBFBD><EFBFBD>
addr = ReadAddress();
AngleScale(); //У<><D0A3>
Data[0] = 0xFF; //<2F>ɹ<EFBFBD>
Com_SendData(Data,1,addr,COMMAND_TYPE_SCALE);
break;
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>У<EFBFBD><D0A3>
case COMMAND_TYPE_SCALE_RESET:
addr = ReadAddress();
AngleScale_Reset(); //У<><D0A3><EFBFBD><EFBFBD>λ
Data[0] = 0xFF; //<2F>ɹ<EFBFBD>
Com_SendData(Data,1,addr,COMMAND_TYPE_SCALE_RESET);
break;
//<2F><>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD>
case COMMAND_TYPE_CALL_ORIGINALVALUE:
temp = pFrame[FRAME_DATE_START];; //1<><31>x,2:y
StartWave(temp);
break;
//<2F><>ȡװ<C8A1><D7B0><EFBFBD><EFBFBD>Ϣ
case COMMAND_TYPE_CALLINFO:
addr = ReadAddress();
temp = GetSensorInfo(Data);
Com_SendData(Data,temp,addr,COMMAND_TYPE_CALLINFO);
break;
default:
break;
}
}
/******************************************************************************
* Function: UpSendDataActive
* Description:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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();
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GetAngleMeasureValue(&xValue,&yValue,&zValue);
//Xֵ
memcpy(Data,(void*)&xValue,4);
//Yֵ
memcpy(Data + 4,(void*)&yValue,4);
//Zֵ
memcpy(Data + 8,(void*)&zValue,4);
//<2F><><EFBFBD><EFBFBD>
Com_SendData(Data,12,addr,COMMAND_TYPE_GETDATA);
g_UpSenDataIP = SD_FALSE;
}
/******************************************************************************
* Function: UpSendDataCall
* Description:Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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();
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GetAngleMeasureValue(&xValue,&yValue,&zValue);
//Xֵ
memcpy(Data,(void*)&xValue,4);
//Yֵ
memcpy(Data + 4,(void*)&yValue,4);
//Zֵ
memcpy(Data + 8,(void*)&zValue,4);
//<2F><><EFBFBD><EFBFBD>
Com_SendData(Data,12,addr,COMMAND_TYPE_GETDATA);
}
/******************************************************************************
* Function: CheckComData
* Description:<EFBFBD>ӽ<EFBFBD><EFBFBD>ܻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ
g_ComDataRecv.Lock = SD_TRUE;
readIP = g_ComDataRecv.ReadIP;
g_ComDataRecv.ReadIP = (g_ComDataRecv.ReadIP + 1) % ASDU_MUN;
//<2F><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʶ
g_ComDataRecv.Lock = SD_FALSE;
pBuffer = (DT_UINT8 *)g_ComDataRecv.AsduFrame[readIP];
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡
Handle_upSend_Data(pBuffer);
counter ++;
//<2F><><EFBFBD><EFBFBD><E0B4A6><EFBFBD><EFBFBD>֡
if(counter >= 3)
{
break;
}
}
}
/******************************************************************************
* Function: Com_RecvFram_Handle
* Description:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ֡<EFBFBD><EFBFBD><EFBFBD>ݻص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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;
//<2F><><EFBFBD>ȳ<EFBFBD><C8B3><EFBFBD>Χ
if(usart1.FrameLen > ASDU_LEN_MAX)
{
return;
}
if (usart1.RecvBuf[FRAME_CMD_TYPE]==COMMAND_TYPE_GETDATA)
{
myDelay_ms(2);
UpSendDataCall();
return;
}
//<2F><>
if((WriteIP + 1) % ASDU_MUN == g_ComDataRecv.ReadIP)
{
if(SD_TRUE == g_ComDataRecv.Lock)
{
return;
}
//<2F>޸<EFBFBD>дָ<D0B4><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
g_ComDataRecv.ReadIP = (g_ComDataRecv.ReadIP + (ASDU_MUN / 4)) % ASDU_MUN;
}
memcpy((DT_UINT8 *)g_ComDataRecv.AsduFrame[WriteIP],usart1.RecvBuf,usart1.FrameLen);
//<2F>ı<EFBFBD>дָ<D0B4><D6B8>
g_ComDataRecv.WriteIP = (WriteIP + 1) % ASDU_MUN;
}
#endif