stm32RS485串口通信——中断接收发送数据

功能:通过PC机的串口调试助手发送数据给串口A,串口B接收到串口A发送的数据,再由串口B将接收到的数据返回给PC机的串口调试助手。
PC->串口A->串口B->PC。

实验平台:stm32f407
/*********************************************************************
本平台具有六个串口:
com1  485
com2  
com3  232 需一个管脚控制DTU
com4 485
com5  调试串口  TTL
com6  485
*********************************************************************/
本实验用的的串口A对应为com1,串口B对应为com4。

本程序的设计思路:
通过PC机串口调试助手发送数据,当串口A发现有数据过来时,产生串口接收中断,把数据保存到自定义的接收缓冲区中,然后串口接收函数去缓冲区中去取数据保存在发送缓冲区中,再将数据通过串口B发送给串口调试助手。

代码:
multiple_serial.h
  1. #ifndef multiple_serial_H
  2. #define multiple_serial_H
  3. #define MAX_RECV_BUF_LEN (64)
  4. #define MAX_RECV_DATA_MASK (0x3f)
  5. #define MAX_COM_COUNT (6)
  6. typedef signed char int8_t;
  7. typedef signed short intint16_t;
  8. typedef signed int int32_t;
  9. typedef unsigned char uint8_t;
  10. typedef unsigned short intuint16_t;
  11. typedef unsigned int uint32_t;
  12. #define NEXTAI_COM1_NAME"COM1"
  13. #define NEXTAI_COM2_NAME"COM2"
  14. #define NEXTAI_COM3_NAME"COM3"
  15. #define NEXTAI_COM4_NAME"COM4"
  16. #define NEXTAI_COM5_NAME"COM5"
  17. #define NEXTAI_COM6_NAME"COM6"
  18. enum{
  19. NEXTAI_COM1_ID=1,
  20. NEXTAI_COM2_ID,
  21. NEXTAI_COM3_ID,
  22. NEXTAI_COM4_ID,
  23. NEXTAI_COM5_ID,
  24. NEXTAI_COM6_ID
  25. };
  26. typedef struct _NEXTAI_CHANNEL_INFO_
  27. {
  28. uint8_t local_com_id;
  29. //nextai_uint8 *databuf;
  30. //nextai_uint16 data_read_p;
  31. //nextai_uint16 data_write_p;
  32. uint8_t used_flag;
  33. }NEXTAI_CHANNEL_INFO;
  34. /*******************************************************************************
  35. * local_channel_data : 通道数据结构
  36. * data_recv_flag : 是否有数据flag,1表示
  37. *******************************************************************************/
  38. typedef struct _NEXTAI_MGR_INFO_
  39. {
  40. NEXTAI_CHANNEL_INFO local_channel_data[MAX_COM_COUNT];
  41. uint8_t data_recv_flag;
  42. }NEXTAI_MGR_INFO;
  43. static NEXTAI_MGR_INFO* local_com_mgr_p=0;
  44. void serial_nvic_Configuration(uint8_t comID);
  45. static void serial_gpio_configuration(uint8_t comID);
  46. static void serial_rcc_configuration(uint8_t comID);
  47. uint8_t serial_Configuration(uint8_t*comName);
  48. uint8_t serial_send(uint8_t comID,uint8_t*databuf,uint32_t datalen);
  49. uint32_t serial_recv(uint8_t comID,uint8_t*databuf,uint32_t datalen);
  50. void clean_rebuff(void);
  51. #endif

