CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Receive SMS with Picdem.net 2 and a gsmModem via RS232
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jojo337



Joined: 05 Apr 2013
Posts: 8

View user's profile Send private message

Receive SMS with Picdem.net 2 and a gsmModem via RS232
PostPosted: Fri Apr 05, 2013 3:19 am     Reply with quote

Hello all ! (Sorry for my mistakes, I'm french)

I actually have a Development card Picdem.net2 with a Pic18f97j60. And my program for compilation is PCW, CCS C compiler.

I'm a beginner in coding.

So I got a Telit gt864-quad modem GSM who works fine. I know a bit the AT commands. I can send sms with PIC, this is working. But I don't know how to got the answer of the command, my code doesn't work for this, so I think I can't read SMSs too.

I use printf("...") to send commands.
I use printf(lcd_putc, "...") to show things on the LCD.

For example, when I send the command AT+CPIN=0000, there is an echo, and it says OK with \r\n at the end (0d 0a in hexa) when I try it with Hyperterminal.
But with my code, i obtain "AT" on the lcd, and the code stop, don't continue.

I'm not good at coding, can you help me ? Here is the code of my program :

Here, I tried to put on the LCD the result of sending the first command.

Code:
#include "C:\Documents and Settings\Administrateur\Mes documents\testSMS\main.h"
#include <LCD.C>
#include <PCF8570.C>

void main()
{
   char test[50];
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_spi2(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_4(T4_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   lcd_init();
   
    while (1)         // Boucle infinie
   {
   if (input(pin_B0)== false)//Appui bouton 0
   {
   int i;
   printf("AT+CPIN=5635\r\n");
   for(i=0; i<50; i++)
   {
   test[i] = getch();
   printf(lcd_putc, "%c" test[i]);
   }

   output_high(PIN_J7);
   //printf(lcd_putc,"Activation PIN");
   delay_ms(10000);
   
   fprintf("AT+CMEE=2\r\n"); //mode erreurs text
   lcd_init();
   printf(lcd_putc,"Chargement");
   output_high(PIN_J6);
   delay_ms(5000);
   
   printf("AT+CMGF=1\r\n");
   lcd_init();
   printf(lcd_putc,"SMS mode text");
   output_high(PIN_J5);
   delay_ms(5000);
   
   printf("AT+CMGS=+336********\r");
   lcd_init();
   printf(lcd_putc,"Ecriture numero");
   output_high(PIN_J4);
   delay_ms(5000);
   
   printf("Ca marche");
   putchar(0x1a); //CTRL+Z ascii
   printf("\r\n");
   lcd_init();
   printf(lcd_putc,"Ecriture message");
   output_high(PIN_J3);
   delay_ms(5000);
   printf(lcd_putc,"\nMessage envoye"); //send
   output_j(0xFF);delay_ms(500);output_j(0x00);delay_ms(500);output_j(0xFF);delay_ms(500);output_j(0x00);delay_ms(500);
   output_j(0xFF);delay_ms(500);output_j(0x00);delay_ms(500);output_j(0xFF);delay_ms(500);output_j(0x00);
   lcd_init();
}
}
}


Also, my cable is a nullmodem, I weld it myself and it works. Pin 2 and 3 inverted between the two connectors, and ground at pin5.

Thanks for reading.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 3:49 am     Reply with quote

Some comments first:

setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);

Hopefully you are not using the ADC. If so, either don't call setup_adc, or use:

setup_adc(ADC_OFF);

The maximum CPU clock speed supported by your setting is 2.86MHz.....

setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);

Wrong.

setup_spi(FALSE);
setup_spi2(FALSE);

you are enabling the spi, and saying not to use slave select.....

Then the key thing is to remember that your code needs to be receiving data from the serial whenever it arrives. You can't sit in a delay, then go and see if anything has come, since it will have been lost (except for a couple of characters in the hardware buffer). You need to be using code like EX_SISR.C. to receive everything that arrives from the modem, then parse it from the buffer.

Now you don't show your RS232 setup, this _must_ have the keyword ERRORS in it. This _must_ always be present when using the hardware serial, unless your own code, handles errors. Without this, and without an interrupt driven serial receive, two characters will be received, then the UART hardware buffer will overflow. The response to this is to switch off the UART, till the error is cleared. These two characters are the 'AT' you get. Everything after this has been lost.

So:
Add errors.
Use code like EX_SISR, setup a buffer with this perhaps 64 characters long, and then read the data from this.

Best Wishes
jojo337



Joined: 05 Apr 2013
Posts: 8

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 6:35 am     Reply with quote

Thanks for your answer !

So for all the setup_*** lines, it was already on the code at start. I used "PIC Wizard", where we can configure a bit the code.

Here is the main.h :

Code:

#include <18F97J60.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES H4_SW                    //High speed osc with SW enabled 4x PLL
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES PRIMARY                  //Primary clock is system clock when scs=00
#FUSES ECCPE                    //Enhanced CCP PWM outpts multiplexed with RE6 thorugh RE3
#FUSES ETHLED                   //Ethernet LED enabled
#FUSES NOWAIT                   //Wait selections unavailable for Table Reads or Table Writes
#FUSES BW16                     //16-bit external bus mode
#FUSES MCU                      //Microcontroller Mode
#FUSES EASHFT                   //Address shifting enabled
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#use delay(clock=24000000)
#define SRAM_SCL   PIN_C3
#define SRAM_SDA   PIN_C4
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)



