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... */
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: