简体   繁体   English

编写嵌入式Linux的AT命令

[英]Writing AT commands embedded linux

I am having issues writing AT commands to a GSM module. 我在将AT命令写入GSM模块时遇到问题。 It works flawless when i use minicom -b 115200 -D /dev/ttySP0 --term=vt100 But i cant figure out how to do the same thing in C code. 当我使用minicom -b 115200 -D / dev / ttySP0 --term = vt100时,它可以完美地工作,但是我无法弄清楚如何在C代码中执行相同的操作。 I do not receive any errors, but the module does no react to the commands. 我没有收到任何错误,但是模块对命令没有任何反应。 Anyone know what could be wrong? 有人知道怎么了吗?

#include <sys/types.h>                                                  
#include <sys/stat.h>                                                      
#include <fcntl.h>                                                       
#include <termios.h>                                                    
#include <stdio.h>    
#define BAUDRATE B115200
#define COM1 "/dev/ttySP0"
static int fd;
static struct termios oldtio,newtio;

//==============================================================
int tty_read(char *buf,int nbytes)
{
  int temp;
temp = read(fd,buf,nbytes);
printf("Read string: %s\n", buf);
return temp;
}
//==============================================================
int tty_end()
{
    tcsetattr(fd,TCSANOW,&oldtio);
    close(fd);
}

//==============================================================
int tty_writecmd(char *buf,int nbytes)
{
int i;
for(i=0; i<nbytes; i++) {
write(fd,&buf[i],1);
usleep(100);
}
write(fd,"\n",1); //Tried \0 \r aswell
usleep(300000);
return tcdrain(fd);
}

//==============================================================
int baud = B115200;
int tty_init()
{
fd = open(COM1, O_RDWR );
if (fd <0) {
      perror(COM1);
      exit(1);
    }
    tcgetattr(fd,&oldtio);
bzero(&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,&newtio);
return 0;
}

int main(int argc, char *argv[])
{
  char recv[10];
  char command[] = "AT+CSQ";
  tty_init();
  printf("Write: %d\n", tty_writecmd(command, sizeof(command)));
  usleep(40000);
  printf("Read: %d\n", tty_read(recv ,sizeof(recv)));
  tty_end();
}

The first thing you should do is to run 您应该做的第一件事就是运行

stty -F /dev/ttySP0 stty -F / dev / ttySP0

Do this while minicom is running and while your program is running. 在minicom运行时和程序运行时执行此操作。 Check everything and compare. 检查所有内容并进行比较。 There are lots of things that can cause you issues. 有很多事情可能导致您遇到问题。

Once you have those matching, you want to make sure the data you send is going out. 匹配后,您要确保发送的数据正确无误。

cat /proc/tty/driver/serial 猫/ proc / tty /驱动程序/序列

Compare the tx value before and after you send data to make sure it is going out. 在发送数据之前和之后比较tx值,以确保它不会丢失。

If it is, then you can check the rx value. 如果是,则可以检查rx值。 If you get no response, you are probably going to need an oscilloscope to inspect the data on the lines. 如果没有响应,则可能需要使用示波器检查线路上的数据。 If you can't do this, then triple check the baud rate and flow control. 如果您无法执行此操作,请三重检查波特率和流量控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM