iphone AT指令发送短信 AT COmmand

#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/tty.debug", 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 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]);
 fprintf(stderr,"/t/t%s /"AT+XGENDATA/"/n",argv[0]);
 fprintf(stderr,"/t/t%s /"AT+CLCK=///"SC///",2/"/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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值