• AVR Freaks

Hot!UART receive string !!

Author
hichamrol
Starting Member
  • Total Posts : 84
  • Reward points : 0
  • Joined: 2019/07/10 07:22:53
  • Location: 0
  • Status: offline
2019/10/18 01:05:34 (permalink)
0

UART receive string !!

Hello everyone , 
i am programming my pic24fj1024GB610 , and i faced a problem with receive string from raspberry , but in the other hand , sending string to raspberry is very well , 
so the problem is the pic can only detect the two first chars but for the rest of string , it have someproblem ,
so i wrote two function   so the pic keep reading string until it got a specific char and it  stop reading , 
for configuration i use 8N1 , 115200 bauds 
any help guys : 
void gets_UART1(char *string,char kk) 
{
     unsigned char ee=0,rr=0;
     do
     {
         *(string+ee)= UART1_Receive();
         rr = *(string+ee);
         ee++;
     }
     while((rr!=kk));
}
 unsigned char UART1_Receive(void)
{
     unsigned char cc;
     while(_U1RXIF==0);
     cc = U1RXREG;
     _U1RXIF=0;
     return cc;
}


int main(void)
{

    IEC0bits.U1TXIE = 0;
    IEC0bits.U1RXIE = 0;
    U1MODE = 0x8008;
    U1STA = 0x00;
    U1BRG = 0x08;
    U1MODEbits.UARTEN = 1;
    U1STAbits.UTXEN = 1;
    

char kkkk[8];

    while(1)
    {


        gets_UART1(kkkk,'!');
         if(kkkk[0]=='F' && kkkk[1]=='L' && kkkk[2]=='S' && kkkk[3]=='H')
          {
          LED_D4_LAT=1;
        
           }
       
}
}

#1

