STM32/STM8技术论坛
直播中

廖靖华

7年用户 161经验值
私信 关注
[问答]

串口通信在超级终端发了一个命令后SBSB语句没返回



的意图是用电脑的超级终端先向usart1发送任意一个命令,然后usart1返回SBSB这个语句,接着USART2发送出一条命令oreder,和Usart2连接的模块收到这个命令后会返回一个命令rec。但我在超级终端发了一个命令后,连SBSB这个语句都没返回。求教
int main(void)
{
char i;
char j;
uint32_t oreder[ ] = {0x01, 0x03,0x00,0x48,0x00,0x08,0xC4,0x1A};
uint32_t rec[ ]={0x01, 0x03,0x00,0x48,0x00,0x08,0xC4,0x1A};;
USART1_Config();
USART2_Config();
NVIC_Configuration();
while(1){
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{}
printf("SBSB");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{}

for(i=0;i<8;i++) {
USART_SendData(USART2, oreder);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
{} }
for(j=0;i<37;j++) {
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
{}
rec[j] = (USART_ReceiveData(USART2));
USART_SendData(USART1, rec[j]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{}
}





} }








void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
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);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 115200;
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_Cmd(USART1, ENABLE);
}
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 4800;
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_Cmd(USART2, ENABLE);
}
/*
* 函数名:fputc
* 描述 :重定向c库函数printf到USART1
* 输入 :无
* 输出 :无
* 调用 :由printf调用
*/
int fputc(int ch, FILE *f)
{
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));

return (ch);
}

回帖(7)

廖靖华

2018-9-26 09:40:20
求求各位大神了
举报

尤两畅

2018-9-26 09:49:06
帮顶,希望能有答案
举报

高建明

2018-9-26 09:55:08
帮顶         
举报

郎渐海

2018-9-26 10:10:17
帮顶     .。。。。。。。。。。
举报

谢剑萍

2018-9-26 10:27:24
你在超级终端发了一个字节后,先让一个LED闪烁,检测一下收数据的情况。
举报

李子崴

2018-9-26 10:36:07
看不到哎
举报

孙婷婷

2018-9-26 10:47:21
RCC_APB2Periph_AFIO,把这个时钟开一下试试。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分