multiple_serial.c
  1. #include"multiple_serial.h"
  2. #include"stm32f4xx_gpio.h"
  3. #include"stm32f4xx_usart.h"
  4. #include
  5. unsigned char uart_buf[MAX_RECV_BUF_LEN]={0};
  6. uint8_t com1_recv_buf[MAX_RECV_BUF_LEN]={0};
  7. int com1_gloabl_p=0;
  8. int com1_read_p=0;
  9. uint8_t com4_recv_buf[MAX_RECV_BUF_LEN]={0};
  10. int com4_gloabl_p=0;
  11. int com4_read_p=0;
  12. uint8_t com5_recv_buf[MAX_RECV_BUF_LEN]={0};
  13. int com5_gloabl_p=0;
  14. int com5_read_p=0;
  15. uint8_t com6_recv_buf[MAX_RECV_BUF_LEN]={0};
  16. int com6_gloabl_p=0;
  17. int com6_read_p=0;
  18. //串口gpio的配置
  19. static void serial_gpio_configuration(uint8_t comID)
  20. {
  21. GPIO_InitTypeDef GPIO_InitStructure;
  22. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  23. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  24. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  25. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  26. /* Configure USART Tx and Rx as alternate function push-pull */
  27. if(comID == NEXTAI_COM1_ID)
  28. {
  29. GPIO_StructInit(&GPIO_InitStructure);
  30. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  31. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  32. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
  33. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_100MHz;
  34. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
  35. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  36. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9;
  37. GPIO_Init(GPIOA,&GPIO_InitStructure);
  38. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
  39. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  40. GPIO_Init(GPIOA,&GPIO_InitStructure);
  41. GPIO_StructInit(&GPIO_InitStructure);
  42. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;
  43. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_100MHz;
  44. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
  45. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  46. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_4;
  47. GPIO_Init(GPIOF,&GPIO_InitStructure);
  48. }
  49. elseif(comID== NEXTAI_COM4_ID)
  50. {
  51. GPIO_StructInit(&GPIO_InitStructure);
  52. GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4);
  53. GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_UART4);
  54. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
  55. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  56. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
  57. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  58. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_1;
  59. GPIO_Init(GPIOA,&GPIO_InitStructure);
  60. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_0;
  61. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  62. GPIO_Init(GPIOA,&GPIO_InitStructure);
  63. GPIO_StructInit(&GPIO_InitStructure);
  64. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;
  65. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  66. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
  67. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  68. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
  69. GPIO_Init(GPIOA,&GPIO_InitStructure);
  70. }
  71. elseif(comID== NEXTAI_COM5_ID)
  72. {
  73. GPIO_StructInit(&GPIO_InitStructure);
  74. GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
  75. GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
  76. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
  77. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  78. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
  79. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  80. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_2;
  81. GPIO_Init(GPIOD,&GPIO_InitStructure);
  82. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_12;
  83. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  84. GPIO_Init(GPIOC,&GPIO_InitStructure);
  85. }
  86. elseif(comID== NEXTAI_COM6_ID)
  87. {
  88. GPIO_StructInit(&GPIO_InitStructure);
  89. GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
  90. GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
  91. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
  92. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_100MHz;
  93. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
  94. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  95. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_6;
  96. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  97. GPIO_Init(GPIOC,&GPIO_InitStructure);
  98. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
  99. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  100. GPIO_Init(GPIOC,&GPIO_InitStructure);
  101. GPIO_StructInit(&GPIO_InitStructure);
  102. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;
  103. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  104. GPIO_InitStructure.GPIO_OType= GPIO_OType_PP;
  105. GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
  106. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_8;
  107. GPIO_Init(GPIOC,&GPIO_InitStructure);
  108. }
  109. }
  110. //串口时钟的配置
  111. static void serial_rcc_configuration(uint8_t comID)
  112. {
  113. /* Enable USART clock */
  114. if(comID== NEXTAI_COM1_ID)
  115. {
  116. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  117. }
  118. elseif(comID== NEXTAI_COM4_ID)
  119. {
  120. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
  121. }
  122. elseif(comID== NEXTAI_COM5_ID)
  123. {
  124. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
  125. }
  126. elseif(comID== NEXTAI_COM6_ID)
  127. {
  128. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
  129. }
  130. }
  131. //配置各个串口的中断处理函数,优先级相同
  132. void serial_nvic_Configuration(uint8_t comID)
  133. {
  134. NVIC_InitTypeDef NVIC_InitStructure;
  135. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  136. if(comID== NEXTAI_COM1_ID)
  137. {
  138. NVIC_InitStructure.NVIC_IRQChannel= USART1_IRQn;
  139. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  140. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  141. NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
  142. NVIC_Init(&NVIC_InitStructure);
  143. }
  144. elseif(comID== NEXTAI_COM4_ID)
  145. {
  146. NVIC_InitStructure.NVIC_IRQChannel= UART4_IRQn;
  147. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  148. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  149. NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
  150. NVIC_Init(&NVIC_InitStructure);
  151. }
  152. elseif(comID== NEXTAI_COM5_ID)
  153. {
  154. NVIC_InitStructure.NVIC_IRQChannel= UART5_IRQn;
  155. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  156. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  157. NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
  158. NVIC_Init(&NVIC_InitStructure);
  159. }
  160. elseif(comID== NEXTAI_COM6_ID)
  161. {
  162. NVIC_InitStructure.NVIC_IRQChannel= USART6_IRQn;
  163. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  164. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  165. NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
  166. NVIC_Init(&NVIC_InitStructure);
  167. }
  168. }
  169. /*串口参数配置*/
  170. uint8_t serial_Configuration(uint8_t*comName)
  171. {
  172. USART_InitTypeDef USART_InitStructure;
  173. uint8_t comID =0;
  174. if(strcmp(NEXTAI_COM1_NAME,(constchar*)comName)==0)
  175. {
  176. comID= NEXTAI_COM1_ID;
  177. }
  178. elseif(strcmp(NEXTAI_COM4_NAME,(constchar*)comName)==0)
  179. {
  180. comID= NEXTAI_COM4_ID;
  181. }
  182. elseif(strcmp(NEXTAI_COM5_NAME,(constchar*)comName)==0)
  183. {
  184. comID= NEXTAI_COM5_ID;
  185. }
  186. elseif(strcmp(NEXTAI_COM6_NAME,(constchar*)comName)==0)
  187. {
  188. comID= NEXTAI_COM6_ID;
  189. }
  190. /* System Clocks Configuration */
  191. serial_rcc_configuration(comID);
  192. /* Configure the GPIO ports */
  193. serial_gpio_configuration(comID);
  194. USART_InitStructure.USART_WordLength= USART_WordLength_8b;
  195. USART_InitStructure.USART_Parity= USART_Parity_No;
  196. USART_InitStructure.USART_HardwareFlowControl= USART_HardwareFlowControl_None;
  197. USART_InitStructure.USART_Mode= USART_Mode_Rx| USART_Mode_Tx;
  198. if(comID== NEXTAI_COM1_ID)
  199. {
  200. USART_InitStructure.USART_BaudRate=9600;
  201. USART_InitStructure.USART_StopBits= USART_StopBits_1;
  202. USART_Init(USART1,&USART_InitStructure);
  203. }
  204. elseif(comID== NEXTAI_COM4_ID)
  205. {
  206. USART_InitStructure.USART_BaudRate=38400;
  207. USART_InitStructure.USART_StopBits= USART_StopBits_1;
  208. USART_Init(UART4,&USART_InitStructure);
  209. }
  210. elseif(comID== NEXTAI_COM5_ID)
  211. {
  212. USART_InitStructure.USART_BaudRate=115200;
  213. USART_InitStructure.USART_StopBits= USART_StopBits_1;
  214. USART_Init(UART5,&USART_InitStructure);
  215. }
  216. elseif(comID== NEXTAI_COM6_ID)
  217. {
  218. USART_InitStructure.USART_BaudRate=9600;
  219. USART_InitStructure.USART_StopBits= USART_StopBits_1;
  220. USART_Init(USART6,&USART_InitStructure);
  221. }
  222. serial_nvic_Configuration(comID);
  223. if(comID== NEXTAI_COM1_ID)
  224. {
  225. USART_Cmd(USART1, ENABLE);
  226. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  227. }
  228. elseif(comID== NEXTAI_COM4_ID)
  229. {
  230. USART_Cmd(UART4, ENABLE);
  231. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
  232. }
  233. elseif(comID== NEXTAI_COM5_ID)
  234. {
  235. USART_Cmd(UART5, ENABLE);
  236. USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
  237. }
  238. elseif(comID== NEXTAI_COM6_ID)
  239. {
  240. USART_Cmd(USART6, ENABLE);
  241. USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
  242. }
  243. return comID;
  244. }
  245. /*******************************************************************************
  246. * 函数名 : serial_send
  247. * 描 述 : 发送某个端口的数据
  248. * 输 入 : comID: 串口标识号
  249. databuf: 发送数据缓冲区
  250. datalen: 发送数据长度
  251. * 输 出 : None
  252. * 返 回 : None
  253. *******************************************************************************/
  254. uint8_t serial_send(uint8_t comID,uint8_t*databuf,uint32_t datalen)
  255. {
  256. int i =0;
  257. uint8_t nextai_timer =0;
  258. if(comID== NEXTAI_COM1_ID)
  259. {
  260. GPIO_SetBits(GPIOF, GPIO_Pin_4);
  261. //清除标志位,否则第1位数据会丢失
  262. USART_GetFlagStatus(USART1, USART_FLAG_TC);
  263. for(i=0; i< datalen; i++)
  264. {
  265. //485 需添加超时检测
  266. USART_SendData(USART1,databuf[i]);
  267. while(USART_GetFlagStatus(USART1, USART_FLAG_TC)== RESET)
  268. {
  269. nextai_timer++;
  270. if(nextai_timer>>8)
  271. return0;
  272. }
  273. }
  274. GPIO_ResetBits(GPIOF, GPIO_Pin_4);
  275. }
  276. elseif(comID== NEXTAI_COM4_ID)
  277. {
  278. GPIO_SetBits(GPIOA, GPIO_Pin_7);
  279. //清除标志位,否则第1位数据会丢失
  280. USART_GetFlagStatus(UART4, USART_FLAG_TC);
  281. for(i=0; i< datalen; i++)
  282. {
  283. //485
  284. USART_SendData(UART4,databuf[i]);
  285. while(USART_GetFlagStatus(UART4, USART_FLAG_TC)== RESET)
  286. {
  287. nextai_timer++;
  288. if(nextai_timer>>8)
  289. return0;
  290. }
  291. }
  292. GPIO_ResetBits(GPIOA, GPIO_Pin_7);
  293. }
  294. elseif(comID== NEXTAI_COM5_ID)
  295. {
  296. //清除标志位,否则第1位数据会丢失
  297. USART_GetFlagStatus(UART5,USART_FLAG_TC);
  298. for(i=0; i< datalen; i++)
  299. {
  300. while(USART_GetFlagStatus(UART5, USART_FLAG_TC)== RESET);
  301. USART_SendData(UART5,databuf[i]);
  302. while(USART_GetFlagStatus(UART5, USART_FLAG_TC)== RESET)
  303. {
  304. nextai_timer++;
  305. if(nextai_timer>>8)
  306. return0;
  307. }
  308. }
  309. }
  310. elseif(comID== NEXTAI_COM6_ID)
  311. {
  312. GPIO_SetBits(GPIOC, GPIO_Pin_8);
  313. //清除标志位,否则第1位数据会丢失
  314. USART_GetFlagStatus(USART6,USART_FLAG_TC);
  315. for(i=0; i< datalen; i++)
  316. {
  317. //485
  318. USART_SendData(USART6,databuf[i]);
  319. while(USART_GetFlagStatus(USART6, USART_FLAG_TC)== RESET)
  320. {
  321. nextai_timer++;
  322. if(nextai_timer>>8)
  323. return0;
  324. }
  325. }
  326. GPIO_ResetBits(GPIOC, GPIO_Pin_8);
  327. }
  328. return datalen;
  329. }
  330. /*******************************************************************************
  331. * 函数名 : serial_recv
  332. * 描 述 : 接收一定长度的数据(从内存中读取数据)
  333. * 输 入 : comID: 串口标识号
  334. databuf: 接收数据缓冲区
  335. datalen: 需要接收数据长度
  336. * 输 出 : None
  337. * 返 回 : 实际接收到的数据长度
  338. *******************************************************************************/
  339. uint32_t serial_recv(uint8_t comID,uint8_t*databuf,uint32_t datalen)
  340. {
  341. uint16_t i =0, nextai_indx=0, j=0;
  342. NEXTAI_MGR_INFO*me= local_com_mgr_p;
  343. if((comID> NEXTAI_COM6_ID)||(comID< NEXTAI_COM1_ID))
  344. return 0xffffffff;
  345. nextai_indx= comID- NEXTAI_COM1_ID;
  346. if(me->local_channel_data[nextai_indx].used_flag==0)
  347. return 0xffffffff;
  348. if(!(me->data_recv_flag&(1<< nextai_indx)))
  349. return 0xffffffff;
  350. if(comID== NEXTAI_COM1_ID)
  351. {
  352. i=0;
  353. while(1)
  354. {
  355. if(com1_read_p== com1_gloabl_p)
  356. break;
  357. databuf[i]= com1_recv_buf[com1_read_p];
  358. com1_read_p++;
  359. com1_read_p&= MAX_RECV_DATA_MASK;
  360. i++;
  361. if(i>= datalen)
  362. break;
  363. for(j=0; j<100; j++);
  364. }
  365. me->data_recv_flag= me->data_recv_flag&(~(1<< nextai_indx));
  366. }
  367. if(comID== NEXTAI_COM4_ID)
  368. {
  369. i=0;
  370. while(1)
  371. {
  372. if(com4_read_p== com4_gloabl_p)
  373. break;
  374. databuf[i]= com4_recv_buf[com4_read_p];
  375. com4_read_p++;
  376. com4_read_p&= MAX_RECV_DATA_MASK;
  377. i++;
  378. if(i>= datalen)
  379. break;
  380. for(j=0; j<100; j++);
  381. }
  382. me->data_recv_flag= me->data_recv_flag&(~(1<< nextai_indx));
  383. }
  384. if(comID== NEXTAI_COM5_ID)
  385. {
  386. i=0;
  387. while(1)
  388. {
  389. if(com5_read_p== com5_gloabl_p)
  390. break;
  391. databuf[i]= com5_recv_buf[com5_read_p];
  392. com5_read_p++;
  393. com5_read_p&= MAX_RECV_DATA_MASK;
  394. i++;
  395. if(i>= datalen)
  396. break;
  397. for(j=0; j<100; j++);
  398. }
  399. me->data_recv_flag= me->data_recv_flag&(~(1<< nextai_indx));
  400. }
  401. if(comID== NEXTAI_COM6_ID)
  402. {
  403. i=0;
  404. while(1)
  405. {
  406. if(com6_read_p== com6_gloabl_p)
  407. break;
  408. databuf[i]= com6_recv_buf[com6_read_p];
  409. com6_read_p++;
  410. com6_read_p&= MAX_RECV_DATA_MASK;
  411. i++;
  412. if(i>= datalen)
  413. break;
  414. for(j=0; j<100; j++);
  415. }
  416. me->data_recv_flag= me->data_recv_flag&(~(1<< nextai_indx));
  417. }
  418. return i;
  419. }
  420. //中断处理函数只是将发送过来的数据保存到内存
  421. void USART1_IRQHandler(void)
  422. {
  423. NEXTAI_MGR_INFO*me= local_com_mgr_p;
  424. if(USART_GetITStatus(USART1, USART_IT_RXNE)!= RESET)
  425. {
  426. USART_ClearITPendingBit(USART1,USART_IT_RXNE);
  427. com1_recv_buf[com1_gloabl_p]= USART_ReceiveData(USART1);
  428. com1_gloabl_p++;
  429. com1_gloabl_p&= MAX_RECV_DATA_MASK;
  430. }
  431. me->data_recv_flag|=0x01;
  432. }

  433. //void UART5_IRQHandler(void)
  434. //{
  435. // uint8_t temp;
  436. // if(USART_GetITStatus(UART5, USART_IT_RXNE)!= RESET)
  437. // {
  438. // temp= USART_ReceiveData(UART5);
  439. // USART_SendData(UART5,temp);
  440. // }
  441. //}
  442. void UART4_IRQHandler(void)
  443. {
  444. NEXTAI_MGR_INFO*me= local_com_mgr_p;
  445. if(USART_GetITStatus(UART4, USART_IT_RXNE)!= RESET)
  446. {
  447. com4_recv_buf[com4_gloabl_p++]= USART_ReceiveData(UART4);
  448. com4_gloabl_p&= MAX_RECV_DATA_MASK;
  449. }
  450. me->data_recv_flag|=0x08;
  451. }
  452. void UART5_IRQHandler(void)
  453. {
  454. NEXTAI_MGR_INFO *me = local_com_mgr_p;
  455. if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
  456. {
  457. com5_recv_buf[com5_gloabl_p++] = USART_ReceiveData(UART5);
  458. com5_gloabl_p &= MAX_RECV_DATA_MASK;
  459. }
  460. me->data_recv_flag |= 0x10;
  461. }
  462. void USART6_IRQHandler(void)
  463. {
  464. NEXTAI_MGR_INFO*me= local_com_mgr_p;
  465. if(USART_GetITStatus(USART6, USART_IT_RXNE)!= RESET)
  466. {
  467. com6_recv_buf[com6_gloabl_p++]= USART_ReceiveData(USART6);
  468. com6_gloabl_p&= MAX_RECV_DATA_MASK;
  469. }
  470. me->data_recv_flag|=0x20;
  471. }

main.c
  1. #include"multiple_serial.h"
  2. #include"stm32f4xx_gpio.h"
  3. #include"stm32f4xx_usart.h"
  4. #include
  5. #include
  6. int main()
  7. {
  8. char*COM1="COM1";
  9. char*COM4="COM4";
  10. serial_Configuration((uint8_t*)COM1);
  11. serial_Configuration((uint8_t*)COM4);

  12. extern unsigned char uart_buf[64];

  13. int i=0,j;
  14. unsigned char printf_buf[128]={0};
  15. uint8_t flag=0;
  16. while(1)
  17. {
  18. flag= serial_recv(1, uart_buf,64);
  19. //延时让serial_recv函数接收完PC机串口调试助手发来的全部数据
  20. for(i=10000;i!=0;i--)
  21. for(j=3000;j!=0;j--);
  22. if(flag>0)
  23. {
  24. sprintf((char*)printf_buf,"receive data len is %d\n", flag);//将字符串写入printf_buf中
  25. serial_send(4, printf_buf, strlen((char*)printf_buf));//发送printf_buf中的内容到串口

  26. serial_send(4, uart_buf, flag+1);
  27. }

  28.    }
  29. return 0;
  30. }

测试结果如下图所示:
stm32RS485串口通信——中断接收发送数据_第1张图片