I don't understand what is ERRORS. I don't see this in the EX_SISR.c.

But I m reading this code, trying to understand. Because I started recently to developt in school.

Thanks
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 6:47 am     Reply with quote

Hi,

Do this:

Code:

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, Errors)


When you do this, the compiler will automatically add the necessary code to prevent the hardware UART from locking up following an 'overrun' condition. I'm not sure why this isn't the default because you either need to do it this way, or you need to handle UART errors in your code (which no one seems to ever do!)

John
jojo337



Joined: 05 Apr 2013
Posts: 8

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 7:01 am     Reply with quote

The compilator says that there is a warning, that rs232_errors is never used when I compile.

Thanks.
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 7:04 am     Reply with quote

hay John..yup...'defaults'..are based on the programmer's 'typical' use.
Maybe 'way back then' when the compiler was created, programmers took care of 'details' like error checking themselves...
The really bad 'default' is MCHP having I/O pins defaulting to analog and not digital use.
Might be why we're supposeed to read the datasheets and manuals !

cheers
jay
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 7:06 am     Reply with quote

Hi,

That's "normal", you can just ignore the warning.

Just as a friendly suggestion, all of this (GSM projects, UART data reception, 'Errors', etc., etc.) have been covered extensively in the forum. Your standard method for solving a problem should be something like this:

1. Search CCS documentation
2. Search CCS forum
3. Search Internet (Google, etc.)
4. Ask question on forum!

The question you just asked has been asked/answered 100's of times here on the forum!

John
jojo337



Joined: 05 Apr 2013
Posts: 8

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 7:25 am     Reply with quote

I understand. I am searching since some days now, but it wasn't the "same type of coding" (this wasn't CCS I think).

So now I can see all the text in the LCD when I added "Errors" thanks !

But if launch a second time the program, the message is not the same. It is cutted. Because the char test[50] is not resetted ?

Thanks again.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 8:37 am     Reply with quote

Hi,

Who knows? It really depends on what you mean by "launch a second time the program". A more important point is that the hardware is right there in front of you, it's not in front of us. That means you should be trying to troubleshoot and solve the problem yourself, rather than running to the forum to ask a question every time you encounter something unexpected!

You've taken your GSM project a lot further than most here on the forum, so I highly encourage you to keep experimenting and keep learning, and only ask questions here when you absolutely cannot find the answer any other way!

John
jojo337



Joined: 05 Apr 2013
Posts: 8

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 8:53 am     Reply with quote

Ok, I will do this. Thanks.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Fri Apr 05, 2013 12:11 pm     Reply with quote

Hi again,

Another thought! LCD's actually make a fairly poor tool for diagnostics during software development. I prefer to dedicate a pin on my PIC to use for sending serial diagnostic messages to my PC. Connect the pin you use to the serial port of your PC via a MAX232 device. All my boards include a 4 pin header for serial diagnostics (Vcc, TxD, RxD, and Gnd). I have a small interface board that I plug into the header during program development. If you plan to do any embedded development at all, it's worth your while to do something similar! You can setup a 2nd serial port on your PIC, and use 'Streams' to direct serial data appropriately!

John
jojo337



Joined: 05 Apr 2013
Posts: 8

View user's profile Send private message

PostPosted: Mon Apr 08, 2013 1:12 am     Reply with quote

Hi. We don't have a MAX232 device in my school, and the project will be finished in 1 week Sad

Last question : Is it possible to check a received message if it contains a word in the PIC ? I Try to do 'if test != "ERROR"' but didn't worked, the program stops.
I think that it is because it search only if the message is "ERROR", and not if the message contains "ERROR". But I don't find any thing about this in internet. Hae you an Idea ?

Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Mon Apr 08, 2013 4:25 am     Reply with quote

This is basic C.
In C, a string is not a 'type', but an array of characters. Since it is an array, you can't compare it like a number. Look at strcmp, and get yourself a C textbook.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Apr 08, 2013 6:46 am     Reply with quote

Hi,

I've been trying to gently "encourage" you to spend more time researching your questions, and solving problems on your own. If you can't do this, you aren't going to ever be worth much a an "engineer"......

There is a complete program posted in the forum that receives TXT messages, and then sets relays in response. This program was written by forum user 'Gabriel', and can be found in the Code Library. Since you haven't figured out how to search the forums yet, here is the link: http://www.ccsinfo.com/forum/viewtopic.php?t=42527

If I sound frustrated, that's because I am. There is a complete program already available to do what you want, but you could not be bothered to do any research and find it! This is becoming an epidemic lately on the forum with lots of people just wanting "solutions", but not really focused on learning anything.....

John
jojo337



Joined: 05 Apr 2013
Posts: 8

View user's profile Send private message

PostPosted: Mon Apr 08, 2013 7:23 am     Reply with quote

Hi.

I apologize to have bothered you. I don't want the solution and just copy/paste it. I already saw Gabriel's code but because I just started programming, I don't understand some parts of his code, even if there are comments. I try to adapt it to my code, because this was not exactly the same type of coding, not the same writing.

I will watch again this week this code with my friend. Thanks.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group