STM32串口DMA发送数据不连续

2019-07-14 14:13发布

STM32串口DMA发送的数据不连续,已经试了一些办法,仍未解决,不知是什么原因?
用串口监控得到的数据是:
10,9:07:54,io.exe(5532),IRP_MJ_WRITE,COM8,7,        4D 00 01 01 06 00 08  | M#0#1#1#6#0#8,
12,9:07:54,io.exe(5532),IRP_MJ_READ,COM8,1,        4D  | M,
13,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        00  | #0,
14,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        01  | #1,
15,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        23  | #,
16,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        00  | #0,
17,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        03  | #3,
18,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        5C  | ,
19,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        88  | ?,
20,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        00  | #0,
21,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        13  | #19,
22,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        CB  | ?,
23,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        12  | #18,
24,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        00  | #0,
25,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        00  | #0,
26,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        90  | ?,
27,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        D2  | ò,
28,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        07  | #7,
29,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        EE  | ?,
30,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        17  | ,
31,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        E1  | á,
32,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        00  | #0,
33,9:07:56,io.exe(5532),IRP_MJ_READ,COM8,1,        0D  | #13,
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
11条回答
亲爱的娜娜
1楼-- · 2019-07-14 22:35
 精彩回答 2  元偷偷看……
zzw0719
2楼-- · 2019-07-15 03:23
帮顶                 
djw200988
3楼-- · 2019-07-15 04:20
同意2楼,不知道这是什么,帮顶
cmh4
4楼-- · 2019-07-15 06:52
USART串口通讯的代码如下:

void GPIO_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
}

//串口初始化:
void USART_Configuration(void)
{
        USART_InitTypeDef USART_InitStructure;
        USART_InitStructure.USART_BaudRate = 9600;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_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(USART1, &USART_InitStructure);
        USART_ITConfig(USART1, USART_IT_IDLE , ENABLE);
        USART_Cmd(USART1, ENABLE);  
        USART_ClearFlag(USART1, USART_FLAG_TC); // 清发送完成标志
}       

//DMA配置:
void DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA1时钟
  DMA_DeInit(DMA1_Channel4);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = 0x40013804;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)TX1_SendBuf;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;        //外设作为DMA的目的端
  DMA_InitStructure.DMA_BufferSize = 512;         // 根据实际发送数据长度确定
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;        //外设地址不增加
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                        //内存地址自增1
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//(只传送一次)                        DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;                 //DMA传送优先级
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel4, &DMA_InitStructure);
  DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
  DMA_ITConfig(DMA1_Channel4, DMA_IT_TE, ENABLE);
  USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
  DMA_Cmd(DMA1_Channel4, DISABLE);

  DMA_DeInit(DMA1_Channel5);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = 0x40013804;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RX1_RcvBuf;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 512;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//(只传送一次)                       
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;         //DMA传送优先级
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel5, &DMA_InitStructure);
  DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
  DMA_ITConfig(DMA1_Channel5, DMA_IT_TE, ENABLE);

  USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
  DMA_Cmd(DMA1_Channel5, ENABLE);
}

//中断优先级配置:
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                 //中断分组设置
       
/* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;                  //设置串口1中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;                 //抢占优先级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;                                         //响应优先级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Enable the USART2 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;                 //抢占优先级 1
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;                                         //响应优先级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;                //抢占优先级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;                                        //响应优先级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void USART1_IRQHandler(void)        //USART1收发全用DMA
{                 
        u16 DATA_LEN;

        if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)//如果为空闲总线中断
        {
                DMA_Cmd(DMA1_Channel5, DISABLE);
                if(RX1_dataOverflow == 0)         //1=接收的数据超过缓冲区数量
                {
                        DATA_LEN = 512 - DMA_GetCurrDataCounter(DMA1_Channel5); //已经传输的字节数

                        if(RX1_RcvBuf[0] == 'M')//;判断是否为帧头,MODBUS数据接收标志,0x4D='M'
                        {
                                RX1_Data_Len = DATA_LEN;
                                Handle_MODBUS_CMD();
                        }
                        else if(RX1_RcvBuf[0] == 126)//检查USART1的国军标协议接收标志,为1则有命令帧需处理
                        {       
                                HandleCMD()        ;
                        }
                }
                RX1_dataOverflow = 0;         //1=接收的数据超过缓冲区数量

                DMA_ClearFlag(DMA1_FLAG_GL5 | DMA1_FLAG_TC5 | DMA1_FLAG_TE5 | DMA1_FLAG_HT5);//清标志
                DMA1_Channel5->CNDTR = 512;//重装填
                DMA_Cmd(DMA1_Channel5, ENABLE);//处理完,重开DMA
                DATA_LEN = USART1->SR;
                DATA_LEN = USART1->DR;
        }

        if(USART_GetITStatus(USART1, USART_IT_PE | USART_IT_FE | USART_IT_NE) != RESET)//出错
        {
                USART_ClearITPendingBit(USART1, USART_IT_PE | USART_IT_FE | USART_IT_NE);
        }
        USART_ClearITPendingBit(USART1, USART_IT_TC);
        USART_ClearITPendingBit(USART1, USART_IT_IDLE);
}
cmh4
5楼-- · 2019-07-15 06:57
用其它单片机与PC软件通讯,监测到的数据如下,这是正确的,单片机串口发送数据无间断:
序号,时间,进程(PID),IO类型,COM口,数据长度,数据,
74,10:49:20,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 02 01 06 00 09  | M#0#2#1#6#0#9,
75,10:49:20,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 02 23 00 03 5B CC 00 13 C7 7B 00 00 90 B1 06 AB 17 E0 00 05 06 AB 42 F4 0D 9D 42 03 AB 6F 24 14 75 00 01 00 03 0A 80  | M#0#2##0#3[ì#0#19?{#0#0?±#6?à#0#5#6?B?#13?B#3?o$u#0#1#0#3#10?,
76,10:49:22,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 01 01 06 00 08  | M#0#1#1#6#0#8,
77,10:49:22,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 01 23 00 03 5B D4 00 13 C7 CB 00 00 90 AA 0A 27 17 E0 00 18 07 BB 42 F2 C0 81 42 00 7F 15 22 27 72 00 01 00 03 0A 8E  | M#0#1##0#3[?#0#19??#0#0?a#10'à#0#7?Bòà?B#0"'r#0#1#0#3#10?,
78,10:49:22,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 02 01 06 00 09  | M#0#2#1#6#0#9,
79,10:49:22,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 02 23 00 03 5B CC 00 13 C7 7D 00 00 90 B1 06 B7 17 E0 00 05 06 B7 42 F4 0F BC 42 03 AD F2 24 14 B0 00 01 00 03 0B 7B  | M#0#2##0#3[ì#0#19?}#0#0?±#6·à#0#5#6·B?#15?B#3-ò$°#0#1#0#3#11{,
80,10:49:24,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 01 01 06 00 08  | M#0#1#1#6#0#8,
81,10:49:24,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 01 23 00 03 5B D5 00 13 C7 CE 00 00 90 AB 0A 20 17 E0 00 18 07 BB 42 F2 BF 30 42 00 7D 87 22 27 4E 00 01 00 03 0A 86  | M#0#1##0#3[?#0#19??#0#0??#10 à#0#7?Bò?0B#0}?"'N#0#1#0#3#10?,
82,10:49:26,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 02 01 06 00 09  | M#0#2#1#6#0#9,
83,10:49:26,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 02 23 00 03 5B CD 00 13 C7 80 00 00 90 B1 06 CF 17 E0 00 05 06 CF 42 F4 14 09 42 03 B3 09 24 15 28 00 01 00 03 09 97  | M#0#2##0#3[í#0#19??#0#0?±#6?à#0#5#6?B?#9B#33#9$(#0#1#0#3#9?,
84,10:49:26,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 01 01 06 00 08  | M#0#1#1#6#0#8,
85,10:49:26,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 01 23 00 03 5B D6 00 13 C7 D0 00 00 90 AB 0A 14 17 E0 00 18 07 BB 42 F2 BD 11 42 00 7B 04 22 27 13 00 01 00 03 09 9C  | M#0#1##0#3[?#0#19?D#0#0??#10à#0#7?Bò?#17B#0{#4"'#19#0#1#0#3#9?,
86,10:49:28,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 02 01 06 00 09  | M#0#2#1#6#0#9,
87,10:49:28,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 02 23 00 03 5B CD 00 13 C7 82 00 00 90 B1 06 E6 17 E0 00 06 06 E6 42 F4 18 52 42 03 B8 1B 24 15 9E 00 01 00 03 0A A2  | M#0#2##0#3[í#0#19??#0#0?±#6?à#0#6#6?B?RB#3?$?#0#1#0#3#10¢,
88,10:49:30,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 01 01 06 00 08  | M#0#1#1#6#0#8,
89,10:49:30,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 01 23 00 03 5B D7 00 13 C7 D3 00 00 90 AB 09 FC 17 E0 00 18 07 BB 42 F2 B8 C4 42 00 75 ED 22 26 9B 00 01 00 03 0C 9F  | M#0#1##0#3[×#0#19?ó#0#0??#9üà#0#7?Bò??B#0uí"&?#0#1#0#3#12?,
90,10:49:30,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 02 01 06 00 09  | M#0#2#1#6#0#9,
91,10:49:30,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 02 23 00 03 5B CE 00 13 C7 85 00 00 90 B1 07 0D 17 E0 00 07 07 0D 42 F4 1F 5B 42 03 C0 6D 24 16 61 00 01 00 03 09 25  | M#0#2##0#3[?#0#19??#0#0?±#7#13à#0#7#7#13B?[B#3àm$a#0#1#0#3#9%,
92,10:49:32,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 01 01 06 00 08  | M#0#1#1#6#0#8,
93,10:49:32,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 01 23 00 03 5B D8 00 13 C7 D5 00 00 90 AB 09 E5 17 E0 00 17 07 BB 42 F2 B4 7B 42 00 70 DB 22 26 25 00 01 00 03 0B B0  | M#0#1##0#3[?#0#19??#0#0??#9?à#0#7?Bò′{B#0p?"&%#0#1#0#3#11°,
94,10:49:34,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 02 01 06 00 09  | M#0#2#1#6#0#9,
95,10:49:34,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 02 23 00 03 5B CF 00 13 C7 88 00 00 90 B2 07 3C 17 E0 00 08 07 3C 42 F4 27 C5 42 03 CA 62 24 17 4B 00 01 00 03 09 E5  | M#0#2##0#3[?#0#19??#0#0?2#7<à#0#8#7<B?'?B#3êb$K#0#1#0#3#9?,
96,10:49:34,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 01 01 06 00 08  | M#0#1#1#6#0#8,
97,10:49:34,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 01 23 00 03 5B D9 00 13 C7 D8 00 00 90 AC 09 BE 17 E0 00 16 07 BB 42 F2 AD 72 42 00 68 89 22 25 62 00 01 00 03 0B 5F  | M#0#1##0#3[ù#0#19??#0#0??#9?à#0#7?Bò-rB#0h?"%b#0#1#0#3#11_,
98,10:49:36,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 02 01 06 00 09  | M#0#2#1#6#0#9,
99,10:49:36,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 02 23 00 03 5B CF 00 13 C7 8A 00 00 90 B2 07 64 17 E0 00 08 07 64 42 F4 2E E5 42 03 D2 CF 24 18 10 00 01 00 03 0A 99  | M#0#2##0#3[?#0#19??#0#0?2#7dà#0#8#7dB?.?B#3ò?$#16#0#1#0#3#10?,
100,10:49:38,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 01 01 06 00 08  | M#0#1#1#6#0#8,
101,10:49:38,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 01 23 00 03 5B DA 00 13 C7 DB 00 00 90 AC 09 8F 17 E0 00 15 07 BB 42 F2 A5 07 42 00 5E 94 22 24 78 00 01 00 03 0A D6  | M#0#1##0#3[ú#0#19??#0#0??#9?à#0#7?Bò¥#7B#0^?"$x#0#1#0#3#10?,
102,10:49:38,KCJC_Set.exe(2228),IRP_MJ_WRITE,COM8,7,
        4D 00 02 01 06 00 09  | M#0#2#1#6#0#9,
103,10:49:38,KCJC_Set.exe(2228),IRP_MJ_READ,COM8,41,
        4D 00 02 23 00 03 5B D0 00 13 C7 8D 00 00 90 B2 07 9E 17 E0 00 0A 07 9E 42 F4 39 52 42 03 DF 26 24 19 32 00 01 00 03 0A 12  | M#0#2##0#3[D#0#19??#0#0?2#7?à#0#10#7?B?9RB#3?&$2#0#1#0#3#10#18,
cmh4
6楼-- · 2019-07-15 08:52
 精彩回答 2  元偷偷看……

一周热门 更多>