LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


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

Rep: Reputation: 0
problem in recieving information from a terminal device


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();
}
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting terminal/console information Woodsman Slackware 6 11-12-2005 12:34 PM
Terminal Information Files or directory ankush174 Red Hat 1 07-02-2004 02:40 PM
Device files / Listening for information ? xconspirisist Programming 3 03-20-2004 04:46 PM
problem in recieving information from a terminal device (globe trotter gprs card) dinesh_2001 Linux - General 1 02-13-2004 10:32 AM
getting information from a webpage in terminal charlie123 Linux - Newbie 1 01-31-2003 05:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration