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

Page: < 12 Showing page 2 of 2
Author
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/08 22:47:52 (permalink)
0
"buf_ptr==" is not working better, i'm still not able to limit the table contents and avoid overloads.  [8D]

This code bellow works perfectly with Pic and my laptop connected to hyperterminal if string received is less than 32 characters .
I also added "LF" character detection on it :



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

/*     // This is commented out because it does not work :
        if (buf_ptr==64)
        {
        buffer_uart[64] = '\0';
        got_char=TRUE;
        }
*/

    buf_ptr++;
   }

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


But there is a problem i can't explain :

If i connect my GSM Modem to the Pic, the captured response is not readable even GSM modem is configured at the same speed as PIC ( 9600 / 8 / N / 1 )

From hyperterminal if send : "AT" command to the modem it responds :OK + characters "CR" and also "LF"
If i connect it to the Pic, and send AT the reponse is displayed as a non readable character ( small box with FF FD inside )

Do you have an idea what shoud get this problem ?


#21
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/09 02:18:24 (permalink)
0
ok Smile we got something at least!

1) how large is the buffer? it used to be 20 chars in your first code... then you can't check for 32 or 64, you would overflow in RAM...

1bis) be careful with that double-check for CR and LF: one of the 2 will come first, you will get out of the loop BUT you'll get a LF or CR at the beginning on the next...

2) I'm not sure. Does it work when you send simple fixed chars from the PIC to the PC?

GENOVA :D :D ! GODO
#22
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/11 05:50:58 (permalink)
0
My buffer is size 32.

Now, i added i can check how many bytes we received and close the buffer when it reaches 32.  This is working fine now. Smile

The GSM Modem i'm using is send its responds and finish them with CR + LF
I've changed my code to detect CR character and replace it by star character '*'
Once LF character is been received, it closes the buffer with '\0'

I'm also using a second buffer to avoid having the first one displayed while bytes are coming in.
When LF character is been detected it generates the string to be displayed. ( capture_buffer )

I think i'm almost done however 2 problems remains :

When i connect the Pic board to hyperterminal and enter bytes from my laptop keyboard, the bytes are captured and string is displayed properly. grin
If i connect the GSM Modem (Tellit GM862 ) to the Pic board and send commands , the GSM modem replies are not captured, i only get displayed a string with a single character that is the star '*'  

Normaly, when Pic sends : AT and (CR)
Modem should respond : OK (CR) (LF)
At this time i'm only able to display '*'  it looks that OK is NOT read, only CR ( replaced by * ) and LF looks to be detected.
I don't know why ...  sad

As someone experienced trouble to receive responses from Tellit GSM Modem to Pic ?

Maybe, a Synchronous or Asynchonous UART / USART wrong settings in my code  ?

I hope someone could help me .


Here is my last tested code :

unsigned char buffer_uart[32] = "-";   
unsigned char capture_uart[32] = "-";
static int buf_ptr;
static char got_char;

...

placed into infinite loop :
   if (DataRdyUART())        // Check if datas are ready to be read.   
    {
         buffer_uart[buf_ptr] = ReadUART();     // Stores byte from uart in the buffer
                       
         if (buffer_uart[buf_ptr]== '\r')      // CR character received ?
         {
          buffer_uart[buf_ptr] = '*';        // Replace it with * character and ends the buffer string
         }
 
         if (buffer_uart[buf_ptr]== '\n')      // LF character received ?
         {
         buffer_uart[buf_ptr] = '\0';          // Replace it with \0 character and ends the buffer string
         got_char=TRUE;
         }

          // Overload check : If string is > 32 bytes long then ends it with '\0'
         if (buf_ptr==31)           
         {
         buffer_uart[31] = '\0';
         got_char=TRUE;
         }
 
     buf_ptr++;
    }
   
 if(got_char)                                  // If buffer ready to be used : do something :
 {
 buf_ptr=0;
 got_char=0;
 capture_uart[0]='\0';                             // clear old displayed datas
 strcpy (capture_uart,buffer_uart);        // copy current buffer to buffer to be displayed
 buffer_uart[0]='\0';                              // clear current buffer and be ready for next incoming string
 }


 


