Home > other >  STM32 serial port to send and receive
STM32 serial port to send and receive

Time:09-21

To achieve a serial communication function, process to monitor whether UART3 is over (receiving interrupt), if any, in the interrupt processing of UART3 receiving logo position 1, at this time to process the data received for cache array, and then through the cache array UART2 send interrupt () to send out, the real-time monitor whether access to the UART2 serial data, if any, to obtain the data sent via UART3 (sending interruption), the UART2 as 485 serial communication,

The situation now is UART3 normally receive serial data, and perform the UART2 send steps, but UART2: in the process of sending 1, sending data is not complete, such as setting to send 8 bytes cache array, actually sent only 2 bytes of data; 2, can only be transformed to the received data for the first time send, follow-up the data received cannot be sent again, I doubt if there are other interrupt interrupted UART2 transmission process, the following are the main code:

The principal part of the code is generated automatically by the CUBEMX, including HAL_Init (); , SystemClock_Config (); , MX_GPIO_Init (); And accurate atomic related initialization program delay_init (180); , KEY_Init (); , RS485Init (); I am RS485Init () have been adjusted to conform to the actual situation:
Void RS485Init (void)
{
PCF8574_Init ();//initialization PCF8574, used to control foot RE
MX_USART2_UART_Init ();//initialize USART2
MX_USART3_UART_Init ();//initialize USART3
RS485_MODE_Set (0);//set to receive mode
}
Then before entering the infinite loop, called HAL_UART_Receive_IT (& amp; Huart3, Rx_Buf2, 8); To unlock UART3 receiving interrupt,

Below into the infinite loop (only put U3 - U2 part of contents) :
While (1)
{
If (rx_flag_U3)//UART3 received information signs, placed in the receiving interrupt the callback function 1
{
Uart_U3toU2 (Rx_Buf2);
HAL_UART_Receive_IT (& amp; Huart3, Rx_Buf2, 8);
}
}

Void uart_U3toU2 (uint8_t * pData)
{
If (pData [0]==0 x0a)
{
for(i=0; I<4. I++)
{
Trans_buffer [0]=slav_num [I];
Trans_buffer [1]=pData [1].
Trans_buffer [2]=pData [2].
Trans_buffer [3]=pData [3].
Trans_buffer [4]=0 x00;
Trans_buffer [5]=0 x02;
CRC_value=https://bbs.csdn.net/topics/CRC_Chek ((uint8_t *) & trans_buffer, 6);//calculate CRC value
Trans_buffer [6]=(uint8_t CRC_value);//CRC high eight
Trans_buffer [7]=(uint8_t) (CRC_value & gt;> 8);
delay_ms(10);
RS485_MODE_Set (1);//set to send pattern
HAL_UART_Transmit_IT (& amp; Huart2, trans_buffer, 8);//serial port to send data 2
While (__HAL_UART_GET_FLAG (& amp; Huart2, UART_FLAG_TC)!=SET);//waiting to send complete
RS485_MODE_Set (0);//set to receive mode
BEEP ();
HAL_UART_Receive_IT (& amp; Huart2 Rx_Buf1, 9);//serial port 2 receive enabled, receives the length is 9 bytes
delay_ms(10);//delay the end
10 ms}
for(j=0; J<50; J++) {trans_buffer [j]=0; PData [j]=0; }
}

}

My side UART3 time constant at 100 ms intervals 14 frames the serial data sent to come over, every 2 s, for example, in 100 ms time interval to send 14 frames "0 01 03 04 00 00 00 00 a.", every 2 s repeat send the frame data,
STM32 side monitoring to each frame data can be received, but only received the first frame data can enter the data conversion/send program (that is, the buzzer rang the 4 times), after all the data have been sent out, but could hear the buzzer sound but the sound of voice is weak, should be the implementation of the switch/router, but the problem here:
1, a serial port monitoring software, according to the first frame of the data are not normal, such as "01 01 FB" "02 01 FB 03 01" FB "" 04 01 FB";
2, the subsequent have never send out data, namely the actual not perform data sending function??????

Please god help me to look at, where there is a problem, thank you!!!!

CodePudding user response:

Communicate to each other on both sides to set aside receiving, processing time, can't continuous transceiver,

CodePudding user response:

reference 1st floor xuyaqi029 response:
communication to each other on both sides to set aside receiving, processing time, can't continuous transceiver,

A while loop and 10 ms delay sent a message to enough? Should not be...

CodePudding user response:

reference 1st floor xuyaqi029 response:
communication to each other on both sides to set aside receiving, processing time, can't continuous transceiver,

I changed the later, is indeed a continuous sent didn't have enough time, thank you
  • Related