How uart1_read() works?

Discussion in 'Embedded Systems and Microcontrollers' started by varden, Aug 8, 2016.

  1. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    I don't understand the logic of this command. When we say
    Code (Text):
    1. if(UART1_Data_Ready())
    2. uart_rd=UART1_Read();
    does it store all coming data in uart_rd[0]? Is there any way to store daha not in just one sector but in all array extended?
    I mean not
    Code (Text):
    1. uart_rd[0]= 'data'
    but
    Code (Text):
    1. uart_rd[0]='d';
    2. uart_rd[1]='a';
    3. uart_rd[2]='t';
    4. uart_rd[3]='a';
     
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,964
    387
    Which processor, which compiler?
     
    varden likes this.
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    1,828
    365
    Just individual characters.
     
    varden likes this.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    If uart_rd is truly an array then your statement

    uart_rd =UART_Rd();

    Should throw a compile error. Thay symbol is a pointer and not a variable, so you are attempting to change a pointer, and a pointer to an array it is also a constant pointer that cannot be changed.

    I would guess (not having your library docs) that the read function returns a single character as that is typical, you receive one character at a time. Your job as the programmer is to assemble the characters into someone more meaningful by your understanding of the data being sent, what the actual content is to be expected.

    Sometimes this is as simple as all messages end with a trailing zero character, so you can read characters up until you get that zero and copy each into the array using an index variable.
     
    varden likes this.
  5. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    I use mikro C and pic 18F4550. Actually uart_rd =UART_Rd(); works. I send data with uart. Like words or sentences. And can print them into LCD without any problem. But no matter how I try, I can't get them in array as I told before. Also I am very confused. Please can you give some examples?
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    First, what does your incoming data look like? Does it have a constant length or a zero at the end? How can you tell one chunk of data from the next?
     
    varden likes this.
  7. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,564
    2,379
    A Uart clocks what is usually a maximum 8 bit word into its register a bit at a time at the baud rate selected, sometimes having a word register storage with the next byte ready to clock in,
    Once the buffer is full it has to be read in order for the next word to be clocked in.
    Max.

    ,
     
    varden likes this.
  8. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    I send data with bluetooth module to uart. With bluetooth terminal program, I write anything and send it.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    Literally "anything" is a very tough data set to parse out items that make sense.

    How about you try for something like "I limit strings to X characters because that is the width of my LCD."

    Add in something like "and I always send a trailing zero" and you have the working of a spec you can write code against.
     
    varden likes this.
  10. varden

    Thread Starter New Member

    Jul 11, 2016
    17
    0
    I forget to mention actually I limited my string at 16 and messages never run over 16 character :D But I mean it can be 5 or 10 character.
    I didn't try to send zero character at the end. I thought it adds it by itself automatically. Thanks I will try it.
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    If by "it" you mean your Bluetooth terminal program that would depend on that program, so read the documentation carefully. It may instead be sending a carriage return/line feed combo, or some other symbol.

    If there is an ASCII character you never use, such as an asterisk, you can insert that at the end of the string as you type it, then you look for that to determine the ending.
     
Loading...