以编程方式通过AT + CMGS命令发送短信

时间:2015-11-17 08:06:09

标签: c gsm at-command tty

我正在尝试用C ++编程方式通过MC7304调制解调器发送短信。对于AT命令,linux共享/ dev / ttyUSB1端口。这是我的代码:

fd_set readSet, writeSet;
struct timeval timeval;

int cellFd = open("/dev/ttyUSB1", O_RDWR, 0);
writeSet = readSet;

timeval.tv_sec = 0;
timeval.tv_usec = 3000000;

int n = 0;

int written = 0;
int readed = 0;
int totalRead = 0;

char buffer[1001];
memset(buffer, '\0', 1001);



written = write(cellFd, "AT+CMGF=1\r\n", 11);
printf("AT+CMGF=1 command sent with result: %d. Waiting for response...\n", written);


while(true)
{
FD_ZERO(&readSet);
FD_SET(cellFd, &readSet);

timeval.tv_sec = 2;
timeval.tv_usec = 0;

n = select(cellFd + 1, &readSet, &writeSet, NULL, &timeval);

if(n == 0)
{
  break;
}
else if(n > 0)
{     
  readed = read(cellFd, buffer + totalRead, 1000 - totalRead);

  totalRead += readed;
}
else
{
  printf("Error\n");
  exit(0);
}
}


if(totalRead > 0)
{
  cout << buffer << endl;
}
else
{
  printf("No data received\n");
}

totalRead = 0;
readed = 0;
memset(buffer, '\0', 1001);







written = write(cellFd, "AT+CSCS=\"GSM\"\r\n", 15);
printf("AT+CSCS=\"GSM\" command sent with result: %d. Waiting for response...\n", written);

while(true)
{
FD_ZERO(&readSet);
FD_SET(cellFd, &readSet);

timeval.tv_sec = 2;
timeval.tv_usec = 0;

n = select(cellFd + 1, &readSet, &writeSet, NULL, &timeval);

if(n == 0)
{
  break;
}
else if(n > 0)
{     
  readed = read(cellFd, buffer + totalRead, 1000 - totalRead);

  totalRead += readed;
}
else
{
  printf("Error\n");
  exit(0);
}
}


if(totalRead > 0)
{
  cout << buffer << endl;
}
else
{
  printf("No data received\n");
}

totalRead = 0;
readed = 0;
memset(buffer, '\0', 1001);











written = write(cellFd, "AT+CMGS=\"MY_NUMBER\"\r\n", 24);
printf("AT+CMGS=\"+MY_NUMBER\" command sent with %d result. Waiting for response...\n", written);

while(true)
{
FD_ZERO(&readSet);
FD_SET(cellFd, &readSet);

timeval.tv_sec = 3;
timeval.tv_usec = 0;

n = select(cellFd + 1, &readSet, &writeSet, NULL, &timeval);

if(n == 0)
{
  printf("No data\n");
  break;
}
else if(n > 0)
{     
  memset(buffer, '\0', 1001);
  readed = read(cellFd, buffer, 1000);

  for(int i=0; i<readed; i++)
  {
    printf("%02x ", buffer[i]);
  }

  printf("\n");

  cout << buffer << endl;

  if((*(buffer) == 0x3E) && (*(buffer + 1) == 0x20) && (*(buffer + 2) == 0x0A))
  {
    written = write(cellFd, "Test\x1A", 5);
    printf("SMS text sent with %d result. Waiting for response...\n", written);
  }
}
else
{
  printf("Error\n");
  exit(0);
}
}

前两个命令工作正常并返回'OK'响应。不幸的是,在发送AT + CMGS命令后,调制解调器响应为0x0A(换行)两次,接下来响应0x3E 0x20 0x0A(&gt;)(看起来没问题)。在此序列之后,我发送的文本以0x1A(CTRL-Z)结尾,但调制解调器仍然以&gt;响应。这种情况重复了数百次,最后我得到错误+ CMS错误:305。看起来像:

root@Moxa:/home/SMS# ./SMS
AT+CMGF=1 command sent with result: 11. Waiting for response...
OK
AT+CSCS="GSM" command sent with result: 15. Waiting for response...
OK
AT+CMGS="(SOME NUMBER)" command sent with 24 result. Waiting for response...
>
SMS text sent with 5 result. Waiting for response...
>
[ situation repeats ]
SMS text sent with 5 result. Waiting for response...
+CMS ERROR: 305

此代码有什么问题,我该如何解决?

0 个答案:

没有答案
相关问题