2020-06-02 05:42 AM
Hello,
I try to make an UART communication between Nucleo STM32F030RE and a STM32F030K6 mounted on a custom pcb.
Here is the code i use for NUCLEO:
int data1 = 0x21 ;
int data2 = 0x22 ;
char *datachar1 = (char*)&data1 ;
char *datachar2 = (char*)&data2;
char *data = (char*)strcat(datachar1, datachar2) ;
char *msg = "KO \n\r";
char *Rxbuffer = NULL ;
int status ;
while (1)
{
HAL_UART_Transmit(&huart1, (uint8_t*)data, strlen(data), 0xFFFF);
status = HAL_UART_Receive(&huart1, (uint8_t*)Rxbuffer, 2, 65000) ;
if ( status == HAL_OK )
{
HAL_UART_Transmit(&huart2, (uint8_t*)Rxbuffer, strlen(Rxbuffer), 0xFFFF); //send to monitor the reception of uart1
} else {
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 0xFFFF);
}
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5) ; //to visualiy check on the nucleo that the main loop is still running
HAL_Delay(5000) ;
}
Basically, NUCLEO UART1 sends 2 hexadecimal commands to STM32F030K6.
STM32F030K6 reads, interprets it and replies in function of the command received.
STM32F030K6 replie is 6 bytes sent to NUCLEO on PA_10 (UART1 Rx).
To check what NUCLEO has received from STM32F030K6, i sent what it has received directly to UART2 which is transmit via usb to a computer to visually read.
If i got a wrong status from the UART1 receive function, i send a readable "KO" status to the computer... which happens all the time.
Any tips ?
Note: whatever the timeout value i put as argument of the HAL_UART_Receive function, the delay time i see thanks to the data monitored is the same. Is it normal ?
Solved! Go to Solution.
2020-06-03 05:46 AM
char RxBuffer[6] ;
char *ptRxBuffer = &RxBuffer[6] ;
ptRxBuffer now points to the byte after the end of RxBuffer. Not what you want. You want ptRxBuffer = RxBuffer. No need to create another variable.
int data[2] = {0x81,0x17} ;
int *ptdata = &data[2] ;
ptdata points to the byte at data[2], which is the byte after 0x17, past the end of your array. Again, what you want is ptdata = data, so no need for another variable.
2020-06-02 05:53 AM
> char *data = (char*)strcat(datachar1, datachar2) ;
This isn't safe. datachar1 is a character, not a null-terminated string.
> char *Rxbuffer = NULL ;
You shouldn't be writing to this. It's a null pointer, not a space for data. Maybe you want char Rxbuffer[32];
> Any tips ?
Can you see the transmission on the line? Check that the PA10 pin is set to the correct mode. Could also be that the message is sent before the board is in HAL_UART_Receive.
2020-06-02 07:07 AM
First of all, thank you for your reply and sorry for my beginner questions..
> char *data = (char*)strcat(datachar1, datachar2) ;
This isn't safe. datachar1 is a character, not a null-terminated string.
> Indeed, i just want to send hexadecimal values throught UART using HAL_UART_Transmit and this is only way i found for it. Maybe you would have a better solution?
> char *Rxbuffer = NULL ;
You shouldn't be writing to this. It's a null pointer, not a space for data. Maybe you want char Rxbuffer[32];
I thought this was the way to initialize pointer to a string with no specific value in it.
I guess that the following declaration/initialisation is more appropriate:
char RxBuffer[6] ;
char *ptRxBuffer = &RxBuffer[6] ;
.. because it now works !
Thank you TDK
2020-06-03 02:02 AM
Hello all, hello @TDK ,
I can send and receive if i dont change the data content from the initialisation..
I have tried different ways to change the value of data to change the command sent but, i do not succeed at all.
char data[2] ;
char *ptdata = &data[2] ;
data[1] = 0x81 ;
data[2] = 0x17 ;
HAL_UART_Transmit(&huart2, (uint8_t*)ptdata, sizeof(data), 0xFFFF);
But when i read what i have received on a terminal, i see 68 20 values in hexa, which is not what i have sent to uart.
I have tried a cast conversion, this way:
data [1] = (char)0x81
but it changes nothing..
I have also tried to put directly the valued in a table of int because i dont really think it is necessary to put as argument a point of char of the HAL_UART_transmit function..
int data[2] = {0x81,0x17} ;
int *ptdata = &data[2] ;
HAL_UART_Transmit(&huart2, (uint8_t*)ptdata, sizeof(data), 0xFFFF);
and i get other printed hexadcimal values : 28 and 14
Is there any way to send several hexadecimal values throught HAL_UART_Transmit ?
Thank you in advance
2020-06-03 05:46 AM
char RxBuffer[6] ;
char *ptRxBuffer = &RxBuffer[6] ;
ptRxBuffer now points to the byte after the end of RxBuffer. Not what you want. You want ptRxBuffer = RxBuffer. No need to create another variable.
int data[2] = {0x81,0x17} ;
int *ptdata = &data[2] ;
ptdata points to the byte at data[2], which is the byte after 0x17, past the end of your array. Again, what you want is ptdata = data, so no need for another variable.
2020-06-03 06:46 AM
And again... This has nothing to do with HAL, STM32 or ST. It is the lack of basic understanding of C and programming in general. This is why one has to first learn the basics of programming and C language and only then advance to more complex projects.
2020-06-03 06:56 AM
@Piranha yes, i totally agree but i have now understood my mistakes.
So thank you for your time @TDK