cancel
Showing results for 
Search instead for 
Did you mean: 

UART

L C
Associate II
Posted on July 24, 2017 at 08:08

Hi!

I am study UART to communicate with PC.

My target is use PC like a terminal to input AND out message to STM32.

My code is using HAL library.

I want to operate code like below.

**************************************

ch = ''UART test\r\n'';

HAL_UART_Transmit(&huart1,(uint8_t *)&ch, 10, 0xFFFF);

***************************

But appear error when compile.

Please let me know your comment.

BR

0690X00000607foQAA.png
14 REPLIES 14
Posted on July 27, 2017 at 19:12

Hi L C,

About the issue with the transmision and the use of the CubeMX you could check this post:

https://community.st.com/0D70X000006SciuSAC

For uart tx I prefer to use the char array declaration:

uint8_t var_name[] = ''insert you text here'';

That's useful when you have a fixed size text to transmit, because you can use the function sizeof() to determinate the size of your array and in the other hand the array variable is a pointer so you don't have to make any cast in the function, so in the uart transmit function could be:

HAL_UART_Transmit(&huart2, var_name, sizeof(var_name)-1, 20);

the

sizeof(var_name)-1 is because when you create and array on this way its size is the number of chars plus one for a null value in at the end of this, so if you don't want to transmit the null char you have to put there the -1. Otheradvantage to use this form is that if you change the text in your array you don't have to modify the uart transmit function.

Facing the uart receive there is a couple of things to take on count:

  • For a good firmware design byany way the microcontroller should know when the data transmision start and finish, so for example is your are going to receive only chars you should send firsts an especial char as flag, after that all the chars and in the end another or the same especial char, and you should make the algoritm in the microcontroller to capture the text on this way.

  • For the data reception, you have to declare an array with a fix size most of time because you must to have a dedicated memory space to store the data, so you have to declare it too.
  • In other cases the simple way to solve the reception problem is to use fixed size frames, so you always are attempt to receive the same amount of data.
  • And for finish, you have to take on count the returned value of the receive function to ensure if any data have been received, if the function return a HAL_OK the amount of data have been received good, in your example even if you have not received data you transmit it back.

Attached to this post is a code made for and Blue Pill board that have a F103 micro for the IDE SW4STM32, in the same folder is the .ioc file is you want to check the CubeMX configuration.

I'm using the char '\r' as flag to receive frames until 30 chars, you could check the main.c file, I think that the same code will work on your board, here is screenshots of the code:

0690X00000607i4QAA.png

0690X00000607eYQAQ.png

And I test it with Hercules, just include the flags in first and the last place of your frame and its done, I'm using a USB-to-serial converter wired to the board, and the uart is configurated to 115200, parity none, 8bit:

0690X00000607iOQAQ.png

Hope it helps to you!

________________

Attachments :

F103C8_UART.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyE9&d=%2Fa%2F0X0000000b9P%2FdgL6yDhqYU1E_IRoU8BPfUu5GS3ExG1kBqqZ7Y5u.i0&asPdf=false
Posted on July 27, 2017 at 19:31

Hello!!

or this

//♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯

void SendMessage(const char msg[])

{

    unsigned short i=0;

    while (msg[i++]);

   HAL_UART_Transmit(&huart2, msg, i-1, 200);

}

//♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯

SendMessage('hello there\0');

SendMessage(..Sending a CR/LF..\r\n\0');

SendMessage('xxx'); //no need terminating null character by many compilers

Posted on August 29, 2017 at 03:08

Elkin

You code is work fine. 

I can input a message to PC terminal and display it in LCD(stm32).

However I can't to clear the receive buffer after display.

For example, the first message have 10 characters and second message has 3 characters only.

The display always display all of receive buffer.

If the buffer is first time to use, we mag detect the '\0' for ending(it is just my idea only). How do we do in next time(like my example that the message size is bigger than next one.)

I have try to input empty(array_rx[] =''), but error happen.

Can we use backspace function in terminal console to revise typing error?

With your sample, it doesn't allow.

First problem, it seen send out everything at once after key in in PC terminal.

Second problem, the receive buffer will append everything while received.

Please let me know your comment.

Posted on August 29, 2017 at 10:34

Hello!

The general idea was to use NULL terminated strings.

If we put at every string's end, the NULL character (\0) the problem solves.

Observe that while (msg[i++]); searches for NULL character .

'

Can we use backspace function in terminal console to revise typing error?

'

Yes it is posible. I use it.

But have in mind you must have an echo back from the controller(or just from your keyboard) to your terminal. In addition Your terminal must be capable to translate the received backspace character to a real backspace at your screen.

You must examine every character received and if you observe a backspace you must act accordingly

Posted on August 29, 2017 at 16:19

Hi L C,

As

FORTOUNAS.EVANGELOS

‌ point out, you can use the backspace but the PC terminal must be able to receive the command back for the microcontroller. One important question in this point (for me) is: exactly, what do you want to do? and I ask it because I'm not able to understand the flow of your program.

I think that other issue is related with the terminal software that you are using, for example with the hercules you have a button that allows to you to send a fixed size frames with the flags, but there are others that sends the chars of the keys that you press on you keyboard one by one. How you describe the problem, I think that you want to do a kind of shell controlled by the microcontroller, and again it depends of the terminal software that you are using.

About the error of array_rx[]=''; it appears because you are telling to the compiler: create a pointer with the size 0 that points to nothing (because your array have no items), I'm not pretty sure if you can have arrays that can change dinamically its size (maybe with malloc? the experts please correct to me).

So if you could describe the program flows (i.e., first the micro send a text to the console, after that I type a command of non-fixed size in the terminal and thissends one by one the chars that a typed, after I press introand the command is shown in the LCD.... etc) that give us a idea of how to manage the issue, by the way, what kind of lcd are you using, alphanumeric? graphic? color tft? and what to you want to show in it?