post edited by lcoulon - 2010/07/11 06:25:15
#23
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/11 14:43:35 (permalink)
0
Ok for the 1st part.

As for the second, are we sure that the GSM module uses "real" RS232 levels, and not TTL? I've not used that one, I used only Simcom ones

GENOVA :D :D ! GODO
#24
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/11 22:05:39 (permalink)
0
When i'm using a laptop connected to GSM Module, I do a level conversion with MAX3232.

When PIC is connected to the GSM Module, i use a bi-directionnal TTL level conversion 2.8V <-> 3.3V with (MAX3002)

GSM Module sends and receives 2.8V levels.
Pic sends and receives 3.3V levels.



#25
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/15 01:36:03 (permalink)
0
I see.
I wonder if that TTL converter is by chance doing inversion too or not

GENOVA :D :D ! GODO
#26
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/15 02:51:59 (permalink)
0
No, MAX3002 do not invert signals .

I've done further tests and found out that only CR and LF characters are captured when the GSM modem sends its response to the PIC.

I don't know why only CR and LF, but it is the case.

I've done this smaller code for tests purposes it should light a LED when a characters is found, this code does not take care about expected character position in the string.

If i enter from hyperterminal the following letters turn by turn :
<T><E><S><T><O>K<>
then,  the LED lights one time very quickly when "O" character is just typed - in.

This is normal and i'm happy with that.
The led is immediately turned off due to DataRdyUART returned to "0" state.


Now, let's use the same test code and connect the GSM modem to the Pic board.

I send out AT command to the modem, it responds to the PIC :
<CR><LF><O><K><CR><LF>

Then, no LED lights and i don't know why !  sad

If i modify my test code in order to detect <CR> character instead of <O> character, then LED lights as expected , this is amazing.


Should it be possible that Ethernet stack is not able to run quickly enough the test code from the infinite loop and loose the received characters from the modem.

I tried to transmit at 9600 Bps and 4800 Bps but problem stills the same.

I would say no, because it can detect <CR> or <LF> characters ....

Any help would be much appreciated.

 if (!DataRdyUART())         // If UART port is not receiving any data => All Leds off
        LED1_IO = 0;
        LED2_IO = 0;


while(DataRdyUART())        // During DataRdUART flag is on, read datas from UART
    {
   
    LED1_IO = 1;            // Lights LED1 when datas are ready to be read from UART
    buf_ptr=ReadUART();
   
               
        if(buf_ptr=='O')
        {       
        LED2_IO = 1;        // "O" character received , lights LED2
        break;
        }

    buf_ptr++;
    }





#27
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/18 02:32:06 (permalink)
0
Well, sure the ethernet stack might "hang" for some while, I'd say some 10s of mSec and this could cause overflow and loss of incoming UART characters.

If that's the case, you'll have to use the Interrupt-driven, Circular buffer routines for UART. It has been explained many times on the forum.

GENOVA :D :D ! GODO
#28
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/19 02:41:26 (permalink)
0
If ethernet stack is set to store in priority bytes received to the UART and after runs the rest of the main loop, then i should not loose incomming characters.

I tried to set up interrupt for Pic UART, the code compiles properly but the pic freezes when a bytes is been received in UART Rx pin, can you tell me what i've done wrong please ?

/******************************************************************************
* Setup interrupt handling for PIC18 Devices                                 *
*                                                                            *
* NOTE: Several PICs, including the PIC18F4620 revision A3 have a RETFIE     *
* FAST/MOVFF bug. The interruptlow keyword is used to work around the bug    *
* when using the C18 compiler                                                *
******************************************************************************/
#if defined(__18CXX)

#pragma interruptlow LowISR
void LowISR(void)
{
    TickUpdate();
}

#pragma code lowVector=0x18



void LowVector(void)
{
    _asm goto LowISR _endasm
}
#pragma code  // Return to default code section


//  Added for UART interrupt handling :
//===================================================================================

