串口调制解调器没有返回'>'

时间:2015-04-21 00:44:41

标签: c serial-port at-command

我通过串口通信GSM调制解调器。并发送AT命令来控制GSM调制解调器。使用下面的代码。除发送短信外,工作正常。 发送短信:

AT+CMGS="+31628870634"
> This is the text message.→
+CMGS: 198
OK

当您发送AT+CMGS="+31628870634"时,必须立即返回>。但我的代码不会返回>并返回> ERROR。我在哪里弄错了?

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#define BAUDRATE B38400            
#define dev "/dev/ttyUSB0"
#define _POSIX_SOURCE 1

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE; 

main()
{
  char pinn[20];
  char buf[255];
  int fd,res=0;
  printf("%s\n", dev);
  struct termios oldtio,newtio;
  fd = open(dev, O_RDWR | O_NOCTTY ); 
  if (fd <0) {perror(dev); exit(-1); }
  tcgetattr(fd,&oldtio); /* save current serial port settings */
  bzero(&newtio, sizeof(newtio));
  newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR | ICRNL;
  newtio.c_oflag = 0;
  newtio.c_lflag = ICANON;
  newtio.c_cc[VMIN]     = 1;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd,TCSANOW,&newtio);


  if (fd < 0)
  {
      printf("Error opening serial port\n");
      exit(1);
  }
  // while(1){

    scanf("%s",pinn);
    strcat(pinn,"\r");

    if (write(fd, pinn, strlen(pinn)) < strlen(pinn)) printf("Write error - %s \n", strerror(errno));
    pinn[strlen(pinn)-1]=0;
    while(1){
      res = read(fd,buf,255);
      buf[res]=0;
      buf[res-1]=0;
      if (res>1&&NULL==strstr(buf,pinn)) break;
    }
    printf("\"%s\"\n", buf);
  tcsetattr(fd,TCSANOW,&oldtio);
  close(fd);
}

这是我的代码输出:

./serial_test
AT+CMGS="99928626"

过了一会儿回来:

"> "
"ERROR"

0 个答案:

没有答案