写AT命令嵌入式linux [英] Writing AT commands embedded linux

查看:233
本文介绍了写AT命令嵌入式linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有写AT指令到GSM模块的问题。当我使用它的工作完美无瑕的Minicom -b 115200 -D的/ dev / ttySP0 --term = VT100
但我不能想出如何做同样的事情在C $ C $℃。
我没有收到任何错误,但模块仍没有来的命令做出反应。任何人都知道什么地方出错了?

 的#include< SYS / types.h中>
#包括LT&; SYS / stat.h>
#包括LT&;&fcntl.h GT;
#包括LT&;&termios.h GT;
#包括LT&;&stdio.h中GT;
波特率的#define B115200
#定义COM1的/ dev / ttySP0
静态INT FD;
静态结构termios的oldtio,newtio;// ================================================ ==============
INT tty_read(字符* BUF,诠释为nbytes)
{
  INT温度;
TEMP =读取(FD,BUF,为nbytes);
的printf(读字符串:%S \\ n,BUF);
返回温度;
}
// ================================================ ==============
INT tty_end()
{
    tcsetattr(FD,TCSANOW,&安培; oldtio);
    关闭(FD);
}// ================================================ ==============
INT tty_writecmd(字符* BUF,诠释为nbytes)
{
INT I;
对于(i = 0; I<为nbytes;我++){
写(FD,和放大器; BUF [I],1);
usleep(100);
}
写(FD,\\ n,1); //尝试\\ 0 \\ r藏汉
usleep(300000);
返回tcdrain(FD);
}// ================================================ ==============
INT波特= B115200;
INT的tty_init()
{
FD =开(COM1,O_RDWR);
如果(FD℃,){
      PERROR(COM1);
      出口(1);
    }
    tcgetattr(FD,&安培; oldtio);
bzero(安培; newtio,sizeof的(newtio));
newtio.c_cflag =波特| CRTSCTS | CS8 | CLOCAL | CREAD;newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
 了nettio.c_cc [VINTR] = 0;
 了nettio.c_cc [VQUIT] = 0;
 了nettio.c_cc [VERASE] = 0;
 了nettio.c_cc [VKILL] = 0;
 了nettio.c_cc [VEOF] = 4;
 了nettio.c_cc [VTIME] = 0;
 了nettio.c_cc [VMIN] = 1;
 了nettio.c_cc [VSWTC] = 0;
 了nettio.c_cc [VSTART] = 0;
 了nettio.c_cc [VSTOP] = 0;
 了nettio.c_cc [VSUSP] = 0;
 了nettio.c_cc [VEOL] = 0;
 了nettio.c_cc [VREPRINT] = 0;
 了nettio.c_cc [VDISCARD] = 0;
 了nettio.c_cc [VWERASE] = 0;
 了nettio.c_cc [VLNEXT] = 0;
 了nettio.c_cc [VEOL2] = 0;
  tcflush(FD,TCIFLUSH);
tcsetattr(FD,TCSANOW,&安培; newtio);
返回0;
}INT主(INT ARGC,CHAR *的argv [])
{
  焦炭的recv [10];
  字符命令[] =AT + CSQ
  的tty_init();
  的printf(写数:%d \\ n,tty_writecmd(命令的sizeof(命令)));
  usleep(40000);
  的printf(请阅读数:%d \\ n,tty_read(recv的,的sizeof(RECV)));
  tty_end();
}


解决方案

你应该做的第一件事就是运行


  

的stty -F的/ dev / ttySP0


这样做,而Minicom是运行期间,你的程序正在运行。一切检查和比较。有很多东西,可以使你的问题。

一旦你拥有了这些匹配,你要确保你发送的数据是走出去。


  

执行cat / proc / TTY /驱动器/串行


您发送的数据,以确保它是走出去之前和之后比较tx值。

如果是的话,你可以检查RX值。如果你没有反应,你可能会需要一台示波器检查上线的数据。如果你不能做到这一点,那么三倍检查波特率和流量控制。

I am having issues writing AT commands to a GSM module. 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. 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

Do this while minicom is running and while your program is running. 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

Compare the tx value before and after you send data to make sure it is going out.

If it is, then you can check the rx value. 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.

这篇关于写AT命令嵌入式linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