buffer.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. #include "ring_buffer.h"
  2. #include <rtthread.h>
  3. #include "usart.h"
  4. #include "weight.h"
  5. #include "flash.h"
  6. #include "plan.h"
  7. #include "display.h"
  8. #include "button.h"
  9. #include "xBeeAppEscapingMode.h"
  10. rt_sem_t thread_EmptyBuffer_sem = RT_NULL;
  11. rt_sem_t uart1_lock = RT_NULL;
  12. rt_sem_t uart2_lock = RT_NULL;
  13. rt_sem_t uart3_lock = RT_NULL;
  14. rt_sem_t uart4_lock = RT_NULL;
  15. rt_sem_t uart5_lock = RT_NULL;
  16. #define BUF_SIZE 256
  17. ring_buffer usart1_buf,usart2_buf,usart3_buf,uart4_buf,uart5_buf;
  18. ring_buffer *rb_usart1,*rb_usart2,*rb_usart3,*rb_uart4,*rb_uart5;
  19. unsigned char rb_buffer1[BUF_SIZE],rb_buffer2[BUF_SIZE*4],rb_buffer3[BUF_SIZE],rb_buffer4[BUF_SIZE],rb_buffer5[BUF_SIZE];
  20. ring_buffer display_buf;
  21. ring_buffer *rb_display;
  22. unsigned char rb_display_buffer[BUF_SIZE];
  23. ring_buffer _SendWeightQueue,_XbeeQueue,_XbeeAPIQueue,_RemoteQueue;
  24. ring_buffer *SendWeightQueue,*XbeeQueue,*XbeeAPIQueue,*RemoteQueue;
  25. unsigned char SendWeightQueue_bf[BUF_SIZE*4],XbeeQueue_bf[BUF_SIZE*2],XbeeAPIQueue_bf[BUF_SIZE*4],RemoteQueue_bf[BUF_SIZE*2];
  26. ring_buffer _RecWeightQueue;
  27. ring_buffer *RecWeightQueue;
  28. uint8_t RecWeightQueue_bf[128];
  29. uint8_t usart1_send_data[64];
  30. uint8_t usart2_send_data[256];
  31. uint8_t usart3_send_data[64];
  32. uint8_t uart4_send_data[256];
  33. uint8_t flag_uart1_send = 0;
  34. uint8_t flag_uart2_send = 0;
  35. uint8_t flag_uart3_send = 0;
  36. uint8_t flag_uart4_send = 0;
  37. unsigned char XbeeConst_CenterAddress[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF}; //开始地址 0xFF
  38. unsigned char XbeeConst_CCAddress[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF}; //开始地址 0xFF //开始地址 0x10
  39. unsigned char XbeeConst_RemoteAddress = 0x01; // 远程TMR地址
  40. static unsigned char XbeeConst_FRxBufAPI[100]; //处理无线临时表
  41. unsigned char XbeeConst_ReadAPINum = 0;
  42. static unsigned char XbeeConst_needEscape=0;
  43. unsigned char XbeeConst_FRemoteTxBuf[]= {0x00,0x7E,0x15,0x00,0xF1,0x7E,0x45,
  44. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48. }; //处理铲车临时表
  49. static unsigned char XbeeConst_FRxBuf0[100]; //处理完整无线临时表
  50. static unsigned char XbeeConst_FRxBuf0_Remote[100]; //处理完整铲车无线临时表
  51. static unsigned char XbeeConst_FrameLength0 = 0;
  52. static unsigned char XbeeConst_ReadNum0 = 0;
  53. static unsigned char XbeeConst_FrameLength0_Remote = 0, XbeeConst_ReadNum0_Remote = 0;
  54. unsigned char XbeeConst_FrameNumLast=0; //上次发送的帧编号
  55. unsigned char XbeeConst_FTxBuf0[50]; //处理发送无线临时表
  56. unsigned char* XbeeConst_PTxBuf0; //处理发送无线临时表
  57. unsigned char* XbeeConst_PRemoteTxBuf0; //处理发送远程显示临时表
  58. unsigned char ccsend = 0;
  59. extern unsigned char iscowweight;
  60. unsigned char readyPop = 0;
  61. static unsigned char XbeeConst_FRxBuf12[100]; //处理完整无线临时表
  62. void RB_init(void)
  63. {
  64. rb_usart1 = &usart1_buf;
  65. rb_init(rb_usart1, sizeof(rb_buffer1)/sizeof(rb_buffer1[0]), rb_buffer1);
  66. rb_usart2 = &usart2_buf;
  67. rb_init(rb_usart2, sizeof(rb_buffer2)/sizeof(rb_buffer2[0]), rb_buffer2);
  68. rb_usart3 = &usart3_buf;
  69. rb_init(rb_usart3, sizeof(rb_buffer3)/sizeof(rb_buffer3[0]), rb_buffer3);
  70. rb_uart4 = &uart4_buf;
  71. rb_init(rb_uart4, sizeof(rb_buffer4)/sizeof(rb_buffer4[0]), rb_buffer4);
  72. rb_uart5 = &uart5_buf;
  73. rb_init(rb_uart5, sizeof(rb_buffer5)/sizeof(rb_buffer5[0]), rb_buffer5);
  74. // if (WeightConst_SBType==4||WeightConst_SBType==6) {
  75. rb_display = &display_buf;
  76. rb_init(rb_display, sizeof(rb_display_buffer)/sizeof(rb_display_buffer[0]), rb_display_buffer);
  77. // }
  78. SendWeightQueue = &_SendWeightQueue;
  79. rb_init(SendWeightQueue, sizeof(SendWeightQueue_bf)/sizeof(SendWeightQueue_bf[0]), SendWeightQueue_bf);
  80. XbeeQueue = &_XbeeQueue;
  81. rb_init(XbeeQueue, sizeof(XbeeQueue_bf)/sizeof(XbeeQueue_bf[0]), XbeeQueue_bf);
  82. XbeeAPIQueue = &_XbeeAPIQueue;
  83. rb_init(XbeeAPIQueue, sizeof(XbeeAPIQueue_bf)/sizeof(XbeeAPIQueue_bf[0]), XbeeAPIQueue_bf);
  84. RecWeightQueue = &_RecWeightQueue;
  85. rb_init(RecWeightQueue, sizeof(RecWeightQueue_bf)/sizeof(RecWeightQueue_bf[0]), RecWeightQueue_bf);
  86. RemoteQueue = &_RemoteQueue;
  87. rb_init(RemoteQueue, sizeof(RemoteQueue_bf)/sizeof(RemoteQueue_bf[0]), RemoteQueue_bf);
  88. }
  89. void USART_Push(unsigned char address, unsigned char data)
  90. {
  91. if (address==1)
  92. {
  93. rb_push_insert(rb_usart1, data);
  94. }
  95. else if (address==2)
  96. {
  97. rb_push_insert(rb_usart2, data);
  98. }
  99. else if (address==3)
  100. {
  101. rb_push_insert(rb_usart3, data);
  102. }
  103. else if (address==4)
  104. {
  105. rb_push_insert(rb_uart4, data);
  106. }
  107. else if (address==5)
  108. {
  109. rb_push_insert(rb_uart5, data);
  110. }
  111. }
  112. void RX_API(void) {
  113. unsigned char tempval, checksum = 0;
  114. if (rb_full_count(XbeeAPIQueue)> 0) {
  115. tempval = rb_remove(XbeeAPIQueue); //继续存数据
  116. // rt_kprintf(" %02X",tempval);
  117. switch (XbeeConst_ReadAPINum) {
  118. case 0:
  119. if (tempval == 0x7e) { //协议头前面1个7E判断
  120. XbeeConst_FRxBufAPI[0] = tempval;
  121. XbeeConst_ReadAPINum = 1;
  122. }
  123. break;
  124. case 1:
  125. if (tempval == 0x7e) { //协议头前面1个7E判断
  126. XbeeConst_FRxBufAPI[0] = tempval;
  127. XbeeConst_ReadAPINum = 1;
  128. }
  129. else {
  130. if (tempval == 0x7D) XbeeConst_needEscape = 1;
  131. else
  132. {
  133. if (XbeeConst_needEscape) {
  134. XbeeConst_needEscape=0;
  135. tempval = tempval^0x20;
  136. }
  137. XbeeConst_FRxBufAPI[XbeeConst_ReadAPINum++] = tempval;
  138. }
  139. }
  140. break;
  141. case 2:
  142. if (tempval == 0x7e) { //协议头前面1个7E判断
  143. XbeeConst_FRxBufAPI[0] = tempval;
  144. XbeeConst_ReadAPINum = 1;
  145. }
  146. else {
  147. if (tempval == 0x7D) XbeeConst_needEscape = 1;
  148. else
  149. {
  150. if (XbeeConst_needEscape) {
  151. XbeeConst_needEscape=0;
  152. tempval = tempval^0x20;
  153. }
  154. XbeeConst_FRxBufAPI[XbeeConst_ReadAPINum++] = tempval;
  155. }
  156. }
  157. break;
  158. default:
  159. if (tempval == 0x7e) { //协议头前面1个7E判断
  160. XbeeConst_FRxBufAPI[0] = tempval;
  161. XbeeConst_ReadAPINum = 1;
  162. }
  163. else {
  164. if (tempval == 0x7D) XbeeConst_needEscape = 1;
  165. else
  166. {
  167. if (XbeeConst_needEscape) {
  168. XbeeConst_needEscape=0;
  169. tempval = tempval^0x20;
  170. }
  171. XbeeConst_FRxBufAPI[XbeeConst_ReadAPINum++] = tempval;
  172. }
  173. }
  174. if (XbeeConst_ReadAPINum > XbeeConst_FRxBufAPI[2]+3) { //读完已知长度的数据
  175. // rt_kprintf("\n");
  176. for (unsigned short i = 3; i < XbeeConst_FRxBufAPI[2]+3; i++)
  177. checksum += XbeeConst_FRxBufAPI[i];
  178. if (((0xff - checksum)&0xFF) == XbeeConst_FRxBufAPI[XbeeConst_FRxBufAPI[2]+3]) //校验成功
  179. {
  180. if (XbeeConst_FRxBufAPI[3] == 0x90) {
  181. if((XbeeConst_FRxBufAPI[18]&0xF0) == 0xA0 || (XbeeConst_FRxBufAPI[18]) == 0x14) //铲车数据
  182. {
  183. if ((XbeeConst_FRxBufAPI[18] == 0xA1 && WeightConst_SBType == 2) ||
  184. (XbeeConst_FRxBufAPI[18] != 0xA1 ))
  185. for (unsigned short i = 15; i < XbeeConst_FRxBufAPI[2]+3; i++)
  186. rb_push_insert(RemoteQueue, XbeeConst_FRxBufAPI[i]); //赋值数据到数组
  187. }
  188. else if (XbeeConst_FRxBufAPI[18] == 0xB2 && XbeeConst_FRxBufAPI[19] == 0x03) //收到设置铲车定向
  189. {
  190. for (unsigned short i = 0; i < 8; i++) XbeeConst_CCAddress[i] = XbeeConst_FRxBufAPI[i+4];
  191. ccsend = (XbeeConst_FRxBufAPI[17] == XbeeConst_DeviceAddress);
  192. }
  193. else if (XbeeConst_FRxBufAPI[17] == XbeeConst_DeviceAddress || XbeeConst_FRxBufAPI[17] == 0xFF)
  194. for (unsigned short i = 15; i < XbeeConst_FRxBufAPI[2]+3; i++)
  195. rb_push_insert(XbeeQueue, XbeeConst_FRxBufAPI[i]); //赋值数据到数组
  196. if(XbeeConst_FRxBufAPI[18]==0x18)
  197. for (unsigned short i = 0; i < 8; i++)
  198. XbeeConst_CenterAddress[i] = XbeeConst_FRxBufAPI[i+4];
  199. }
  200. }
  201. XbeeConst_ReadAPINum = 0; //循环计数清零
  202. }
  203. break;
  204. }
  205. }
  206. }
  207. static unsigned char planreget_save,planreget = 0;
  208. void RX(void) {
  209. unsigned char tempval, checksum = 0;
  210. if (rb_full_count(XbeeQueue)> 0) {
  211. tempval = rb_remove(XbeeQueue); //继续存数据
  212. switch (XbeeConst_ReadNum0) {
  213. case 0:
  214. if (tempval == 0x7e) { //协议头前面1个7E判断
  215. XbeeConst_FRxBuf0[XbeeConst_ReadNum0] = tempval;
  216. XbeeConst_ReadNum0++;
  217. }
  218. break;
  219. case 1:
  220. XbeeConst_FRxBuf0[XbeeConst_ReadNum0] = tempval; //这个字节为帧长度
  221. XbeeConst_ReadNum0++;
  222. XbeeConst_FrameLength0 = XbeeConst_FRxBuf0[1] + 1; //算出帧长度
  223. break;
  224. default:
  225. XbeeConst_FRxBuf0[XbeeConst_ReadNum0] = tempval; //读数据
  226. if (XbeeConst_ReadNum0 >= XbeeConst_FrameLength0) { //读完已知长度的数据
  227. for (unsigned short i = 0; i < XbeeConst_FrameLength0; i++)
  228. checksum += XbeeConst_FRxBuf0[i];
  229. if ((0xff - checksum) == XbeeConst_FRxBuf0[XbeeConst_FrameLength0]) //校验成功
  230. {
  231. rt_strncpy(dataframehead.Buffer, XbeeConst_FRxBuf0, 4);
  232. if ((dataframehead.data.addr&0x0F) == XbeeConst_DeviceAddress || dataframehead.data.addr == 0xFF) { //收到本设备的数据,不是本设备的不处理
  233. if (dataframehead.data.frameType == 0x10) { //实时重量
  234. if (XbeeConst_FRxBuf0[XbeeConst_FrameLength0-1] == XbeeConst_FrameNumLast) {
  235. save_read_ts('w');
  236. XbeeConst_CanSend=0x01;//取消无线发送锁定
  237. }
  238. }
  239. else if (dataframehead.data.frameType == 0x11 ) { //1号键回复
  240. if (XbeeConst_FRxBuf0[XbeeConst_FrameLength0-1] == XbeeConst_FrameNumLast) {
  241. save_read_ts('k');
  242. XbeeConst_CanSend=0x01;//取消无线发送锁定
  243. }
  244. }
  245. else if (dataframehead.data.frameType == 0x12 || dataframehead.data.frameType == 0x15) { //2号键内容
  246. // rt_kprintf("%02X//////////////////////////%02X \n",dataframehead.data.frameType,XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]);
  247. if (dataframehead.data.frameType == 0x12)
  248. {
  249. // if(isn==1) {
  250. // clearScreenAll_();
  251. // delay_ms(100);
  252. // }
  253. if (getPlaning==1)
  254. {
  255. planreget_save = 0;
  256. readyPop = 1;
  257. for (int i=0;i<XbeeConst_FRxBuf0[1]+2;i++)
  258. XbeeConst_FRxBuf12[i] = XbeeConst_FRxBuf0[i];
  259. }
  260. //setWeightConst_initProduct();
  261. // rt_sem_release(pop_product_sem);//需要弹出最新内容
  262. }
  263. else if (dataframehead.data.frameType == 0x15)
  264. {
  265. // rt_kprintf("--XbeeConst_lastsort:%02X \n", XbeeConst_lastsort);
  266. // rt_kprintf("0x15 ------index:%02X \n",XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]);
  267. if (XbeeConst_lastsort==0xFD && XbeeConst_lastsort < XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1])
  268. reset_read_ts('p');
  269. if (readyPop==1 && getPlaning==1)
  270. {
  271. // readyPop = 0;
  272. getPlaning=0;
  273. planreget = XbeeConst_FRxBuf12[XbeeConst_FRxBuf12[1]-3]&0x01;
  274. reset_read_ts('p');
  275. ts_pushArrary(&ts_plandb, XbeeConst_FRxBuf12, XbeeConst_FRxBuf12[1]+2);
  276. // rt_kprintf("\n");
  277. // rt_kprintf("\n");
  278. // rt_kprintf("\n");
  279. // rt_kprintf("0x12-----------------------------\n");
  280. // rt_kprintf("pop index:%02X \n",XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]);
  281. rt_sem_release(pop_product_sem);//需要弹出最新内容
  282. }
  283. if (planreget_save==0 && planreget==1){
  284. XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-2] = XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-2] || 0x01;
  285. }
  286. planreget_save =1;
  287. uint8_t foundlastplan_ = 0xF0;
  288. foundlastplan_ = findlastplan();
  289. if (XbeeConst_lastsort != XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]
  290. && foundlastplan_ != XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1])
  291. ts_pushArrary(&ts_plandb, XbeeConst_FRxBuf0, XbeeConst_FRxBuf0[1]+2);
  292. save_read_ts('k');
  293. XbeeConst_CanSend=0x01;//取消无线发送锁定
  294. if (XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]<0xFD && getPlaning==0) { //如果没有结束,则发送05协议
  295. XbeeConst_lastsort = XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1];
  296. XbeeConst_lastinorout = XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-3]>>7;
  297. // rt_kprintf("XbeeConst_lastsort get:%02X \n", XbeeConst_lastsort);
  298. sendkey2(0xF5);
  299. }
  300. else if (XbeeConst_lastsort==0xFD && readyPop==0 && XbeeConst_lastsort < XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1])
  301. {
  302. rt_sem_release(pop_product_sem);//如果直接收到15,如全天结束或空计划,需要弹出最新内容
  303. }
  304. if (readyPop==1) readyPop = 0;
  305. }
  306. }
  307. else if (dataframehead.data.frameType == 0x14) { //无线按键处理
  308. RemoteSendDisplay = 0;
  309. if (isCC)
  310. checksum =0x7E + 0x05 + ((XbeeConst_FRxBuf0_Remote[2] & 0x0F) << 4 ) || XbeeConst_DeviceAddress
  311. + 0xA4 + XbeeConst_FRxBuf0[4] + XbeeConst_FRxBuf0[5];
  312. else
  313. checksum =0x7E + 0x05 + XbeeConst_DeviceAddress + 0xA4 + XbeeConst_FRxBuf0[4] + XbeeConst_FRxBuf0[5];
  314. XbeeConst_FRemoteTxBuf[0] = 0x07;
  315. XbeeConst_FRemoteTxBuf[1] = 0x7E;
  316. XbeeConst_FRemoteTxBuf[2] = 0x05;
  317. if (isCC)
  318. XbeeConst_FRemoteTxBuf[3] = ((XbeeConst_FRxBuf0_Remote[2] & 0x0F) << 4 ) | XbeeConst_DeviceAddress;
  319. else
  320. XbeeConst_FRemoteTxBuf[3] = XbeeConst_DeviceAddress;
  321. XbeeConst_FRemoteTxBuf[4] = 0xA4;
  322. XbeeConst_FRemoteTxBuf[5] = XbeeConst_FRxBuf0[4];
  323. XbeeConst_FRemoteTxBuf[6] = XbeeConst_FRxBuf0[5];
  324. XbeeConst_FRemoteTxBuf[7] = 0xff - checksum;
  325. if (XbeeConst_UseAPI>0) {
  326. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  327. setSendFrame_t(0x01,0x01,0xC1);
  328. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);
  329. for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
  330. usart2_send_data[i - 1] = XbeeConst_PTxBuf0[i];
  331. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  332. rt_sem_release(uart2_lock);
  333. }
  334. else {
  335. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  336. for (int i = 1; i <= XbeeConst_FRemoteTxBuf[0]; i++) //将缓冲区发出
  337. usart2_send_data[i - 1] = XbeeConst_FRemoteTxBuf[i];
  338. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_FRemoteTxBuf[0]);
  339. rt_sem_release(uart2_lock);
  340. }
  341. RemoteSendDisplay = 1;
  342. if (XbeeConst_FRxBuf0[4]==0x01 && button_state.b1 ==0 && delayKeying == 0)
  343. {
  344. if (WeightConst_DELAYON == 0 && delayKeying == 0)
  345. {
  346. rt_sem_release(delay_key_sem); //收到1号键
  347. sendkey1(0xF3, ((XbeeConst_FRxBuf0_Remote[2] >> 4) & 0x0F) );
  348. WeightConst_AutoSingleNSecondPoint=0; // 防止自动跳转,重置自动记录指针
  349. WeightConst_oksum = 0;
  350. WeightConst_allsum = 0;
  351. button_state.b1=1; //5秒内不可按键
  352. }
  353. }
  354. else if (XbeeConst_FRxBuf0[4]==0x02 && button_state.b2==0) //收到2号键
  355. // sendkey2(0xF4);
  356. if(WeightConst_DELAYON==0)
  357. {
  358. if (tsdb_recordcount('k')==0)
  359. {
  360. sendkey2(0xF4);
  361. // WeightConst_AutoSingleNSecondPoint=0; // 防止自动跳转,重置自动记录指针
  362. // WeightConst_oksum = 0;
  363. // WeightConst_allsum = 0;
  364. }
  365. }
  366. button_state.b2=1; //5秒内不可按键
  367. }
  368. else if (dataframehead.data.frameType == 0xA1) { //铲车显示处理
  369. for (unsigned short i=4; i < 22; i++) { //显示重量
  370. uart4_send_data[i - 4] = XbeeConst_FRxBuf0[i];
  371. uart4_send_data[18]=0;
  372. for (int i = 2; i <18; i++) //校验和
  373. uart4_send_data[18]=uart4_send_data[18]+uart4_send_data[i];
  374. rt_sem_take(uart4_lock, RT_WAITING_FOREVER);
  375. HAL_UART_Transmit_DMA(&huart4, uart4_send_data, 19);
  376. rt_sem_release(uart4_lock);
  377. }
  378. }
  379. else if (dataframehead.data.frameType == 0xA4) { //无线按键成功处理
  380. beep();
  381. }
  382. else if (dataframehead.data.frameType == 0x16) { //设置无线模式
  383. XbeeConst_UseAPI = XbeeConst_FRxBuf0[4];
  384. write_Flash("API", &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI));
  385. setAPI();
  386. beep();
  387. }
  388. else if (dataframehead.data.frameType == 0x17) { //设置屏幕类型
  389. WeightConst_SBType = XbeeConst_FRxBuf0[4];
  390. write_Flash("PM", &WeightConst_SBType, sizeof(WeightConst_SBType));
  391. beep();
  392. }
  393. else if (dataframehead.data.frameType == 0x19) { //设置地址
  394. XbeeConst_DeviceAddress = XbeeConst_FRxBuf0[4];
  395. write_Flash("CH", &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress));
  396. beep();
  397. }
  398. else if (dataframehead.data.frameType == 0x1A) { //设置提取类型
  399. WeightConst_TQ = XbeeConst_FRxBuf0[4];
  400. write_Flash("TQ", &WeightConst_TQ, sizeof(WeightConst_TQ));
  401. //---------- USART_Configuration();
  402. beep();
  403. }
  404. else if (dataframehead.data.frameType == 0x1B) { //设置网络编号
  405. beep();
  406. XbeeConst_NetAddress = XbeeConst_FRxBuf0[4];
  407. write_Flash("WL", &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress));
  408. setNetAddress();
  409. beep();
  410. }
  411. else if (dataframehead.data.frameType == 0x18) { //设置时间
  412. XbeeConst_CanSend=0x01;//取消无线发送锁定
  413. TimeConst.Buffer[0] = XbeeConst_FRxBuf0[7];
  414. TimeConst.Buffer[1] = XbeeConst_FRxBuf0[6];
  415. TimeConst.Buffer[2] = XbeeConst_FRxBuf0[5];
  416. TimeConst.Buffer[3] = XbeeConst_FRxBuf0[4];
  417. save_read_ts('k');
  418. rt_sem_take(setTIME_lock, RT_WAITING_FOREVER);
  419. write_Flash("TimeConst", &TimeConst, sizeof(TimeConst));
  420. rt_sem_release(setTIME_lock);
  421. CanButton = 0x01;
  422. beep();
  423. }
  424. }
  425. }
  426. XbeeConst_ReadNum0 = 0; //循环计数清零
  427. XbeeConst_FrameLength0 = 0;
  428. } else {
  429. XbeeConst_ReadNum0++; //小于帧长度
  430. if (XbeeConst_ReadNum0>90)
  431. {
  432. XbeeConst_ReadNum0 = 0; //循环计数清零
  433. XbeeConst_FrameLength0 = 0;
  434. }
  435. }
  436. break;
  437. }
  438. }
  439. }
  440. void RX_Remote(void) {
  441. unsigned char tempval, checksum = 0;
  442. if (rb_full_count(RemoteQueue) > 0) {
  443. tempval = rb_remove(RemoteQueue); //继续存数据
  444. switch (XbeeConst_ReadNum0_Remote) {
  445. case 0:
  446. if (tempval == 0x7e) { //协议头前面1个7E判断
  447. XbeeConst_FRxBuf0_Remote[XbeeConst_ReadNum0_Remote] = tempval;
  448. XbeeConst_ReadNum0_Remote++;
  449. }
  450. break;
  451. case 1:
  452. XbeeConst_FRxBuf0_Remote[XbeeConst_ReadNum0_Remote] = tempval; //这个字节为帧长度
  453. XbeeConst_ReadNum0_Remote++;
  454. XbeeConst_FrameLength0_Remote= XbeeConst_FRxBuf0_Remote[1] + 1; //算出帧长度
  455. break;
  456. default:
  457. XbeeConst_FRxBuf0_Remote[XbeeConst_ReadNum0_Remote] = tempval; //读数据
  458. if (XbeeConst_ReadNum0_Remote >= XbeeConst_FrameLength0_Remote) { //读完已知长度的数据
  459. for (unsigned short i = 0; i < XbeeConst_FrameLength0_Remote; i++)
  460. checksum += XbeeConst_FRxBuf0_Remote[i];
  461. if ((0xff - checksum) == XbeeConst_FRxBuf0_Remote[XbeeConst_FrameLength0_Remote]) //校验成功
  462. {
  463. if (
  464. (((XbeeConst_FRxBuf0_Remote[2] & 0x0F) == XbeeConst_RemoteAddress && (WeightConst_SBType == 2 || WeightConst_SBType == 7))
  465. ||
  466. ((XbeeConst_FRxBuf0_Remote[2] & 0x0F) == XbeeConst_DeviceAddress && (WeightConst_SBType != 2 && WeightConst_SBType != 7)))
  467. ||
  468. (XbeeConst_RemoteAddress==0 && XbeeConst_FRxBuf0_Remote[2]==0x0A)) { //收到本设备的数据,不是本设备的不处理
  469. if ((XbeeConst_FRxBuf0_Remote[3] == 0xA1 && (WeightConst_SBType == 2 || WeightConst_SBType == 7) )
  470. || (XbeeConst_FRxBuf0_Remote[3] == 0xF1 && (WeightConst_SBType == 2 || WeightConst_SBType == 7) )
  471. )
  472. { //铲车显示处理
  473. if (XbeeConst_FRxBuf0_Remote[14]==0xC8 && XbeeConst_FRxBuf0_Remote[15]==0xB7) //确认开始
  474. {
  475. XbeeConst_FRxBuf0_Remote[21] = 0x20;
  476. XbeeConst_FRxBuf0_Remote[20] = 0x20;
  477. XbeeConst_FRxBuf0_Remote[19] = 0x20;
  478. XbeeConst_FRxBuf0_Remote[18] = 0x20;
  479. XbeeConst_FRxBuf0_Remote[17] = 0x20;
  480. XbeeConst_FRxBuf0_Remote[16] = 0x20;
  481. XbeeConst_FRxBuf0_Remote[15] = 0x20;
  482. XbeeConst_FRxBuf0_Remote[14] = XbeeConst_RemoteAddress + 0x30;
  483. }
  484. else
  485. {
  486. XbeeConst_FRxBuf0_Remote[16] = XbeeConst_FRxBuf0_Remote[15];
  487. XbeeConst_FRxBuf0_Remote[15] = XbeeConst_FRxBuf0_Remote[14];
  488. XbeeConst_FRxBuf0_Remote[14] = XbeeConst_RemoteAddress + 0x30;
  489. }
  490. for (unsigned short i=4; i < 22; i++) //显示重量
  491. uart4_send_data[i - 4] = XbeeConst_FRxBuf0_Remote[i];
  492. uart4_send_data[18]=0;
  493. for (int i = 2; i <18; i++) //校验和
  494. uart4_send_data[18]=uart4_send_data[18]+uart4_send_data[i];
  495. // if (WeightConst_SBType == 7)
  496. // {
  497. // for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++) {
  498. // WeightConst_feed[SendDisplay_i] = uart4_send_data[2+SendDisplay_i] ;
  499. // WeightConst_Weight_display[7-SendDisplay_i] = uart4_send_data[10+SendDisplay_i] ;
  500. // WeightConst_Weight_display[7] = 0x20;
  501. // }
  502. // get_msg(4,//Xstart
  503. // 8,//Ystart
  504. // 1,//size
  505. // 0,//reset
  506. // 0,//speed
  507. // 8,//字符个数
  508. // 3,//界面数
  509. // 0,//id
  510. // 0x5B,//Xend
  511. // 0,
  512. // &WeightConst_feed[0]//字码
  513. // );
  514. // delay_ms(200);
  515. // get_msg(92,//Xstart
  516. // 4,//Ystart
  517. // 2,//size
  518. // 0,//reset
  519. // 0,//speed
  520. // 8,//字符个数
  521. // 3,//界面数
  522. // 1,//id
  523. // 0xBF,//Xend
  524. // 1,
  525. // &WeightConst_Weight_display[0]//字码
  526. // );
  527. // }
  528. // else
  529. {
  530. rt_sem_take(uart4_lock, RT_WAITING_FOREVER);
  531. HAL_UART_Transmit_DMA(&huart4, uart4_send_data, 19);
  532. rt_thread_mdelay(50);
  533. rt_sem_release(uart4_lock);
  534. }
  535. }
  536. else if (XbeeConst_FRxBuf0_Remote[3] == 0xA4 && (WeightConst_SBType == 2 || WeightConst_SBType == 7) )
  537. { //铲车收到无线按键成功处理
  538. beep();
  539. }
  540. else if (
  541. (XbeeConst_FRxBuf0_Remote[3] == 0xA2 || XbeeConst_FRxBuf0_Remote[3] == 0x14)
  542. && (WeightConst_SBType != 2 && WeightConst_SBType != 7) && XbeeConst_FRxBuf0_Remote[1] == 0x05
  543. // (XbeeConst_FRxBuf0_Remote[3] == 0xA2 && WeightConst_SBType != 2 && XbeeConst_UseAPI)
  544. // || (XbeeConst_FRxBuf0_Remote[3] == 0x14 && WeightConst_SBType != 2 && !XbeeConst_UseAPI)
  545. ) { //TMR收到无线按键处理
  546. RemoteSendDisplay = 0;
  547. XbeeConst_FRemoteTxBuf[0] = 7;
  548. XbeeConst_FRemoteTxBuf[1] = 0x7E;
  549. XbeeConst_FRemoteTxBuf[2] = 0x05;
  550. if (isCC)
  551. XbeeConst_FRemoteTxBuf[3] = ((XbeeConst_FRxBuf0_Remote[2] & 0x0F) << 4 ) | XbeeConst_DeviceAddress;
  552. else
  553. XbeeConst_FRemoteTxBuf[3] = XbeeConst_DeviceAddress;
  554. XbeeConst_FRemoteTxBuf[4] = 0xA4;
  555. XbeeConst_FRemoteTxBuf[5] = XbeeConst_FRxBuf0_Remote[4];
  556. XbeeConst_FRemoteTxBuf[6] = XbeeConst_FRxBuf0_Remote[5];
  557. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0;
  558. for (int i = 1; i < XbeeConst_FRemoteTxBuf[0]; i++)
  559. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
  560. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[i];
  561. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];
  562. if (XbeeConst_UseAPI>0) {
  563. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  564. setSendFrame_t(0x01,0x01,0xC1);
  565. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);
  566. for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
  567. usart2_send_data[i - 1] = XbeeConst_PTxBuf0[i];
  568. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  569. rt_thread_mdelay(50);
  570. rt_sem_release(uart2_lock);
  571. }
  572. else
  573. {
  574. if (iscowweight<1 )
  575. {
  576. rt_sem_take(uart3_lock, RT_WAITING_FOREVER);
  577. for (int i = 1; i <= XbeeConst_FRemoteTxBuf[0]; i++) //将缓冲区发出
  578. usart3_send_data[i - 1] = XbeeConst_FRemoteTxBuf[i];
  579. HAL_UART_Transmit_DMA(&huart3, usart3_send_data, XbeeConst_FRemoteTxBuf[0]);
  580. rt_thread_mdelay(50);
  581. rt_sem_release(uart3_lock);
  582. }
  583. }
  584. RemoteSendDisplay = 1;
  585. if (XbeeConst_FRxBuf0_Remote[4]==0x01 && button_state.b1==0)//收到1号键
  586. {
  587. if (pf_comp.data.needBegin == 1 && WeightConst_WeightHaveBegin==0)
  588. {
  589. beep();
  590. rt_sem_take(weight_lock, RT_WAITING_FOREVER);
  591. WeightConst_WeightBegin = WeightConst_WeightCur;
  592. rt_sem_release(weight_lock);
  593. rt_sem_take(setTIME_lock, RT_WAITING_FOREVER);
  594. write_Flash("begintime", &TimeConst, sizeof(TimeConst));
  595. rt_sem_release(setTIME_lock);
  596. write_Flash("beginweight", &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin));
  597. getTargetWeight();
  598. }
  599. else if (pf_comp.data.pfsort != 0xFE && delayKeying == 0)
  600. {
  601. if (WeightConst_DELAYON == 0 && delayKeying == 0)
  602. {
  603. rt_sem_release(delay_key_sem);
  604. sendkey1(0xF3, ((XbeeConst_FRxBuf0_Remote[2] >> 4) & 0x0F));
  605. WeightConst_AutoSingleNSecondPoint=0; // 防止自动跳转,重置自动记录指针
  606. WeightConst_oksum = 0;
  607. WeightConst_allsum = 0;
  608. button_state.b1=1; //5秒内不可按键
  609. }
  610. }
  611. }
  612. else if (XbeeConst_FRxBuf0_Remote[4]==0x02 && button_state.b2==0) //收到2号键
  613. {
  614. if(!WeightConst_DELAYON>0)
  615. {
  616. if (tsdb_recordcount('k')==0)
  617. sendkey2(0xF4);
  618. }
  619. // WeightConst_AutoSingleNSecondPoint=0; // 防止自动跳转,重置自动记录指针
  620. // WeightConst_oksum = 0;
  621. // WeightConst_allsum = 0;
  622. button_state.b2=1; //5秒内不可按键
  623. }
  624. }
  625. }
  626. }
  627. XbeeConst_ReadNum0_Remote = 0; //循环计数清零
  628. XbeeConst_FrameLength0_Remote = 0;
  629. } else {
  630. XbeeConst_ReadNum0_Remote++; //小于帧长度
  631. if (XbeeConst_ReadNum0_Remote>90)
  632. {
  633. XbeeConst_ReadNum0_Remote = 0; //循环计数清零
  634. XbeeConst_FrameLength0_Remote = 0;
  635. }
  636. }
  637. break;
  638. }
  639. }
  640. }
  641. static void threadEmptyBuffer_entry(void *parameter)
  642. {
  643. unsigned char tempval;
  644. while(1)
  645. {
  646. rt_sem_take(thread_EmptyBuffer_sem, RT_WAITING_FOREVER); //等待串口消息
  647. while (rb_full_count(rb_usart1) > 0) {
  648. ReadWeight();
  649. }
  650. while (rb_full_count(rb_usart2) > 0) {
  651. if (XbeeConst_UseAPI>0) {
  652. rb_push_insert(XbeeAPIQueue,rb_remove(rb_usart2));
  653. RX_API();
  654. }
  655. else
  656. rb_push_insert(XbeeQueue,rb_remove(rb_usart2));
  657. while(rb_full_count(XbeeQueue)>0) RX();
  658. while(rb_full_count(RemoteQueue)>0) RX_Remote();
  659. }
  660. while (rb_full_count(rb_usart3) > 0) {
  661. if (XbeeConst_UseAPI>0) {
  662. if (WeightConst_BLE==1 || WeightConst_BLE== 3)
  663. {
  664. rb_push_insert(rb_usart1, rb_remove(rb_usart3));
  665. }
  666. else {
  667. rb_remove(rb_usart3);
  668. } //不能删除,用于清空3号串口
  669. }
  670. else
  671. {
  672. rb_push_insert(RemoteQueue, rb_remove(rb_usart3));
  673. while(rb_full_count(RemoteQueue)>0) RX_Remote();
  674. }
  675. }
  676. while (rb_full_count(rb_uart4) > 0) {
  677. tempval = rb_remove(rb_uart4); //继续存数据
  678. //if (tempval==0x59 && finish_send==0) finish_send=1;
  679. }
  680. }
  681. }
  682. static rt_thread_t uart_tid = RT_NULL;
  683. void threadUart_init(void)
  684. {
  685. uart_tid = rt_thread_create("EmptyBuf",
  686. threadEmptyBuffer_entry, RT_NULL,
  687. 0x500,
  688. 4, 5);
  689. /* 如果获得线程控制块,启动这个线程 */
  690. if (uart_tid != RT_NULL) rt_thread_startup(uart_tid);
  691. }