Hot!Receive and display UART frames (AT responses from GSM Modem) on Pic UART

Page: 12 > Showing page 1 of 2
Author
lcoulon
Super Member
  • Total Posts : 585
  • Reward points : 0
  • Joined: 2008/07/01 14:26:09
  • Location: France
  • Status: offline
2010/07/05 14:27:15 (permalink)
0

Receive and display UART frames (AT responses from GSM Modem) on Pic UART

Hello

I would like to capture GSM Modem replies sent to the Pic uart and display last uart line received on my web page.

Do to that, i'm using the following function from uart.c driver : getsUART in my infinite loop
this returns to 0x73 variable random characters, also the code can run only if datas are received into the pic uart.

If nothing comes up, then the program stops running and will start again only if datas come up.

Did i missed someone somewhere ?
Maybe getsUART is suitable to capture uart strings ?

I will appreciate any help.


 
char buffer_uart[20];
char buffer_uart[20] = {'/0'};
...

#define VAR_buffer_uart            (0x73)   //  assign value to web page display

...

WORD HTTPGetVar(BYTE var, WORD ref, BYTE* val)
{

    switch(var)
    {
       case VAR_buffer_uart:
      *val = buffer_uart[(BYTE)ref];
      if (buffer_uart[(BYTE)ref] == '\0') {
       return HTTP_END_OF_VAR;
      } else if (buffer_uart[(BYTE)++ref] == '\0') {
      return HTTP_END_OF_VAR;
  }
  return ref

    }

    return HTTP_END_OF_VAR;
}


...

    while(1)
    {
    ...
    getsUART (20,buffer_uart,20);      // captures 20 characters long, place it into buffer_uart char, delay = 20
    ...
    }


post edited by lcoulon - 2010/07/05 14:31:47
#1

