[Tutorial] Send SMS with iPhone modem

Discussion in 'iOS Development' started by Cyanidepoison, Feb 12, 2009.

  1. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    So you know C, you like iPhones, and you want to programmatically send an SMS. I spent a few days of my life a few months ago figuring this out.

    Here are the includes and a pretty generic funtion to connect to the iPhone modem and let you do some magic.
    (I realize some of those headers aren't actually needed, I think. I copied them from my command-line tool that uses these functions.)
    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>
    #include <sys/ioctl.h>
    
    int connect(){
      int speed = 115200;
      unsigned int null = 0;
      unsigned int handshake = TIOCM_DTR | TIOCM_RTS | TIOCM_CTS| TIOCM_DSR;
      struct termios term;
    
      int fd = open("/dev/tty.debug", O_RDWR | 0x20000 | O_NOCTTY);
    
      if(fd == -1)
      {
        perror"Error:  ");
        exit(1);
      }
    
    
      ioctl(fd, 0x2000740D);
      fcntl(fd, 4, 0);
      tcgetattr(fd, &term);
      ioctl(fd, 0x8004540A, &null);
      cfsetspeed(&term, speed);
      cfmakeraw(&term);
      term.c_cc[VMIN] = 0;
      term.c_cc[VTIME] = 5;
      term.c_iflag = (term.c_iflag & 0xFFFFF0CD) | 5;
      term.c_oflag =  term.c_oflag & 0xFFFFFFFE;
      term.c_cflag = (term.c_cflag & 0xFFFC6CFF) | 0x3CB00;
      term.c_lflag =  term.c_lflag & 0xFFFFFA77;
      term.c_cflag = (term.c_cflag & ~CSIZE) | CS8;
      term.c_cflag &= ~PARENB;
      term.c_lflag &= ~ECHO;
      tcsetattr(fd, TCSANOW, &term);
      ioctl(fd, TIOCSDTR);
      ioctl(fd, TIOCCDTR);
      ioctl(fd, TIOCMSET, &handshake);
    
      return fd;
    }
    
    
    You don't really need to understand all of that. If you want to, Google ioctl and the other functions used.

    Code:
    void send(char *number, char *message, int modem)
    {
      write(modem,"AT+CMGF=1\r\n",11);
      sleep(1);
      write(modem,"AT+CMGS=\"",9);
      write(modem,number,strlen(number));
      write(modem,"\"\r\n",3);
      sleep(1);
      write(modem,message,strlen(message));
      write(modem,"\x1A",1);
      sleep(1);
      close(modem);
    }
    
    There is a function that takes a pointer to two strings and an int (the file-descriptor of the modem). AT+CMGF=1 sets the phone into SMS mode. AT+CMGS is the command to start writing the address and message into memory, which is terminated with "\x1A" (^Z) to finish the sending process off.

    There you go. Switching plain C out for Objective C shouldn't be too hard.
  2. morgman

    morgman New Member

    Joined:
    Feb 23, 2009
    Messages:
    4
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    thanks so much...

    thanks for the quick tutorial... I've been looking for something that pulls these pieces together. I've been reading about sending sms this way. But the details eluded me. thanks again. I threw something together to demonstrate the concept in a couple of minutes based on the code you provided.

    Please Register or Log in to view images

  3. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    post for cyanide, just to make him happy.
  4. yellowxi

    yellowxi New Member

    Joined:
    Mar 10, 2009
    Messages:
    2
    Likes Received:
    0
    Device:
    iPhone
    If you have these piece of code run on you iphone you would have a trouble receiving any sms. Just have a try. You would not able to receive any sms after running the code. The reason is that "AT+CMGF=1\r\n" has set the sms mode from normally PDU to TEXT, so you have to set the sms mode back to its original mode in PDU. Just add
    write(modem,"AT+CMGF=0\r\n",11);
    sleep(1);
    BEFORE
    close(modem);
    HAVE FUN!
  5. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Thanks!

    This is why I love open-source.
  6. karthikwifin

    karthikwifin New Member

    Joined:
    May 22, 2009
    Messages:
    3
    Likes Received:
    0
    Hi,

    Thanks for this upload. It is working in my device perfectly, but not on other device, can you tell me what is the cause of the problem, also how can I identify the sms is sent. is any flag is return?

    Please help for me.

    Thanks,
    Karthik.
  7. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    I really have no idea why this works on some iPhones and not others. I only have one to test with, so you're kind of out of luck.

    As for return values, I suppose you could change send() to return an int instead of void and check that. I wasn't too concerned with error checking when I wrote this, because I spent four days just getting it to work.
  8. karthikwifin

    karthikwifin New Member

    Joined:
    May 22, 2009
    Messages:
    3
    Likes Received:
    0
    Hi,

    Thanks for reply, I really don't know what value should be return from send() method, any other solution for sending sms through code?

    Regards,
    Karthik.
  9. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    connect() returns the file descriptor of the modem so you can write to it. Don't worry about that.

    You'd modify send() yourself to do error checking and such to make sure everything went well. I don't know what value it would return because it doesn't return anything at the moment

    Please Register or Log in to view images

    .
  10. karthikwifin

    karthikwifin New Member

    Joined:
    May 22, 2009
    Messages:
    3
    Likes Received:
    0
    Hi Don't worry, we can check those each lines working or not inside the send() method like this,

    Code:
    
    if(write(modem,"AT+CMGF=1\r\n",11) == -1){
    	NSLog(@"Error ....\n");
    }
    
    
    Cheers,
    Karthik.

    Please Register or Log in to view images

Share This Page