2024-04-16 08:46 AM
Hello,
I've been doing a small project lately and ran into a problem in one part. I have two STM32 microcontrollers and I want to communicate both of them with eachother over UART. One of them is designed to send a fixed array of 6 characters
uint8_t k[6]={3,7,9,12,16,19};
The other microcontroller is simply to receive it. When I send a single byte over the UART it all works without a problem. When I want to send the whole array it only fills the first element.This first element is filled with random numbers from this sent array.
I know for sure there is no problem on the sending side. I checked it on an oscilloscope. The problem lies in the receiving side.
I suspect that the problem lies in the timing, but I don't know how to fix it
In both microcontrollers the baud rate is the same – 9600. The data frame is also the same 8N1.
RX device code
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t buff[6];
/* USER CODE END 0 */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_UART_Receive(&huart2, buff, sizeof(buff), 10000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
TX device code
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t k[6]={3,7,9,12,16,19};
/* USER CODE END 0 */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_UART_Transmit(&huart2, k, sizeof(k), 10);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
Solved! Go to Solution.
2024-04-17 09:54 AM
@Raf_al wrote:Even when I start transmission after 10 minutes after power up it fills only first element of the array.
Again, how do you start that transmission?
2024-04-16 09:22 AM
Perhaps the issue is not with the code you're choosing to share?
Make sure boards have a common ground, and the RX/TX pair cross over when connecting from one to the other.
Settings could be wrong, clocks could be wrong.
You ignore any error returned by the routine. Watch for framing, noise and similar errors.
Using HSI or HSE clocks? Is HSE_VALUE correct for your board?
Send 0x55 'U' patterns from each, scope and check the bit timing is consistent with 9600 baud
2024-04-16 09:30 AM - edited 2024-04-16 09:34 AM
@Raf_al wrote:One of them is designed to send a fixed array of 6 characters
uint8_t k[6]={3,7,9,12,16,19}
What do you mean by "characters" there?
That array isn't going to give you human-readable ASCII text characters!
If you made them actual characters, you could test your code against a PC terminal; eg,
uint8_t k[6]={ '3', '7', '9', 'A', 'B', 'C' };
Please use this button to properly post source code:
@Raf_al wrote:it only fills the first element.This first element is filled with random numbers from this sent array.
How are you starting the transmission?
Beware that it's common to get a "junk" character or two when a microcontroller first starts (from reset or power-up) - dues to glitches as the pins go from their default state to their configured states.
As @Tesla DeLorean suggests, such glitches could well give you Framing errors, etc, which would mess up the reception ...
2024-04-17 09:25 AM
Boards have crossed RX and TX. In both STM I used HSI and for both microcontrolers clock settings are the same.
I checked the timing for both microcontrolers (I send 0x55 to have this rectangular transmision shape) and bit duration is 104us so it seems like it is ok.
Also I checked what this HAL_UART_Transmit function returns and sometimes it returns HAL_TIMEOUT and sometimes HAL_OK
Grounds are OK. I power supply both microcontrollers from the same source.
2024-04-17 09:29 AM
That's my mistake. I mean integers not characters. Even when I start transmission after 10 minutes after power up it fills only first element of the array.
2024-04-17 09:54 AM
@Raf_al wrote:Even when I start transmission after 10 minutes after power up it fills only first element of the array.
Again, how do you start that transmission?
2024-04-17 10:21 AM
Basically there is no start for the transmission. TX device is sending this array all the time and RX device is trying to catch this.
So if I understand this correctly. I need to "align" both devices to start transmit and receive at the same time?
2024-04-17 10:26 AM
@Raf_al wrote:Basically there is no start for the transmission.
Now I'm confused!
So what did you mean by, "when I start transmission after 10 minutes after power up" ?
@Raf_al wrote:So if I understand this correctly. I need to "align" both devices to start transmit and receive at the same time?
Well, your receiver code is expecting to receive exactly 6 bytes - so, unless it's ready when the transmitter starts transmitting, it's going to miss some or all of the transmission.
2024-04-17 10:40 AM
Always check the return codes from both ..._Transmit() and ..._Receive().
I'd suggest adding a delay to the transmitter so it only pops off the 6 bytes once every 3 seconds or so. A continuous stream of bytes really does you no good at this point.
2024-04-17 10:55 AM
@Raf_al wrote:So if I understand this correctly. I need to "align" both devices to start transmit and receive at the same time?
Well, your receiver code is expecting to receive exactly 6 bytes - so, unless it's ready when the transmitter starts transmitting, it's going to miss some or all of the transmission.
OK, tomorrow I will do test, beacuse now I dont have access to those boards. First I will power up receiver and it will be waiting to receive 6 bytes (I will change timeout in receive function to HAL_MAX_DELAY). Then I will change code on TX side a little bit, and after pushing some button I will send those 6 bytes.
@Andrew Neil wrote:
@Raf_al wrote:Basically there is no start for the transmission.
Now I'm confused!
So what did you mean by, "when I start transmission after 10 minutes after power up" ?
Better not to admit it :face_with_tears_of_joy::face_with_tears_of_joy: