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 to enter messages trough the AT commands defined for the modem.
#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");
}
compile: cc -D_REENTRANT gsm1.c -o gsm1 -lpthread