usart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. #include "usart.h"
  2. #include "mmodbus.h"
  3. #include "dlt645.h"
  4. #include "gateway_message.h"
  5. #include "tcp_server.h"
  6. #include "timer.h"
  7. #include "updata.h"
  8. uint8_t IRQstate = 0;
  9. uint8_t rxByte;
  10. uint8_t rx_232_Buffer[RX_BUFFER_SIZE];
  11. uint8_t recv_232_done;
  12. unsigned int rxIndex = 0;
  13. uint8_t UART6_RX_BUF[BUFF_SIZE];
  14. uint8_t UART6_RX_STAT=0;
  15. uint32_t UART6_RX_NUM = 0;
  16. UART_HandleTypeDef USART_InitStruct_485;
  17. UART_HandleTypeDef USART_InitStruct_232;
  18. UART_HandleTypeDef USART_InitStruct_DEBUG;
  19. UART_HandleTypeDef USART_InitStruct_Test;
  20. DMA_HandleTypeDef DMA_DEBUG_RX;
  21. //重定向c库函数printf到串口,重定向后可使用printf函数
  22. int fputc(int ch, FILE *f)
  23. {
  24. // /* 发送一个字节数据到串口 */
  25. // USART_232->DR = (ch & (uint16_t)0x01FF);
  26. //
  27. // /* 等待发送完毕 */
  28. // while (__HAL_UART_GET_FLAG(&USART_InitStruct_232, USART_FLAG_TXE) == RESET);
  29. // return (ch);
  30. }
  31. /* 232串口配置 */
  32. //void USART_232_config(void)
  33. //{
  34. // __HAL_RCC_GPIOA_CLK_ENABLE();
  35. // __HAL_RCC_USART1_CLK_ENABLE();
  36. //
  37. // GPIO_InitTypeDef GPIO_InitStruct = {0};
  38. // GPIO_InitStruct.Pin = USART_232_TX_PIN | USART_232_RX_PIN;
  39. // GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  40. // GPIO_InitStruct.Pull = GPIO_PULLUP;
  41. // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  42. // GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  43. // HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  44. //
  45. // USART_InitStruct_232.Instance = USART_232;
  46. // USART_InitStruct_232.Init.BaudRate = USART_232_BAUDRATE;
  47. // USART_InitStruct_232.Init.WordLength = UART_WORDLENGTH_8B;
  48. // USART_InitStruct_232.Init.StopBits = UART_STOPBITS_1;
  49. // USART_InitStruct_232.Init.Parity = UART_PARITY_NONE;
  50. // USART_InitStruct_232.Init.Mode = UART_MODE_TX_RX;
  51. // USART_InitStruct_232.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  52. // USART_InitStruct_232.Init.OverSampling = UART_OVERSAMPLING_8;
  53. // HAL_UART_Init(&USART_InitStruct_232);
  54. //
  55. // HAL_NVIC_SetPriority(USART_232_IRQ, 1, 0);
  56. // HAL_NVIC_EnableIRQ(USART_232_IRQ);
  57. //
  58. //// HAL_UART_Receive_IT(&USART_InitStruct_232, &rxByte, 1);
  59. // __HAL_UART_ENABLE_IT(&USART_InitStruct_232, UART_IT_RXNE); // 使能接收中断
  60. // __HAL_UART_ENABLE_IT(&USART_InitStruct_232, UART_IT_IDLE); // 使能空闲中断
  61. //}
  62. void USART_232_config(void)
  63. {
  64. __HAL_RCC_GPIOA_CLK_ENABLE();
  65. __HAL_RCC_USART1_CLK_ENABLE();
  66. GPIO_InitTypeDef GPIO_InitStruct = {0};
  67. GPIO_InitStruct.Pin = USART_232_TX_PIN | USART_232_RX_PIN;
  68. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  69. GPIO_InitStruct.Pull = GPIO_PULLUP;
  70. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  71. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  72. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  73. USART_InitStruct_232.Instance = USART_232;
  74. USART_InitStruct_232.Init.BaudRate = USART_232_BAUDRATE;
  75. USART_InitStruct_232.Init.WordLength = UART_WORDLENGTH_8B;
  76. USART_InitStruct_232.Init.StopBits = UART_STOPBITS_1;
  77. USART_InitStruct_232.Init.Parity = UART_PARITY_NONE;
  78. USART_InitStruct_232.Init.Mode = UART_MODE_TX_RX;
  79. USART_InitStruct_232.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  80. USART_InitStruct_232.Init.OverSampling = UART_OVERSAMPLING_8;
  81. HAL_UART_Init(&USART_InitStruct_232);
  82. HAL_NVIC_SetPriority(USART_232_IRQ, 1, 0);
  83. HAL_NVIC_EnableIRQ(USART_232_IRQ);
  84. HAL_UART_Receive_IT(&USART_InitStruct_232, &rxByte, 1);
  85. }
  86. // void USART_232_IRQHandler(void){
  87. // HAL_UART_IRQHandler(&USART_InitStruct_232);
  88. // }
  89. void USART_232_IRQHandler(void){
  90. GATEWAY_PARAMS *get;
  91. get= get_gateway_config_params();
  92. if(get->comProtocol)// get->protocol 1:232 0:485
  93. {
  94. if(get->device_params->protocol == 3)
  95. mmodbus_callback(&USART_InitStruct_232);
  96. else if(get->device_params->protocol == 1 || get->device_params->protocol == 2)
  97. dlt_callback(&USART_InitStruct_232);
  98. }
  99. else
  100. {
  101. HAL_UART_IRQHandler(&USART_InitStruct_232);
  102. if(__HAL_UART_GET_FLAG(&USART_InitStruct_232, UART_FLAG_RXNE) && __HAL_UART_GET_IT_SOURCE(&USART_InitStruct_232, UART_IT_RXNE))
  103. {
  104. if (mmodbus.rxIndex < RX_BUFFER_SIZE - 1)
  105. {
  106. rx_232_Buffer[rxIndex] = USART_InitStruct_232.Instance->DR;
  107. rxIndex++;
  108. }
  109. else
  110. {
  111. uint8_t data = USART1->DR;
  112. }
  113. }
  114. if (RESET != __HAL_UART_GET_FLAG(&USART_InitStruct_232, USART_FLAG_IDLE) && __HAL_UART_GET_IT_SOURCE(&USART_InitStruct_232, UART_IT_IDLE))
  115. {
  116. uint8_t i;
  117. i = USART_InitStruct_232.Instance->SR;
  118. i = USART_InitStruct_232.Instance->DR;
  119. rxIndex = 0;
  120. recv_232_done = 1;
  121. return;
  122. }
  123. }
  124. }
  125. void USART_232_Send(uint8_t *message,uint16_t size)
  126. {
  127. recv_232_done = 0;
  128. HAL_UART_Transmit(&USART_InitStruct_232, message,size,HAL_MAX_DELAY);
  129. }
  130. /* 485串口配置 */
  131. void USART_485_DE_init()
  132. {
  133. __HAL_RCC_GPIOC_CLK_ENABLE();
  134. GPIO_InitTypeDef GPIO_InitStruct = {0};
  135. GPIO_InitStruct.Pin = USART_485_DE_PIN;
  136. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  137. GPIO_InitStruct.Pull = GPIO_PULLUP;
  138. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  139. HAL_GPIO_Init(USART_485_DE_GPIO_PORT, &GPIO_InitStruct);
  140. }
  141. void USART_485_DE_TX()
  142. {
  143. HAL_GPIO_WritePin(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,1);
  144. }
  145. void USART_485_DE_RX()
  146. {
  147. HAL_GPIO_WritePin(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,0);
  148. }
  149. // 485发送数据
  150. void USART_485_Send(uint8_t *message,uint16_t size)
  151. {
  152. uint16_t i=0;
  153. USART_485_DE_TX();
  154. HAL_UART_Transmit(&USART_InitStruct_485, message, size, HAL_MAX_DELAY);
  155. USART_485_DE_RX();
  156. }
  157. // USART_485 初始化
  158. void USART_485_config()
  159. {
  160. USART_485_DE_init();
  161. __HAL_RCC_USART3_CLK_ENABLE();
  162. __HAL_RCC_GPIOC_CLK_ENABLE();
  163. GPIO_InitTypeDef GPIO_InitStruct = {0};
  164. // 配置 GPIOC Pin 11 和 10 分别作为 USART3 的 RX 和 TX 引脚
  165. GPIO_InitStruct.Pin = USART_485_RX_PIN | USART_485_TX_PIN;
  166. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  167. GPIO_InitStruct.Pull = GPIO_PULLUP;
  168. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  169. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  170. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  171. USART_InitStruct_485.Instance = USART_485;
  172. USART_InitStruct_485.Init.BaudRate = USART_485_BAUDRATE;
  173. USART_InitStruct_485.Init.WordLength = UART_WORDLENGTH_8B;
  174. USART_InitStruct_485.Init.StopBits = UART_STOPBITS_1;
  175. USART_InitStruct_485.Init.Parity = UART_PARITY_NONE;
  176. USART_InitStruct_485.Init.Mode = UART_MODE_TX_RX; // 既支持发送也支持接收
  177. USART_InitStruct_485.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  178. USART_InitStruct_485.Init.OverSampling = UART_OVERSAMPLING_16;
  179. HAL_UART_Init(&USART_InitStruct_485);
  180. HAL_NVIC_SetPriority(USART_485_IRQ, 0, 0);
  181. HAL_NVIC_EnableIRQ(USART_485_IRQ);
  182. // 初始化 DE 引脚 接收数据
  183. HAL_GPIO_WritePin(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,GPIO_PIN_RESET);
  184. // HAL_UART_Receive_IT(&USART_InitStruct_485, &rxByte, 1);
  185. __HAL_UART_ENABLE_IT(&USART_InitStruct_485, UART_IT_RXNE); // 使能接收中断
  186. __HAL_UART_ENABLE_IT(&USART_InitStruct_485, UART_IT_IDLE); // 使能空闲中断
  187. }
  188. /*
  189. * 函数名:void Config_485_Port(uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol)
  190. * 输入参数:baudrate,databits,stopbits,parity,flowcontol 串口的配置参数
  191. * 输出参数:无
  192. * 返回值:无
  193. * 函数作用:根据输入参数进行配置485串口参数
  194. */
  195. void Config_485_Port(uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol)
  196. {
  197. uint8_t wordLength;
  198. if(parity!=0)
  199. {
  200. wordLength=databits+1;
  201. }else wordLength=databits;
  202. __HAL_RCC_USART3_CLK_ENABLE();
  203. __HAL_RCC_GPIOC_CLK_ENABLE();
  204. GPIO_InitTypeDef GPIO_InitStruct = {0};
  205. // 配置 GPIOC Pin 11 和 10 分别作为 USART3 的 RX 和 TX 引脚
  206. GPIO_InitStruct.Pin = USART_485_RX_PIN | USART_485_TX_PIN;
  207. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  208. GPIO_InitStruct.Pull = GPIO_PULLUP;
  209. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  210. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  211. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  212. USART_InitStruct_485.Instance = USART_485;
  213. USART_InitStruct_485.Init.BaudRate = baudrate;
  214. USART_InitStruct_485.Init.WordLength = (wordLength== 9) ? UART_WORDLENGTH_9B : UART_WORDLENGTH_8B;
  215. USART_InitStruct_485.Init.StopBits = UART_STOPBITS_1;
  216. USART_InitStruct_485.Init.Parity = (parity == 1) ? (UART_PARITY_ODD) : (parity == 2 ? UART_PARITY_EVEN : UART_PARITY_NONE);
  217. USART_InitStruct_485.Init.Mode = UART_MODE_TX_RX; // 既支持发送也支持接收
  218. USART_InitStruct_485.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  219. USART_InitStruct_485.Init.OverSampling = UART_OVERSAMPLING_16;
  220. HAL_UART_Init(&USART_InitStruct_485);
  221. HAL_NVIC_SetPriority(USART_485_IRQ, 0, 0);
  222. HAL_NVIC_EnableIRQ(USART_485_IRQ);
  223. // 初始化 DE 引脚 接收数据
  224. HAL_GPIO_WritePin(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,GPIO_PIN_RESET);
  225. // HAL_UART_Receive_IT(&USART_InitStruct_485, &rxByte, 1);
  226. __HAL_UART_ENABLE_IT(&USART_InitStruct_485, UART_IT_RXNE); // 使能接收中断
  227. __HAL_UART_ENABLE_IT(&USART_InitStruct_485, UART_IT_IDLE); // 使能空闲中断
  228. }
  229. void USART_485_IRQHandler(void){
  230. GATEWAY_PARAMS *get;
  231. get= get_gateway_config_params();
  232. if(get->device_params->protocol == 3)
  233. mmodbus_callback(&USART_InitStruct_485);
  234. else if(get->device_params->protocol == 1 || get->device_params->protocol == 2)
  235. dlt_callback(&USART_InitStruct_485);
  236. }
  237. void DEBUG_USART_Config(void)
  238. {
  239. GPIO_InitTypeDef GPIO_InitStruct = {0};
  240. // Enable USART6 clock
  241. __HAL_RCC_USART6_CLK_ENABLE();
  242. // Enable GPIOC clock
  243. __HAL_RCC_GPIOC_CLK_ENABLE();
  244. // Configure USART6 pins
  245. GPIO_InitStruct.Pin = DEBUG_USART_RX_PIN | DEBUG_USART_TX_PIN;
  246. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  247. GPIO_InitStruct.Pull = GPIO_PULLUP;
  248. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  249. GPIO_InitStruct.Alternate = GPIO_AF8_USART6;// Connect USART6 pins to AF8
  250. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  251. // Configure USART6
  252. USART_InitStruct_DEBUG.Instance = DEBUG_USART;
  253. USART_InitStruct_DEBUG.Init.BaudRate = DEBUG_USART_BAUDRATE;
  254. USART_InitStruct_DEBUG.Init.WordLength = UART_WORDLENGTH_8B;
  255. USART_InitStruct_DEBUG.Init.StopBits = UART_STOPBITS_1;
  256. USART_InitStruct_DEBUG.Init.Parity = UART_PARITY_NONE;
  257. USART_InitStruct_DEBUG.Init.Mode = UART_MODE_TX_RX; // 既支持发送也支持接收
  258. USART_InitStruct_DEBUG.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  259. USART_InitStruct_DEBUG.Init.OverSampling = UART_OVERSAMPLING_16;
  260. HAL_UART_Init(&USART_InitStruct_DEBUG);
  261. HAL_NVIC_SetPriority(DEBUG_USART_IRQ, 2, 1);
  262. HAL_NVIC_EnableIRQ(DEBUG_USART_IRQ);
  263. // Enable USART6
  264. __HAL_UART_ENABLE_IT(&USART_InitStruct_DEBUG, UART_IT_IDLE); // 使能空闲中断
  265. // __HAL_UART_ENABLE_IT(&USART_InitStruct_DEBUG, UART_IT_RXNE); // 使能接收中断
  266. __HAL_DMA_ENABLE(&DMA_DEBUG_RX); //使能DMA
  267. __HAL_UART_ENABLE(&USART_InitStruct_DEBUG);
  268. // __HAL_UART_CLEAR_FLAG(&USART_InitStruct_DEBUG, USART_FLAG_TC);
  269. }
  270. void DEBUG_USART_IRQHandler(void)
  271. {
  272. if(RESET != __HAL_UART_GET_FLAG(&USART_InitStruct_DEBUG, UART_FLAG_IDLE)) // 判断是否是空闲中断
  273. {
  274. __HAL_UART_CLEAR_IDLEFLAG(&USART_InitStruct_DEBUG); //清空空闲中断标志位
  275. //关闭DMA
  276. __HAL_DMA_DISABLE(&DMA_DEBUG_RX);
  277. // HAL_UART_DMAStop(&USART_InitStruct_DEBUG);
  278. UART6_RX_NUM = BUFF_SIZE - __HAL_DMA_GET_COUNTER(&DMA_DEBUG_RX);
  279. //清除DMA中断
  280. __HAL_DMA_CLEAR_FLAG(&DMA_DEBUG_RX,DMA_FLAG_TCIF1_5);
  281. __HAL_DMA_ENABLE(&DMA_DEBUG_RX);
  282. //接收状态置位
  283. UART6_RX_STAT=1;
  284. }
  285. }
  286. void USART_DMA_Config(void)
  287. {
  288. // 使能串口和DMA时钟
  289. __HAL_RCC_USART6_CLK_ENABLE();
  290. __HAL_RCC_DMA2_CLK_ENABLE();
  291. DMA_DEBUG_RX.Instance = DEBUG_USART_DMA_STREAM;
  292. DMA_DEBUG_RX.Init.Channel = DEBUG_USART_DMA_CHANNEL;
  293. /*方向:从外设到内存*/
  294. DMA_DEBUG_RX.Init.Direction = DMA_PERIPH_TO_MEMORY;
  295. /*外设地址不增*/
  296. DMA_DEBUG_RX.Init.PeriphInc = DMA_PINC_DISABLE;
  297. /*内存地址自增*/
  298. DMA_DEBUG_RX.Init.MemInc = DMA_MINC_ENABLE;
  299. /*外设数据单位*/
  300. DMA_DEBUG_RX.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  301. /*内存数据单位 8bit*/
  302. DMA_DEBUG_RX.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  303. /*DMA模式:不断循环*/
  304. DMA_DEBUG_RX.Init.Mode = DMA_NORMAL;
  305. /*优先级:中*/
  306. DMA_DEBUG_RX.Init.Priority = DMA_PRIORITY_MEDIUM;
  307. /*禁用FIFO*/
  308. DMA_DEBUG_RX.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  309. DMA_DEBUG_RX.Init.FIFOThreshold= DMA_FIFO_THRESHOLD_FULL;
  310. HAL_DMA_Init(&DMA_DEBUG_RX);
  311. __HAL_LINKDMA(&USART_InitStruct_DEBUG, hdmarx, DMA_DEBUG_RX);
  312. SET_BIT(USART_InitStruct_DEBUG.Instance->CR3, USART_CR3_DMAR);
  313. HAL_DMA_Start(&DMA_DEBUG_RX,DEBUG_USART_DR_BASE, (uint32_t)UART6_RX_BUF, BUFF_SIZE);
  314. __HAL_DMA_ENABLE_IT(&DMA_DEBUG_RX,DMA_IT_TC);
  315. HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 2, 2);
  316. HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
  317. }
  318. void DMA2_Stream1_IRQHandler() {
  319. if (__HAL_DMA_GET_IT_SOURCE(&DMA_DEBUG_RX,DMA_IT_TC))
  320. {
  321. __HAL_DMA_CLEAR_FLAG(&DMA_DEBUG_RX,DMA_IT_TC);
  322. }
  323. }
  324. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *husart)
  325. {
  326. // USART 232
  327. if(husart->Instance == USART_232)
  328. {
  329. // 将接收到的数据存储在缓冲区中
  330. rx_232_Buffer[rxIndex] = rxByte;
  331. rxIndex++;
  332. HAL_UART_Receive_IT(husart, &rxByte, 1);
  333. }
  334. // USART 485
  335. if(husart->Instance == USART_485)
  336. {
  337. if (rxIndex < RX_BUFFER_SIZE)
  338. {
  339. // 将接收到的数据存储在缓冲区中
  340. rx_232_Buffer[rxIndex] = rxByte;
  341. rxIndex++;
  342. }
  343. memset(rx_232_Buffer, 0, rxIndex);
  344. rxIndex = 0;
  345. HAL_UART_Receive_IT(husart, &rxByte, 1);
  346. }
  347. if(husart->Instance == USART6)
  348. {
  349. }
  350. }
  351. /* 上位机功能 */
  352. int size1;
  353. //储存上位机发送的config数据,并返回上位机操作结果
  354. void save_config_232(uint8_t* buf)
  355. {
  356. GATEWAY_PARAMS *get;
  357. get= get_gateway_config_params();
  358. size1 = my_mem_perused(SRAMEX);
  359. char* saveData = mymalloc(SRAMEX,20 * 1024);//(RECV_BUF_SIZE);// 存储config数据 最大20K,暂定3K
  360. size1 = my_mem_perused(SRAMEX);
  361. if(saveData == NULL)
  362. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"recv buf malloc fail");
  363. memset(saveData,0,strlen(saveData));
  364. size1 = my_mem_perused(SRAMEX);
  365. // 插入tcp_config标志位,后续不通过http获取默认配置数据
  366. sprintf(saveData, "tcp_config");
  367. sprintf(saveData + strlen(saveData),"%s",buf);
  368. // 删除当前文件,写入新文件
  369. DeleteDirFile("device.txt");
  370. write_file("device.txt",saveData,strlen(saveData));// 储存到flash 不用判断失败
  371. // 释放结构体
  372. myfree(SRAMEX, get->device_params->params->gateway_read_dlt645_command);
  373. get->device_params->params->gateway_read_dlt645_command = NULL;
  374. myfree(SRAMEX, get->device_params->params->gateway_read_modbus_command);
  375. get->device_params->params->gateway_read_modbus_command = NULL;
  376. myfree(SRAMEX, get->device_params->params->gateway_write_modbus_command);
  377. get->device_params->params->gateway_write_modbus_command = NULL;
  378. myfree(SRAMEX, get->device_params->params);
  379. get->device_params->params = NULL;
  380. myfree(SRAMEX, get->device_params);
  381. get->device_params = NULL;
  382. get = NULL;
  383. // 重新解析config数据
  384. addGatewayParams(saveData);
  385. myfree(SRAMEX, saveData);
  386. char* retMsg = mymalloc(SRAMEX,32);
  387. memset(retMsg, 0, strlen(retMsg));
  388. retMsg = "{\"write_config\":\"success\"}";
  389. USART_232_Send((uint8_t*)retMsg, strlen(retMsg));
  390. myfree(SRAMEX ,retMsg);
  391. }
  392. void read_device_params(char* device_params)
  393. {
  394. GATEWAY_PARAMS *get;
  395. get= get_gateway_config_params();
  396. DEVICE_PARAMS *current_device=get->device_params;
  397. sprintf(device_params, "{\"read_config\":\"success\",\"baudrate\":%d,\"checkBit\":%d,\"commandTopic\":\"%s\",\"dataBit\":%d,\
  398. \"deviceId\":\"%s\",\"host\":\"%s\",\"inboundTime\":%d,\"messageTopic\":\"%s\",\"port\":%d,\"stopBit\":%d,\"deviceList\":[",get->baudrate,
  399. get->checkBit,get->commandTopic,get->dataBits,get->deviceId,get->host,get->inboundTime,get->messageTopic,get->port,get->stopBit);
  400. while(current_device != NULL)
  401. {
  402. sprintf(device_params + strlen(device_params),"{\"protocol\":%d,\"bigLittleFormat\":%d,\"deviceId\":\"%s\",\"sensorData\":[",
  403. current_device->protocol,current_device->MDBbigLittleFormat,current_device->deviceID);
  404. GATEWAY_READ_DLT645_COMMAND *read_dlt645_command = current_device->params->gateway_read_dlt645_command;
  405. GATEWAY_READ_MODBUS_COMMAND *read_modbus_command = current_device->params->gateway_read_modbus_command;
  406. GATEWAY_WRITE_MODBUS_COMMAND *write_modbus_command = current_device->params->gateway_write_modbus_command;
  407. // dlt645 read
  408. while(read_dlt645_command != NULL)
  409. {
  410. sprintf(device_params + strlen(device_params),"{\"identifier645\":%d,\"identifier\":\"%s\",\"deviceID645\":\"%s\"},",
  411. read_dlt645_command->Identification,read_dlt645_command->keyword,read_dlt645_command->deviceID645);
  412. read_dlt645_command = read_dlt645_command->nextParams;
  413. }
  414. // modbus read
  415. while(read_modbus_command != NULL)
  416. {
  417. sprintf(device_params + strlen(device_params),"{\"rFunctionCode\":%d,\"registerAddress\":%d,\"slaveAddress\":%d,\"registerByteNum\":%d,\"identifier\":\"%s\",\"precise\":%d},",
  418. read_modbus_command->functionCode, read_modbus_command->registerAddress,read_modbus_command->slaveAddress,
  419. read_modbus_command->registerByteNum,read_modbus_command->keyword,read_modbus_command->decimalPoint);
  420. read_modbus_command = read_modbus_command->nextParams;
  421. }
  422. // modbus write
  423. sprintf(device_params + strlen(device_params)-1,"], \"commandData\":[");//sensorData:[
  424. while(write_modbus_command != NULL)
  425. {
  426. sprintf(device_params + strlen(device_params),"{\"registerAddress\":%d,\"slaveAddress\":%d,\"wFunctionCode\":%d,\"registerByteNum\":%d},",
  427. write_modbus_command->registerAddress,write_modbus_command->slaveAddress,write_modbus_command->functionCode,write_modbus_command->registerByteNum);
  428. write_modbus_command = write_modbus_command->nextParams;
  429. }
  430. sprintf(device_params + strlen(device_params)-1,"]},");// commandData:[
  431. current_device = current_device->nextDevice;
  432. }
  433. sprintf(device_params + strlen(device_params) -1 ,"]}");
  434. }
  435. void read_config_232(void)
  436. {
  437. GATEWAY_PARAMS *get;
  438. get= get_gateway_config_params();
  439. char* device_params = mymalloc(SRAMEX, 3 * 1024);
  440. memset(device_params,0,3 * 1024);
  441. if(get->device_params == NULL)
  442. {
  443. sprintf(device_params, "{\"read_config\":\"error\"}");
  444. USART_232_Send((uint8_t*)device_params, strlen(device_params));
  445. }
  446. else
  447. {
  448. read_device_params(device_params);
  449. USART_232_Send((uint8_t*)device_params, strlen(device_params));
  450. }
  451. myfree(SRAMEX, device_params);
  452. }
  453. // 设备嗅探
  454. void find_device_232(void)
  455. {
  456. char deviceId[50];// 发送设备名
  457. GATEWAY_PARAMS *get;
  458. get= get_gateway_config_params();
  459. if(get->device_params == NULL)
  460. {
  461. // sprintf(deviceId, "{\"find_device\":\"%s\"}", gatewayId);
  462. sprintf(deviceId, "{\"find_device\":\"error\"}");
  463. USART_232_Send((uint8_t*)deviceId, strlen(deviceId));
  464. }
  465. else
  466. {
  467. sprintf(deviceId, "{\"find_device\":\"%s\"}", get->deviceId);
  468. USART_232_Send((uint8_t*)deviceId, strlen(deviceId));
  469. }
  470. memset(deviceId, 0, 50);
  471. }
  472. // 储存上位机发送的config_add数据,并返回上位机操作结果
  473. void add_config_232(uint8_t* dataBuf)
  474. {
  475. GATEWAY_PARAMS *get;
  476. get= get_gateway_config_params();
  477. DEVICE_PARAMS *device=get->device_params;
  478. char* retMsg = mymalloc(SRAMEX,32);
  479. memset(retMsg, 0, strlen(retMsg));
  480. while(device != NULL)// 一直轮询到当前为NULL
  481. {
  482. device = device->nextDevice;
  483. }
  484. addDevice((char*)dataBuf);
  485. // 再检查更新后的deviceId是否为NULL
  486. if(device == NULL)// error
  487. {
  488. retMsg = "{\"write_config\":\"error\"}";
  489. USART_232_Send((uint8_t*)retMsg, strlen(retMsg));
  490. }
  491. else// success
  492. {
  493. retMsg = "{\"write_config\":\"success\"}";
  494. USART_232_Send((uint8_t*)retMsg, strlen(retMsg));
  495. }
  496. myfree(SRAMEX, retMsg);
  497. }
  498. // 切换工作模式
  499. void work_mode_232(uint8_t* buf)
  500. {
  501. /* 用标志位开关data_task任务中,发送数据的方式 */
  502. // 内置协议模式
  503. if(strstr((char*)buf,"protocolsMode") != NULL)
  504. {
  505. ProtocolsModeFlag = 1;// 开启内置协议模式
  506. TransparentModeFlag = 0; // 关闭透明传输模式
  507. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"ProtocolsMode");
  508. }
  509. // 透明传输模式
  510. if(strstr((char*)buf,"TransparentMode") != NULL)
  511. {
  512. ProtocolsModeFlag = 0;// 关闭内置协议模式
  513. TransparentModeFlag = 1; // 开启透明传输模式
  514. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"TransparentModeFlag");
  515. }
  516. }
  517. // 更新OTA数据
  518. void updata_232(void)
  519. {
  520. int i;
  521. uint16_t crc;
  522. uint8_t databuf[1024];// 保存包数据
  523. uint16_t packId = 0x01;
  524. uint8_t stx = 0x02,eot = 0x04,ack = 0x06,nak = 0x15,C = 0x43;
  525. // 向上位机发送就绪信号
  526. char* retMsg = mymalloc(SRAMEX,32);
  527. memset(retMsg, 0, strlen(retMsg));
  528. retMsg = "{\"software_update\":\"ready\"}";
  529. USART_232_Send((uint8_t*)retMsg, strlen(retMsg));
  530. myfree(SRAMEX, retMsg);
  531. memset(rx_232_Buffer, 0, strlen((char*)rx_232_Buffer));
  532. delay_ms(300);
  533. // 发送第一个C
  534. USART_232_Send(&C, 1);
  535. delay_ms(300);
  536. __start:
  537. i = 0;
  538. //等待接收到消息,最长等待3S
  539. while(recv_232_done == 0 && i < 3000)
  540. {
  541. delay_ms(1);
  542. i++;
  543. }
  544. if(recv_232_done == 0) USART_232_Send(&C, 1);//如果还没接收到消息,再发送一个C
  545. do{
  546. // 接收到结束信号,反馈ack
  547. if(rx_232_Buffer[0] == eot)
  548. {
  549. USART_232_Send(&ack, 1);
  550. delay_ms(300);
  551. break;
  552. }
  553. // 检测头标志
  554. if(rx_232_Buffer[0] != stx)
  555. {
  556. USART_232_Send(&nak, 1);
  557. delay_ms(300);
  558. goto __start;
  559. }
  560. // 检测包序列
  561. if(rx_232_Buffer[1] != packId)
  562. {
  563. USART_232_Send(&nak, 1);
  564. delay_ms(300);
  565. goto __start;
  566. }
  567. // 检测包号反码
  568. if(rx_232_Buffer[2] !=(~packId & 0xff))
  569. {
  570. USART_232_Send(&nak, 1);
  571. delay_ms(300);
  572. goto __start;
  573. }
  574. // 提取数据包
  575. for(int i = 0; i < 1024; i++)
  576. {
  577. databuf[i] = rx_232_Buffer[3 + i];
  578. }
  579. crc = Checkcrc16(databuf, 1024);
  580. // 检测数据包的校验码
  581. if (((crc & 0x00FF) != rx_232_Buffer[1028]) || (((crc & 0xFF00) >> 8) != rx_232_Buffer[1027]))
  582. {
  583. USART_232_Send(&nak, 1);
  584. delay_ms(300);
  585. goto __start;
  586. }
  587. // 对数据包进行操作
  588. /* */
  589. memset(databuf, 0, sizeof(databuf));
  590. // 准备接收下一包数据
  591. USART_232_Send(&ack, 1);
  592. delay_ms(300);
  593. packId += 1;
  594. }while(recv_232_done);
  595. // 所有数据包接受完成,准备重启
  596. __set_PRIMASK(1);
  597. NVIC_SystemReset();
  598. }
  599. void USART_232_task(void const * argument)
  600. {
  601. int j;
  602. char* recv_cmd[] = {"\"cmd\":\"write_config\"","\"cmd\":\"write_config_add\"",
  603. "\"cmd\":\"read_config\"","\"cmd\":\"find_device\"",
  604. "\"cmd\":\"ip_config\"","\"cmd\":\"toggle_work_mode\"",
  605. "\"cmd\":\"software_update\"","\"cmd\":\"reboot\"",
  606. "\"cmd\":\"cmd error\"" };
  607. while(1)
  608. {
  609. while(recv_232_done != 1)
  610. {
  611. vTaskDelay(500);
  612. }
  613. rxIndex = 0;
  614. j = 0;
  615. for(int i = 0; i < sizeof(recv_cmd)/sizeof(recv_cmd[0]); i++)
  616. {
  617. if(strstr((char*)rx_232_Buffer,recv_cmd[i]) != NULL)
  618. {
  619. i = sizeof(recv_cmd)/sizeof(recv_cmd[0]);
  620. }
  621. j++;
  622. }
  623. if (ProtocolsModeFlag) {
  624. switch (j) {
  625. case WRITE_CONFIG:
  626. save_config_232(rx_232_Buffer);
  627. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "write config");
  628. // __set_PRIMASK(1);
  629. // NVIC_SystemReset();
  630. break;
  631. case WRITE_CONFIG_ADD:
  632. add_config_232(rx_232_Buffer);
  633. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "write config add");
  634. break;
  635. case READ_CONFIG:
  636. read_config_232();
  637. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "read config");
  638. break;
  639. case FIND_DEVICE:
  640. find_device_232();
  641. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "find device");
  642. break;
  643. case TOGGLE_MODE:
  644. work_mode_232(rx_232_Buffer);
  645. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "toggle work mode");
  646. break;
  647. case UPDATE:
  648. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "update");
  649. updata_232();
  650. break;
  651. case REBOOT:
  652. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "reboot");
  653. __set_PRIMASK(1);
  654. NVIC_SystemReset();
  655. break;
  656. case CMD_ERROR:
  657. goto __exit;
  658. }
  659. } else {
  660. switch (j) {
  661. case FIND_DEVICE:
  662. find_device_232();
  663. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "find device");
  664. break;
  665. case TOGGLE_MODE:
  666. work_mode_232(rx_232_Buffer);
  667. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "toggle work mode");
  668. break;
  669. case UPDATE:
  670. updata_232();
  671. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "update");
  672. break;
  673. case REBOOT:
  674. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "reboot");
  675. __set_PRIMASK(1);
  676. NVIC_SystemReset();
  677. break;
  678. case CMD_ERROR:
  679. goto __exit;
  680. }
  681. }
  682. memset(rx_232_Buffer, 0, strlen((char*)rx_232_Buffer));
  683. __exit:
  684. vTaskDelay(500);
  685. }
  686. }
  687. void USART_232_task_creat()
  688. {
  689. osThreadDef(USART_task, USART_232_task, osPriorityNormal, 0, configMINIMAL_STACK_SIZE*4);
  690. osThreadCreate(osThread(USART_task), NULL);
  691. }
  692. /*********************************************END OF FILE**********************/