void InterruptHandlerHigh (void)
{
unsigned int uart_rx;
unsigned char buffer_uart[35];

if(PIR1bits.RCIF)
  {
  LED1_IO = 1;    // lights a LED when bytes is coming-in in UART receive pin
   }

// Clear the interrupt flag /
  PIR1bits.RC1IF =0;
  LED1_IO = 0;
}


void InterruptVectorHigh (void)
{
  _asm goto InterruptHandlerHigh _endasm
}
#pragma code // Return to default code section
//===================================================================================



...  ...



    // Initialize serial RS232 Interface
#if defined(__C30__)
    UART_BRG = SPBRG_VAL;         // Set baud rate and enable UART serial
    UART_MODE = 0x8000;           // RS232 interface
    UART_STA = 0x0400;
    __C30_UART = UART_NO;
#else
    UART_TXSTA = 0b00100000;        // Set baud rate and enable UART serial
    UART_RCSTA = 0b10010000;        // RS232 interface

#if defined(USART_USE_BRGH_HIGH)
    UART_TXSTAbits.BRGH = 1;
#endif

    UART_SPBRG = SPBRG_VAL;
#endif     



  
    ADCON0 = INIT_ADCON0;      // Set up analog features of PORTA
    ADCON1 = INIT_ADCON1;

#if defined(ADCON2) && defined(INIT_ADCON2)
    ADCON2 = INIT_ADCON2;
#endif

//    PIR1bits.ADIF = 0;       // Enable ADC interrupt
//    PIE1bits.ADIE = 1;

    INTCON2bits.RBPU = 0;      // Enable internal PORTB pull-ups

/* Make receive interrupt high priority */
    RCONbits.IPEN = 1;         // Enable Interrupts

/* Enable all high priority interrupts */
    INTCONbits.GIEH = 1;
    INTCONbits.GIEL = 1;

//  Added for UART interrupt handling :
//===================================================================================
  /* Make receive interrupt high priority */
  IPR1bits.RCIP = 1;

/*Enable USART Receive interrupt*/
PIE1bits.RC1IE=1;
//===================================================================================  

post edited by lcoulon - 2010/07/20 00:19:36
#29
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/23 00:30:48 (permalink)
0
Thanks, all is working fine now :

But i finally had to place the UART read loop into an UART interrupt,

Reading incoming bytes from main infinite loop is getting some characters to be lost / not read properly.

In fact, the ethernet stack while(1) loop is not executed quick enough to have an entire string received properly from UART even i'm using a low UART speed : 9600 Bps.

An interrupt solved that problem.

I'm now able to capture and display UART receptions on my web page. 

Thank you very much for your help,
#30
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2010/07/24 01:24:25 (permalink)
0
all right!

GENOVA :D :D ! GODO
#31
al_noor
Starting Member
  • Total Posts : 76
  • Reward points : 0
  • Joined: 2016/12/24 23:17:21
  • Location: 0
  • Status: offline
Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2018/01/07 05:08:35 (permalink)
0
Hello guys,
 
I have also facing the same issue and I have been discussing with the below link
 
http://www.microchip.com/forums/m1032059-p2.aspx
 
Could anyone help to resolve this?
#32
al_noor
Starting Member
  • Total Posts : 76
  • Reward points : 0
  • Joined: 2016/12/24 23:17:21
  • Location: 0
  • Status: offline
Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2018/01/08 09:55:05 (permalink)
0
HEllo guys,
 
Could anyone help me to this issue?
Many thanks in advance!!
#33
DavidBLit
Super Member
  • Total Posts : 1571
  • Reward points : 0
  • Joined: 2012/02/18 13:08:48
  • Location: The Land of Confusion
  • Status: offline
Re:Receive and display UART frames (AT responses from GSM Modem) on Pic UART 2018/01/08 12:50:35 (permalink)
0
No, don't hijack threads.

Yeah, "//Code and stuff".
#34
Page: < 12 Showing page 2 of 2
Jump to:
© 2018 APG vNext Commercial Version 4.5