SMS sample code

This is a discussion on SMS sample code within the C Programming forums, part of the General Programming Boards category; Hi, i'm not sure is there someone who can help me to correct some sameple code that I found on ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    SMS sample code

    Hi, i'm not sure is there someone who can help me to correct some sameple code that I found on the web and assembled ? I'm not a programmer, only a unix administrator and of course i'm trying to pickup asap.

    The whole idea is I want to have a utility that will allow me to send SMS(short messages) with a GSM modem connected to my linux box. The utility should accept command like "sendsms +6198765432 "This is a simple SMS"

    With this code I'm able to do like:

    #sendsms ATZ ----to get modem attention.

    But how can I make it to be able to make sure of the SendSMS function to be able do like :like "sendsms +6198765432 "This is a simple SMS" ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <errno.h>
    #include <time.h>
    
    #define BUFSIZE (65536+100)
    unsigned char readbuf[BUFSIZE];
    
    static struct termios term;
    static struct termios gOriginalTTYAttrs;
    int InitConn(int speed);
    
    void SendCmd(int fd, void *buf, size_t size)
    {
    
      if(write(fd, buf, size) == -1) {
        fprintf(stderr, "SendCmd error. %s\n", strerror(errno));
        exit(1);
      }
    }
    
    void SendStrCmd(int fd, char *buf)
    {
      fprintf(stderr,"Sending command to modem: %s\n",buf);
      SendCmd(fd, buf, strlen(buf));
    }
    
    int ReadResp(int fd)
    {
      int len = 0;
      struct timeval timeout;
      int nfds = fd + 1;
      fd_set readfds;
      int select_ret;
    
      FD_ZERO(&readfds);
      FD_SET(fd, &readfds);
    
      // Wait a second
      timeout.tv_sec = 1;
      timeout.tv_usec = 500000;
    
      fprintf(stderr,"-");
      while (select_ret = select(nfds, &readfds, NULL, NULL, &timeout) > 0)
      {
        fprintf(stderr,".");
        len += read(fd, readbuf + len, BUFSIZE - len);
        FD_ZERO(&readfds);
        FD_SET(fd, &readfds);
        timeout.tv_sec = 0;
        timeout.tv_usec = 500000;
      }
      if (len > 0) {
        fprintf(stderr,"+\n");
      }
      readbuf[len] = 0;
      fprintf(stderr,"%s",readbuf);
      return len;
    }
    
    int InitConn(int speed)
    {
      int fd = open("/dev/modem", O_RDWR | O_NOCTTY);
    
      if(fd == -1) {
        fprintf(stderr, "%i(%s)\n", errno, strerror(errno));
        exit(1);
      }
    
      ioctl(fd, TIOCEXCL);
      fcntl(fd, F_SETFL, 0);
    
      tcgetattr(fd, &term);
      gOriginalTTYAttrs = term;
    
      cfmakeraw(&term);
      cfsetspeed(&term, speed);
      term.c_cflag = CS8 | CLOCAL | CREAD;
      term.c_iflag = 0;
      term.c_oflag = 0;
      term.c_lflag = 0;
      term.c_cc[VMIN] = 0;
      term.c_cc[VTIME] = 0;
      tcsetattr(fd, TCSANOW, &term);
    
      return fd;
    }
    void CloseConn(int fd)
    {
        tcdrain(fd);
        tcsetattr(fd, TCSANOW, &gOriginalTTYAttrs);
        close(fd);
    }
    
    void SendAT(int fd)
    {
      char cmd[5];
    
      //  SendStrCmd(fd, "AT\r");
      sprintf(cmd,"AT\r");
      SendCmd(fd, cmd, strlen(cmd));
    }
    
    void AT(int fd)
    {
      fprintf(stderr, "Sending command to modem: AT\n");
      SendAT(fd);
      for (;;) {
        if(ReadResp(fd) != 0) {
          if(strstr((const char *)readbuf,"OK") != NULL)
          {
    	break;
          }
        }
        SendAT(fd);
      }
    }
    
    int SetPDUMode(int fd)
    {
        AT(fd);
        SendStrCmd(fd, "ATE0\r");
        ReadResp(fd);
        SendStrCmd(fd, "AT+CMGF=0\r");
        ReadResp(fd);
        if(strstr(readbuf,"OK"))
    	   return 0;
       return -1; 
    }
    
    typedef unsigned char	UCHAR;
    typedef unsigned char	*LPSTR;
    typedef unsigned char	*LPBYTE;
    extern LPSTR BinToHex(LPBYTE p, int len);
    
    int SendSMS(int fd, char *to, char *text)
    {
        UCHAR buf[200]; 
        UCHAR at_cmd[1024];
        LPSTR strHex;
        int len;
        int pdu_len;
        int retry = 10;
       
        fd = InitConn(115200); 
        SetPDUMode(fd);
        len = ComposeSubmitSms(buf, sizeof(buf), to, NULL, text);
        strHex = BinToHex(buf, len);
        pdu_len = strlen(strHex)/2-1;
        //printf("%d: %s\n",strlen(strHex)/2-1, strHex);
        sprintf(at_cmd,"AT+CMGS=%d\r",pdu_len);
        fprintf(stderr,"S");
        SendStrCmd(fd, at_cmd);
        do{
    	len = ReadResp(fd);
        }while(!strstr(readbuf,">") && retry-- > 0);
        if(retry > 0)
        {
    	fprintf(stderr,">");
    	sprintf(at_cmd,"%s\032",strHex); //CTRL-Z
    	SendStrCmd(fd, at_cmd);
    	do{
    	    fprintf(stderr,".");
    	    len = ReadResp(fd);
    	}while(!strstr(readbuf,"+CMGS:") && retry-- > 0);
        }
        if(retry > 0)
        {
    	fprintf(stderr,"OK\n");
    	CloseConn(fd);
    	return 0;//success
        }
        else{
    	fprintf(stderr,"%s: %s\n",at_cmd,readbuf);
    	free(strHex);
        }
        CloseConn(fd);
        return -1;//failed
    }
    
    
    int main(int argc, char **argv)
    {
      int fd;
      char cmd[1024]; 
      if(argc < 2)
      {
    	fprintf(stderr,"usage: %s <at command>\n",argv[0]);
    	fprintf(stderr,"examples:\t%s \"AT+XSIMSTATE=1\"\n",argv[0]);
    	exit(1); 
      }
      fd = InitConn(115200);
    
      AT(fd);
      sprintf(cmd,"%s\r",argv[1]);
      SendStrCmd(fd,cmd);
      ReadResp(fd);
      CloseConn(fd);
      return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you'd have to be a fair bit more specific about what you want help with. What is working, and more importantly what is NOT working, and in what way does it not work?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pre-Interviewer asking sample code
    By jayee_spicyguy in forum C Programming
    Replies: 3
    Last Post: 02-09-2009, 10:21 AM
  2. Quick help on bool or while? Sample code...
    By Striph in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 03:05 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. stricmp() where can I view the sample code?
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 06-28-2002, 08:40 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21