![]() |
|
Tài trợ cho PIC Vietnam |
||||||||
| PIC - Thiết kế và Ứng dụng Ý tưởng cho các sản phẩm sử dụng PIC/dsPIC và các sản phẩm của Microchip |
|
|
Ðiều Chỉnh | Xếp Bài |
|
|
#11 | |
|
Đệ tử 6 túi
|
Trích:
- Luôn quan sát NetLight Led có chớp hay không. - Khi nhấn Power Key, Status Led phải sáng và luôn để ý Led này trong suốt quá trình sử dụng. Vì nếu như Led này tự động tắt, rất có thể nguồn cung cấp không đủ... - puts("at+cmgr=1"); và puts("at+cmgd=1"); Rẩt có thể chương trình của bạn đứng tại đây, bạn thiếu \r để kết thúc lệnh. Ví dụ: printf("AT+CMGR=1\r\n"); - Nói chung, code bạn viết là không chặt chẽ. Bạn cần tìm hiểu để viết code chặt chẽ hơn. Tôi gợi ý đoạn code nhận tin nhắn sms như sau: - Đâu tiên, các bạn phải cấu hình/thiết lập nhận tin nhắn: AT+CNMI=1,1,0,0,0 New message indication enable. Khi đó, mỗi lần có tin nhắn mới sẽ nhận được chuỗi data như sau: +CMTI: "SM",<index>\r\n Chú ý: 1. Luôn bắt đầu bằng chuỗi: "+CMTI" 2. Luôn kết thúc bằng: \r\n ==>Dựa vao 1 va 2 để biết ĐÚNG là tin nhắn tới và loại bỏ những data không đúng là tin nhắn... Code:
/* Chú ý:
* Tôi su dung RDA2 de nhan SMS. Khai báo cua tôi nhu sau:
*/
#USE RS232(BAUD=38400, XMIT=PIN_B6, RCV=PIN_B7, STREAM=COM_GSM)
...
//------------------------------------------------------------------------------
#define BUFSMS_SIZE 165 //SMS size the same 160 characters.
#define BUFSMS_MASK BUFSMS_SIZE-2
char buffSMS[BUFSMS_SIZE];
//Define:
#define OK_ 0 // Used to look up in SetSearchString( Response )
#define CMTI_ 1 // Used to look up in SetSearchString( Response )
#define READY_ 3 // Used to look up in SetSearchString( Response )
#define CRLF_ 4 // Used to look up in SetSearchString( Response )
unsigned char OK[] = "OK\r\n"; // "OK"
unsigned char CMTI[] = "+CMTI:"; // New Message arrived
unsigned char READY[] = "> "; // Phone ready to receive text message
unsigned char CR_LF[] = "\r\n"; // Carrige Return Line Feed
unsigned char *searchFor; // Flash pointer
// Private pointer
static unsigned char searchStr;
static int8 rx_i;
int8 nextIn2;
int8 rx_ack=0; //Acknowledge Flag.
void InitBuffCom2(void);
void SetSearchString( unsigned char Response );
void EnableReceivSMS();
/* Int_rad2:
* Có tin nhắn mới: rx_ack= 1, ngược lai rx_ack= 0.
*/
#INT_RDA2
void serial_isr2()
{
int8 i;
char c;
c= buffSMS[nextIn2++]= fgetc(COM_GSM);
//fputc(buffSMS[nextIn2-1],COM_GPS); //Could be used for testing.
if( nextIn2 > BUFSMS_MASK)
{
nextIn2= 0; //Reset write index.
disable_interrupts(int_rda2);// Disable RX interrupt
}
if( searchFor[rx_i] == c)
{
rx_i++;
if( !searchFor[rx_i] )
{
rx_i= 0;
if( searchStr == CMTI_ ) //+CMTI
{
searchFor= CR_LF; //Wait for
searchStr = CRLF_;
}
else
{
rx_ack= 1;
disable_interrupts(int_rda2);// Disable RX interrupt
}
}
}
else
{
rx_i= 0; //Not valid search pattern...start again.
}
}
/*
* Reset receive interrupt SMS data.
*/
void InitBuffCom2(void)
{
disable_interrupts(int_rda2);// Disable receive SMS message.
rx_i= rx_ack = 0;
nextIn2 = 0;
buffSMS[nextIn2] = NULL;//#define NULL '\0'
}
/*
* Set desired search string
* Usage:
* - Call: InitBuffCom2(); <?> //Reset receive buffSMS
* - Call: SetSearchString( unsigned char Response )
* - Call: AT command
* - Call: EnableReceivSMS()
*/
void SetSearchString( unsigned char Response )
{
disable_interrupts(int_rda2);// Disable receive SMS message.
switch (Response)
{
case OK_:
searchFor= OK;
break;
case CMTI_:
searchFor= CMTI;
break;
case READY_:
searchFor= READY;
break;
default:
break;
}
searchStr = Response; //Used in rx_isr
rx_i = 0;
}
/*
* Enable Receiv SMS:
*/
void EnableReceivSMS()
{
enable_interrupts(int_rda2);
}
/* Check acknowledge returned from phone
*
* This function checks if an acknowledge
* has been received from the phone. A counting loop is also
* included to avoid waiting for a acknowledge that never arrives.
*
* Return Value:
* 1 Success, correct acknowledge
* 0 Error, returned "ERROR" or timed out
*/
#define KEYHIT_DELAY 5000 // milliseconds
Int Check_acknowledge()
{
unsigned int32 timeout=0;
int retval;
int16 temp_delay=0;
retval= 0;
rx_ack= 0; //Zero ackowledge flag.
while((rx_ack==0)&& (++timeout<(KEYHIT_DELAY*100)))
delay_us(10);
if(rx_ack) //Everything worked out fine...rx turned off
{
rx_ack= 0; //Reset ackowledge flag.
retval= 1;
}
else //A timeout could result from no acknowledge.
{
// fputc('A',COM_GPS);
InitBuffCom2();
retval= 0;
}
while((temp_delay++)<10000);//Delay a few miliseconds.
return(retval);
}
/*Delete a message at index= 1
*
*/
Void deleteSMS()
{
InitBuffCom2();
SetSearchString( OK_ );
fprintf(COM_GSM,"AT+CMGD=1\r");
EnableReceivSMS();
if( Check_acknowledge() > 0 ) //Acknowledge = "OK"
{
return;
}
else //Acknowledge != "OK"
{
fprintf(COM_GPS,"ERROR: No Ack!\r\n");
return;
}
}
//----------------------------------------------
Void main()
{
...
enable_interrupts(int_rda2);
enable_interrupts(GLOBAL);
...
deleteSMS(); //Chú ý A
InitBuffCom2();
SetSearchString( CMTI_ ); //Waiting the new SMS message.
EnableReceivSMS(); //Ready to receive the new SMS message.
//Main program:
while(1)
{
if( rx_ack )
{
//Xu ly tin nhan moi.
deleteSMS();//Chú ý B
InitBuffCom2();
SetSearchString( CMTI_ ); //Waiting the new SMS message.
EnableReceivSMS(); //Ready to receive the new SMS message.
}
}
}
- Luôn xóa tin nhắn tại index= 1, điều này có nghĩa rằng khi nhận được tin nhắn mới thì chắc chắnc rằng tin nhắn mới sẽ được lưu trong index= 1. Chú ý B: deleteSMS() - Khi xử lý xong tin nhắn mới, phải xóa tin nhắn ở index= 1 này để sẵn sàng nhận tin nhắn mới vào index= 1.
__________________
-------------------------------------------------------------------------------------- Phùng Minh Tuân Email: tuan.phmt@gmail.com "Cuộc đời sóng gió nuôi ta lớn Bao lần thất bại dạy ta khôn". thay đổi nội dung bởi: longtu, 17-06-2012 lúc 07:38 PM. |
|
|
|
|
|
|