cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 Usart data receive problem

pm_brk_pm
Associate II
Posted on November 25, 2016 at 07:32

Hello all,

Directly, I want to talk about a problem I have encountered.I am working on 2 types of microcontroller and I use stm32f103 model to get data from the other one via rs4 So, For transmitting data from stm32f030 to stm32f103 , I am checking data transmition andif the correct data is being sent.In this part everything seems fine, the data is sent correctly. Then I am trying to get this data with stm32f103 microcontroller.In this part I use USART1 for communication via rs4 But unfortunately , during transmittion, stm32f103 does not indicate first character of the data and it continues untill the data transmission ends. So, Inthis case I don't want to miss this first character and I don't want to get this same data again and again in one go. Here is my receiving code of stm32f103(Expressions are available near codes)

extern
''C''
{
void
USART1_IRQHandler(
void
)
{
if
(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
//Data available in the Read Register.
//if(USART1->SR & USART_SR_RXNE)
{
// GPIO_SetBits(GPIOB,GPIO_Pin_0);
GPIO_WriteBit(GPIOB,GPIO_Pin_0, Bit_RESET);
//DE pin activation for receiving mode(rs485)
char
ch = rs485data.serialPort.getch();
//USART1 ''USART_ReceiveData(USART1);'' method
// if(ch!=50){
if
(ch!=10){
//if there is no new line in incoming data
if
(ch!=13){
//if there is no carriage return in incoming data
rs485data.serialPort.rxTrailerbuffer[rs485data.serialPort.rxTrailerbufferReadCounter++]=ch;
//fill buffer with character which comes from Usart1
}}
// } 
if
(rs485data.rs485detect== 
false
){
for
(
int
i = 0;i<rs485data.serialPort.rxTrailerbufferReadCounter;i++){
TrailerData[i]=rs485data.serialPort.rxTrailerbuffer[i];
//After we get the data from Usart1, we set data from trailerbuffer to TrailerData 
rs485data.serialPort.rxTrailerbuffer[i]=0;
//clear Trailer buffer
}
TrailerData[rs485data.serialPort.rxTrailerbufferReadCounter]=0;
//clear Trailer Data
rs485data.rs485detect=
true
;
rs485data.serialPort.rxTrailerbufferReadCounter=0;
//clear Trailerbufferreadcounter
// 
// if(rs485data.serialPort.rxTrailerbufferReadCounter>6){
// rs485data.serialPort.rxTrailerbufferReadCounter=0;}
}
//char ch = sp1.getch();
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//Clear pending bits for usart
}
}
}

Here is , which data I want to send with stm32f030 0690X0000060MnbQAE.gif Here is, What I get with stm32fAs you see the data is coming correctly. 0690X0000060MncQAE.gif This image is from my server, I am trying send this data (345) to server with GSM module and as ı mentioned above it skips first character of (345) in the beginning and add it to the end of this string. 0690X0000060MndQAE.gif
1 REPLY 1
pm_brk_pm
Associate II
Posted on November 25, 2016 at 08:33

Also I would like to share , how I send the data with stm32fIn this case, I used HAL drivers for some reason.

uint8_t buffer[100];
uint8_t *s;
int
len;
//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//{
// if (huart->Instance == USART1) //current UART
// {
// HAL_UART_Transmit_IT(&huart1, buffer, len); 
// 
// HAL_Delay(1500);
// }
//}
int
main(
void
)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
//HAL_HalfDuplex_Init(&huart1);
//HAL_UART_IRQHandler(&huart1);
HAL_RS485Ex_Init(&huart1,1,0,100);
//HAL_UART_RxCpltCallback(&huart1);
//HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while
(1)
{
/* USER CODE END WHILE */
//printf(''HELLO

'');
// HAL_Delay(500);
sprintf
(buffer,
''345

''
);
//


s=buffer;
len=
strlen
(buffer);
//len=sizeof(buffer) / sizeof(buffer[0]);
//HAL_UART_RxCpltCallback(&huart1);
if
(__HAL_UART_GET_IT(&huart1, UART_IT_TXE)!= RESET){
//HAL_UART_Transmit_IT(&huart1, buffer, len);
//HAL_UART_Transmit_IT(&huart1,buffer,len);
HAL_UART_Transmit(&huart1,s,len,1000);
for
(
int
x=0;x<len;x++){
buffer[x]=0;
s=0;
}
len = 0;
//j++;
//if (j>len) j=0;
//HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
//UU_PutString(USART1,buffer);
HAL_Delay(35000);
}
/* USER CODE BEGIN 3 */
}