33 Replies Related Threads

    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/05 14:31:41 (permalink)
    0
    If I recall correctly, getsUSART will wait for 20 chars (in your case) UNTIL they're received.
    So it's not a good way to do the job, in a cooperative multitasking environment. You should use getcUSART and DataRDYUsart to store one a byte a time.

    forget about me, subhumans. adieu

    if only this planet would detonate...
    #2
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/05 14:41:38 (permalink)
    0
    Do you think getsUSART should better suit my my needs, it uses getcUART

    However i got with that line into my infinite loop , again, it freezes the code :

    getsUSART(buffer_uart,20);

    Can you show me the way to avoid pic to freeze and allow multi-tasking ?




       
    void getsUSART(char *buffer, unsigned char len)
    {
        char i;                    // Length counter
        unsigned char data;

        for(i=0;i<len;i++)         // Only retrieve len characters
        {
            while(!DataRdyUSART());// Wait for data to be received

            data = getcUART();     // Get a character from the USART
                                   // and save in the string
            *buffer = data;
            buffer++;              // Increment the string pointer
        }

    post edited by lcoulon - 2010/07/05 14:52:30
    #3
    bkamen
    Super Member
    • Total Posts : 822
    • Reward points : 0
    • Joined: 2010/01/26 12:39:27
    • Location: Central Illinois, USA
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/05 14:55:31 (permalink)
    0
    You should be using an IRQ based UART receive routine.

    It should clear the UART buffer on any call (in case there's a FIFO and it triggers on say 3 chars received) and put the data someplace else (like a software FIFO or something)

    and then set a flag that data has been received.


    #4
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/05 15:12:10 (permalink)
    0
    oh ... i think it is too complicated for my skills.

    I'll have to simplify my project :

    Will it be easier to wait for expected frames in the UART while program runs other task ?

    I will explain with an example :
    i would like to do something only when UART receives that string : "RING"

    What kind of code should i use to wait for a defined string to come up, if i don't come the program continues to run properly .

    Many thanks for your help,

    #5
    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/06 01:36:43 (permalink)
    0
    Smile yeah, the interrupt thing is the smartest:
    but you can still save each incoming char by NOT placing the above code in a loop, rather calling at each "pass" - while the Stack is still executing. Use a static variable as the buffer pointer: when it reaches 20, you know that the whole string has arrived.

    forget about me, subhumans. adieu

    if only this planet would detonate...
    #6
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/06 10:03:50 (permalink)
    0
    I tried the following but it won't work as expected, would you please tell me what is wrong ?

     
    if (DataRdyUSART())
    {

    for ( i = 0; i < 20; i++ )
           {
              buffer_uart = ReadUART();
           }

    // ending the string with /0 in order to display it on the web page :

    buffer_uart[20]='\0'; 

    }



    The characters looks to be received from uart but the problem is that the string issued from the uart finishes with CR
    CR character immediately erase the captured string and i can't display it.

    Maybe should i store characters until i get "CR"  ?
    if so how should i do that ?

    Many thanks,

    post edited by lcoulon - 2010/07/06 10:11:48
    #7
    trevor
    Super Member
    • Total Posts : 567
    • Reward points : 0
    • Joined: 2010/05/05 21:02:18
    • Location: The LAN Downunder
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/06 14:08:05 (permalink)
    0
    If the CR character is the problem, and your string is a fixed length as your code indicates, then simply overwrite it with a null.


    buffer_uart[19]='\0';

    #8
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/06 14:18:00 (permalink)
    0
    The received string's lenght varies but it is always shorter than 20 characters.

    I tried the following code but it does not work either i don't know why   sad

     
    if (DataRdyUSART())
    {

    for ( i = 0; i < 20; i++ )
           {
              buffer_uart[i] = ReadUART();

                if (buffer_uart[i]==13)   // CR character detected ?
                    {
                     buffer_uart[i]='\0';   // If CR found, replace it with "/0"
                    }

           }

    }

    post edited by lcoulon - 2010/07/06 14:19:08
    #9
    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/06 15:22:44 (permalink)
    0
    you're again in a loop Smile

    And also, you must use DataRDYUSART() to check that a new char is available.

    forget about me, subhumans. adieu

    if only this planet would detonate...
    #10
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/06 22:16:54 (permalink)
    0
    I do not see how i should capture incomming bytes to make a string until CR character is detected without a loop ...

    The GSM Modem is responding to AT commands and sends response back with something like these :

    OK (+CR character)
    ERROR (+CR character)


    I do not see how you want me to proceed to store each char into a string, without a loop, would you please post a code snippet for me ?

    i'm lost.  [8D]


    #11
    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/07 02:56:43 (permalink)
    0
    Smile State Machine.

    You program has a main loop; the main loop does several things, one at a time; among these, you place a snippet of code which:

    checks for DataRDYUSART
    if available, reads the char
    stores the char at  buffer_uart + the STATIC index

    checks if the last char was a CR

    if it was, perform processing, and resets STATIC index to buffer (ready for next time)

    forget about me, subhumans. adieu

    if only this planet would detonate...
    #12
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/07 06:08:36 (permalink)
    0
    Thanks Dario,

    Do you think that code is better ?



    for ( i = 0; i < 20; i++ )                     // Loop to be proceeded 20 times ( buffer_uart is 20 long table )
         {
             if (DataRdyUSART())                 // Check if datas are ready to be read.
             {
                  buffer_uart[i] = ReadUART();     // Store byte from uart in the buffer
          
          
                 if (buffer_uart[i]==13)         // Is stored character CR ?
                     {
                     buffer_uart[i] = '/0';         // Replace it with /0 character and ends the buffer string
                     }
          
             }
         
        // Buffer_uart is now full :
        // Terminates the string to get it displayable on the web page
        buffer_uart[20] = '/0';

        }


    post edited by lcoulon - 2010/07/07 13:09:23
    #13
    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/07 13:48:17 (permalink)
    0
    It's better, but it still "blocks" or rather it still wants to get all of the chars in one single call...

    forget about me, subhumans. adieu

    if only this planet would detonate...
    #14
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/07 13:54:33 (permalink)
    0
    Yes, i tested my code and it only displays last received byte, not the entire string once CR is detected.

    Would you please show me what i should modify to get it working ?

    I will really appreciate it.
    #15
    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/07 14:23:49 (permalink)
    0
    sure, only it's not easy to... describe! (the State Machine in itself I mean).

    Suppose this is your main loop:

    {
    static int buf_ptr;
    static char got_char;

    do_thing1();

    do_thing2();

    //check for chars
            if (DataRdyUSART())                 // Check if datas are ready to be read.
              {
                   buffer_uart[buf_ptr] = ReadUART();     // Store byte from uart in the buffer
           
           
                  if (buffer_uart[buf_ptr]==13)         // Is stored character CR ?
                      {
                      buffer_uart[buf_ptr] = '/0';         // Replace it with /0 character and ends the buffer string
    got_char=TRUE;
                      }
            buf_ptr++;
              }
           
    if(got_char) {
    process_chars();
    got_char=0;
    but_ptr=0;
    }

    }


    post edited by DarioG - 2010/07/07 15:41:52

    forget about me, subhumans. adieu

    if only this planet would detonate...
    #16
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/07 14:36:55 (permalink)
    0
    Thanks , i can see you do not increment "buf_ptr" with a loop.

    How do you fill-in buffer_uart with each byte received ?

    With your method, is there no need to specify how many bytes we want to receive to built the string ?


    post edited by lcoulon - 2010/07/07 14:48:26
    #17
    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/07 15:41:01 (permalink)
    0
    sorry Smile of course it *has* to be incremented; and a sanity check for it maximum value has to be added too (I left this out for next step).

    forget about me, subhumans. adieu

    if only this planet would detonate...
    #18
    lcoulon
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2008/07/01 14:26:09
    • Location: France
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/07 22:40:53 (permalink)
    0
    I tried to limit the lenght of received bytes at max 20 bytes with the following line but i terminates the string at each first character received, so it is not correct, i don't know why :
    if ((buffer_uart[buf_ptr]==13) || (buf_ptr=20))



      
    if (DataRdyUSART())        // Check if datas are ready to be read.
       {
        LED1_IO = 0;
        buffer_uart[buf_ptr] = ReadUART();    // Stores byte from uart in the buffer
          
          
               if (buffer_uart[buf_ptr]==13)     // CR character received ?
            {
            buffer_uart[buf_ptr] = '\0';      // Replace it with \0 character and ends the buffer string
            got_char=TRUE;
            }

       buf_ptr++;
       }


    if(got_char)     // If buffer ready to be used : do something :
    {
    LED1_IO = 1;
    got_char=0;
    buf_ptr=0;
    }

    post edited by lcoulon - 2010/07/08 08:57:02
    #19
    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/08 14:24:15 (permalink)
    0
    buf_ptr=20

    make it == Smile

    forget about me, subhumans. adieu

    if only this planet would detonate...
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2018 APG vNext Commercial Version 4.5