468 lines
12 KiB
C
468 lines
12 KiB
C
#include "agreement.h"
|
||
#include "system.h"
|
||
|
||
|
||
QUEUE_DATA RecQueue;
|
||
WORK_TIME WorkTime;
|
||
SYS_DATA s_Data;
|
||
//GET_DATA_FREQUENCY data_parameter[20]; //采样设置段
|
||
|
||
void RecQueueInit(void)
|
||
{
|
||
RecQueue.front=0;
|
||
RecQueue.rear=0;
|
||
}
|
||
void Float2char(unsigned char *Data,float Fldata)
|
||
{
|
||
Data[0]=(unsigned char)(((int)(Fldata))&0x00FF);
|
||
Data[1]=(unsigned char)(((((int)(Fldata)))&0xFF00)>>8);
|
||
}
|
||
void SysDataInit(void)
|
||
{
|
||
s_Data.ReceiveOffset=0;
|
||
s_Data.ReceiveOKFlag=_FALSE;
|
||
s_Data.UartStaus=Receive_head;
|
||
|
||
|
||
s_Data.Allow_GetData_Flag = 0; //初始化为禁止采集
|
||
s_Data.Allow_GetData_Count = 0; //数据计数
|
||
|
||
RecQueueInit();
|
||
|
||
}
|
||
|
||
|
||
uint8_t PushQueue(QUEUE_DATA *que_data,uint8_t data)
|
||
{
|
||
if((que_data->rear+1)%(RECEIVE_BUFFSIZE)==que_data->front)// full
|
||
{
|
||
return 0;
|
||
}
|
||
else
|
||
{
|
||
que_data->Buf[que_data->rear]=data;
|
||
que_data->rear=(que_data->rear+1)%(RECEIVE_BUFFSIZE);
|
||
return 1;
|
||
}
|
||
}
|
||
|
||
uint8_t PopQueue(QUEUE_DATA *que_data,uint8_t* data)
|
||
{
|
||
if(que_data->rear==que_data->front)//empty
|
||
{
|
||
return 0;
|
||
}
|
||
else
|
||
{
|
||
*data = que_data->Buf[que_data->front];
|
||
que_data->front =(que_data->front+1)%(RECEIVE_BUFFSIZE);
|
||
return 1;
|
||
}
|
||
}
|
||
//双地址判断程序
|
||
void Uart1_Sevice(void)
|
||
{
|
||
uint8_t temp = 0;
|
||
if(s_Data.ReceiveOKFlag==_FALSE)
|
||
{
|
||
if(PopQueue(&RecQueue,&temp))
|
||
{
|
||
|
||
switch (s_Data.UartStaus)
|
||
{
|
||
case Receive_head:
|
||
{
|
||
if(temp == HEAD_AB)
|
||
{
|
||
s_Data.ReceiveBuf[s_Data.ReceiveOffset++]=temp;
|
||
s_Data.UartStaus=Receive_addr;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
}
|
||
};
|
||
break;
|
||
case Receive_addr:
|
||
{
|
||
if(temp == ADDRESS_NB) //广播地址和原地址0A
|
||
{
|
||
s_Data.ReceiveBuf[s_Data.ReceiveOffset++]=temp;
|
||
s_Data.UartStaus=ReceiveSend_addr; //置为获取主地址状态
|
||
s_Data.IsOneSlaveAddrFlag = 1; //主机为单地址
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
}
|
||
else if(temp == HEAD_AB)
|
||
{
|
||
s_Data.ReceiveOffset = 1;
|
||
s_Data.UartStaus=Receive_addr;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
}
|
||
else
|
||
{
|
||
s_Data.ReceiveOffset = 0;
|
||
s_Data.UartStaus=Receive_head;
|
||
}
|
||
};
|
||
break;
|
||
case ReceiveSend_addr: //获取主地址状态
|
||
{
|
||
if(temp == ADDRESS_HOST) //地址长度为,地址0x34 0x21
|
||
{
|
||
s_Data.ReceiveBuf[s_Data.ReceiveOffset++]=temp;
|
||
g_Data.Uart1RcvOverTime=500;
|
||
s_Data.IsTwoHostAddrFlag = 2; //主机为双地址
|
||
}
|
||
else if((temp == ADDRESS_SEC)||(temp == ADDRESS_FAR)) //地址0A 主机第二字节地址 0x21
|
||
{
|
||
s_Data.ReceiveBuf[s_Data.ReceiveOffset++]=temp;
|
||
s_Data.UartStaus=Receive_data; //置为接收数据状态
|
||
g_Data.Uart1RcvOverTime=500;
|
||
}
|
||
else if(temp == HEAD_AB)
|
||
{
|
||
s_Data.ReceiveOffset = 1;
|
||
s_Data.UartStaus=Receive_addr;
|
||
g_Data.Uart1RcvOverTime=500;
|
||
}
|
||
else //主机为单地址
|
||
{
|
||
s_Data.ReceiveBuf[s_Data.ReceiveOffset++]=temp;
|
||
s_Data.UartStaus=Receive_data;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
s_Data.IsTwoHostAddrFlag = 1; //数据长度增加两个字节,主机为单地址
|
||
}
|
||
};
|
||
break;
|
||
case Receive_data:
|
||
{
|
||
s_Data.ReceiveBuf[s_Data.ReceiveOffset++]=temp;
|
||
if(s_Data.IsTwoHostAddrFlag == 1)
|
||
{
|
||
if(s_Data.IsOneSlaveAddrFlag == 2) //从机双地址
|
||
{
|
||
if((s_Data.ReceiveOffset>=5)&&(s_Data.ReceiveOffset>=s_Data.ReceiveBuf[4]+5))
|
||
{
|
||
s_Data.UartStaus = Receive_crc;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
}
|
||
}
|
||
else if(s_Data.IsOneSlaveAddrFlag == 1) //从机单地址
|
||
{
|
||
if((s_Data.ReceiveOffset>=4)&&(s_Data.ReceiveOffset>=s_Data.ReceiveBuf[3]+4))
|
||
{
|
||
s_Data.UartStaus = Receive_crc;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
}
|
||
}
|
||
}
|
||
else if(s_Data.IsTwoHostAddrFlag == 2)
|
||
{
|
||
if((s_Data.ReceiveOffset>=5)&&(s_Data.ReceiveOffset>=s_Data.ReceiveBuf[4]+5))
|
||
{
|
||
s_Data.UartStaus = Receive_crc;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
}
|
||
}
|
||
};
|
||
break;
|
||
case Receive_crc:
|
||
{
|
||
s_Data.ReceiveBuf[s_Data.ReceiveOffset++]=temp;
|
||
if(s_Data.IsTwoHostAddrFlag == 1) //主机单地址,主机双地址
|
||
{
|
||
if(s_Data.IsOneSlaveAddrFlag == 2) //从机双地址
|
||
{
|
||
if(data_crc8((uint8_t*)&(s_Data.ReceiveBuf[0]),s_Data.ReceiveBuf[4]+5)==s_Data.ReceiveBuf[s_Data.ReceiveBuf[4]+5])
|
||
{
|
||
s_Data.UartStaus = Receive_busy;
|
||
s_Data.ReceiveOKFlag=_TRUE;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
s_Data.Allow_GetData_Flag = 0; //接收数据成功,停止数据采集 22.5.10
|
||
}
|
||
else
|
||
{
|
||
s_Data.UartStaus = Receive_head;
|
||
s_Data.ReceiveOffset=0;
|
||
g_Data.Uart1RcvOverTime = 0;
|
||
}
|
||
}
|
||
else if(s_Data.IsOneSlaveAddrFlag == 1) //从机单地址
|
||
{
|
||
if(data_crc8((uint8_t*)&(s_Data.ReceiveBuf[0]),s_Data.ReceiveBuf[3]+4)==s_Data.ReceiveBuf[s_Data.ReceiveBuf[3]+4])
|
||
{
|
||
s_Data.UartStaus = Receive_busy;
|
||
s_Data.ReceiveOKFlag=_TRUE;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
s_Data.Allow_GetData_Flag = 0; //接收数据成功,停止数据采集 22.5.10
|
||
}
|
||
else
|
||
{
|
||
s_Data.UartStaus = Receive_head;
|
||
s_Data.ReceiveOffset=0;
|
||
g_Data.Uart1RcvOverTime = 0;
|
||
}
|
||
}
|
||
}
|
||
else if(s_Data.IsTwoHostAddrFlag == 2) //主机双地址,主机双地址
|
||
{
|
||
if(data_crc8((uint8_t*)&(s_Data.ReceiveBuf[0]),s_Data.ReceiveBuf[4]+5)==s_Data.ReceiveBuf[s_Data.ReceiveBuf[4]+5])
|
||
{
|
||
s_Data.UartStaus = Receive_busy;
|
||
s_Data.ReceiveOKFlag=_TRUE;
|
||
g_Data.Uart1RcvOverTime = 500;
|
||
s_Data.Allow_GetData_Flag = 0; //接收数据成功,停止数据采集 22.5.10
|
||
}
|
||
else
|
||
{
|
||
s_Data.UartStaus = Receive_head;
|
||
s_Data.ReceiveOffset=0;
|
||
g_Data.Uart1RcvOverTime = 0;
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
case Receive_busy:
|
||
{
|
||
|
||
};
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//设置发送目的地址和原地址,以及数据的长度。
|
||
void SwapAddr_DataLen(uint8_t data_len)
|
||
{
|
||
s_Data.ReceiveBuf[1] = s_Data.ReceiveBuf[2]; //交换地址;
|
||
s_Data.ReceiveBuf[2] = ADDRESS_NB;
|
||
s_Data.ReceiveBuf[3] = data_len; //长度
|
||
}
|
||
|
||
//设置发送目的地址和原地址,以及数据的长度。
|
||
void IsOneAddr_DataLen(uint8_t data_len)
|
||
{
|
||
s_Data.ReceiveBuf[1] = s_Data.ReceiveBuf[3]; //交换地址;
|
||
//s_Data.ReceiveBuf[2] = s_Data.ReceiveBuf[4]; //交换地址;
|
||
s_Data.ReceiveBuf[2] = ADDRESS_NBF;
|
||
s_Data.ReceiveBuf[3] = ADDRESS_NBT;
|
||
s_Data.ReceiveBuf[4] = data_len; //长度
|
||
}
|
||
|
||
//成功返回0,不成功返回-1
|
||
int my_strncmp(uint8_t *pstr1, uint8_t *pstr2, int16_t len)
|
||
{
|
||
while ( len -- )
|
||
{
|
||
if((*pstr1 ++) != (*pstr2 ++))
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
return 0;//返回比较结果
|
||
}
|
||
|
||
//读取实时数据
|
||
void r_GetData(void)
|
||
{
|
||
unsigned char i;
|
||
for(i = 0;i < 18;i++)
|
||
{
|
||
s_Data.ReceiveBuf[i+6] = s_Data.ucMeasureData[i];
|
||
}
|
||
// SwapAddr_DataLen(19);
|
||
IsOneAddr_DataLen(19);
|
||
s_Data.ReceiveBuf[24]=data_crc8((uint8_t *)&(s_Data.ReceiveBuf[0]),24);
|
||
UART_SendStr((uint8_t *)&(s_Data.ReceiveBuf[0]),25); //发送存储数振动据组数
|
||
}
|
||
//读取实时数据
|
||
void GetData(void)
|
||
{
|
||
uint8_t i;
|
||
switch(s_Data.ReceiveBuf[3])
|
||
{
|
||
case 0x40: //获取近端工程参数数据
|
||
for(i=0;i<12;i++) //赋值压力差值,内压能量,外压能量 48-59 6-17 3
|
||
{
|
||
MeasurementData_Nearbit.ucDataValueBuf[i+48] = s_Data.ReceiveBuf[6+i];
|
||
}
|
||
for(i=0;i<4;i++) //赋值钻压均值 8-11 18-21 1
|
||
{
|
||
MeasurementData_Nearbit.ucDataValueBuf[i+8] = s_Data.ReceiveBuf[18+i];
|
||
}
|
||
for(i=0;i<8;i++) //赋值钻压幅度值,钻压方差 60-67 22-29 2
|
||
{
|
||
MeasurementData_Nearbit.ucDataValueBuf[i+60] = s_Data.ReceiveBuf[22+i];
|
||
}
|
||
for(i=0;i<4;i++) //赋值扭矩均值 12-15 30-33 1
|
||
{
|
||
MeasurementData_Nearbit.ucDataValueBuf[i+12] = s_Data.ReceiveBuf[30+i];
|
||
}
|
||
for(i=0;i<8;i++) //赋值扭矩幅度值,扭矩方差 68-75 34-41 2
|
||
{
|
||
MeasurementData_Nearbit.ucDataValueBuf[i+68] = s_Data.ReceiveBuf[34+i];
|
||
}
|
||
for(i=0;i<4;i++) //赋值转速均值 16-19 42-45 1
|
||
{
|
||
MeasurementData_Nearbit.ucDataValueBuf[i+16] = s_Data.ReceiveBuf[42+i];
|
||
}
|
||
for(i=0;i<20;i++) //赋值转速幅度,转速方差,X轴振动严重性,Y轴振动严重性,Z轴振动严重性 76-95 46-65 5
|
||
{
|
||
MeasurementData_Nearbit.ucDataValueBuf[i+76] = s_Data.ReceiveBuf[46+i];
|
||
}
|
||
// for(i=0;i<76;i++)
|
||
// {
|
||
// MeasurementData_Nearbit.ucDataValueBuf[i] = s_Data.ReceiveBuf[6+i];
|
||
// }
|
||
// MeasurementData_Nearbit.ucDataValueBuf[]
|
||
break;
|
||
case 0x41: //获取远端工程参数数据
|
||
for(i=0;i<12;i++) //赋值压力差值,内压能量,外压能量 48-59 6-17 3
|
||
{
|
||
MeasurementData_Farbit.ucDataValueBuf[i+48] = s_Data.ReceiveBuf[6+i];
|
||
}
|
||
for(i=0;i<4;i++) //赋值钻压均值 8-11 18-21 1
|
||
{
|
||
MeasurementData_Farbit.ucDataValueBuf[i+8] = s_Data.ReceiveBuf[18+i];
|
||
}
|
||
for(i=0;i<8;i++) //赋值钻压幅度值,钻压方差 60-67 22-29 2
|
||
{
|
||
MeasurementData_Farbit.ucDataValueBuf[i+60] = s_Data.ReceiveBuf[22+i];
|
||
}
|
||
for(i=0;i<4;i++) //赋值扭矩均值 12-15 30-33 1
|
||
{
|
||
MeasurementData_Farbit.ucDataValueBuf[i+12] = s_Data.ReceiveBuf[30+i];
|
||
}
|
||
for(i=0;i<8;i++) //赋值扭矩幅度值,扭矩方差 68-75 34-41 2
|
||
{
|
||
MeasurementData_Farbit.ucDataValueBuf[i+68] = s_Data.ReceiveBuf[34+i];
|
||
}
|
||
for(i=0;i<4;i++) //赋值转速均值 16-19 42-45 1
|
||
{
|
||
MeasurementData_Farbit.ucDataValueBuf[i+16] = s_Data.ReceiveBuf[42+i];
|
||
}
|
||
for(i=0;i<20;i++) //赋值转速幅度,转速方差,X轴振动严重性,Y轴振动严重性,Z轴振动严重性 76-95 46-65 5
|
||
{
|
||
MeasurementData_Farbit.ucDataValueBuf[i+76] = s_Data.ReceiveBuf[46+i];
|
||
}
|
||
// for(i=0;i<76;i++)
|
||
// {
|
||
// MeasurementData_Farbit.ucDataValueBuf[i] = s_Data.ReceiveBuf[6+i];
|
||
// }
|
||
// MeasurementData_Farbit.ucDataValueBuf[]
|
||
break;
|
||
}
|
||
}
|
||
void AgreementService(void)
|
||
{
|
||
unsigned char buf[2]={0};
|
||
Uart1_Sevice();
|
||
if(s_Data.ReceiveOKFlag==_TRUE)
|
||
{
|
||
switch(s_Data.IsTwoHostAddrFlag)
|
||
{
|
||
case 1:
|
||
{
|
||
if(s_Data.IsOneSlaveAddrFlag == 1) //从地址为双地址
|
||
{
|
||
switch (s_Data.ReceiveBuf[5])
|
||
{
|
||
|
||
case READ_DATA_0x34: //实时数据读取
|
||
r_GetData();
|
||
break;
|
||
|
||
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
s_Data.ReceiveOKFlag=_FALSE;
|
||
s_Data.ReceiveOffset=0;
|
||
s_Data.UartStaus=Receive_head;
|
||
s_Data.Allow_GetData_Count = 0;//数据发送完成,等待开启采集
|
||
g_Data.GetUart1OverFlag = 1; //接收完成
|
||
}
|
||
break;
|
||
case 2: //主地址两个字节
|
||
{
|
||
switch (s_Data.ReceiveBuf[5])
|
||
{
|
||
case READ_DATA_0x34: //实时数据读取
|
||
GetData();
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
s_Data.ReceiveOKFlag=_FALSE;
|
||
s_Data.ReceiveOffset=0;
|
||
s_Data.UartStaus=Receive_head;
|
||
s_Data.Allow_GetData_Count = 0;//数据发送完成,等待开启采集
|
||
g_Data.GetUart1OverFlag = 1; //接收完成
|
||
}
|
||
break;
|
||
default:
|
||
s_Data.ReceiveOKFlag=_FALSE;
|
||
s_Data.ReceiveOffset=0;
|
||
s_Data.UartStaus=Receive_head;
|
||
s_Data.Allow_GetData_Count = 0;//数据发送完成,等待开启采集
|
||
g_Data.GetUart1OverFlag = 1; //接收完成
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
//数据发送
|
||
void Send_Slave(unsigned char Data)
|
||
{
|
||
unsigned char SendComment[8] = {0};
|
||
switch(Data)
|
||
{
|
||
case 1: //读取近端工参数据
|
||
SendComment[0] = 0xFF;
|
||
SendComment[1] = 0xAB;
|
||
SendComment[2] = 0x34;
|
||
SendComment[3] = 0x40;
|
||
SendComment[4] = 0x0A;
|
||
SendComment[5] = 0x01;
|
||
SendComment[6] = 0x34;
|
||
SendComment[7] = 0x37;
|
||
UART_SendStr(SendComment,8);
|
||
break;
|
||
case 2: //读取远端工参数据
|
||
SendComment[0] = 0xFF;
|
||
SendComment[1] = 0xAB;
|
||
SendComment[2] = 0x34;
|
||
SendComment[3] = 0x41;
|
||
SendComment[4] = 0x0A;
|
||
SendComment[5] = 0x01;
|
||
SendComment[6] = 0x34;
|
||
SendComment[7] = 0x9F;
|
||
UART_SendStr(SendComment,8);
|
||
break;
|
||
case 3: //读取介电通道0数据
|
||
SendComment[0] = 0xAA;
|
||
SendComment[1] = 0x55;
|
||
SendComment[2] = 0x10;
|
||
SendComment[3] = 0x80;
|
||
SendComment[4] = 0x8F;
|
||
UART3_SendStr(SendComment,5);
|
||
break;
|
||
case 4: //读取介电通道1数据
|
||
SendComment[0] = 0xAA;
|
||
SendComment[1] = 0x55;
|
||
SendComment[2] = 0x10;
|
||
SendComment[3] = 0x81;
|
||
SendComment[4] = 0x90;
|
||
UART3_SendStr(SendComment,5);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|