History.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*--------------------------------------------------------------------------------------
  2. * @file History.c
  3. * @author ZhangJing
  4. * @version base on stm32f0x
  5. * @date 2015.09.11
  6. * @brief 历史数据处理
  7. ---------------------------------------------------------------------------------------*/
  8. #include "stm32f10x_gpio.h"
  9. #include "TypeDefine.h"
  10. #include "USARTSetup.h"
  11. #include "FlashSetup.h"
  12. #include "FM31256.h"
  13. #include "History.h"
  14. #include "math.h"
  15. HistoryDataFrame historyRecord;//历史数据存储
  16. uint16_t historyRecordQTY = 0;//历史数据存储数量
  17. uint16_t historyRecordPointer = 0;//历史数据存储指针
  18. uint8_t historyRefreshFlag = 0;//历史数据刷新标志
  19. uint16_t FackhistoryRecordQTY = 0;//假历史数据存储数量
  20. uint8_t historyRecordBuf[HISTORY_DATA_BYTES];//历史数据存储数组
  21. /*************************************************************************************
  22. * Function: HistorySendToPC
  23. * Object: 向PC机发送历史数据
  24. * 输入: 无
  25. * 输出: 无
  26. * 备注: 1、uint8_t tempIndex = 0; 发送数组的临时索引
  27. * 2、usart1XmitBuffer USART1的发送数组
  28. * 3、向PC发送数据用USART1
  29. **************************************************************************************/
  30. void HistorySendToPC( void )
  31. {
  32. uint8_t i;
  33. uint8_t tempIndex = 0;
  34. if( TaskSchedulerFlag.historyRequestFlag == TASK_FLAG_SET )//发送历史数据
  35. {
  36. if( usart1RecvBuffer[2] == 0x10 )
  37. {
  38. usart1XmitBuffer[tempIndex++] = 0xaa;//帧头
  39. usart1XmitBuffer[tempIndex++] = 23;//数据长度
  40. usart1XmitBuffer[tempIndex++] = 0x11;//功能码
  41. for( i = 0 ; i < 16 ; i++ )
  42. {
  43. usart1XmitBuffer[tempIndex++] = setParamInfo.deviceID[i];
  44. }
  45. usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
  46. usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
  47. usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
  48. usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
  49. usart1XmitBuffer[tempIndex++] = verifyInfo.sickroom;
  50. usart1XmitBuffer[tempIndex++] = verifyInfo.bedNO;
  51. usart1XmitBuffer[tempIndex++] = 0x55;
  52. USART1Send( usart1XmitBuffer, 26);
  53. }
  54. else if( usart1RecvBuffer[2] == 0x20 )
  55. {
  56. usart1XmitBuffer[tempIndex++] = 0xaa;//帧头
  57. usart1XmitBuffer[tempIndex++] = 29;//数据长度
  58. usart1XmitBuffer[tempIndex++] = 0x21;//功能码
  59. usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
  60. usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
  61. usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
  62. usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
  63. usart1XmitBuffer[tempIndex++] = verifyInfo.sickroom;
  64. usart1XmitBuffer[tempIndex++] = verifyInfo.bedNO;
  65. usart1XmitBuffer[tempIndex++] = historyRecord.totalDose / 256;
  66. usart1XmitBuffer[tempIndex++] = historyRecord.totalDose % 256;
  67. usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.firstDose;
  68. usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.continueDose;
  69. usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.superaddition;
  70. usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.lockTime;
  71. usart1XmitBuffer[tempIndex++] = historyRecord.limitDose / 256;
  72. usart1XmitBuffer[tempIndex++] = historyRecord.limitDose % 256;
  73. usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeMonth;
  74. usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeDate;
  75. usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeHour;
  76. usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeMinute;
  77. usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum1 / 256;
  78. usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum1 % 256;
  79. usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum2 / 256;
  80. usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum2 % 256;
  81. usart1XmitBuffer[tempIndex++] = historyRecord.inputed / 256;
  82. usart1XmitBuffer[tempIndex++] = historyRecord.inputed % 256;
  83. usart1XmitBuffer[tempIndex++] = 0x55;
  84. USART1Send( usart1XmitBuffer, tempIndex);//32
  85. }
  86. TaskSchedulerFlag.historyRequestFlag = TASK_FLAG_CLEAR;
  87. }
  88. }
  89. /*************************************************************************************
  90. * Function: ReadHistoryRecordQTY
  91. * Object: 读取历史数据数量
  92. * 输入: 无
  93. * 输出: 无
  94. * 备注: 1、uint8_t tempRecordBuf[4] = {0};存储数据组合用数组
  95. * 2、uint8_t tempRes = 0; 返回读取状态
  96. **************************************************************************************/
  97. void ReadHistoryRecordQTY( void )
  98. {
  99. uint8_t tempRecordBuf[6] = {0};
  100. // uint8_t tempRes = 0;
  101. FlashReadOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
  102. //// FlashReadOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
  103. // tempRes = Read_Multi31256RAM( HISTORY31256_RECORDQTY_ADDR , tempRecordBuf , HISTORY_RECORDQTY_SIZE );
  104. // if( tempRes != 1 )
  105. // {
  106. // return;
  107. // }
  108. historyRecordQTY = tempRecordBuf[0]; //历史数据总条数
  109. historyRecordQTY <<= 8;
  110. historyRecordQTY += tempRecordBuf[1];
  111. historyRecordPointer = tempRecordBuf[2]; //历史数据存储指针
  112. historyRecordPointer <<= 8;
  113. historyRecordPointer += tempRecordBuf[3];
  114. FackhistoryRecordQTY = tempRecordBuf[4]; //假历史数据
  115. FackhistoryRecordQTY <<= 8;
  116. FackhistoryRecordQTY += tempRecordBuf[5];
  117. // historyRecordQTY = 0;//test
  118. // historyRecordPointer = 0;//test
  119. if( historyRecordQTY > 10 )
  120. {
  121. historyRecordQTY = 0;
  122. }
  123. if( historyRecordPointer > 10 )
  124. {
  125. historyRecordPointer = 0;
  126. }
  127. if(FackhistoryRecordQTY > 100)
  128. {
  129. FackhistoryRecordQTY = 0;
  130. }
  131. }
  132. /*************************************************************************************
  133. * Function: ReadHistoryRecord
  134. * Object: 读取历史数据
  135. * 输入: 读取第几条数据,从0开始到99,共100条
  136. * 输出: 无
  137. * 备注: 1、uint8_t tempRes = 0; 返回读取状态
  138. * 2、uint8_t tempIndex = 0;数组临时索引
  139. * 3、uint16_t tempPointer = 0;读取地址的临时指针
  140. **************************************************************************************/
  141. void ReadHistoryRecord( void )
  142. {
  143. uint8_t tempIndex = 0;
  144. // uint8_t tempRes = 0;
  145. uint16_t tempPointer = 0;
  146. /*if( ( historyRecordPointer + historyRecord.readIndex ) >= 10 )
  147. {
  148. tempPointer = ( historyRecordPointer + historyRecord.readIndex - 10 ) * HISTORY_DATA_BYTES;
  149. }
  150. else
  151. {
  152. tempPointer = ( historyRecordPointer + historyRecord.readIndex ) * HISTORY_DATA_BYTES;
  153. }*/
  154. tempPointer = (historyRecord.readIndex * HISTORY_DATA_BYTES);
  155. FlashReadOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + tempPointer ) );
  156. //FlashReadOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + historyRecord.readIndex * HISTORY_DATA_BYTES ) );
  157. // tempRes = Read_Multi31256RAM( ( HISTORY31256_DATA_START_ADDR + tempPointer ) ,historyRecordBuf,HISTORY_DATA_BYTES );
  158. // if( tempRes != 1 )
  159. // {
  160. // return;
  161. // }
  162. historyRecord.totalDose = historyRecordBuf[tempIndex++]; //总量
  163. historyRecord.totalDose <<= 8;
  164. historyRecord.totalDose += historyRecordBuf[tempIndex++];
  165. if( historyRecord.totalDose > 999 )
  166. {
  167. historyRecord.totalDose = 0;
  168. }
  169. historyRecord.firstDose = historyRecordBuf[tempIndex++]; //首次量
  170. historyRecord.firstDose <<= 8;
  171. historyRecord.firstDose += historyRecordBuf[tempIndex++];
  172. if( historyRecord.firstDose > 99 )
  173. {
  174. historyRecord.firstDose = 0;
  175. }
  176. historyRecord.continueDose = historyRecordBuf[tempIndex++]; //持续量
  177. historyRecord.continueDose <<= 8;
  178. historyRecord.continueDose += historyRecordBuf[tempIndex++];
  179. if( historyRecord.continueDose > 999 )
  180. {
  181. historyRecord.continueDose = 0;
  182. }
  183. historyRecord.superaddition = historyRecordBuf[tempIndex++]; //追加量
  184. historyRecord.superaddition <<= 8;
  185. historyRecord.superaddition += historyRecordBuf[tempIndex++];
  186. if( historyRecord.superaddition > 999 )
  187. {
  188. historyRecord.superaddition = 0;
  189. }
  190. historyRecord.lockTime = historyRecordBuf[tempIndex++]; //锁时
  191. historyRecord.lockTime <<= 8;
  192. historyRecord.lockTime += historyRecordBuf[tempIndex++];
  193. if( historyRecord.lockTime > 99 )
  194. {
  195. historyRecord.lockTime = 0;
  196. }
  197. historyRecord.limitDose = historyRecordBuf[tempIndex++]; //极限值
  198. historyRecord.limitDose <<= 8;
  199. historyRecord.limitDose += historyRecordBuf[tempIndex++];
  200. if( historyRecord.limitDose > 90 )
  201. {
  202. historyRecord.limitDose = 0;
  203. }
  204. historyRecord.inputed = historyRecordBuf[tempIndex++]; //已输入
  205. historyRecord.inputed <<= 8;
  206. historyRecord.inputed += historyRecordBuf[tempIndex++];
  207. if( historyRecord.inputed > 9999 )
  208. {
  209. historyRecord.inputed = 0;
  210. }
  211. historyRecord.additionalNum1 = historyRecordBuf[tempIndex++];
  212. historyRecord.additionalNum1 <<= 8;
  213. historyRecord.additionalNum1 += historyRecordBuf[tempIndex++];
  214. if( historyRecord.additionalNum1 > 999 )
  215. {
  216. historyRecord.additionalNum1 = 0;
  217. }
  218. historyRecord.additionalNum2 = historyRecordBuf[tempIndex++];
  219. historyRecord.additionalNum2 <<= 8;
  220. historyRecord.additionalNum2 += historyRecordBuf[tempIndex++];
  221. if( historyRecord.additionalNum2 > 999 )
  222. {
  223. historyRecord.additionalNum2 = 0;
  224. }
  225. historyRecord.stopTimeMonth = historyRecordBuf[tempIndex++];
  226. if( historyRecord.stopTimeMonth > 12 )
  227. {
  228. historyRecord.stopTimeMonth = 1;
  229. }
  230. historyRecord.stopTimeDate = historyRecordBuf[tempIndex++];
  231. if( historyRecord.stopTimeDate > 31 )
  232. {
  233. historyRecord.stopTimeDate = 1;
  234. }
  235. historyRecord.stopTimeHour = historyRecordBuf[tempIndex++];
  236. if( historyRecord.stopTimeHour > 23 )
  237. {
  238. historyRecord.stopTimeHour = 0;
  239. }
  240. historyRecord.stopTimeMinute = historyRecordBuf[tempIndex++];
  241. if( historyRecord.stopTimeMinute > 59 )
  242. {
  243. historyRecord.stopTimeMinute = 0;
  244. }
  245. historyRecord.alarmTimeMonth = historyRecordBuf[tempIndex++];
  246. if( historyRecord.alarmTimeMonth > 12 )
  247. {
  248. historyRecord.alarmTimeMonth = 1;
  249. }
  250. historyRecord.alarmTimeDate = historyRecordBuf[tempIndex++];
  251. if( historyRecord.alarmTimeDate > 31 )
  252. {
  253. historyRecord.alarmTimeDate = 1;
  254. }
  255. historyRecord.alarmTimeHour = historyRecordBuf[tempIndex++];
  256. if( historyRecord.alarmTimeHour > 23 )
  257. {
  258. historyRecord.alarmTimeHour = 0;
  259. }
  260. historyRecord.alarmTimeMinute = historyRecordBuf[tempIndex++];
  261. if( historyRecord.alarmTimeMinute > 59 )
  262. {
  263. historyRecord.alarmTimeMinute = 0;
  264. }
  265. historyRecord.alarmType = historyRecordBuf[tempIndex++];
  266. if( historyRecord.alarmType > 8 )//报警类型值>8则为非法值
  267. {
  268. historyRecord.alarmType = 0;
  269. }
  270. }
  271. /*************************************************************************************
  272. * Function: WriteHistoryRecordQTY
  273. * Object: 写入历史数据数量
  274. * 输入: 无
  275. * 输出: 无
  276. * 备注: 1、uint8_t tempRecordBuf[4] = {0};写数据用数组
  277. **************************************************************************************/
  278. void WriteHistoryRecordQTY( void )
  279. {
  280. uint8_t tempRecordBuf[6] = {0};
  281. if( ( historyRecordQTY > 10 ) && ( historyRecordQTY < 200 ) )//防止读出来的第一条数据为255
  282. {
  283. historyRecordQTY = 10;
  284. historyRecordPointer++;
  285. if( historyRecordPointer >= 10 )
  286. {
  287. historyRecordPointer = 0;
  288. }
  289. }
  290. else if( historyRecordQTY >= 200 )//防止读出来的第一条数据为255//
  291. {
  292. historyRecordQTY = 0;
  293. }
  294. if(FackhistoryRecordQTY > 100)
  295. {
  296. FackhistoryRecordQTY = 100;
  297. }
  298. tempRecordBuf[0] = historyRecordQTY / 256;
  299. tempRecordBuf[1] = historyRecordQTY % 256;
  300. tempRecordBuf[2] = historyRecordPointer / 256;
  301. tempRecordBuf[3] = historyRecordPointer % 256;
  302. tempRecordBuf[4] = FackhistoryRecordQTY / 256;
  303. tempRecordBuf[5] = FackhistoryRecordQTY % 256;
  304. FlashWriteOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
  305. // FlashWriteOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
  306. // Write_Multi31256RAM( HISTORY31256_RECORDQTY_ADDR , tempRecordBuf , HISTORY_RECORDQTY_SIZE );
  307. }
  308. /*************************************************************************************
  309. * Function: WriteHistoryRecord
  310. * Object: 写入历史数据
  311. * 输入: 无
  312. * 输出: 无
  313. * 备注: 1、uint8_t tempIndex = 0;数组临时索引
  314. * 2、uint16_t tempPointer = 0;写入地址的临时指针
  315. **************************************************************************************/
  316. void WriteHistoryRecord( void )
  317. {
  318. uint8_t tempIndex = 0;
  319. uint16_t tempPointer = 0;
  320. historyRecordBuf[tempIndex++] = setParamInfo.totalDose / 256;
  321. historyRecordBuf[tempIndex++] = setParamInfo.totalDose % 256;
  322. historyRecordBuf[tempIndex++] = setParamInfo.firstDose / 256;
  323. historyRecordBuf[tempIndex++] = setParamInfo.firstDose % 256;
  324. historyRecordBuf[tempIndex++] = runParamInfo.continueDose / 256;
  325. historyRecordBuf[tempIndex++] = runParamInfo.continueDose % 256;
  326. historyRecordBuf[tempIndex++] = runParamInfo.superaddition / 256;
  327. historyRecordBuf[tempIndex++] = runParamInfo.superaddition % 256;
  328. historyRecordBuf[tempIndex++] = runParamInfo.lockTime / 256;
  329. historyRecordBuf[tempIndex++] = runParamInfo.lockTime % 256;
  330. historyRecordBuf[tempIndex++] = runParamInfo.limitDose / 256;
  331. historyRecordBuf[tempIndex++] = runParamInfo.limitDose % 256;
  332. historyRecordBuf[tempIndex++] = realTimeData.inputDose / 256;
  333. historyRecordBuf[tempIndex++] = realTimeData.inputDose % 256;
  334. historyRecordBuf[tempIndex++] = realTimeData.validCount / 256;
  335. historyRecordBuf[tempIndex++] = realTimeData.validCount % 256;
  336. historyRecordBuf[tempIndex++] = realTimeData.invalidCount / 256;
  337. historyRecordBuf[tempIndex++] = realTimeData.invalidCount % 256;
  338. historyRecordBuf[tempIndex++] = displayTimeBuf.month;//historyRecord.stopTimeMonth;
  339. historyRecordBuf[tempIndex++] = displayTimeBuf.date;//historyRecord.stopTimeDate;
  340. historyRecordBuf[tempIndex++] = displayTimeBuf.hour;//(uint8_t)historyRecord.stopTimeHour;
  341. historyRecordBuf[tempIndex++] = displayTimeBuf.minute;//(uint8_t)historyRecord.stopTimeMinute;
  342. historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeMonth;
  343. historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeDate;
  344. historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeHour;
  345. historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeMinute;
  346. historyRecordBuf[tempIndex++] = alarmRecord.alarmType;
  347. for( ; tempIndex < 32; tempIndex++ )
  348. {
  349. historyRecordBuf[tempIndex] = 0;
  350. }
  351. if( historyRecordQTY >= 10 )//当存储条数小于10条时存储地址根据存储条数变化,否则根据存储指针变化
  352. {
  353. if( historyRecordPointer == 0 )
  354. {
  355. tempPointer = ( historyRecordQTY - 1 ) * HISTORY_DATA_BYTES;
  356. }
  357. else
  358. {
  359. // tempPointer = ( historyRecordPointer - 1 + historyRecordQTY - 10 ) * HISTORY_DATA_BYTES;//删除当前指针的前一条数据,写入新数据
  360. tempPointer = ( historyRecordPointer - 1 ) * HISTORY_DATA_BYTES;//删除当前指针的前一条数据,写入新数据
  361. }
  362. }
  363. else
  364. {
  365. tempPointer = ( historyRecordQTY - 1 ) * HISTORY_DATA_BYTES;
  366. }
  367. FlashWriteOperate( historyRecordBuf,100,( HISTORY_DATA_START_ADDR + tempPointer ) );
  368. //FlashWriteOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + historyRecordQTY * HISTORY_DATA_BYTES ) );
  369. // Write_Multi31256RAM( ( HISTORY31256_DATA_START_ADDR + tempPointer ) ,historyRecordBuf,HISTORY_DATA_BYTES );
  370. }
  371. /*************************************************************************************
  372. * Function: HistoryQtyAdd
  373. * Object: 历史数据数量自加
  374. * 输入: 无
  375. * 输出: 无
  376. * 备注: 1、
  377. **************************************************************************************/
  378. void HistoryQtyAdd()
  379. {
  380. historyRecordQTY++;//历史数据数量自加
  381. FackhistoryRecordQTY ++;//假历史数据数量自加
  382. if(FackhistoryRecordQTY > 100)
  383. {
  384. FackhistoryRecordQTY = 100;
  385. }
  386. }