I need to write a code in C to access GSM Modem thro' serial port. I already done successfully. But I need help to write a method in C which is equivalent to DoEvents in VB 6.0. That is, I declared global variable OK in C. If I try to retrieve the value in the middle of the process I could not able to get the correct value though the I set the OK value equal to true in the middle of the process. This can be acheieved using DoEvents method in VB 6.0 and already did this and got the result correctly. But not in C. I am new to C. Please go thro' my code and help me.
I opened the Serial port in Non-Overlapped mode.
Code:
#include <string>
#include <windows.h>
#include <time.h>
using namespace std;
typedef struct{
char splitRecs[25];
} phBook;
phBook record[50];
char INBUFFER[500];
char OUTBUFFER[500];
char * chrArray;
static string strArray;
bool greaterSign = false;
bool Message_Store = false;
string Message_Buffer;
char *SMS_MesgNum = NULL;
char *SMS_TelephoneNum = NULL;
HANDLE h_GSM = NULL;
DCB dcbConfig;
COMMTIMEOUTS CommTimeouts;
DWORD dwEventMask;
DWORD bytes_read = 0;
DWORD bytes_written = 0;
bool OK = false;
bool Error = false;
bool RING = false;
int WriteCOM();
int ReadCOM();
static void doEvents();
void wait_for_response();
char *substring(size_t start, size_t stop, char src[]);
int split(char input[], const char *delims);
void ComEvent(char Event[]);
void wait_for_response();
static void doEvents();
int WriteCOM(){
if(!WriteFile(h_GSM,
&OUTBUFFER,
20,
&bytes_written,
NULL)){
printf("Error in writing\n");
return 0;
}
return 0;
}
int ReadCOM(){
strcpy(&INBUFFER[0], "");
memset(INBUFFER, 0, sizeof(INBUFFER));
do
{
if(ReadFile(h_GSM, &INBUFFER, sizeof(INBUFFER),
&bytes_read, NULL) != 0){
}
}while(bytes_read > 0);
return 0;
}
char *substring(size_t start, size_t stop, char src[])
{
size_t size = sizeof(src);
char* dst = (char*) malloc(strlen(src) * sizeof(char));
int count = stop - start;
sprintf(dst, "%.*s", count, src + start);
return dst;
}
int split(char input[], const char *delims){
char *output[500];
int loop;
double NumOfStr_Input = strlen(input);
int i = 0;
output[0] = strtok(input, delims);
if(output[0]==NULL)
{
printf("No text to search.\n");
exit(0);
}
for(loop=1;loop<numofstr_input;loop++)>
{
output[loop]=strtok(NULL,delims);
if(output[loop]==NULL)
break;
}
for(loop=0;loop<numofstr_input;loop++)>
{
if(output[loop]==NULL)
break;
strcpy(record[i].splitRecs, output[loop]);
i++;
}
return 0;
}
void ComEvent(char Event[]){
if (strcmp(Event, "OK") == 0){
OK = true;
}
if(strcmp(Event, "Error") == 0){
Error = true;
}
if(strcmp(Event, "RING") == 0){
RING = true;
strcpy(&OUTBUFFER[0], "AT+CLCC\r");
WriteCOM();
}
substring(0, 5, Event);
if(strcmpi(substring(0, 5, Event), "+CPBR") == 0){
split(Event, ",");
}
if(strcmpi(substring(0, 5, Event), "+CMTI") == 0){
OK = false;
Error = false;
split(Event, ",");
int lenSMS_MesgNum = strlen(record[1].splitRecs);
SMS_MesgNum = (char*) malloc(lenSMS_MesgNum * sizeof(int));
sprintf(SMS_MesgNum,"%s",record[1].splitRecs);
char *MesgCmd = NULL;
char ATCommand[] = "AT+CMGR=";
char *Param = SMS_MesgNum;
char term_char[] = "\r";
MesgCmd = (char*) malloc((strlen(ATCommand) * sizeof(char)) + (strlen(Param) * sizeof(int)) + (strlen(term_char) * sizeof(char)) );
sprintf(MesgCmd, "%s%s%s",ATCommand,Param,term_char );
strcpy(&OUTBUFFER[0], MesgCmd);
WriteCOM();
wait_for_response();
if (OK == true){
split(Event, ",");
printf("%s\n", substring(1, 12, record[1].splitRecs));
SMS_TelephoneNum = substring(1, 12, record[1].splitRecs);
printf("%s\n", SMS_TelephoneNum);
}
}
if(strcmpi(substring(0, 5, Event), "+CLCC") == 0){
split(Event, ",");
Event="";
OK = false;
Error = false;
if (strcmpi(substring(4, 14, record[5].splitRecs), "9944232334") == 0){
printf("%s\n", "GSM Admin Calling...");
}
else{
printf("%s\n", "Anonymous Call...");
strcpy(&OUTBUFFER[0], "ATH\r");
WriteCOM();
wait_for_response();
}
}
}
void wait_for_response(){
time_t seconds;
seconds = time (NULL);
while (1)
{
doEvents();
if(time(NULL) - seconds >= 5){
break;
}
}
}
static void doEvents(){
MSG msg;
HWND hWnd;
while( PeekMessage( &msg, hWnd, 0, 0, PM_REMOVE )){
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
int main(int argc, char* argv[]){
h_GSM = CreateFile(TEXT("COM1"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (h_GSM == INVALID_HANDLE_VALUE){
printf("Error Opening Port\n");
return 0;
}
CommTimeouts.ReadIntervalTimeout = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 100;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 100;
if(!SetCommTimeouts(h_GSM ,&CommTimeouts)){
printf("Error setting timeouts");
return 0;
}
if(!GetCommState(h_GSM, &dcbConfig)){
printf("Error getting Comm Settings");
return 0;
}
dcbConfig.BaudRate = 9600;
dcbConfig.StopBits = ONESTOPBIT;
dcbConfig.ByteSize = 8;
dcbConfig.Parity = NOPARITY;
if(!SetCommState(h_GSM, &dcbConfig)){
printf("Error getting Comm Settings");
return 0;
}
strcpy(&OUTBUFFER[0], "ATE0\r");
WriteCOM();
wait_for_response();
PROBLEM HERE!!!
******** HERE I HAVE TO CHECK THE STATUS OF "OK". IF IT IS '1' THEN START TO EXECUTE THE NEXT LINE. BUT I COULD NOT ABLE TO RECEIVE THE CORRECT STATUS OF THE "OK'. I ALWAYS RECEIVE FALSE.**********
IF(OK == true){
// SET THE SMS IN TEXT MODE (PDU MODE IS DEFAULT = 0)
strcpy(&OUTBUFFER[0], "AT+CMGF=1\r");
WriteCOM();
wait_for_response();
}
// SETTING COM MASK TO FORCE THE PORT TO INFORM THE USER ONLY
// IF IT RECEIVES ANY EVENTS FROM GSM MODEM
//if(!SetCommMask(h_GSM, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING | EV_RLSD)){
if(!SetCommMask(h_GSM, EV_RXCHAR)){
printf("Set Comm Mask Error!!!\n");
return 0;
}
// IF WAIT COM EVENT FAILURE
if (!WaitCommEvent(h_GSM, &dwEventMask, NULL)){
printf("Wait Comm Event Error!!!\n");
return 0;
}
//WAIT UNTIL EVENTS OCCURED IN THE PORT
while(WaitCommEvent(h_GSM, &dwEventMask, NULL)) {
// IF CHARACTER RECEIVED (EV_RXCHAR)
if (dwEventMask & EV_RXCHAR){
// READ THE OUTPUT OF PORT
ReadCOM();
:sigh:
int lenStrArray;
int j = 0;
// STORE BUFFER VALUES INTO STRING
strArray = INBUFFER;
lenStrArray = strArray.length();
//POINTER DECLARED AS GLOBAL VARIABLE (SEE AT THE TOP)
chrArray = (char*) realloc(chrArray,lenStrArray * sizeof(char));
//chrArray = (char*) malloc(lenStrArray * sizeof(char));
if (chrArray == NULL){
printf("Memory allocation failed\n" );
return 0;
}
// READ OUTPUT DATA ONE BY ONE TO ELIMINATE CARRIAGE RETURN CHARACTERS AND
// LINE FEED CHARACTERS AND TO EXTRACT ONLY VALID DATA
for(int i = 0; i < lenStrArray; i++){
/*
if (Message_Store){
if(strcat((char*)Message_Buffer[i], (char*)chrArray[i]) == 0){
printf("Message store\n");
}
} */
switch(strArray[i]){
case '\x0A': //if line feed detected in the com output
break;
case '\x3E':
greaterSign = true;
printf("%d\n", greaterSign);
break;
case '\x0D': //if enter or carriage return detected in the com output
chrArray[j] = '\0';
int lenInput;
lenInput = strlen(chrArray);
if (lenInput> 0){
printf("%s\n", chrArray);
ComEvent(chrArray);
j=0;
}
break;
default:
chrArray[j] = strArray[i]; //Store the data one by one into buffer
j++;
break;
}
}
free(chrArray); // free memory
chrArray = '\0'; // nullify the pointer
}
}
CloseHandle(h_GSM); // Closing the Port
h_GSM = INVALID_HANDLE_VALUE;
return 0;
}
</time.h></windows.h></string>