首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ubuntu 12.04中的C向teltonika GSM调制解调器发送AT命令

使用ubuntu 12.04中的C向teltonika GSM调制解调器发送AT命令
EN

Stack Overflow用户
提问于 2014-04-08 22:27:06
回答 1查看 1.3K关注 0票数 0

您好,我正在尝试发送AT命令到teltonika gsm调制解调器使用我的ubuntu桌面的com端口(TtyS0)。问题是,不是得到对AT命令(或任何其他命令或字符串)的"OK“回复,而是返回相同的AT命令。在这方面的任何帮助都将受到感谢。下面是我正在编写的C代码:

代码语言:javascript
复制
#include <sys/types.h>                                                  
#include <sys/stat.h>                                                      
#include <fcntl.h>                                                       
#include <termios.h>                                                    
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <unistd.h> 
#include <time.h>
#include <errno.h>   
#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,&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 <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[])
{ 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

谢谢附言:这个行为发生在Ubuntu桌面上,程序没有从VMware站的串口读取任何东西。

EN

回答 1

Stack Overflow用户

发布于 2014-12-01 02:22:17

不使用usleep(10000);使用usleep(1000);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22940050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档