title: sending AT command to teltonika GSM modem using C in ubuntu 12.04
body: <p>Hi I am trying to send AT commands to teltonika gsm modem using com port(ttyS0) of my ubuntu desktop. The problem is instead of getting "OK" reply to the AT command ( or any other command or string), the same AT command is getting echo'ed back. Any help in this regard will be appreciated. Here's the C code m working on:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;                                                  
  #include &lt;sys/stat.h&gt;                                                      
  #include &lt;fcntl.h&gt;                                                       
  #include &lt;termios.h&gt;                                                    
  #include &lt;stdio.h&gt; 
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt; 
  #include &lt;time.h&gt;
  #include &lt;errno.h&gt;   
  #define BAUDRATE B115200
  #define COM1 "/dev/ttyS0"
  static int fd;
  static struct termios oldtio,newtio;
  
  //==============================================================
  int tty_read(char *buf1,int nbytes)
  {
  int temp;
  temp = read(fd,buf1,nbytes);
  printf("Read string: %s\n", buf1);
  return temp;
  }
  //==============================================================
  int tty_end()
  {
  tcsetattr(fd,TCSANOW,&amp;oldtio);
  close(fd);
  }
  //==============================================================
  int tty_writecmd(char *buf,int nbytes)
  { 
  
  write(fd,buf,nbytes);
  
  usleep(1000);
  return tcdrain(fd);
  }
  //==============================================================
  int baud = B115200;
  int tty_init()
  {
  fd = open(COM1, O_RDWR );
  if (fd &lt;0) {
    perror(COM1);
    exit(1);
  }
  tcgetattr(fd,&amp;oldtio);
  bzero(&amp;newtio, sizeof(newtio)); 
  newtio.c_cflag = baud | CRTSCTS | CS8 | CLOCAL | CREAD ;
  
  newtio.c_iflag = IGNPAR | ICRNL; 
  newtio.c_oflag = 0; 
  newtio.c_lflag = ICANON;
  newtio.c_cc[VINTR]    = 0;     
  newtio.c_cc[VQUIT]    = 0;     
  newtio.c_cc[VERASE]   = 0;     
  newtio.c_cc[VKILL]    = 0;    
  newtio.c_cc[VEOF]     = 4;     
  newtio.c_cc[VTIME]    = 0;
  newtio.c_cc[VMIN]     = 1;
  newtio.c_cc[VSWTC]    = 0;     
  newtio.c_cc[VSTART]   = 0;     
  newtio.c_cc[VSTOP]    = 0;
  newtio.c_cc[VSUSP]    = 0; 
  newtio.c_cc[VEOL]     = 0;
  newtio.c_cc[VREPRINT] = 0; 
  newtio.c_cc[VDISCARD] = 0;
  newtio.c_cc[VWERASE]  = 0;
  newtio.c_cc[VLNEXT]   = 0;
  newtio.c_cc[VEOL2]    = 0; 
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd,TCSANOW,&amp;newtio);
  return 0;
  }
  int main(int argc, char *argv[])
  { int wr,rd;
  char *buff;
  char recv[15];
  char command[] = "AT\r\n";
  tty_init();
  printf("Write: %d\n", tty_writecmd(command, sizeof(command)));
  
  usleep(10000);
  printf("Read: %d\n", tty_read(recv ,sizeof(recv)));
  tty_end();
  }
  
  and the output is like
  write:0
  Read string: AT
  Read:3
  </code></pre>
  
  <p>Thanks
  P.S : This behaviour occurs in Ubuntu desktop, the program reads nothing from serial port in VMware station.</p>