Home > other >  STM32F103RCT6 serial port 1 and serial data forwarding of 2 (for)
STM32F103RCT6 serial port 1 and serial data forwarding of 2 (for)

Time:12-29

I now because some things need to use single chip microcomputer to programming, to understand a little before 51, simple to use arduino, do not understand the essence of programming, now must use 32, completely cocked hat,
Computer now I need to use the 32 USART1, USART2 connect a SIM card module, (hardware is connected well, and is on a board USART2 didn't take out, can not be sure,) I need to write a piece of code to complete the AT command forwarding, among this forward from USART1 receives the data to USART2, AT the same time wait for USART2 return data, and then through the USART1 sent to the computer, is equivalent to 32 made a relay,
I now have no clue, someone to show how the programming thought of this program should be, for example to disrupt does not use, or order inside the main function,

CodePudding user response:

Interface is very simple, UART1 to the received data, as is passed to the UART2, UART2 receive data forwarding UART1 as original

CodePudding user response:

I was thinking, but each success

CodePudding user response:

Stm32 serial code online a lot, look for a reference, respectively each serial port to send and receive good debugging is simple

CodePudding user response:

Initialize the interrupt and so on serial port 1 already test no problem, but I don't have an external serial port 2, and, I am now stuck in the how to let two serial data exchange

CodePudding user response:

The received data stored in the array, a send function called not to go, in a serial port receiving interrupt 1, serial port to send 2 function called, in serial port receiving interrupt of 2, send function calls a serial port 1,

CodePudding user response:

USES to accept some interrupt simply, USART2 usart1 accept interrupt routine direct start to send, the received byte forward into a
Similarly, USART2 accept interrupt, start USART1 send, forward,

CodePudding user response:

6th floor positive solution,

CodePudding user response:

A module is done, I talk to you about, uart port 1 will receive the same issue uart2, uart2 receive DTU module (insert SIM card can be on the sort of) public uart2 do automatic initialization and links of the base station (including the reconnection) of the action, then uart1 have detected data over the package to the AT command by DTU module turn out, uart2 receive data in the solution AT part can be dropped again to uart1, the parameters of the whole equipment including IP address, port, such as through setting, uart1 (the whole module function is actually a serial port turn network functions, projects to use online to buy a this stuff to about $500, so I did it by himself)

CodePudding user response:

Do their serial transceiver success first, and then forward it is much easier, is online

CodePudding user response:

Single-chip microcomputer serial port, are generally adopt active sending, interrupt accept! You there, two serial ports to accept interrupted, after receiving the data, single-chip microcomputer forwarding, of course, also can consider to the DMA, but I have not tried can or not, if you can, you configured channels, and then you can do nothing!

CodePudding user response:

With serial assistants first these tools to adjust each serial port, the computer serial port assistant, two of the stm32 serial transceiver is adjustable, and then the sim card, I don't know if you have this sim card serial port reply information; Sim card is also is to have the response, a simple serial assistants debugged and see if I can work normally is ok, then how do you consider the overall # 3 # 6 has given method

CodePudding user response:

Can redirect it

CodePudding user response:

1, open the second serial port receiving interrupt, judge received after send
2, open the second serial port receiving interrupt, setting flags; In the main loop repeatedly visit this flag, when mark display is completed, start sending
The main loop function:
 void uart2_tips (void) 
{
If (USART2_RX_STA & amp; 0 x8000)
{
Len=USART2_RX_STA & amp; 0 x3fff;//get the received data length
Printf (" \ r \ n you sent USART2 message is: \ r \ n \ r \ n ");
For (t=0; t{
USART_SendData (USART1, USART2_RX_BUF [t]);//send data to the serial port 1
While (USART_GetFlagStatus (USART1, USART_FLAG_TC)!=SET);//wait for sending end
}
Command_operate (USART2_RX_BUF USART2_RX_STA);
printf("\r\n\r\n");//insert line breaks
USART2_RX_STA=0;
} the else
{
Times++;
If (times %==0 5000)
{
Printf (" \ r \ nSTM32 USART2 \ r \ n ");
}
delay_ms(10);
}
}

A serial port interrupt handling function (routine) to simulate the punctual atom:
 void USART2_IRQHandler (void)//serial port 1 interrupt service routines 
{
U8 Res;
# if SYSTEM_SUPPORT_OS//if SYSTEM_SUPPORT_OS is true, then you need to support OS.
OSIntEnter();
# endif
If (USART_GetITStatus (USART2, USART_IT_RXNE)!=RESET)//receiving interrupt (the received data must be 0 x0d 0 x0a end)
{
Res=USART_ReceiveData (USART2);//read the received data

If ((USART2_RX_STA & amp; 0 x8000)==0)//receiving unfinished
{
If (USART2_RX_STA & amp; 0 x4000)//received 0 x0d
{
If (Res!=0 x24) USART2_RX_STA=0;//receiving errors, start
The else USART2_RX_STA |=0 x8000;//receive completed
}
Haven't received 0 x0d else//
{
If (Res==0 x24)
USART2_RX_STA |=0 x4000;
The else
{
USART2_RX_BUF x3fff] [USART2_RX_STA & amp; 0=Res;
USART2_RX_STA + +;
If (USART2_RX_STA & gt; USART2_RX_STA (USART_REC_LEN - 1))=0;//receiving data errors, start receiving
}
}
}
}
# if SYSTEM_SUPPORT_OS//if SYSTEM_SUPPORT_OS is true, then you need to support OS.
OSIntExit();
# endif
}

A serial port 2 Settings:
 void uart2_init (u32 bound) {
//set the GPIO port
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);//can make USART1, GPIOA clock
RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2, ENABLE);

//USART1_TX GPIOA. 9
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_2; 9
//PA.GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF_PP;//multiplexing push-pull output
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);//initialize GPIOA. 9

//USART1_RX GPIOA. 10 initialization
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_3;//PA10
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;//floated input
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);//initialize GPIOA. 10

//Usart1 NVIC configuration
NVIC_InitStructure. NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=3;//take priority 3
NVIC_InitStructure. NVIC_IRQChannelSubPriority=3;//child priority 3
NVIC_InitStructure. NVIC_IRQChannelCmd=ENABLE;//IRQ channel can make
NVIC_Init (& amp; NVIC_InitStructure);//based on the specified parameters initialization VIC register

//USART initialization setting

USART_InitStructure. USART_BaudRate=bound;//serial port baud rate
USART_InitStructure. USART_WordLength=USART_WordLength_8b;//word length of 8 bits of data format
USART_InitStructure. USART_StopBits=USART_StopBits_1;//a stop bit
USART_InitStructure. USART_Parity=USART_Parity_No; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related