usart.c 24 KB

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