I have a problem in recieving information from a terminal device (GPRS Globe TROTTER CARD).
I am able to connect to that device and able to send the commands through write command. when I send data to the device for the first time I am able to recieve the information but for the second and third time I am unable to recieve the data. I don't know why.
Here I am listing all the code I have written for this to work.
Code:
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <iostream.h>
#include <fstream.h>
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyS17"
#define ENDMINITERM 2 /* ctrl-b to quit miniterm */
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
class Gprs_card
{
int fd1,c,n;
struct termios oldtio,newtio,oldstdtio,newstdtio;
struct sigaction sa;
char buffer[100],buf[50];
bool pin;
public :
void runModem();
void checkCard();
void checkGSM();
bool enterPin();
char *perform(char *command);
};
void Gprs_card::runModem()
{
checkCard();
fd1 = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd1 <0) {perror(MODEMDEVICE); exit(-1); }
pin=enterPin();
if (pin==true)
checkGSM();
}
void Gprs_card::checkCard()
{
int found=0;
system("cardctl ident > card.txt 2>&1");
ifstream infile("card.txt");
while (infile)
{
infile.getline(buffer,100);
if(strcmp(buffer," manfid: 0x0013, 0x0000")==0)
found++;
}
if (found>0)
cout<<"Hurray: GPRS Card Found"<<endl;
}
void Gprs_card::checkGSM()
{
char *command="at+creg?\r";
char *result;
cout<<"in Gsm"<<endl;
result=perform(command);
if (strstr(result,"+CREG: 0,1")==NULL)
cout<<"GSM Network is not on"<<endl;
else
cout<<"GSM Network is on"<<endl;
}
bool Gprs_card::enterPin()
{
char *command="at+cpin?\r";
char *result;
result=perform(command);
if (strstr(result,"+CPIN: SIM PIN")==NULL)
cout<<"No need to Enter Pin"<<endl;
else
{
cout<<"Need To Enter Pin"<<endl;
memset(result, 0, sizeof(result));
result=perform("at+cpin=5583\r");
if (strstr(result,"at+cpin=5583")==NULL)
{
cout<<"False Pin"<<endl;
return(false);
}
else
{
cout<<"Pin Entered Successfully"<<endl;
return(true);
}
}
}
char *Gprs_card:
erform(char *command)
{
tcgetattr(fd1,&oldtio);
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd1, TCIFLUSH);
tcsetattr(fd1,TCSANOW,&newtio);
tcsetattr(1,TCSANOW,&newtio);
tcgetattr(0,&oldstdtio);
tcgetattr(0,&newstdtio);
newstdtio.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0,TCSANOW,&newstdtio);
switch (fork())
{
case 0: // child
close(1);
if(write(fd1,command,(size_t) strlen(command))==-1)
{
perror("error writing to device!");
close(fd1);
}
tcsetattr(fd1,TCSANOW,&oldtio); /* restore old modem setings */
tcsetattr(0,TCSANOW,&oldstdtio); /* restore old tty setings */
close(fd1);
exit(0); /* will send a SIGCHLD to the parent */
break;
case -1:
perror("fork");
tcsetattr(fd1,TCSANOW,&oldtio);
close(fd1);
exit(-1);
default: /* parent */
close(0); /* stdin not needed */
sa.sa_handler = child_handler;
sa.sa_flags = 0;
sigaction(SIGCHLD,&sa,NULL); /* handle dying child */
memset(buf, 0, sizeof(buf));
if ((n=read(fd1,buf,50))==-1)
{
perror("error reading from device !");
close(fd1);
}
cout<<buf<<endl;
wait(); /* wait for child to die or it will become a zombie */
break;
}
return(buf);
}
int main(int argc, char *argv[])
{
Gprs_card g;
g.runModem();
}