Hi. I am having a little problem. I have my MCB900 evaluation board connected to a gsm modem via serial cable. I have wrote a simple program to send out the "AT" command to the modem from the eval board. I then read back on the serial and store the charters in a buffer and I then compare the buffer and light the LEDs depending on what I get. At the moment I have the code set up that all 8LEDs light if I recieve "AT on the serial but I know it should be reading in "OK" from the GSM module.
Q. Do I have to clear SBUF after I send the message to the GSM modem and before I start to reading back from the GSM modem
/*------------------------------------------------------------------------------ ------------------------------------------------------------------------------*/ #include <REG935.H> #include <stdio.h> #include <string.h> int u=0; int step = 1; char sms[2]; void init(void); /*------------------------------------------------ The main C function. ------------------------------------------------*/ void main() { SCON = 0x52; /* initialize UART */ BRGR0 = 0xF0; /* 9600 baud, 8 bit, no parity, 1 stop bit */ BRGR1 = 0x02; BRGCON = 0x03; ADMODA = 0x30; /* SCAN=1 single conversion */ ADINS = 0xF0; /* Enable AD10..AD13 inputs */ ADMODB = 0x60; /* CLK: 1.5MHz for 12MHz XTAL */ ADCON1 = 0x05; /* Enable+start A/D converter */ while (1) { if(u !=2){ P2 = 0x01; } if(step==1){ init(); } } } void init(void){ if(u !=2){ P2 = 0x03; } if(u < 1){ printf ("AT\n"); /*output AT message to modem*/ } while (RI != 1) {;} /*wait for serial input from modem*/ sms[u] = SBUF; /*store modem message in buffer*/ SBUF=0;/*clear SBUF*/ RI= 0; u++;/*increment next character of message*/ if(u==2){ if(sms[0] == 0X41 && sms[1] == 0X54){ /*comparing if message from modem is "AT" should be "OK"*/ P2 = 0xFF; /*if "AT" light all 8 LED's*/ }else{ P2 = 0x1F; /*else light 5 LED's*/ } step = 1; } }
.
Haven't you ever heard about echo? Never tried to connect a serial port to a "normal" modem, and sent characters to the modem and seen the characters being returned back?
Ever read the manual for the modem and looked at available settings to turn on/off echo?
SBUF can only handle one character at a time. But SBUF is not a memory cell. So there is no "last character stored". SBUF is a gateway for forwarding data from your program into the UART. The same moment you send a character into SBUF, you no longer knows anything about where that character is. By definition.
But you have a status flag you can check, to verify when SBUF, the gateway or portal, is ready to forward a new character into the UART.
And SBUF is a dual-direction gateway/portal. So writing to SBUF means data going from program into UART. Reading means picking up data received from the UART.
But if you see SBUF as a gateway or portal - then think about this: Does the door to your apartment/house "store" people? Does the door of a car "store" people? So if two people gets into the car, would the last person then be stored in the doorway?