Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

STM32F072B - DISCO (how to send text from UART)

Status
Not open for further replies.

desgin

Full Member level 1
Full Member level 1
Joined
Apr 7, 2017
Messages
96
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
813
Hello All,

I am using STM32F072B - Discovery board.
I am beginner with this board.
Can you please tell me from below code, how to send text from UART1
in Pic18f Micro controller, i was sending this way:
UART2_Write_Text(ch_Data);
but here, by checking reference code - i used
HAL_UART_Transmit(&huart1, (uint8_t*)ch_Data, sizeof(ch_Data), 6);

please help me
Its very urgent!

Code:
//My code------------------------------
char ch_Data[5];
void SystemClock_Config(void);

int main(void)
{
  HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();
  MX_CAN_Init();
  MX_USART1_UART_Init();

    BSP_LED_Init(LED3);
    BSP_LED_Init(LED4);
    BSP_LED_Init(LED5);
    BSP_LED_Init(LED6);

  __HAL_RCC_CLEAR_RESET_FLAGS();

  MX_IWDG_Init();

  while (1)
  {
      
       strcat(ch_Data,"D");
        strcat(ch_Data,"E");
        strcat(ch_Data,"S");
        strcat(ch_Data,"G");
        strcat(ch_Data,"I");
        strcat(ch_Data,"N");
        HAL_UART_Transmit(&huart1, (uint8_t*)ch_Data, sizeof(ch_Data), 6);
        HAL_Delay(100);

       WWDG_Feed();
      
        BSP_LED_Toggle(LED3);           
        HAL_Delay(100);
        BSP_LED_Toggle(LED4);           
        HAL_Delay(100);
  }
}
//-------------------------------------------------------------------------

waiting for reply!
Thanks in advance :)
 
Last edited by a moderator:

Hi,

With PIC you use a (NUL terminated) string. But here you use an array of bytes. Since this is not NULL terminated you need to say how many (sizeof) bytes you want to send.
Just define an array of uint8_t where you store the text.

There are many examples around.

Did you do an internet search about "HAL UART usage", or "HAL UART example" ?

Klaus
 

    desgin

    Points: 2
    Helpful Answer Positive Rating
Hi,

With PIC you use a (NUL terminated) string. But here you use an array of bytes. Since this is not NULL terminated you need to say how many (sizeof) bytes you want to send.
Just define an array of uint8_t where you store the text.

There are many examples around.

Did you do an internet search about "HAL UART usage", or "HAL UART example" ?

Klaus
Thanks for reply!

I tried according to what you said but i am getting 5 warnings for strcat instruction, why ? I am getting Output but why this warning?
and you were saying the same thing ? - Please just check in my below code!
and ya in Output at a same time ABCDE is printing 10 times, it should print once at a time, right ? How to do that ?
Please help me :-(

Code:
  while (1)
  {
        uint8_t buffer[5];
        strcat(buffer,"A");
        strcat(buffer,"B");
        strcat(buffer,"C");
        strcat(buffer,"D");
        strcat(buffer,"E");
        HAL_UART_Transmit(&huart1, buffer, sizeof(buffer), HAL_MAX_DELAY);
        HAL_Delay(100);

       WWDG_Feed();
  }
}

Thanks in advance!
 
Last edited by a moderator:

Hi,

I´m no programmer. But try something like this:

Code:
...
  uint8_t buffer[5];
... 
        buffer[0] = "A";
        buffer[1] = "B";
        buffer[2] = "C";
        buffer[3] = "D";
        buffer[4] = "E";
...
 while (1)
  {
        HAL_UART_Transmit(&huart1, buffer, sizeof(buffer), HAL_MAX_DELAY);
        HAL_Delay(100);
       WWDG_Feed();
  }
}
Thanks in advance!
 

    desgin

    Points: 2
    Helpful Answer Positive Rating
