完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
`接收数据为:BB 97 11 30 00 11 22 33 44 55 66 77 88 99 00 11 22 FD 6F 03 1A 0D 0A 发送端一直不定时发此类数据 但是我用STM32F103RCT6单片机接收丢包挺多,丢包的频率很规律 应该是我程序代码没写严谨,求大神指点 void Init_NVIC(void) { NVIC_InitTypeDef NVIC_InitStructure; //¶¨ÒåÒ»¸öNVICÏòÁ¿±í½á¹¹Ìå±äÁ¿ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃÖжÏ×é Ϊ2 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //ÅäÖô®¿Ú1ΪÖжÏÔ´ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÉèÖÃÕ¼ÏÈÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //ÉèÖø±ÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ´®¿Ú1ÖÐ¶Ï NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯ÖжϼĴæÆ÷ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //ÅäÖô®¿Ú1ΪÖжÏÔ´ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÉèÖÃÕ¼ÏÈÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //ÉèÖø±ÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ´®¿Ú1ÖÐ¶Ï NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯ÖжϼĴæÆ÷ NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //ÅäÖô®¿Ú1ΪÖжÏÔ´ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÉèÖÃÕ¼ÏÈÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //ÉèÖø±ÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ´®¿Ú1ÖÐ¶Ï NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯ÖжϼĴæÆ÷ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //ÅäÖô®¿Ú1ΪÖжÏÔ´ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÉèÖÃÕ¼ÏÈÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //ÉèÖø±ÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ´®¿Ú1ÖÐ¶Ï NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯ÖжϼĴæÆ÷ } void Init_Usart2(void) { GPIO_InitTypeDef GPIO_InitStructure; //¶¨ÒåÒ»¸öGPIO½á¹¹Ìå±äÁ¿ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO ,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE); //ʹÄܸ÷¸ö¶Ë¿ÚʱÖÓ£¬ÖØÒª£¡£¡£¡ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //ÅäÖô®¿Ú·¢ËͶ˿ڹҽӵ½9¶Ë¿Ú GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´Óù¦ÄÜÊä³ö¿ªÂ© GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ÅäÖö˿ÚËÙ¶ÈΪ50M GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯GPIOA¼Ä´æÆ÷ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //½ÓÊÕ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿ÕÊäÈë(¸´Î»×´Ì¬); GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯GPIOA¼Ä´æÆ÷ } void Usart2_Configuration(uint32_t BaudRate) { USART_InitTypeDef USART_InitStructure; //¶¨ÒåÒ»¸ö´®¿Ú½á¹¹Ìå USART_InitStructure.USART_BaudRate = BaudRate ; //²¨ÌØÂÊ115200 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //´«Êä¹ý³ÌÖÐʹÓÃ8λÊý¾Ý USART_InitStructure.USART_StopBits = USART_StopBits_1; //ÔÚÖ¡½áβ´«Êä1λֹͣλ USART_InitStructure.USART_Parity = USART_Parity_No ; //ÆæżʧÄÜ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//Ó²¼þÁ÷ʧÄÜ USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //½ÓÊպͷ¢ËÍģʽ USART_Init(USART2, &USART_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯´®¿Ú¼Ä´æÆ÷ USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //ʹÄÜ´®¿ÚÖжϽÓÊÕ USART_Cmd(USART2, ENABLE); //ʹÄÜ´®¿ÚÍâÉè } void USART2_IRQHandler() { int i=0; u8 a=0; extern u8 UART2_Save_data_box[UART2_Save_data_box_number]; //·ÅÖÃUART2½ÓÊÕµ½µÄ×Ö½Ú ÐèÒªÒç³ö¹é0 extern u16 UART2_Save_data_box_pointer; //·ÅÖÃUART2½ÓÊÕµ½µÄ×Ö½ÚºÐ×ÓÏÂÒ»¸öÊý¾Ý·ÅÖõÄλÖÃÖ¸Ïò±êÖ¾ ÐèÒªÒç³ö¹é0 extern u32 EPC_box[EPC_number]; extern u16 EPC_box_pointer; extern u8 back_correct; extern u32 error1; extern u32 error2; extern u32 error3; extern u32 EPC_count_l; extern u32 EPC_count_h; extern u32 timer1; extern u8 zisuo3; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //¶ÁÈ¡½ÓÊÕÖжϱê־λUSART_IT_RXNE //USART_FLAG_RXNE:½ÓÊÕÊý¾Ý¼Ä´æÆ÷·Ç¿Õ±ê־λ //1£ºÃ¦×´Ì¬ 0£º¿ÕÏÐ(ûÊÕµ½Êý¾Ý£¬µÈ´ý¡£¡£¡£) { if(UART2_Save_data_box[0]==0xBB) //ÅжÏÊý¾Ý°üÍ· { UART2_Save_data_box[UART2_Save_data_box_pointer]=USART_ReceiveData(USART2); //¼ÌÐø´æ´¢Êý¾Ý UART2_Save_data_box_pointer++; /* if(UART2_Save_data_box[1]==0x98&&UART2_Save_data_box[5]==0x0D && UART2_Save_data_box[6]==0x0A) //ÅжÏÊý¾Ý°üβ { for(i=1;i<4;i++) //¼ÆËãУÑéÖµ { a+=(u8)UART2_Save_data_box; } if(a==UART2_Save_data_box[4]) //УÑé³É¹¦ { if(UART2_Save_data_box[3]==0x01) { back_correct=1; OUT2_LED=0; } else { back_correct=2; } } for(i=0;i UART2_Save_data_box=0; //Ò»µ©Åжϵ½Êý¾Ý°üµÄβ²¿ÔòÊý×é³õʼ»¯ } UART2_Save_data_box_pointer=1; //Ò»µ©Åжϵ½Êý¾Ý°üµÄβ²¿ÔòÖÃ1 } else */if(UART2_Save_data_box[1]==0x97&&UART2_Save_data_box[21]==0x0D && UART2_Save_data_box[22]==0x0A) //ÅжÏÊý¾Ý°üβ { for(i=1;i<20;i++) //¼ÆËãУÑéÖµ { a+=(u8)UART2_Save_data_box; } if(a==UART2_Save_data_box[20]) //УÑé³É¹¦ { for(i=0;i<3;i++) { EPC_box[EPC_box_pointer]=(UART2_Save_data_box[i*4+5]<<24)+(UART2_Save_data_box[i*4+1+5]<<16)+(UART2_Save_data_box[i*4+2+5]<<8)+(UART2_Save_data_box[i*4+3+5]<<0); //´æ´¢EPC EPC_box_pointer++; } zisuo3=0; OUT1_jingbao=1; //Êä³ö±¨¾¯ timer1=0; //¼ÆʱÇå0 EPC_count_l++; //±êÇ©×ÜÊý+1 if(EPC_count_l>=1000000000) { EPC_count_l=0; EPC_count_h++; } } else { error1++; //żȻ´íÎó ´íÎó±êÖ¾¼Ó1 } for(i=0;i UART2_Save_data_box=0; //Ò»µ©Åжϵ½Êý¾Ý°üµÄβ²¿ÔòÊý×é³õʼ»¯ } UART2_Save_data_box_pointer=1; //Ò»µ©Åжϵ½Êý¾Ý°üµÄβ²¿ÔòÖÃ1 } else if(UART2_Save_data_box_pointer>=23) //ż¶ûÐÔµÄÊý¾Ý´íÎó ¿ªÊ¼Çå0 { for(i=0;i UART2_Save_data_box=0; } UART2_Save_data_box_pointer=1; error2++; //żȻ´íÎó ´íÎó±êÖ¾¼Ó1 } if(EPC_box_pointer>=EPC_number) { EPC_box_pointer=0; } } else if(UART2_Save_data_box[0]==0x00) //Èç¹ûÉϸöÊý¾Ý²»ÊÇÊý¾Ý°üÍ· { UART2_Save_data_box[0]=USART_ReceiveData(USART2); //´¢´æÊý¾Ýµ½UART2 } else { error3++; UART2_Save_data_box[0]=USART_ReceiveData(USART2); //´¢´æÊý¾Ýµ½UART2 } USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Çå³ýÖжϱê־λ } } ` |
|
相关推荐
9个回答
|
|
error2 和 error3 都有错误 但是error1一直为0 说明校验是成功的
|
|
|
|
串口接收中断里只做接收数据的缓存,处理拿到外面去做。
|
|
|
|
对于包内数据的判断应该是在完整的接收了一个数据包之后才做的
|
|
|
|
|
|
|
|
|
|
|
|
我重新贴一下代码
|
|
|
|
你的程序有些乱,注释有不能正常显示,只能说我的思路了,作为接收数据包来说,应该是先判断接收到的是否是数据包的包头,当接收到数据包包头时,存储包头,同时UART2_Save_data_box_number = 1,后面则时判断接收的数据是否是包尾,当接收到包尾时,则认为数据包接收完毕,如果时定长的数据包,则可以同时判断数据包长度是否正确。
你的程序虽然有对一些数据包的特定数据进行判断处理,但这些都是基于接收完数据包的操作,并没有看到接收数据包过程的操作。 |
|
|
|
顶一个~~~~~~~~~~~~~~~
|
|
|
|
路过帮顶
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1786 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1657 浏览 3 评论
4216 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1805 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2291 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-13 14:54 , Processed in 0.894941 second(s), Total 90, Slave 72 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号