How to send a String from USART and show it in a TextViewer in the GUI?

1,330 views
asked Sep 13, 2016 in System Integration by eghbalmohsen
Hi.

I want to send a string from usart and show it in the GUI. what should I do? I need some help about USART. I tried DeviceIntegration example and I found out how to  send a string to usart. But I want to send a string from USART to the GUI.

Best Regards

Mohsen Eghbal

1 Answer

–1 vote
answered Sep 17, 2016 by Manfred Schweyer

Dear Mohsen Eghbal,

the DeviceIntegration example is good starting point - so let us enhance this project in order to receive and display charcters from a terminal via USART. Please follow these steps:

Step 1: Preparing the GUI application

Open the class 'Application::Application' within the example 'DeviceIntegration' and add a simple text view (for the description) and a Label to display the string. Maybe you need to rearrange other components.


 

Step2: Adding a new interface to the device class

Open the class 'Application::DeviceClass' and add a new property from the Gallery folder 'Device'. Rename the property to 'TerminalString' and choose the type 'string'. The properties OnSet method is not necessary and can be deleted. Rename the update method to 'UpdateTerminalString' and change its parameter to type 'char'. The result should look like this:


 

The implementation of 'UpdateTerminalString( char aChar )' is (just for demo purposes) quite simple:

/* keep the last 9 characters of the string and add the new one */
pure TerminalString = TerminalString.right( 9 ) + aChar;

/* notify all associated property observers... */ 
notifyobservers ^TerminalString;

This means: The property 'TerminalString' represents a string that contains the last 10 characters received from the device driver (which are received via USART from a connected terminal).
The method 'UpdateTerminalString', is called by the device driver to notify the GUI application about the last received character. As a result, all observers of this property get notified.

Step 3: Connecting the GUI application with the new device interface

Each time a new character is received, all observers will be notified. Let's add a property observer to update the label of the GUI: Open again the class 'Application::Application' in order to add a 'Property Observer' from the Gallery folder 'Device'. Rename the property observer to 'TerminalObserver' and the slot method to 'onTerminalEvent'.

Select the property observer and set its property 'Outlet' to '^Application::Device.TerminalString' and ensure that its property 'OnEvent' will refer to 'onTerminalEvent'. This means: Each time the device class updates the received string, the method onTerminalEvent() will be called. The implementation of this slot method is quite simple:

/* just assign the new string to the text label... */
TerminalLabel.String = Application::Device.TerminalString;

The GUI application is now ready - just generate the code.

Step 4: Adaptation of the Device Driver to receive the character from USART

The function DeviceDriver_ProcessData() is the right location to receive one character from the USART and to inform the device class about the new data. Just add the following C-code to this function:

  /* When a character is received from the serial terminal, call the method 
     'UpdateTerminalString' of the device class 'DeviceClass' within the unit
     'Application' and provide the character */
  ch = EwBspGetCharacter();
  if (( ch >= 'A' ) && ( ch <= 'Z' ) || ( ch >= 'a' ) && ( ch <= 'z' ) 
    || ( ch >= '0' ) && ( ch <= '9' ) || ( ch == ' ' ))
  {  
    ApplicationDeviceClass__UpdateTerminalString( DeviceObject, (XChar)ch );
    needUpdate = 1;
  }

Step 5: Let's see the result...

Compile and download the sample to your hardware, connect a terminal and type 'Hello' - then you should see the following result:

 

commented Sep 12 by DRGDHORLER
Manfred, thanks for your quick response, you were correct Manfred :)

However, on trying to send messages only two characters appear, a zero and the the first letter of the message e.g. if I transmit "test", the screen shows 0t, no other messages or characters get through, yet the other widgets work correctly. This was observed and reported without as much detail by Elek...any suggestions please?

Thnx
commented Sep 12 by DRGDHORLER

Some supplementary detail on this issue.

#ifdef _ApplicationDeviceClass_

/* When a character is received from the serial terminal, call the method

     'UpdateTerminalString' of the device class 'DeviceClass' within the unit

     'Application' and provide the character */

      char ch = EwBspGetCharacter();

   if (( ch >= 'A' ) && ( ch <= 'Z' ) || ( ch >= 'a' ) && ( ch <= 'z' )

    || ( ch >= '0' ) && ( ch <= '9' ) || ( ch == ' ' ))

  { 

    ApplicationDeviceClass__UpdateTerminalString( DeviceObject, (XChar)ch );

    needUpdate = 1;

  }

 

Note the forum suggestions has the “char” missing for the line   char ch = EwBspGetCharacter();

Without char, does not compile, with char compiles with following warning….is this significant?

-------------------------------------------------
Creating object and binary directories
-------------------------------------------------
Compiling ../../Source/DeviceDriver.c
../../Source/DeviceDriver.c: In function 'DeviceDriver_ProcessData':
../../Source/DeviceDriver.c:265:14: warning: implicit declaration of function 'EwBspGetCharacter'; did you mean 'EwGetCharLower'? [-Wimplicit-function-declaration]
    char ch = EwBspGetCharacter();
              ^~~~~~~~~~~~~~~~~
              EwGetCharLower
../../Source/DeviceDriver.c:266:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
    if (( ch >= 'A' ) && ( ch <= 'Z' ) || ( ch >= 'a' ) && ( ch <= 'z' )
        ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
../../Source/DeviceDriver.c:267:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
     || ( ch >= '0' ) && ( ch <= '9' ) || ( ch == ' ' ))
        ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
Linking EmbeddedWizard-STM32F746-Discovery

Program runs in all respects except that:

 If I transmit “querty” the textlabel shows “0q” and then hangs, no other messages get through, though the rest of the GUI works perfectly.

commented Sep 16 by Fridolin

Hi GDHORLER,

 

I have tried the example in this thread right now and it works with my STM32F746G Discovery.

 

Can you make sure that you have added ' #include "ew_bsp_serial.h" ' in your ' DeviceDriver.c '. If that is missing you'll get the warnings.

Could you please try that first and then input something via terminal again?

 

As Manfred already mentioned, can you also verify that the 'Terminal String' has the correct datata type?

 

Greetings

Fridolin

commented Sep 16 by DRGDHORLER

Fridolin, thanks for your suggestion.

The include does indeed remove the warnings.

However, I'm still getting the same reception problem  i.e. send Test, receive 0T ?

I believe the "String" setting is correct, see below

commented Sep 16 by Fridolin

Hi GDHORLER,

 

another idea what the problem could be, is the terminal application and the use of it.

For making sure that this is not the problem, can you please try TeraTerm as terminal application?

Did you enter the text character by character or did you copy the whole text with Ctrl+V into the terminal?

 

Can you also verfiy that your MCU really receives all characters by adding an 'EwPrint()' statement, which traces the received character:

  char ch = EwBspGetCharacter();
  if (( ch >= 'A' ) && ( ch <= 'Z' ) || ( ch >= 'a' ) && ( ch <= 'z' )
    || ( ch >= '0' ) && ( ch <= '9' ) || ( ch == ' ' ))
  {
	EwPrint("\n Received character = %c", ch);
    ApplicationDeviceClass__UpdateTerminalString( DeviceObject, (XChar)ch );
    needUpdate = 1;
  }

 

Then you should see the character that you input on you keyboard also in you terminal application, like that:

 

Greetings

Fridolin

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...