刚才改了下程序:
main.c
  1. #include"multiple_serial.h"
  2. #include"stm32f4xx_gpio.h"
  3. #include"stm32f4xx_usart.h"
  4. #include
  5. #include
  6. int main()
  7. {
  8. char*COM1="COM1";
  9. char*COM4="COM4";
  10. char*COM5="COM5";
  11. char*COM6="COM6";
  12. serial_Configuration((uint8_t*)COM1);
  13. serial_Configuration((uint8_t*)COM6);
  14. extern unsigned char uart_buf[64];
  15.         int i=0,j;
  16. unsigned char printf_buf[128]={0};
  17.         uint8_t flag=0;
  18. while(1)
  19. {
  20. //从串口调试助手发送数据,串口A接收到数据后产生中断,在中断函数中将数据保存到com6_recv_buf中,
  21.      //然后通过serial_recv函数将数据从com6_recv_buf中保存到接收uart_buf中,最后调用serial_send回传给串口调试助手。
  22. flag= serial_recv(1, uart_buf,64);
  23. if(flag>0)
  24. {
  25. serial_send(4, uart_buf, flag+1);
  26. }
  27. }
  28. return0;
  29. }

测试结果如下图所示:
stm32RS485串口通信——中断接收发送数据_第2张图片

通过发送和接收的数据对比,发现程序设计的还是比较稳定的,没有出现数据丢失的现象。


你可能感兴趣的:(stm32)