| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- /*--------------------------------------------------------------------------------------
- * @file History.c
- * @author ZhangJing
- * @version base on stm32f0x
- * @date 2015.09.11
- * @brief 历史数据处理
- ---------------------------------------------------------------------------------------*/
- #include "stm32f10x_gpio.h"
- #include "TypeDefine.h"
- #include "USARTSetup.h"
- #include "FlashSetup.h"
- #include "FM31256.h"
- #include "History.h"
- #include "math.h"
- HistoryDataFrame historyRecord;//历史数据存储
- uint16_t historyRecordQTY = 0;//历史数据存储数量
- uint16_t historyRecordPointer = 0;//历史数据存储指针
- uint8_t historyRefreshFlag = 0;//历史数据刷新标志
- uint16_t FackhistoryRecordQTY = 0;//假历史数据存储数量
- uint8_t historyRecordBuf[HISTORY_DATA_BYTES];//历史数据存储数组
- /*************************************************************************************
- * Function: HistorySendToPC
- * Object: 向PC机发送历史数据
- * 输入: 无
- * 输出: 无
- * 备注: 1、uint8_t tempIndex = 0; 发送数组的临时索引
- * 2、usart1XmitBuffer USART1的发送数组
- * 3、向PC发送数据用USART1
- **************************************************************************************/
- void HistorySendToPC( void )
- {
- uint8_t i;
- uint8_t tempIndex = 0;
-
- if( TaskSchedulerFlag.historyRequestFlag == TASK_FLAG_SET )//发送历史数据
- {
- if( usart1RecvBuffer[2] == 0x10 )
- {
- usart1XmitBuffer[tempIndex++] = 0xaa;//帧头
- usart1XmitBuffer[tempIndex++] = 23;//数据长度
- usart1XmitBuffer[tempIndex++] = 0x11;//功能码
- for( i = 0 ; i < 16 ; i++ )
- {
- usart1XmitBuffer[tempIndex++] = setParamInfo.deviceID[i];
- }
- usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
- usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
- usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
- usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
-
- usart1XmitBuffer[tempIndex++] = verifyInfo.sickroom;
- usart1XmitBuffer[tempIndex++] = verifyInfo.bedNO;
-
- usart1XmitBuffer[tempIndex++] = 0x55;
- USART1Send( usart1XmitBuffer, 26);
- }
- else if( usart1RecvBuffer[2] == 0x20 )
- {
- usart1XmitBuffer[tempIndex++] = 0xaa;//帧头
- usart1XmitBuffer[tempIndex++] = 29;//数据长度
- usart1XmitBuffer[tempIndex++] = 0x21;//功能码
- usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
- usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
- usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
- usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
-
- usart1XmitBuffer[tempIndex++] = verifyInfo.sickroom;
- usart1XmitBuffer[tempIndex++] = verifyInfo.bedNO;
-
- usart1XmitBuffer[tempIndex++] = historyRecord.totalDose / 256;
- usart1XmitBuffer[tempIndex++] = historyRecord.totalDose % 256;
-
- usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.firstDose;
- usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.continueDose;
- usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.superaddition;
- usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.lockTime;
-
- usart1XmitBuffer[tempIndex++] = historyRecord.limitDose / 256;
- usart1XmitBuffer[tempIndex++] = historyRecord.limitDose % 256;
-
- usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeMonth;
- usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeDate;
- usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeHour;
- usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeMinute;
-
- usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum1 / 256;
- usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum1 % 256;
-
- usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum2 / 256;
- usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum2 % 256;
-
- usart1XmitBuffer[tempIndex++] = historyRecord.inputed / 256;
- usart1XmitBuffer[tempIndex++] = historyRecord.inputed % 256;
-
- usart1XmitBuffer[tempIndex++] = 0x55;
- USART1Send( usart1XmitBuffer, tempIndex);//32
- }
- TaskSchedulerFlag.historyRequestFlag = TASK_FLAG_CLEAR;
- }
- }
- /*************************************************************************************
- * Function: ReadHistoryRecordQTY
- * Object: 读取历史数据数量
- * 输入: 无
- * 输出: 无
- * 备注: 1、uint8_t tempRecordBuf[4] = {0};存储数据组合用数组
- * 2、uint8_t tempRes = 0; 返回读取状态
- **************************************************************************************/
- void ReadHistoryRecordQTY( void )
- {
- uint8_t tempRecordBuf[6] = {0};
- // uint8_t tempRes = 0;
- FlashReadOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
- //// FlashReadOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
- // tempRes = Read_Multi31256RAM( HISTORY31256_RECORDQTY_ADDR , tempRecordBuf , HISTORY_RECORDQTY_SIZE );
- // if( tempRes != 1 )
- // {
- // return;
- // }
- historyRecordQTY = tempRecordBuf[0]; //历史数据总条数
- historyRecordQTY <<= 8;
- historyRecordQTY += tempRecordBuf[1];
-
- historyRecordPointer = tempRecordBuf[2]; //历史数据存储指针
- historyRecordPointer <<= 8;
- historyRecordPointer += tempRecordBuf[3];
-
- FackhistoryRecordQTY = tempRecordBuf[4]; //假历史数据
- FackhistoryRecordQTY <<= 8;
- FackhistoryRecordQTY += tempRecordBuf[5];
- // historyRecordQTY = 0;//test
- // historyRecordPointer = 0;//test
- if( historyRecordQTY > 10 )
- {
- historyRecordQTY = 0;
- }
-
- if( historyRecordPointer > 10 )
- {
- historyRecordPointer = 0;
- }
- if(FackhistoryRecordQTY > 100)
- {
- FackhistoryRecordQTY = 0;
- }
- }
- /*************************************************************************************
- * Function: ReadHistoryRecord
- * Object: 读取历史数据
- * 输入: 读取第几条数据,从0开始到99,共100条
- * 输出: 无
- * 备注: 1、uint8_t tempRes = 0; 返回读取状态
- * 2、uint8_t tempIndex = 0;数组临时索引
- * 3、uint16_t tempPointer = 0;读取地址的临时指针
- **************************************************************************************/
- void ReadHistoryRecord( void )
- {
- uint8_t tempIndex = 0;
- // uint8_t tempRes = 0;
- uint16_t tempPointer = 0;
-
- /*if( ( historyRecordPointer + historyRecord.readIndex ) >= 10 )
- {
- tempPointer = ( historyRecordPointer + historyRecord.readIndex - 10 ) * HISTORY_DATA_BYTES;
- }
- else
- {
- tempPointer = ( historyRecordPointer + historyRecord.readIndex ) * HISTORY_DATA_BYTES;
- }*/
-
- tempPointer = (historyRecord.readIndex * HISTORY_DATA_BYTES);
-
- FlashReadOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + tempPointer ) );
- //FlashReadOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + historyRecord.readIndex * HISTORY_DATA_BYTES ) );
- // tempRes = Read_Multi31256RAM( ( HISTORY31256_DATA_START_ADDR + tempPointer ) ,historyRecordBuf,HISTORY_DATA_BYTES );
- // if( tempRes != 1 )
- // {
- // return;
- // }
- historyRecord.totalDose = historyRecordBuf[tempIndex++]; //总量
- historyRecord.totalDose <<= 8;
- historyRecord.totalDose += historyRecordBuf[tempIndex++];
- if( historyRecord.totalDose > 999 )
- {
- historyRecord.totalDose = 0;
- }
-
- historyRecord.firstDose = historyRecordBuf[tempIndex++]; //首次量
- historyRecord.firstDose <<= 8;
- historyRecord.firstDose += historyRecordBuf[tempIndex++];
- if( historyRecord.firstDose > 99 )
- {
- historyRecord.firstDose = 0;
- }
-
- historyRecord.continueDose = historyRecordBuf[tempIndex++]; //持续量
- historyRecord.continueDose <<= 8;
- historyRecord.continueDose += historyRecordBuf[tempIndex++];
- if( historyRecord.continueDose > 999 )
- {
- historyRecord.continueDose = 0;
- }
-
- historyRecord.superaddition = historyRecordBuf[tempIndex++]; //追加量
- historyRecord.superaddition <<= 8;
- historyRecord.superaddition += historyRecordBuf[tempIndex++];
- if( historyRecord.superaddition > 999 )
- {
- historyRecord.superaddition = 0;
- }
-
- historyRecord.lockTime = historyRecordBuf[tempIndex++]; //锁时
- historyRecord.lockTime <<= 8;
- historyRecord.lockTime += historyRecordBuf[tempIndex++];
- if( historyRecord.lockTime > 99 )
- {
- historyRecord.lockTime = 0;
- }
-
- historyRecord.limitDose = historyRecordBuf[tempIndex++]; //极限值
- historyRecord.limitDose <<= 8;
- historyRecord.limitDose += historyRecordBuf[tempIndex++];
- if( historyRecord.limitDose > 90 )
- {
- historyRecord.limitDose = 0;
- }
-
- historyRecord.inputed = historyRecordBuf[tempIndex++]; //已输入
- historyRecord.inputed <<= 8;
- historyRecord.inputed += historyRecordBuf[tempIndex++];
- if( historyRecord.inputed > 9999 )
- {
- historyRecord.inputed = 0;
- }
-
- historyRecord.additionalNum1 = historyRecordBuf[tempIndex++];
- historyRecord.additionalNum1 <<= 8;
- historyRecord.additionalNum1 += historyRecordBuf[tempIndex++];
- if( historyRecord.additionalNum1 > 999 )
- {
- historyRecord.additionalNum1 = 0;
- }
-
- historyRecord.additionalNum2 = historyRecordBuf[tempIndex++];
- historyRecord.additionalNum2 <<= 8;
- historyRecord.additionalNum2 += historyRecordBuf[tempIndex++];
- if( historyRecord.additionalNum2 > 999 )
- {
- historyRecord.additionalNum2 = 0;
- }
-
- historyRecord.stopTimeMonth = historyRecordBuf[tempIndex++];
- if( historyRecord.stopTimeMonth > 12 )
- {
- historyRecord.stopTimeMonth = 1;
- }
-
- historyRecord.stopTimeDate = historyRecordBuf[tempIndex++];
- if( historyRecord.stopTimeDate > 31 )
- {
- historyRecord.stopTimeDate = 1;
- }
-
- historyRecord.stopTimeHour = historyRecordBuf[tempIndex++];
- if( historyRecord.stopTimeHour > 23 )
- {
- historyRecord.stopTimeHour = 0;
- }
-
- historyRecord.stopTimeMinute = historyRecordBuf[tempIndex++];
- if( historyRecord.stopTimeMinute > 59 )
- {
- historyRecord.stopTimeMinute = 0;
- }
-
- historyRecord.alarmTimeMonth = historyRecordBuf[tempIndex++];
- if( historyRecord.alarmTimeMonth > 12 )
- {
- historyRecord.alarmTimeMonth = 1;
- }
-
- historyRecord.alarmTimeDate = historyRecordBuf[tempIndex++];
- if( historyRecord.alarmTimeDate > 31 )
- {
- historyRecord.alarmTimeDate = 1;
- }
-
- historyRecord.alarmTimeHour = historyRecordBuf[tempIndex++];
- if( historyRecord.alarmTimeHour > 23 )
- {
- historyRecord.alarmTimeHour = 0;
- }
-
- historyRecord.alarmTimeMinute = historyRecordBuf[tempIndex++];
- if( historyRecord.alarmTimeMinute > 59 )
- {
- historyRecord.alarmTimeMinute = 0;
- }
- historyRecord.alarmType = historyRecordBuf[tempIndex++];
- if( historyRecord.alarmType > 8 )//报警类型值>8则为非法值
- {
- historyRecord.alarmType = 0;
- }
- }
- /*************************************************************************************
- * Function: WriteHistoryRecordQTY
- * Object: 写入历史数据数量
- * 输入: 无
- * 输出: 无
- * 备注: 1、uint8_t tempRecordBuf[4] = {0};写数据用数组
- **************************************************************************************/
- void WriteHistoryRecordQTY( void )
- {
- uint8_t tempRecordBuf[6] = {0};
- if( ( historyRecordQTY > 10 ) && ( historyRecordQTY < 200 ) )//防止读出来的第一条数据为255
- {
- historyRecordQTY = 10;
- historyRecordPointer++;
- if( historyRecordPointer >= 10 )
- {
- historyRecordPointer = 0;
- }
- }
- else if( historyRecordQTY >= 200 )//防止读出来的第一条数据为255//
- {
- historyRecordQTY = 0;
- }
- if(FackhistoryRecordQTY > 100)
- {
- FackhistoryRecordQTY = 100;
- }
- tempRecordBuf[0] = historyRecordQTY / 256;
- tempRecordBuf[1] = historyRecordQTY % 256;
- tempRecordBuf[2] = historyRecordPointer / 256;
- tempRecordBuf[3] = historyRecordPointer % 256;
- tempRecordBuf[4] = FackhistoryRecordQTY / 256;
- tempRecordBuf[5] = FackhistoryRecordQTY % 256;
- FlashWriteOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
- // FlashWriteOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
- // Write_Multi31256RAM( HISTORY31256_RECORDQTY_ADDR , tempRecordBuf , HISTORY_RECORDQTY_SIZE );
-
- }
- /*************************************************************************************
- * Function: WriteHistoryRecord
- * Object: 写入历史数据
- * 输入: 无
- * 输出: 无
- * 备注: 1、uint8_t tempIndex = 0;数组临时索引
- * 2、uint16_t tempPointer = 0;写入地址的临时指针
- **************************************************************************************/
- void WriteHistoryRecord( void )
- {
- uint8_t tempIndex = 0;
- uint16_t tempPointer = 0;
- historyRecordBuf[tempIndex++] = setParamInfo.totalDose / 256;
- historyRecordBuf[tempIndex++] = setParamInfo.totalDose % 256;
- historyRecordBuf[tempIndex++] = setParamInfo.firstDose / 256;
- historyRecordBuf[tempIndex++] = setParamInfo.firstDose % 256;
-
- historyRecordBuf[tempIndex++] = runParamInfo.continueDose / 256;
- historyRecordBuf[tempIndex++] = runParamInfo.continueDose % 256;
-
- historyRecordBuf[tempIndex++] = runParamInfo.superaddition / 256;
- historyRecordBuf[tempIndex++] = runParamInfo.superaddition % 256;
-
- historyRecordBuf[tempIndex++] = runParamInfo.lockTime / 256;
- historyRecordBuf[tempIndex++] = runParamInfo.lockTime % 256;
-
- historyRecordBuf[tempIndex++] = runParamInfo.limitDose / 256;
- historyRecordBuf[tempIndex++] = runParamInfo.limitDose % 256;
- historyRecordBuf[tempIndex++] = realTimeData.inputDose / 256;
- historyRecordBuf[tempIndex++] = realTimeData.inputDose % 256;
-
- historyRecordBuf[tempIndex++] = realTimeData.validCount / 256;
- historyRecordBuf[tempIndex++] = realTimeData.validCount % 256;
-
- historyRecordBuf[tempIndex++] = realTimeData.invalidCount / 256;
- historyRecordBuf[tempIndex++] = realTimeData.invalidCount % 256;
-
- historyRecordBuf[tempIndex++] = displayTimeBuf.month;//historyRecord.stopTimeMonth;
- historyRecordBuf[tempIndex++] = displayTimeBuf.date;//historyRecord.stopTimeDate;
- historyRecordBuf[tempIndex++] = displayTimeBuf.hour;//(uint8_t)historyRecord.stopTimeHour;
- historyRecordBuf[tempIndex++] = displayTimeBuf.minute;//(uint8_t)historyRecord.stopTimeMinute;
-
- historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeMonth;
- historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeDate;
- historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeHour;
- historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeMinute;
- historyRecordBuf[tempIndex++] = alarmRecord.alarmType;
-
- for( ; tempIndex < 32; tempIndex++ )
- {
- historyRecordBuf[tempIndex] = 0;
- }
- if( historyRecordQTY >= 10 )//当存储条数小于10条时存储地址根据存储条数变化,否则根据存储指针变化
- {
- if( historyRecordPointer == 0 )
- {
- tempPointer = ( historyRecordQTY - 1 ) * HISTORY_DATA_BYTES;
- }
- else
- {
- // tempPointer = ( historyRecordPointer - 1 + historyRecordQTY - 10 ) * HISTORY_DATA_BYTES;//删除当前指针的前一条数据,写入新数据
- tempPointer = ( historyRecordPointer - 1 ) * HISTORY_DATA_BYTES;//删除当前指针的前一条数据,写入新数据
- }
- }
- else
- {
- tempPointer = ( historyRecordQTY - 1 ) * HISTORY_DATA_BYTES;
- }
- FlashWriteOperate( historyRecordBuf,100,( HISTORY_DATA_START_ADDR + tempPointer ) );
- //FlashWriteOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + historyRecordQTY * HISTORY_DATA_BYTES ) );
- // Write_Multi31256RAM( ( HISTORY31256_DATA_START_ADDR + tempPointer ) ,historyRecordBuf,HISTORY_DATA_BYTES );
- }
- /*************************************************************************************
- * Function: HistoryQtyAdd
- * Object: 历史数据数量自加
- * 输入: 无
- * 输出: 无
- * 备注: 1、
- **************************************************************************************/
- void HistoryQtyAdd()
- {
- historyRecordQTY++;//历史数据数量自加
- FackhistoryRecordQTY ++;//假历史数据数量自加
- if(FackhistoryRecordQTY > 100)
- {
- FackhistoryRecordQTY = 100;
- }
- }
|