16 Replies Related Threads

    ric
    Super Member
    • Total Posts : 24281
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: UART receive string !! 2019/10/18 05:25:15 (permalink)
    5 (1)
    Your problem is that you are using polled receive, so if the Raspberry sends the bytes before you go to fetch them, it will fill up the two locations in the USART itself, then overflow.
    If you always made your code enter the gets_UART1() function before the RBP starts sending, you might get away with it. Otherwise you are going to have to implement interrupt driven receive, and place the received data into a FIFO buffer.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/18 05:43:54 (permalink)
    0
    Plus you don't define a start character.  Or do any buffer overrun checking.
    So if you send "FLSH!" followed by a CR and/or LF you are likely to run into problems.
    Certainly any further attempts to send "FLSH!" will fail, because the CR/LF will be the first characters in the buffer kkkk.
     
    But as ric says, this is almost certainly a hardware buffer overrun issue.  Ideally you need to check for that and handle it.  An overflow will be flagged and typically you must disable and re-enable serial RX to clear it before any more bytes can be received.
     
    And what's with the choice of variable names?
    ee, rr, cc, kkk?
    Do you have a faulty keyboard or perhaps a stutter?
    #3
    hichamrol
    Starting Member
    • Total Posts : 84
    • Reward points : 0
    • Joined: 2019/07/10 07:22:53
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/18 05:45:02 (permalink)
    0
    ric
    Your problem is that you are using polled receive, 

    Hei Ric , thank you for your comnt ; 
    so as i understand from what u said , that i need to create a buffer and stock in it every interrupt receive , and at the end read buffer data !! ? 
    #4
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3315
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/18 06:07:58 (permalink)
    0
    I think there is quite a bit missing.
     
    1. An rx interrupt to handle rx data, not forgetting that the buffer should be inspected again before leaving the rx isr until it is clear.  U1STA,#URXDA
    if that bit is set, there is still data in the rx buffer.
     
    2. Catch uart status errors.
     
    3. What if this is received:  '1'  '2'  '3'  [BACKSPACE] '4' [CR]
    The ISR should detect this and the buffer will contain: '1'  '2'  '4' [CR]
     
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #5
    hichamrol
    Starting Member
    • Total Posts : 84
    • Reward points : 0
    • Joined: 2019/07/10 07:22:53
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/18 08:03:54 (permalink)
    0
    Heii i try to use interrupt service but i got the same problem , when i send "UUU" string  the led 7 toggle that mean 3 interrupt are set but the problem the condition is false !! but when i send Char by char the condition is true and the led 6 toggle , 
    i chose char U cause i can see it  cleary in the oscilloscope 0b01010101 , 
     
     
     
     
     
    void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) // interruption de l'UART
    {


    RXbuff[rxi] = U1RXREG;
    rxi++;
    if(rxi==3)
    {
    rxi=0;
    fullbuff=1;

    }

    IFS0bits.U1RXIF = 0;

    }

     
     
     
    int main(void)
    {

        IEC0bits.U1TXIE = 0;
        IEC0bits.U1RXIE = 0;
        U1MODE = 0x8008;
        U1STA = 0x00;
        U1BRG = 0x08;
        U1MODEbits.UARTEN = 1;
        U1STAbits.UTXEN = 1;
     
     
     
    char RXbuff[3];
    uint8_t rxi=0;
    static bool fullbuff=0;

    while(1)
    {

    if(fullbuff)
            {
                 fullbuff=0;
                 if(RXbuff[0]=='U' && RXbuff[1]=='U' && RXbuff[2]=='U' )
                 {
                     LED_D6_LAT=1-LED_D6_LAT;
                 }
                 else
                 {
                     LED_D7_LAT=1-LED_D7_LAT;
                 }
                 
            }
    }

    }

     
    post edited by hichamrol - 2019/10/18 08:32:07
    #6
    hichamrol
    Starting Member
    • Total Posts : 84
    • Reward points : 0
    • Joined: 2019/07/10 07:22:53
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/18 08:14:20 (permalink)
    0
    here is the string 'UUU' send by rasberry !!! 
     

    Attached Image(s)

    #7
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/18 08:21:35 (permalink)
    0
    Are you debugging the PIC?  Or is it free running?
    #8
    hichamrol
    Starting Member
    • Total Posts : 84
    • Reward points : 0
    • Joined: 2019/07/10 07:22:53
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/18 08:26:26 (permalink)
    0
    its free runing !!
     
    #9
    hichamrol
    Starting Member
    • Total Posts : 84
    • Reward points : 0
    • Joined: 2019/07/10 07:22:53
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/18 08:28:24 (permalink)
    0
    just to let you know ,  when i use a terminal windows to communicate , i never faced that problem ,  even with the first program i posted 
     
    post edited by hichamrol - 2019/10/18 08:30:14
    #10
    1and0
    Access is Denied
    • Total Posts : 9899
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: UART receive string !! 2019/10/18 08:29:33 (permalink)
    5 (1)
    hichamrol
    Heii i try to use interrupt service but i got the same problem , when i send "UUU" string  the led 7 toggle that mean 3 interrupt are set but the problem the condition is false !!

    Because a string is null-terminated; i.e. it has a null character at the end. Your code will overwrite RXbuff[0] with this null character '\0'.
     


    char RXbuff[3];
    uint8_t rxi=0;
    static bool fullbuff=0;


    Put these outside of main().
     


    if(etatbuff)


    I think that should be if(fullbuff).
     


                     LED_D6_LAT=1-LED_D6_LAT;


    Suggest to use
      LED_D6_LAT ^= 1;

    #11
    hichamrol
    Starting Member
    • Total Posts : 84
    • Reward points : 0
    • Joined: 2019/07/10 07:22:53
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/21 00:48:04 (permalink)
    0
    1and0
     the same problem dude , i think that the pic can't read string without delay between every char !!




    #12
    ric
    Super Member
    • Total Posts : 24281
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: UART receive string !! 2019/10/21 01:13:47 (permalink)
    0
    hichamrol
    1and0
     the same problem dude , i think that the pic can't read string without delay between every char !!


    Why are you quoting 1and0 for your own message.
    Of course it can work, if you program it correctly.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    hichamrol
    Starting Member
    • Total Posts : 84
    • Reward points : 0
    • Joined: 2019/07/10 07:22:53
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/21 01:41:44 (permalink)
    0
    ric
    Of course it can work, if you program it correctly.

     i wish.... but it's take me 3 days with no succes , i used the way u told me but without result !
     
     
    #14
    oliverb
    Super Member
    • Total Posts : 227
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/21 02:57:49 (permalink)
    0
    There appear to be two simple problems with the first program. You don't strictly speaking need interrupts unless the PIC is going to be processing (making measurements, controlling something, updating a display) while communicating. To clarify that it is quite possible to code the kind of "respond to a command" program you are making using "soft" emulated serial and not even bothering with the USART at all. Not that I recommend that but I have a MODBUS example that uses emulated serial on RB6,7 so I could use the board's programming connector for TTL serial.
     
    First problem is ee doesn't appear to be limit-checked, so it will happily overrun the buffer. I'd suggest over-sizing the buffer and making it so if it fills then the first character is discarded. Since the "string" isn't null-terminated at that point this is a job for memcpy not strcpy. Alternatively a "circular buffer" could be used.
     
    Second problem is that the program assumes the command will be aligned to the start of the string, but if an extra character was received before the command then it could be in bytes 1-4 instead of 0-3. The test needs to be relative to where the "!" was detected, so your "gets" function needs to return the value of ee so the program can tell where to look.
     
     
     
    post edited by oliverb - 2019/10/21 07:01:46
    #15
    ric
    Super Member
    • Total Posts : 24281
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: UART receive string !! 2019/10/21 04:14:21 (permalink)
    0
    hichamrol
    ...
    i used the way u told me but without result !

    I suspect you are talking about what 1and0 told you, and I also suspect that you have NOT done what he suggested correctly. You didn't show your new code, so we can only guess what you really did.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #16
    hichamrol
    Starting Member
    • Total Posts : 84
    • Reward points : 0
    • Joined: 2019/07/10 07:22:53
    • Location: 0
    • Status: offline
    Re: UART receive string !! 2019/10/21 04:22:08 (permalink)
    0
    Hei ric , here is the new code !!   Interrupt is set when any character is received and transferred from the RSR to the receive buffer


    void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) // interruption de l'UART
    {


    RXbuff[rxi] = U1RXREG;
    rxi++;
    if(rxi==3)
    {
    rxi=0;
    fullbuff=1;

    }

    IFS0bits.U1RXIF = 0;

    }


    int main(void)
    {

        IEC0bits.U1TXIE = 0;
        IEC0bits.U1RXIE = 0;
        U1MODE = 0x8008;
        U1STA = 0x00;
        U1BRG = 0x08;
        IEC0bits.U1RXIE =1;
        IPC2bits.U1RXIP = 2;
        U1MODEbits.UARTEN = 1;
        U1STAbits.UTXEN = 1;
     
     
     
    char RXbuff[3];
    uint8_t rxi=0;
    static bool fullbuff=0;

    while(1)
    {

    if(fullbuff)
            {
                 fullbuff=0;
                 if(RXbuff[0]=='U' && RXbuff[1]=='U' && RXbuff[2]=='U' )
                 {
                     LED_D6_LAT=1-LED_D6_LAT;
                 }
                 else
                 {
                     LED_D7_LAT=1-LED_D7_LAT;
                 }
                 
            }
    }

    }

     
    and here is the string sent by rasberry !! "UUU"
     
    post edited by hichamrol - 2019/10/21 04:36:14

    Attached Image(s)

    #17
    Jump to:
    © 2019 APG vNext Commercial Version 4.5