do you think you are a real c programmer then lets see..


 
Thread Tools Search this Thread
Top Forums Programming do you think you are a real c programmer then lets see..
# 1  
Old 07-20-2009
do you think you are a real c programmer then lets see..

Code:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<termios.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/select.h>
#include<string.h>
#include<sys/time.h>

void *thread_function(void *arg);
char quit='\0';/* this tends to terminate the read thread*/




      int open_port(void)
      {
      int fd;
      fd=open("dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);

      if(fd==-1)
      {
      fprintf(stderr,"open_port : unable to open /dev/ttyS1 -%s\n",strerror(errno));
      }
      return(fd);
      }




      
 int init_modem(int fd)   /* I - Serial port file */
    {

            char buffer[255];  /* Input buffer */
	    int  nbytes;       /* Number of bytes read */
            int  tries;        /* Number of tries so far */

            for (tries = 0; tries < 3; tries ++)
	    {
                    /* send an AT command followed by a CR */
                    if (write(fd, "AT\r", 3) < 3)
	             continue;
		    sleep(1);
		    nbytes=read(fd,&buffer,sizeof(buffer)-1);
		    if(nbytes<=0)
			    return 0;
		    else
		    {
			    buffer[nbytes]='\0';
			    printf("%s\n",buffer);
		    }

		   

		    write(fd,"AT+CREG?\r",9);
		    nbytes=0;
		    sleep(1);
		    nbytes=read(fd,&buffer,sizeof(buffer)-1);
		    buffer[nbytes]='\0';
		    printf("%s\n",buffer);
		    return 1;
	    }
	    return 0;
    }

	 
int  main()
{


      int mainfd=0;
      int res,iout;
      struct termios options;
      pthread_t a_thread;
      void *thread_result;
      char msg[256];


      mainfd=open_port(); 
      fcntl(mainfd,F_SETFL,FNDELAY);
      tcgetattr(mainfd,&options);
      cfsetispeed(&options,B9600);
      cfsetospeed(&options,B9600);



      /*modem setting */
      options.c_cflag |=(CLOCAL | CREAD);
      options.c_oflag &= ~OPOST;
      options.c_cflag &= ~PARENB;
      options.c_cflag &= ~CSTOPB;
      options.c_cflag |= ~CSIZE;
      options.c_cflag |= CS8;
      options.c_cflag &= ~CRTSCTS;

      options.c_lflag &=~(ICANON | ECHO | ISIG);
      tcsetattr(mainfd,TCSANOW,&options);


     if(init_modem(mainfd)==0)
       {
         printf("modem bad, run again\n");
         exit(0);
       }

     
     printf("every thing working continue\n");
     res=pthread_create(&a_thread,NULL,thread_function,(void*)mainfd);
     if(res!=0)
     {
	     perror("thread creation failed\n");
	     exit(EXIT_FAILURE);
     }
     printf("enter commands or message and type q to exit\n");

     while(1)
     {
	     fflush(stdin);
	     printf("#");
	     gets(msg);
	     if(strcmp(msg,"q")==0 || strcmp(msg,"Q")==0)
	     {
		     quit='q';
		     break;
	     }
	     
	    iout= write(mainfd,&msg,strlen(msg));
	     if (iout < 0)
		     {
			     printf("write error %d %s\n", errno, strerror(errno));
		     }
	     else
	     {
		     printf("wrote %d chars:", iout);
	     }
	     sleep(4);/*time for processing*/
     
     }

      res=pthread_join(a_thread,&thread_result);
      if(res!=0)
      {
	      perror("thread join failed\n");
	      exit(EXIT_FAILURE);
      }
      printf("thread joined it returned%s\n",(char*)thread_result);
      printf("application ended\n");
}



void *thread_function(void *arg)
{


	int fd;
	char buffer2[256];
	int n,in;
	int max_fd;
	fd_set input;
	struct timeval timeout;

	/*initilize the input set*/
	fd=*((int*)arg);
	FD_ZERO(&input);
	FD_SET(fd,&input);
	max_fd=fd+1;

	timeout.tv_sec=3;
	timeout.tv_usec=0;

	/*looping with select*/
	while(1)
	{
		n=select(max_fd,&input,NULL,NULL,&timeout);/*this wait for 3 sec for any input in the input bufferand then it just go down to check for status of quit and then again comes back*/
		if(n<0)
			perror("select failed");
		else if(n==0)
		{}
		else
		{
			/*we have input*/
			if(FD_ISSET(fd,&input))
			{
				/*we have something in the input buffer waiting to be read*/
				in=read(fd,&buffer2,255)
				if(in<0)
				{
					printf("read error %d %s\n", errno, strerror(errno));
				}

				else
				{
					buffer2[in] = '\0';
					printf("%s\n",buffer2);
				}	
			}
		}
		if(quit=='q')
			break;
	}
	pthread_exit("reading thread also exiting\n");
}


Last edited by vgersh99; 07-20-2009 at 02:25 PM.. Reason: code tags, PLEASE!
# 2  
Old 07-20-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 01:26 PM ---------- Previous update was at 01:26 PM ----------

What is the question?
# 3  
Old 07-24-2009
It's obvious you are not a "real c programmer" and your attempt to goad us into sifting through this putrid mess is a poor one.

Get a book on C (K&R 2ed is good, Deitel & Deitel How to Program C is also good).

Learn it.

Scour the web for tutorials and examples for doing GSM SMS commands (that's what I did).

Come back less arrogant and with better questions.
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Programming

do you think you are a real c programmer then lets see..

here is a code which sends and receives sms through a serial port. the problem is that its givin segmentation fault. it first sets the file discriptor and the initialises the modem by using AT commands. there are two threads for reading and writing . rest the code is simple you'll get it. user has... (1 Reply)
Discussion started by: harsh_it
1 Replies

2. UNIX for Dummies Questions & Answers

Tough Questoin lets see if any of you can answer it

If an n-input AND or OR gate requires n-transistors, how many transistors are needed for 1 full adder circuit? This is my homework question for my UNIX class and i dont even know where to begin. (1 Reply)
Discussion started by: derekmpage
1 Replies

3. Post Here to Contact Site Administrators and Moderators

Lets give Neo a hand.

Neo, Just wanted to let you know that we all appricate the hardwork that you and your team put in to make this forum what it is. I have been a member since 05-23-2001 and i can honestly say this is one forum that is deffinetly a main stay and a true benifit to everyone that uses it. Mike... (4 Replies)
Discussion started by: Optimus_P
4 Replies
Login or Register to Ask a Question