UART

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
adpac_1560036
Level 4
Level 4
First like received First like given

Hi 

   

What would be the best method to read data on the receiver when 'UART_PutArray' is used on the transmitter?.

   

Thanks in adavnce 

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

What data runs over a connection is defined as "Protocol", so in this configuration it is up to you to have it defined. A good idea would be to send only ASCII characters and have a delimiter at the end of a message. Then you can read at the receiver side byte-by-byte and store it away until the delimiter comes up. Sending messages with a fixed length or sending messages that have the total length included are common solutions too. Nonetheless you will have to read from UART bytewise.

   

 

   

Bob

View solution in original post

0 Likes
13 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

What data runs over a connection is defined as "Protocol", so in this configuration it is up to you to have it defined. A good idea would be to send only ASCII characters and have a delimiter at the end of a message. Then you can read at the receiver side byte-by-byte and store it away until the delimiter comes up. Sending messages with a fixed length or sending messages that have the total length included are common solutions too. Nonetheless you will have to read from UART bytewise.

   

 

   

Bob

0 Likes
adpac_1560036
Level 4
Level 4
First like received First like given

Thanks again Bob,it was very useful.

   

I am transmitting fixed length 8 bit Hex codes. Could receive them in to an array that is used as buffer.

   

I'm really obliged to have you helping me in each step of my learning.!!

0 Likes
Anonymous
Not applicable

Hi Guys, not wanting to hijact the thread but I was going to post a query about the UART RX function.

   

Ive been messing around with the Uart_rx sample code for my psoc 3, it stores the incoming data from the serial port in a variable: char CharBuffer[20];  If you type on the keyboard on hyperterminal the key presses appear on the LCD screen on the PSoC. The characters shift across the screen until the count reaches 16 then it clear the LCD to allow further enteries.

   

I was wondering are all the individual characters from the keyboard stored in the CharBuffer, i wanted to send some predefined information from blueterm on my phone to say turn on or off an led. Can i look at individual indexes of the CharBuffer array i.e check the value of CharArray[1] etc and do something if its a certain value like turn on or off an LED as I mentioned. 

   

thanks

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

As long as the CharBuffer is existing and within the scope you may access every element of it.

   

However, for your case it would be better to accept single characters as commands and immediately act on them.

   

Wait until a character is ready

   

Read Character from UART

   

in a switch-statement act on the character.

   

 

   

Bob

Anonymous
Not applicable

what if i want to send a floating point number i.e 12.5 over UART to the PSoC, Do i need 4 bytes to store that since a float is 32bits?

   

I am familiar with the concept of reading an individual character, I was thinking more along the lines of a case where two floating points which do different things were transmitted to the PSoC and have a method of distinguishing them from each other. So say if I made a windows form application with the case above could I write a character before i send a certain float and another character before i send another value. I was thinking check if received Char is 'A' then if so the next float or 4 bytes are the float number, Or Do you think I am overcomplicating things?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Because the internal representation of floats may be different between the PSoC and your connected device it would be advisable to send printable ASCII codes for the numbers. This of course would need a conversion back to a float representation on the PSoC side as atoi() or sscanf(). As a side-effect you would be able to use a terminal program and key-in the numbers.

   

 

   

Bob

Anonymous
Not applicable

Hi Bob, thanks you for the reply

   

Ive been getting meaningful data from my form application, I just ran the UartRx sample code and sent some data from the form application and it is being correctly displayed on the LCD

   

I was trying to modify the code so that the screen clears and the data is only the most recently sent. The data in the RX sample code seems to be stored as a char string called "char8 ch" the sample code will count the full 16 rows on the screen then send the clear screen command. so if i transmit the number 10, the LCD will increment the position and keep showing 1010101010 until the count 16 is reached.

   

I wanted to modify the sample code to clear the screen on new data and show only the new value received by the UART. so far i have been unsuccessful. so if i had 10 on lcd position 1,0 and updated the value to 11 the 10 would be overwritten.

   

Another query every time i send a new value is the variable 'ch' being flushed and overwritten with the updated data or is is shifting the data in the char8's index? Thanks for your gracious assistance Bob!!

   

 

   

EDIT: the code ive got so far, Ive removed the count display from row 1 as it was not needed

   

        ch = UART_1_GetChar();

   

        /* If byte received */
        if(ch > 0u)
        {
            count++;        
            /* If the count value reaches the count 16 start from first location */
            if(count % LCD_NUM_COLUMNS == 0u)
            {
                pos = 0u; /* resets the count value */
                /* Display will be cleared when reached count value 16 */
                LCD_Char_1_WriteControl(LCD_Char_1_CLEAR_DISPLAY);
            }

   

            LCD_Char_1_Position(0u, pos++);
            LCD_Char_1_PutChar(ch);         /* Print the received character */
      

   

 

   

Interesting function that atoi()

   

Would this be the correct usage for that function, given for example my char8 ch contained "128"

   

int convertedNum;

char8 ch;

   

sprintf(ch, "%i", convertedNum;

LCD_Position(1,0);

LCD_PrintString(ch);

   

Also would i be better using a float as sometimes the value contains a decimal point?  so it'd be float convertedNum; and %f in the sprintf statement.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

sprintf() expects an array of chars large enough to hold the result, not a single character. So you should use

   

char Buffer[17];    //  Line length of LCD + room for a a null byte

   

sprintf(Buffer, "Value =  %d',convertedNum);

   

LCD_PrintString(Buffer);

   

When using floats there might be some linker problems, but I've to admit that I didn't use a PSoC3 for a longer time. Give it a try...

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Ok ive integrated this into the current uart code which gives the code below, However the program should send a value off 255 to the device which it does with the code sample i posted earlier but with the sprintf the value comes up on the LCD as being equal to 12464 for some reason.

   

#include <device.h>
#include <stdio.h>

   

int main()
{   
    char8 ch;       /* Data received from the Serial port */
   

   

char Buffer[17];
 CyGlobalIntEnable; /* Enable all interrupts by the processor. */

   

    LCD_Char_1_Start();
    UART_1_Start();

   

    while(1)
    {
        /* Check the UART status */
        ch = UART_1_GetChar();

   

        /* If byte received */
        if(ch > 0)
        {
         sprintf(Buffer, "Value =  %d",ch);
            LCD_Char_1_Position(0, 1);
           LCD_Char_1_PrintString(Buffer);        

   


        }
    }
}

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

%d format expects an integer, so you should type cast as (int)ch. Not a bad idea to declare ch as unsigned.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

so instead of char8 ch; make it as uint8 ch

   

how do i type cast ch to and int?

   

sorry if the question seems redundant

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Use unsigned char ch;

   

and a type cast is written as I showed in my previous post

   

(unsigned int) ch

   

 

   

Bob

0 Likes
Anonymous
Not applicable

ok,

   

So ive declared char8 ch as unsigned char ch;

   

Ive done the type cast in the sprintf statement. instead of the previous ch, its (unsigned int)ch giving

   

sprintf(Buffer, "Value =  %d",(unsigned int)ch);

0 Likes