Okay, Sure thanks!
Actually i am trying to Read CAN data from PCAN Explorer and convert into String and send them to Serial Terminal to check what i received from PCAN ?
I am getting confuse in CAN read function. Can you please help me out in this ? so i can proceed for UART.
In PIC micro controller, i was using Mikro C library for CAN Read - Msg_Rcvd = CANRead(&Rx_ID , Rx_Data , &Rx_Data_Len, &Can_Rcv_Flags);
But here, i am using STM32 library but its giving me error!

Please help me, very much urgent! :-(
Thanks in advance!
 

Hi,


Would you be so kind to tell "what" error you get?

Klaus

My CAN Read Function:

Code:
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
  /* Get RX message */
  if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
  {
    /* Reception Error */
    Error_Handler();
  }
   if ((RxHeader.StdId == 0x321) && (RxHeader.IDE == CAN_ID_STD) && (RxHeader.DLC == 8))
   {
                        BSP_LED_ON(LED4);            // steady on means no can tx/rx
                        HAL_Delay(50);
           
    }
}

--START My main code:

Code:
int main(void)
{
  HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();
  MX_CAN_Init();
    BSP_LED_Init(LED3);
    BSP_LED_Init(LED4);

  __HAL_RCC_CLEAR_RESET_FLAGS();

  MX_IWDG_Init();

  while (1)
  {
            WWDG_Feed();
            BSP_LED_On(LED3);            // steady on means no can tx/rx
            HAL_Delay(50);
            HAL_CAN_RxFifo0MsgPendingCallback(&hcan);                 
  }
}
--END My main code:

-- My question is - Why in main function i can not use receive direct function ? as like CAN Transmit ?
-- If i delete HAL_Delay(50) from main - I am unable to read CAN data - I mean in Source code - i used BSP_LED_ON(LED4); which is not getting ON, why ?
-- I want to check received CAN Data how to check ? - For that i have to convert into string and send to SERIAL Terminal, right ?
-- But i do not understand, How to read can 8 byte data and convert into string and send to Terminal by UART ?

Please help me !
Thanks in advance !
 
Last edited by a moderator:

Hi,

a byte is a byte. Receive a byte via CAN and send the same byte via UART. Where is the problem?

A string is a datatype, it is an array of bytes .... the only difference to a common array of bytes is, that a string is NULL terminated.
A string is optimized for text. NULL is no (visible) character, thus it can be (and is) used as the END_OF_STRING delimiter.

In an array of bytes all values from 0 to 255 are allowed, thus no value can be used as delimiter. Thus you need an extra value that tells you the count of valid bytes in the array.

Klaus
 

    desgin

    Points: 2
    Helpful Answer Positive Rating
I am able to Transmit CAN ID with data whatever i received but not able to send by UART !
:-(
 
Last edited:

Hi,

still unclear what the problem is.
You don´t give useful infromations.

Klaus
 

Hi,

still unclear what the problem is.
You don´t give useful infromations.

Klaus

If i am using this: HAL_UART_Transmit_IT(&huart1, RxData, 8);
I am able to see my CAN Data which i am sending from my CAN device at SERIAL Terminal in HEX format but unable to send in ASCII format.
I want to send in ASCII Format, Whatever CAN data i received from my CAN Device.

Code:
int main(void)
{
  HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();
    MX_USART1_UART_Init();
  MX_CAN_Init();

  __HAL_RCC_CLEAR_RESET_FLAGS();

  MX_IWDG_Init();

  while (1)
  {
            WWDG_Feed();
                HAL_Delay(50);
            HAL_CAN_RxFifo0MsgPendingCallback(&hcan);
                WWDG_Feed();
                if (RxHeader.StdId == 0x235)
                {
                      HAL_UART_Transmit_IT(&huart1, RxData, 8);   
              HAL_Delay(1000);                 
                }
                if (RxHeader.StdId == 0x297)
                {       
                        HAL_UART_Transmit_IT(&huart1, RxData, 8);   
                        HAL_Delay(1000);   
                }
  }
}

I tried with sprintf but not working at all
:-(
Please help me!
 
Last edited by a moderator:

Hi,

we still don´t see what you expect and what you see instead.

ASCII is a byte, HEX is a byte.
If you can send a byte, then you can send an ASCII character and then you can olso send an HEX byte.

try to send a single byte in different ways via UART to your Terminal:
* Send "M"
* Send 0x4D
* Send 77
* Send 0b01001101
with all four examples you send identical code via UART. Nothing different. It´s all the same. You need to accept and understand this.

How the Terminal displays the received data is a Terminal setup issue, not a software issue, not a UART issue..

****
Why don´t you
* clearly say what Terminal software you use
* show your Terminal setup
* show Terminal screenshot
* show what you expect
* give examples of what you expect and what you get instead

Klaus
 

    desgin

    Points: 2
    Helpful Answer Positive Rating
Hi,

we still don´t see what you expect and what you see instead.

ASCII is a byte, HEX is a byte.
If you can send a byte, then you can send an ASCII character and then you can olso send an HEX byte.

try to send a single byte in different ways via UART to your Terminal:
* Send "M"
* Send 0x4D
* Send 77
* Send 0b01001101
with all four examples you send identical code via UART. Nothing different. It´s all the same. You need to accept and understand this.

How the Terminal displays the received data is a Terminal setup issue, not a software issue, not a UART issue..

****
Why don´t you
* clearly say what Terminal software you use
* show your Terminal setup
* show Terminal screenshot
* show what you expect
* give examples of what you expect and what you get instead

Klaus
Thanks For reply.

1. I am using Docklight Serial Terminal.
2. I have attached Screenshot for setup
2q.JPG


3. I am getting HEX correct value in HEX format, i want to see in ASCII Format same data, attached screenshot
3q.JPG


4. I am expecting HEX value data in ASCII, see like this
4q.JPG


5. I should get like this, what i am sending from my CAN device to Controller (STM32) i just typed in Edit section but it should come in Communication section, see screenshot


5q.JPG


-- Sending HEX data from CAN Device - checking at PCAN Explorer, See screenshot
sending from CAN Device.JPG


- Thanks in advance
-- Desgin
 

Hi,

you want to transform a byte into a HEX string.

Let´s make an exaple with 2 bytes:
input: 0x12, 0x23
output: "12 23 " = 0x31, 0x32, 0x20, 0x32, 0x33, 0x20"

There are many ways:
* you may do an internet search on your own, like: "how to convert HEX into ASCII"
* you may use library functions like printf / sprintf... for sure you should read the documentation.
* you may writ your own function, like this:

(I´m no C specialist, thus see it as pseudo code:)
Code:
int hex_to_ascii(int c){
        int high;
        int low;
        high = (c >>4);
        low = (c and 0x0F);
        if (high < 10){
             high = high + "0";}
       else {
             high = high - 10 + "A";}
       if (low < 10){
             low = low + "0";}
       else {
             low = low - 10 + "A";}
        UART_sendByte (high);
        UART_sendByte (low);
        UART_sendByte (" ");
return;
This is not an optimized solution. It´s more to show how it works.
0x12 becomes divided in high = 1 and low = 2
now if high < 10 (0..9) one simply adds the vaue for an ASCII "0". ASCII "0" = 0x30. so high = 1 = 0x01 becomes high = 0x31 (the ASCII value for "1")
but if high >= 10 (for hex values 0x0A to 0x0F) then i first subtract 10 to get value 0...5, then I add the value for ASCII "A". = 0x41
example "B" becomes 1 = 0x01, then add "A" and get "B" = 0x42

then I just send a SPACE = " " = 0x20 to divide the (input) hex bytes

Klaus
 

    desgin

    Points: 2
    Helpful Answer Positive Rating
If i am reading 3 -4 CAN ID together, its getting stuck.
How many CAN ID can be read at a time ? by STm32 ?
 

Hi,

It mainly depends on your code, but also on hardware and setup, like baud rate...

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top