LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Closed Thread
 
LinkBack Search this Thread
Old 02-13-2004, 09:34 AM   #1
dinesh_2001
LQ Newbie
 
Registered: Feb 2004
Posts: 3

Rep: Reputation: 0
problem in recieving information from a terminal device (globe trotter gprs card)


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();
}
 
Old 02-13-2004, 10:32 AM   #2
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,063
Blog Entries: 4

Rep: Reputation: 385Reputation: 385Reputation: 385Reputation: 385
Please do not post the same thread in more than one forum. Picking the most relevant forum and posting it once there makes it easier for other members to help you and keeps the discussion all in one place.

http://www.linuxquestions.org/rules.php

The post to answer is http://www.linuxquestions.org/questi...hreadid=145647
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting terminal/console information Woodsman Slackware 6 11-12-2005 12:34 PM
mandrake globe trotter kydung Mandriva 0 11-18-2004 01:29 AM
Terminal Information Files or directory ankush174 Red Hat 1 07-02-2004 02:40 PM
problem in recieving information from a terminal device dinesh_2001 Linux - Networking 0 02-13-2004 09:36 AM
getting information from a webpage in terminal charlie123 Linux - Newbie 1 01-31-2003 05:58 AM


All times are GMT -5. The time now is 09:44 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration